Jump to content
  • Advertisement
Sign in to follow this  
TGPrankster

Another Console Question

This topic is 4251 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey again. I managed to get a console running in the background of my Win32 Application, and to allocate it with the applications output (ie cout). How would I be able to know when the console comes into focus? For example, is there a message sent when I select the console window, that I could use to perform a certain function? And is it possible to send a message within the application to bring the console to the foreground and give it focus?

Share this post


Link to post
Share on other sites
Advertisement
Here are my first thoughts on how to go about it. Trying to hook or subclass the console window is more trouble than it's worth. Dealing with WM_SETFOCUS etc is also likely to be more trouble than it's worth. However, dealing with WM_ACTIVATE might do the trick. When your GUI app receives that message check If the low-order word of wParam is WA_INACTIVE, lParam is the handle to the window being activated. Test the window being activated against the window returned by GetConsoleWindow. If it's a match, then the console is coming into focus. GetConsoleWindow is only available on XP/VISTA (it's also available on W2K but you have to use GetModuleHandle on kernel32 and GetProcAddress to load it into a function pointer). Once the console window has been opened (AllocConsole) the hwnd for it won't change so you might consider obtaining it and stashing it in a global variable or in some other variable that you can easily access later where it matters.

If you want to bring the console to the foreground, try using SetForegroundWindow.

Also note that WM_ACTIVATE is not without it's dangers: he dangers of messing with activation when handling a WM_ACTIVATE message

Share this post


Link to post
Share on other sites
I tried to use GetModuleHandle and GetProcAddress like you said, and everything compiles fine, but when I run the program I get an error. I think that the problem is with the GetModuleHandle function. Here is the code I'm using, after initiating the window and the console:


HMODULE consoleModule = GetModuleHandle("kernal32");
if (consoleModule){
FARPROC getConsoleWin = GetProcAddress(consoleModule, "GetConsoleWindow");
consoleWindow = (*getConsoleWin)();
}
else
cout<< "Couldn't load kernal32";

Share this post


Link to post
Share on other sites
It is a long time ago i used this (so i could be wrong) but should it be something like this:

getConsoleWin = (FARPROC)GetProcAddress(consoleModule, "GetConsoleWindow");
if (! getConsoleWin)
{
cout << "GetProcAddr() failed"
exit // some exit code or print to a log file.
}
consoleWindow = *getConsoleWin;

or

consoleWindow = (*getConsoleWin());

Try to debug or print something to a log.
Oh and the word is kernel32 not kernal32 !!

Good luck!
www.nextdawn.nl

Share this post


Link to post
Share on other sites
Quote:
Original post by TGPrankster
I tried to use GetModuleHandle and GetProcAddress like you said, and everything compiles fine, but when I run the program I get an error. I think that the problem is with the GetModuleHandle function. Here is the code I'm using, after initiating the window and the console:


HMODULE consoleModule = GetModuleHandle("kernal32");
if (consoleModule){
FARPROC getConsoleWin = GetProcAddress(consoleModule, "GetConsoleWindow");
consoleWindow = (*getConsoleWin)();
}
else
cout<< "Couldn't load kernal32";


Try "kernel32" or "kernel32.dll" - spelled with two e's.

According to this, adding this prototype to the file should help the linker

extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow ();

but that won't help with GetProcAddress etc.


typedef HWND (WINAPI *GetConsoleWindowT)(void);
GetConsoleWindowT getConsoleWindow = NULL;
HWND hwndConsole = NULL;
getConsoleWindow = (GetConsoleWindowT)GetProcAddress(GetModuleHandle("kernel32"), "GetConsoleWindow");
if ( NULL != getConsoleWindow ) {
hwndConsole = getConsoleWindow();
}



The reason that GetModuleHandle("kernel32") is used instead of LoadLibrary is that kernel32 will already be loaded into the process - it's 99% guaranteed. I've yet to come across an exe that didn't link with K32. That's also why I didn't check the return value. And this way you don't have to call FreeLibrary afterward.


Share this post


Link to post
Share on other sites
The code snippet I provided was straight C, but that function pointer typedef should still work in C++. Use that instead of FARPROC.

Share this post


Link to post
Share on other sites
Okay, I got the handle to the console, and got it to work. Now my last problem is that when I use the WM_ACTIVATE message and compare lParam to the console HWND, I get a compile error! How can I compare lParam to the HWND? I tried

if ((HWND)lParam == consoleHWND)

but it doesnt work either

Share this post


Link to post
Share on other sites
What specific compile error?


// from wndproc
case WM_ACTIVATEAPP :
// wparam == TRUE when window is activated, FALSE when deactivated
if ( !((BOOL)wParam) ) {
HWND other = (HWND)lParam;
if ( other == consoleHWND ) {
// activated window is console
}
}
break;


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!