WinAPI LowLevel Keyboard Hooks

DLimited tanojoshu at googlemail.com
Thu Jul 19 09:52:52 PDT 2012


On Thursday, 19 July 2012 at 16:38:19 UTC, DLimited wrote:
>>
>> I guess you have to 'export' the function:
>> extern (Windows) export LRESULT LowLevelKeyboardProc(int code, 
>> WPARAM
>> wParam, LPARAM lParam)
>>
>> and include
>> EXPORTS
>>  LowLevelKeyboardProc
>>
>> in the .DEF file
>
> Thanks, I changed that. Also, I changed LoadLibraryW( ) to
> LoadLibraryA( ) in the main program and now it works (kinda). I
> feel stupid now, although I still don't get why it wouldn't work
> with LoadLibraryW.

Acutally, that was only the half-truth. The .dll seems to get 
loaded correctly ( GetLastError returns 0), but my 
keyboard-presses aren't captured at all.
My system seems to freeze up for ~5sec, after which everything 
resumes. Any keyboard input seems to get buffered and is 
processed by my terminal after my program closes.

Also I'm unsure about types because the often-used HHOOK is not 
defined with my imports, so I'm left guessing what it is. I used 
the type int function() instead (my best guess).

Here's the code:

< ------- CODE BEGIN --------- >

import std.c.windows.windows;
import std.stdio;
import core.thread;

extern (C) void gc_init();
extern (C) void gc_term();
extern (C) void _minit();
extern (C) void _moduleCtor();
extern (C) void _moduleDtor();
extern (C) void _moduleUnitTests();
extern (Windows) int function() SetWindowsHookExA( int idHook,
     HOOKPROC lpfn,
     HINSTANCE hMod,
     DWORD dwThreadId );
extern (Windows) struct KBDLLHOOKSTRUCT {
   DWORD     vkCode;
   DWORD     scanCode;
   DWORD     flags;
   DWORD     time;
   ULONG_PTR dwExtraInfo;
};
extern (Windows) LRESULT CallNextHookEx(
     int function() hhk,
       int nCode,
        WPARAM wParam,
         LPARAM lParam
);

extern (Windows) bool UnhookWindowsHookEx(
    int function() hhk
);

extern (Windows) HMODULE LoadLibraryA(
    LPCTSTR lpFileName
);


	
extern (Windows)
int WinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int nCmdShow)
{
     int result;

     gc_init();			// initialize garbage collector
     _minit();			// initialize module constructor table

     try
     {
	_moduleCtor();		// call module constructors
	//_moduleUnitTests();	// run unit tests (optional)

	result = myWinMain(hInstance, hPrevInstance, lpCmdLine, 
nCmdShow);

	_moduleDtor();		// call module destructors
     }

     catch (Exception o)		// catch any uncaught exceptions
     {
	MessageBoxA(null, cast(char *)o.toString(), "Error",
		    MB_OK | MB_ICONEXCLAMATION);
	result = 0;		// failed
     }

     gc_term();			// run finalizers; terminate garbage collector
     return result;
}

int myWinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int nCmdShow)
{
	
	HOOKPROC hkprcSysMsg;
	HINSTANCE hinstDLL;
	extern (Windows) int function() hhookSysMsg;

hinstDLL = 
LoadLibraryA(cast(LPCTSTR)"correct_absolute_path\\keydll.dll");
writeln(GetLastError()); //returns 0
hkprcSysMsg = cast(HOOKPROC)GetProcAddress(hinstDLL, 
"LowLevelKeyboardProc");
writeln(GetLastError()); //return 0
hhookSysMsg = SetWindowsHookExA(
                     13,
                     hkprcSysMsg,
                     hinstDLL,
                     0);
	
	writeln(GetLastError()); // returns 0 aswell
	Thread.sleep( dur!("seconds")(10) );
	UnhookWindowsHookEx( hhookSysMsg );

	return 0;
}


More information about the Digitalmars-d-learn mailing list