问题
I try to make the global hook, in order to catch the number from keyboard, change it, and pass it. So for example: if I type "0" in google, the software will change it, and the "1" is inserted instead. In order to learn, how the hooks works, I used WH_KEYBOARD in order to get all keys inserted into my pc. And it worked. However, now I would like to change the key globally, and in order to do that I use WH_GETMESSAGE. And in this case, hook isn't applied globally, but locally instead(in test window). Why is it working in that way, and how to repair it?
The code of my dll:
void testHook() {
HINSTANCE hCurrentDll = GetModuleHandle("testDll.dll");
// g_HookHandle = SetWindowsHookEx(WH_KEYBOARD, TestForHook, hCurrentDll, 0);
g_HookHandle = SetWindowsHookEx(WH_GETMESSAGE, &TestForHook, hCurrentDll, 0);
if(g_HookHandle == NULL)
throw 1;
}
void untestHook() {
if (!UnhookWindowsHookEx(g_HookHandle))
throw 1;
g_HookHandle = NULL;
}
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
//LRESULT CALLBACK TestForHook(int code, WPARAM wParam, LPARAM lParam)
//{
// if( code < 0 ) return CallNextHookEx( 0, code, wParam, lParam );
//
// if( wParam == 0x30)
// {
// std::cout << wParam;
// wParam = 0x31;
// std::cout << wParam;
// return CallNextHookEx( 0, code, wParam, lParam );
// }
//
// return CallNextHookEx( 0, code, wParam, lParam );
//}
LRESULT CALLBACK TestForHook(int code, WPARAM wParam, LPARAM lParam)
{
MSG *lpmsg;
if( code < 0 ) return CallNextHookEx( 0, code, wParam, lParam );
lpmsg = (MSG *)lParam;
std::cout << "dziala";
if((lpmsg ->message) == 258 || (lpmsg ->message) == 257)
{
if((lpmsg -> wParam) == 48)
lpmsg -> wParam = 57;
std::cout << lpmsg -> wParam << " " << lpmsg -> lParam << " " << lpmsg->hwnd << std::endl;
}
return CallNextHookEx( 0, code, wParam, lParam );
}
and the code of my testing program:
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
WNDCLASSEX wc;
std::cout << kwadrat(5);
wc.cbSize = sizeof( WNDCLASSEX );
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground =( HBRUSH )( COLOR_WINDOW + 1 );
wc.lpszMenuName = NULL;
wc.lpszClassName = NazwaKlasy;
wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
g_MyHook = NULL;
if( !RegisterClassEx( & wc ) )
{
MB_ICONEXCLAMATION | MB_OK );
return 1;
}
hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, NazwaKlasy, "Oto okienko", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL );
try{
testHook();
}
catch(int e)
{
if (e == 1)
return 1;
}
if( hwnd == NULL )
{
return 1;
}
ShowWindow( hwnd, nCmdShow );
UpdateWindow( hwnd );
while( GetMessage( & Komunikat, NULL, 0, 0 ) )
{
TranslateMessage( & Komunikat );
DispatchMessage( & Komunikat );
}
return Komunikat.wParam;
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_CLOSE:
DestroyWindow( hwnd );
try{
untestHook();
}
catch(int e)
{
if (e == 1)
return 1;
}
}
break;
case WM_DESTROY:
if(GLOBAL){
try{
untestHook();
}
catch(int e)
{
if (e == 1)
return 1;
}
break;
default:
return DefWindowProc( hwnd, msg, wParam, lParam );
}
return 0;
}
Thanks in advance!! I am using code::blocks
来源:https://stackoverflow.com/questions/26869973/global-hook-using-wh-getmessage-and-wh-keyboard