使用WH_GETMESSAGE和WH_KEYBOARD的全局钩子

时间:2014-11-11 16:44:39

标签: c++ winapi hook msdn keyboard-hook

我尝试创建全局钩子,以便从键盘中捕获数字,更改它并传递它。例如:如果我在谷歌中键入“0”,软件将更改它,而是插入“1”。为了学习钩子是如何工作的,我使用了WH_KEYBOARD来将所有键插入到我的电脑中。它奏效了。但是,现在我想全局更改密钥,为了做到这一点,我使用WH_GETMESSAGE。在这种情况下,钩子不是全局应用,而是局部应用(在测试窗口中)。为什么它以这种方式工作,以及如何修复它?

我的代码:

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 );
}

和我测试程序的代码:

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;
}

提前致谢!!我正在使用code :: blocks

0 个答案:

没有答案