Вот так?
Код | // DLLHook.cpp : Defines the exported functions for the DLL application. //
#include "stdafx.h" #include "DLLHook.h"
#define MUTEX_NAME TEXT("MySuperMutex") #define VK_C 0x043 void HookThreadRoutine(void* ptr); int CALLBACK MousedProc(int nCode, WPARAM wParam, LPARAM lParam); int CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam); HHOOK hMouseHook; HHOOK hKeyboardHook; POINT py ; BOOL moved; extern "C" { DLLHOOK_API void start(void) { printf("!!START\n"); HANDLE hMutex = CreateMutex(NULL, TRUE, MUTEX_NAME); if(ERROR_ALREADY_EXISTS == GetLastError()) return; //do nothinCCg - already started
_beginthread(HookThreadRoutine, 0, hMutex); GetCursorPos(&py); moved = false; } //------------------------------------------------------------- DLLHOOK_API void stop(void) { printf("!!STOP\n"); HANDLE hMutex = CreateMutex(NULL, TRUE, MUTEX_NAME); if(ERROR_ALREADY_EXISTS != GetLastError()) return; //not started ? ReleaseMutex(hMutex); //release lock CloseHandle(hMutex); //finally close mutex SendMessage(NULL, WM_NULL, 123456789, 1); } };
//------------------------------------------------------------- void HookThreadRoutine(void*hMutexPtr) { HANDLE hMutex = static_cast<HANDLE>(hMutexPtr); MSG msg = {0}; // структура сообщения hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, (HOOKPROC)&MousedProc, GetModuleHandle(NULL), 0); hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)&KeyboardProc, GetModuleHandle(NULL), 0);
while(GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg);
if (msg.wParam == 123456789) if(WAIT_OBJECT_0 == WaitForSingleObject(hMutex, 0)) { UnhookWindowsHookEx(hMouseHook); break; //stop the thread } } }
|
я так понимаю так не правильно? как правильно? |