Click to See Complete Forum and Search --> : GetWindowThreadProcessId


mrnt
November 14th, 2005, 04:21 PM
Hello everybody; I'm new to this forum.
I'm experiencing a *really* strange problem... O_o'
I'm using the Win32 API function GetWindowThreadProcessId in a thread different from the primary one and I use it to retrieve only the thread identifier of a window created by another process (a console window); I'm using Visual C++ 6 (Service Pack 5, if I remember correctly) under Windows 2000 Professional and... well, the programm works perfectly if run under the debugger, but it doesn't work if run standalone!! O.o'
there are NO #ifdef _DEBUG directives and the program DOESN'T use IsDebuggerPresent; do you known any other reason a program would be caused to beahve differently when run under a debugger and when run as standalone? if i don't find a workaround, is there any other way to get the thread id of a console window created by another process?
thank you.

PS: sorry for my bad english, I am from Italy ^_^'

wildfrog
November 14th, 2005, 04:38 PM
Read this FAQ:
Visual C++ Debugging: Why does program work in debug mode, but fail in release mode? (http://www.codeguru.com/forum/showthread.php?t=269905).

If you post the piece of code that is failing we might be able to help you.

- petter

kirants
November 14th, 2005, 04:42 PM
ciao mrnt !!

Please post the actual code you are using.
Also, you said you are using it to get thread ID of another process. How are you getting the handle of the window in the different process.

NoHero
November 14th, 2005, 04:52 PM
And a clear description what "not working" actually means, also helps a lot. What is not working? Wich error do you get? Are API functions failing? etc. etc.

golanshahar
November 14th, 2005, 05:13 PM
i tested ::GetWindowThreadProcessId() to locate a console that has been created from a different process and i ran my app in release ( without MSDEV ) and it worked fine :

HWND hwnd = ::FindWindow("ConsoleWindowClass",0);
if ( hwnd )
{
DWORD Pid=0;
DWORD ThreadID = ::GetWindowThreadProcessId(h,&Pid);
}


:confused:

so like you been told already please post some code.

Cheers

mrnt
November 14th, 2005, 05:29 PM
yes, sorry, you are right.
well, I enumerate all the top level windows in the current desktop using EnumWindows, and this is the actual EnumWindowsProc I'm using:

BOOL CALLBACK TrackWndProc(HWND hWnd, PTRACKERDATA pData) {
__asm int 3 ; this breakpoint is always reached
if (IsWindow(hWnd)) {
if (pData->pi.dwThreadId == GetWindowThreadProcessId(hWnd, NULL)) {
// my stuff...
// ...
__asm int 3 ; this breakpoint is only reached when running under VC++!!!
return FALSE;
}
}
return TRUE;
}

the second __asm int 3 breakpoint is reached only if I run the program from Visual C++ (it doesn't matter whether it's run in debug or release configuration), but when the program is run standalone and the first breakpoint is disabled, no interrupt occurs!! :-|
in other words, the first breakpoint always occurs, the second only when not in the presence of the debugger.
the API function doesn't return any error code, it's just like GetWindowThreadProcessId doesn't return the correct thread id ONLY when my process is created from the shell rather than Visual C++.
I am sure the program that I run from VC++ or from the shell is *exactly* the same: I have done these tests after a "Rebuild All" command in both Debug and Release configuration.
I don't know what to do because I can't debug my program: it works perfectly when debugged and doesn't when not... O.o'
as I said, my program calls EnumWindows in a separate thread.
if I'll ever discover the cause of this problem it will be a great interesting thing: I'll know a way to detect the presence of a debugger without using IsDebuggerPresent!!! :D

PS: kirants, sei italiano? :D

mrnt
November 14th, 2005, 05:35 PM
Guys, I'll try to reproduce the problem in a new Win32 program and in simpler conditions; I think that the problem occurs when using EnumWindows in another thread. If I manage to reproduce the error I'll post the code.

kirants
November 14th, 2005, 05:37 PM
Instead of int 3, have you tried MessageBox?

kirants, sei italiano?
No, sono indiano. See here .. http://www.google.com/language_tools?hl=en
:D :D :D

mrnt
November 14th, 2005, 05:57 PM
Instead of int 3, have you tried MessageBox? I did, but it wasn't much useful because when I ran the program from VC++ it showed me the TID I was looking for, while when I ran from Windows shell it didn't show me anything :D
anyway I think I've discovered the cause while I was trying to reproduce the error in a separate program; I think it was a timing issue... I'll make more tests...

<OT>
No, sono indiano. See here .. http://www.google.com/language_tools?hl=en
:D :D :D LOL!!! :D :D
but how did you understand the words "sei italiano"? :D I tried entering them but Google doesn't translate correctly (the word "sei" has two meanings: Google translated "six Italian?", while it should have been "are you Italian?" :D)
</OT>

mrnt
November 14th, 2005, 06:01 PM
I think I've discovered the cause while I was trying to reproduce the error in a separate program; I think it was a timing issue... I'll make more tests... it was a timing issue -.-'
sorry for all this mess about nothing... :D
thank you anyway :)

kirants
November 14th, 2005, 06:05 PM
but how did you understand the words "sei italiano"? :D I tried entering them but Google doesn't translate correctly
For "sei italiano?" I used my common sense instead of google ;)

Anyways, good to know it;s working for you :wave: