Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Window handle from Console App

Status
Not open for further replies.

tmoody

Technical User
Oct 8, 2001
37
0
0
US
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.


#include <windows.h>
#include <wincon.h>

HWND GetConsoleWindow(VOID);

HWND readWindowHandle()
{
HWND hWnd = 0;

// Windows 2000 or higher

if(_winver >= 0x0400) {
HINSTANCE hInstLib = LoadLibrary(&quot;kernel32&quot;);

if(hInstLib) {
typedef HWND (*TProc)(void);

TProc procAddr = (TProc) GetProcAddress(hInstLib, &quot;GetConsoleWindow&quot;);

if(procAddr)
hWnd = (procAddr)();

FreeLibrary(hInstLib);
}
}
else { // Windows 9x, 98, SE, ME(?)
char szTitle[0x40] = {0};

DWORD dw = GetConsoleTitle(szTitle, 0x40);

hWnd = FindWindow(&quot;tty&quot;, szTitle);
}

return hWnd;
}


Hope this helps.

Regards,
Ralf
 
Ralf, Good code and I can almost understand it too. Walking thru it with the debugger I found that after this line of code &quot;TProc procAddr = (TProc) GetProcAddress hInstLib, &quot;GetConsoleWindow&quot;);&quot; 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

#include <windows.h>
#include <wincon.h>

HWND GetConsoleWindow(VOID);

HWND readWindowHandle()
{
HWND hWnd = 0;

// Windows 2000 or higher

if(_winver >= 0x0400) {
HINSTANCE hInstLib = LoadLibrary(&quot;kernel32&quot;);

if(hInstLib) {
typedef HWND (*TProc)(void);

TProc procAddr = (TProc) GetProcAddress(hInstLib, &quot;GetConsoleWindow&quot;);

if(procAddr)
hWnd = (procAddr)();

FreeLibrary(hInstLib);
}
}
else { // Windows 9x, 98, SE, ME(?)
char szTitle[0x40] = {0};

DWORD dw = GetConsoleTitle(szTitle, 0x40);

hWnd = FindWindow(&quot;tty&quot;, szTitle);
}

return hWnd;
}
 
Ralf, Good code and I can almost understand it too. :) Walking thru it with the debugger I found that after this line of code &quot;TProc procAddr = (TProc) GetProcAddress hInstLib, &quot;GetConsoleWindow&quot;);&quot; 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

#include <windows.h>
#include <wincon.h>

HWND GetConsoleWindow(VOID);

HWND readWindowHandle()
{
HWND hWnd = 0;

// Windows 2000 or higher

if(_winver >= 0x0400) {
HINSTANCE hInstLib = LoadLibrary(&quot;kernel32&quot;);

if(hInstLib) {
typedef HWND (*TProc)(void);

TProc procAddr = (TProc) GetProcAddress(hInstLib, &quot;GetConsoleWindow&quot;);

if(procAddr)
hWnd = (procAddr)();

FreeLibrary(hInstLib);
}
}
else { // Windows 9x, 98, SE, ME(?)
char szTitle[0x40] = {0};

DWORD dw = GetConsoleTitle(szTitle, 0x40);

hWnd = FindWindow(&quot;tty&quot;, szTitle);
}

return hWnd;
}
 
I guess you need to install the MS Platform SDK. Tomorrow I will test it on a Windows NT workstation (without the SDK), then I know it for sure.
 
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



HWND handle;

handle=GetDesktopWindow();
 
This does not work. Try the following program:


#include <windows.h>

int main()
{
const int BUFSIZE = 0x80;

TCHAR lpszBuffer[BUFSIZE] = {0};

GetWindowText(GetDesktopWindow(), lpszBuffer, BUFSIZE-1);

OutputDebugString(lpszBuffer);

return 0;
}


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()'.

Regards,
Ralf
 
Well gosh darn. It sure worked OK for me. I'll have to look into this a little more becasue it &quot;seems&quot; to work. I use &quot;handle=GetDesktopWindow();&quot; 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 &quot;handle=GetDesktopWindow();&quot; 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?

Ralf
 
Ralf,

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
 
> Do you know why procAddr is zero??

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.

Ralf
 
Now I'm confused. This also seems to work. Notice I say seems?? GetForegroundWindow( ); Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top