想实现全局键盘钩子,但不知为什么SetWindowsHookEx返回的总是失败,也查了许多资料,从原理上来看实在想不出有什么错误,各位能不能帮我看一下?
/*************************************************************
*KeyBoardHookLib.h
**************************************************************/
#define EXPORT extern "C" __declspec (dllexport)
#pragma data_seg ( "shared" )
HHOOK hKeyboard = NULL ;
HINSTANCE hDllInstance = NULL ;
#pragma data_seg ()
#pragma comment ( linker, "/section:shared,rws" )
EXPORT BOOL WINAPI SetHook ( BOOL isInstall ) ;
/**************************************************************
*KeyBoardHookLib.cpp
**************************************************************/
#include <windows.h>
#include "KeyBoardHookLib.h"
int WINAPI DLLMain ( HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved )
{
hDllInstance = hInstance ;
return true ;
}
LRESULT CALLBACK KeyboardProc ( int nCode, WPARAM wParam, LPARAM lParam )
{
EVENTMSG *keyMSG = ( EVENTMSG * )lParam ;
if( ( nCode == HC_ACTION ) && ( keyMSG->message == WM_KEYDOWN ))
{
char KeyName[10] ;
ZeroMemory ( KeyName, 10 ) ;
GetKeyNameText ( lParam, KeyName, 50 ) ;
MessageBox ( NULL, KeyName, NULL, MB_OK ) ;
}
return CallNextHookEx ( hKeyboard, nCode, wParam, lParam ) ;
}
EXPORT BOOL WINAPI SetHook ( BOOL isInstall )
{
if ( isInstall )
{
hKeyboard = SetWindowsHookEx ( WH_KEYBOARD, KeyboardProc, hDllInstance, 0 ) ;
if ( hKeyboard != NULL )
return true ;
}
else
{
UnhookWindowsHookEx ( hKeyboard ) ;
hKeyboard = NULL ;
hDllInstance = NULL ;
}
return false ;
}
/*************************************************************
*KeyBoardHookTest.cpp
*************************************************************/
#include <windows.h>
#include "KeyBoardHookLib.h"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow )
{
static TCHAR szAppName[] = TEXT ( "全局键盘钩子" ) ;
WNDCLASS wndClass ;
wndClass.style = CS_HREDRAW | CS_VREDRAW ;
wndClass.lpfnWndProc = WndProc ;
wndClass.cbClsExtra = 0 ;
wndClass.cbWndExtra = 0 ;
wndClass.hInstance = hInstance ;
wndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndClass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndClass.lpszMenuName = NULL ;
wndClass.lpszClassName = szAppName ;
if ( !RegisterClass ( &wndClass ) )
{
MessageBox ( NULL, TEXT ( "This program requires Windows NT!" ),
szAppName, MB_ICONERROR ) ;
return 0 ;
}
HWND hwnd ;
hwnd = CreateWindow ( szAppName, TEXT ("实现全局键盘钩子"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL ) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
if ( SetHook ( true ) ) //安装键盘钩子函数
MessageBox ( NULL, "HOOK SUCCESS!", NULL, MB_OK ) ;
else
MessageBox ( NULL, "HOOK FAILED!", NULL, MB_OK ) ;
MSG msg ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc ( HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam )
{
switch ( message )
{
case WM_DESTROY:
SetHook ( false ) ; //卸载键盘钩子函数
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课