I threw together some code that installs a system-wide hook, and some DBP code that uses it. The hook, however, fails to work. I'll put it here so you can play with it. I think it fails because if I remember correctly, DirectX uses a system-wide keyboard hook for DirectInput.
Here is the C++ DLL code:
#include <windows.h>
#include <stdio.h>
#define EXPORT extern "C" __declspec(dllexport)
HINSTANCE g_hInstance;
HHOOK g_HookID;
HWND g_hWnd;
#pragma argsused
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
g_hInstance = hinstDLL;
return 1;
}
//---------------------------------------------------------------------------
EXPORT int InstallHook(LPSTR WindowTitle)
{
HOOKPROC hProcAddress;
HINSTANCE hDLLInstance;
HHOOK hHook;
char msg[255];
HWND hWnd;
hDLLInstance = g_hInstance;
// Find the Window with the title specified.
hWnd = FindWindow(NULL, WindowTitle);
if (hWnd == NULL) {
sprintf(msg, "Cannot locate window with title: %s", WindowTitle);
MessageBox(NULL, msg, "Test", MB_OK);
return 0;
}
g_hWnd = hWnd;
// Get the address of the hook procedure.
hProcAddress = (HOOKPROC) GetProcAddress(hDLLInstance, "KeyboardProc");
if (hProcAddress == NULL) {
sprintf(msg, "Error getting procedure address.");
MessageBox(NULL, msg, "Test", MB_OK);
return 0;
}
hHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC) hHook, hDLLInstance, 0);
if (hHook == NULL) return 0;
MessageBox(NULL, "Hook installed.", "Test", MB_OK);
g_HookID = hHook;
return (int) hHook;
}
EXPORT void RemoveHook(int HookID)
{
g_HookID = 0;
UnhookWindowsHookEx((HHOOK) HookID);
}
EXPORT LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
char msg[255];
static bool alreadyHere = false;
/* if (nCode < 0)
return CallNextHookEx(g_HookID, nCode, wParam, lParam);
*/
// Process keys here.
if (!alreadyHere) {
alreadyHere = true;
sprintf(msg, "You are in the procedure.");
MessageBox(NULL, msg, "Test", MB_OK);
}
return CallNextHookEx(g_HookID, nCode, wParam, lParam);
}
And here is the DBP code that uses it...
` Test of the HookInstaller.dll
load dll "HookInstaller.dll", 1
HookID = call dll(1, "_InstallHook", "test")
print "Hook ID = "; str$(HookID)
print "Press spacebar to quit."
do
if spacekey() = 1 then exit
loop
if result <> 0
temp = call dll(1, "_RemoveHook", HookID)
endif
If you get this to work properly, let me know. When the DBP code is executed, you get a HookID returned properly. But the actuall hook does not work...
Good luck... Wish I had more time to play with this...