我想让某个线程停止工作。线程函数如下:
unsigned __stdcall keyloggingmanager::RunKeyLogger(void * args) {
UNREFERENCED_PARAMETER(args);
wcout << "RunKeyLogger Thread Started !" << endl;
HINSTANCE appInstance = NULL;
HHOOK _keyboardHook = NULL;
appInstance = GetModuleHandle(NULL); // getting the current module handle
_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, ThisObj->KeyPressHandler, appInstance, GLOBAL_HOOK); // setting the hook.
LPMSG msg = { 0 };
while (GetMessage(msg, NULL, 0, 0) != 0)
{
TranslateMessage(msg);
DispatchMessage(msg);
}
cout << "Got WM_QUIT, aborting...\n";
UnhookWindowsHookEx(_keyboardHook);
if (_keyboardHook == NULL) {
wcout << L"UNABLE TO HOOK" << endl;
}
else if(appInstance == NULL)
{
wcout << L"UNABLE TO GET MODULE HANDLE" << endl;
}
return 0;
}
用于停止线程的行如下:
PostThreadMessage(AboveThreadId, (UINT)WM_QUIT, NULL, NULL);
我使用此方法获得访问冲突异常。 我尝试了几种变体,例如:
while ( GetMessage(msg, hWnd, 0, 0) )
{
if ( msg->message == WM_QUIT )
{
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
之前有没有人遇到过这个问题?
答案 0 :(得分:2)
LPMSG msg = { 0 };
这声明了一个指向MSG
结构的指针。然后,将该指针传递给GetMessage
,但不要为指向指向的指针分配实际的MSG
结构。
替换
LPMSG msg = { 0 };
带
MSG msg;
分配MSG
结构。然后像这样打电话给GetMessage
while ( GetMessage(&msg, NULL, 0, 0) )
传递MSG
结构的地址。