messagehook.cpp
00001 #define STRICT
00002 #include <windows.h>
00003
00004 #define MY_NORTIFY_MOUSE (WM_APP + 100)
00005 #define MY_NORTIFY_KEY (WM_APP + 101)
00006
00007 HINSTANCE hInstDLL;
00008
00009 #pragma data_seg(".sharedata")
00010 HWND hWndtgt = NULL;
00011 HHOOK hhook;
00012 #pragma data_seg()
00013
00014
00015 int WINAPI DllMain(HINSTANCE hInstance, DWORD dwNotification, LPVOID lpReserved) {
00016 UNREFERENCED_PARAMETER(hInstance);
00017 UNREFERENCED_PARAMETER(lpReserved);
00018
00019 hInstDLL = hInstance;
00020 return TRUE;
00021 }
00022
00023
00024 LRESULT CALLBACK hookproc(INT hc, WPARAM wParam, LPARAM lParam) {
00025 if (hc >= 0) {
00026 if (hc == HC_ACTION) {
00027 PMSG pmesg = (PMSG)lParam;
00028
00029 switch (pmesg->message) {
00030 case WM_LBUTTONDOWN:
00031 PostMessage(hWndtgt, MY_NORTIFY_MOUSE, pmesg->message, pmesg->lParam);
00032 break;
00033
00034 case WM_KEYDOWN:
00035 PostMessage(hWndtgt, MY_NORTIFY_KEY, pmesg->message, pmesg->wParam);
00036 break;
00037 }
00038 }
00039 }
00040 return CallNextHookEx(hhook, hc, wParam, lParam);
00041 }
00042
00043
00044 int makehook(HWND hWnd) {
00045 hWndtgt = hWnd;
00046
00047 hhook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)hookproc, hInstDLL, 0);
00048 if (hhook == NULL) {
00049 return -1;
00050 }
00051 return 0;
00052 }
00053
00054
00055 void unhook(void) {
00056 if (hhook != NULL) {
00057 UnhookWindowsHookEx(hhook);
00058 }
00059 }
00060