I have a Console App that I created under NT with VC++ 6.0. I need to figure out how to get the hWnd handle for the console(DOS) window?? Or is there one??
Sounds simple but is really tricky... The latest platform SDK contains a function called GetConsoleWindow(). The problem: This function is not prototyped so you have to call it from the library directly... Try the following source code. It works with Windows 2000 and Windows 9x. I am not 100% sure if the _winver condition is correct, but I think so. Maybe you need to install the Microsoft Platform SDK first. Please reply if it does (not) work.
Ralf, Good code and I can almost understand it too. Walking thru it with the debugger I found that after this line of code "TProc procAddr = (TProc) GetProcAddress hInstLib, "GetConsoleWindow"" procAddr is zero, so hWnd is never set. I'm gonna step into it a little further to see if I can figure it out. Thanks, Tim
Ralf, Good code and I can almost understand it too. Walking thru it with the debugger I found that after this line of code "TProc procAddr = (TProc) GetProcAddress hInstLib, "GetConsoleWindow"" procAddr is zero, so hWnd is never set. I'm gonna step into it a little further to see if I can figure it out. Thanks, Tim
Maybe I confused people by asking for the hWnd handle but here is the simplest way I've found to get a window handle for your console app. It took me two days to find this. I hope it helps someone else.
Tim
The window title is empty because the desktop window has no title.
I guess you need an updated version of kernel32.lib so you can call 'GetConsoleTitle()'.
Well gosh darn. It sure worked OK for me. I'll have to look into this a little more becasue it "seems" to work. I use "handle=GetDesktopWindow();" and pass the handle to a Service Routine that requires it. The SR works as expected, but would die horribly if I used NULL. I'm not sure how, or how well it is working and now I wonder if the handle name doesn't default to the app name?? Thanks, Tim
>I use "handle=GetDesktopWindow();" and pass the
>handle to a Service Routine that requires it.
>The SR works as expected, but would die horribly
>if I used NULL.
It is not NULL, but you get the desktop window handle - not the console window handle. I don't know your SR. Maybe this makes no difference but GetDesktopWindow() does not return the console window handle. (However, I don't know which result you get if the console application is in fullscreen mode!)
What will your service routine do with the window handle?
Thanks for all the good advise. I need to work this out because after what you said, once I incorporate this into the main program( I wrote a stand alone wrapper to test it) it will probably die a horrible death by getting the desktop handle and not the console handle. Back to my above post on the 18th, Do you know why procAddr is zero?? I think I have the SDK and I would like to use the code you wrote! Thanks, Tim
I think your version of the library does not contain the procedure. I tested this on a NT4 workstation and got the same result. Unfortunately I cannot install the Platform SDK on this machine to verify if this helps. The kernel32.lib file that works for me (on Win2000) is created 2000/Feb/02, the file shipped with the Visual Studio (that does not work) is created 1998/May/13.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.