问题已经解决了,我那样做是可以的,但是要在DLL_PROCESS_ATTACH下另开新线程,然后在新线程中调用钩子,而且在设置了SetWindowsHookEx后,要添加消息循环队列的处理代码部分,如下:
g_hKey = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_hInstDLL, dwThreadID);
if(g_hKey == NULL)
{
ret = GetLastError();
sprintf(buff,"%d",ret);
MessageBox(NULL,"Set hook keyboard error ",buff,0);
exit(0);
}
else
{
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
g_hKey = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_hInstDLL, dwThreadID);
if(g_hKey == NULL)
{
ret = GetLastError();
sprintf(buff,"%d",ret);
MessageBox(NULL,"Set hook keyboard error ",buff,0);
exit(0);
}
else
{
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}