WinAPI LowLevel Keyboard Hooks

DLimited tanojoshu at googlemail.com
Thu Jul 19 08:49:47 PDT 2012


But what are the differences of loading the Unicode version vs. 
the ANSI version? I called the Unicode one because I figured that 
would be the sensible choice, since Unicode is the default for D 
(if I remember correctly). I have no clue what the actual effects 
of calling the wrong version would be.

Anyway, here's the of my .dll:

< ------ Code begin ------ >

import std.c.windows.windows;
import core.sys.windows.dll;
import core.runtime;


extern (C) void gc_init();
extern (C) void gc_term();
extern (C) void _minit();
extern (C) void _moduleCtor();
extern (C) void _moduleDtor();

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
);


__gshared HINSTANCE g_hInst;


  extern (Windows) BOOL DllMain(HINSTANCE hInstance, ULONG 
ulReason, LPVOID pvReserved) {
	return true;
	switch (ulReason) {
		case DLL_PROCESS_ATTACH:
		g_hInst = hInstance;
		Runtime.initialize;
		//dll_process_attach( hInstance, true );
		break;
		
		case DLL_PROCESS_DETACH:
		dll_process_detach( hInstance, true );
		break;

		case DLL_THREAD_ATTACH:
		dll_thread_attach( true, true );
		break;
		case DLL_THREAD_DETACH:
		dll_thread_detach( true, true );
		break;
		
		default:
		return true;
	}
	return true;
}

extern (Windows) LRESULT LowLevelKeyboardProc(int code, WPARAM 
wParam, LPARAM lParam)
{
     KBDLLHOOKSTRUCT* details = cast(KBDLLHOOKSTRUCT*) lParam;
	MessageBoxA(null, cast(char *)"WHOA", "Error",
		    MB_OK | MB_ICONEXCLAMATION);
     if(code == 0 && wParam == WM_KEYDOWN)
     {
         if(details.vkCode == 0x41)
         {
			
             return 1;
         }
     }

     return CallNextHookEx(null, code, wParam, lParam);
}

< ------ Code End ------ >

Lots of copy&paste was used. I injected some senseless code to 
try and check if a specific function ever gets called, though I 
now realise the DllLoad itself is what fails. Haven't cleaned it 
back up yet, though.

The .def file contains the following: (including newlines)

< ------ .DEF BEGIN ------ >
LIBRARY "keydll.dll"
EXETYPE NT
SUBSYSTEM WINDOWS
CODE PRELOAD
DATA PRELOAD
< ------ .DEF END   ------ >


I compiled the dll using:
dmd -ofkeydll.dll -L/IMPLIB keydll.d keydll.def

No linker/compiler errors.


More information about the Digitalmars-d-learn mailing list