Place a Windows Hook in a wxWidgets Application Topic is solved

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
Angels-Requiem
In need of some credit
In need of some credit
Posts: 6
Joined: Fri Nov 30, 2007 10:57 am

Place a Windows Hook in a wxWidgets Application

Post by Angels-Requiem »

Hey there,

I have some problems to connect a hook in my application.

Perhaps somebody has another suggestion:

I want to intercept ALT+F, CTRL+ALT+DEL, STRG+ESC, ALT+TAB and the Windows-Key.

At this point I can catch them, but I want to complete intercept all trys from the user to close my application or to switch to another program or the windows desktop. (There is an exit button, which the user has to use ;) )



Back to captain hook... As I don't know another way yet, I am trying to get this to work.


It is from an example.




Code: Select all

HHOOK SMSMainframe::ghhookKB = NULL;
//---------------------------------------------------------------------------

void __stdcall SMSMainframe::SetHook(void)
{
	ghhookKB = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)CheckKey,hinst, NULL);
}
//---------------------------------------------------------------------------

void __stdcall SMSMainframe::RemoveHook(void)
{
	UnhookWindowsHookEx(ghhookKB);
}
//---------------------------------------------------------------------------

LRESULT CALLBACK SMSMainframe::CheckKey(int nCode, WORD wParam, LONG lParam)
{
	bool bEatKeyStroke = false;

	if (nCode == HC_ACTION)
	{
		switch (wParam)
		{
		case WM_KEYDOWN:
		case WM_SYSKEYDOWN:
		case WM_KEYUP:
		case WM_SYSKEYUP:

			PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) lParam;

			bEatKeyStroke =
				((p->vkCode == VK_TAB) && ((p->flags & LLKHF_ALTDOWN) != 0)) ||
				((p->vkCode == VK_ESCAPE) && ((p->flags & LLKHF_ALTDOWN) != 0)) ||
				((p->vkCode == VK_ESCAPE) && ((GetKeyState(VK_CONTROL) & 0x8000) != 0));
			break;
		}
	}

	return(bEatKeyStroke ? 1 : CallNextHookEx(ghhookKB, nCode, wParam, lParam));
}

/*static HHOOK ghhookKB;
static void __stdcall SetHook(void);
static void __stdcall RemoveHook(void);*/

static LRESULT CALLBACK CheckKey(int, WORD,LONG);

My main problem is the SetHook Funktion from WinAPI. It wants a pointer to an instance (hinst). I don't get the solution...

ghhookKB = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)CheckKey,hinst, NULL);


The Funktions are declared in the main window class. SMSMAinframe. It's a child of wxFrame.

Thank your for your help and hints.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

Code: Select all

HINSTANCE hinst = GetModuleHandle(NULL);
For the rest, you can probably use the example as it is.
Use the source, Luke!
Angels-Requiem
In need of some credit
In need of some credit
Posts: 6
Joined: Fri Nov 30, 2007 10:57 am

Post by Angels-Requiem »

Argh... I thought only in wx... I'd better checked the WinAPI...

The definition of hinst must be placed in SetHook, I suppose. K. I'll give it a try. (If Wake-up On Lan works this time and my PC will start at the office.)

Thank you for your help.
Angels-Requiem
In need of some credit
In need of some credit
Posts: 6
Joined: Fri Nov 30, 2007 10:57 am

Post by Angels-Requiem »

AOK... Works perfect. Thanks and have a nice week.
Post Reply