Global Keyhook用于32/64位应用程序64位Windows

时间:2015-07-21 09:03:18

标签: c++ windows

我正在尝试安装全局键盘钩子并在每个DLL_PROCESS_ATTACH上通知尝试子类前景窗口。 64位和32位应用程序都有单独的exe和dll。如果只有一种类型(32或64位)exe和dll配对但一次不能同时工作,我的工作正常。如果我先使用32位挂钩然后再使用64位,那么我没有获得32位应用程序的DLL_ATTACH_NOTIFICATION。由于这个原因我无法继承32位应用程序。
DLL的示例代码,包括32位和64位不同的名称

`//#include "stdafx.h"
#include <tchar.h>
#include <process.h>

#pragma data_seg(".HOOKDATA")//Shared data among all instances.
HHOOK g_hKbdHook = NULL;
#pragma data_seg()

#pragma comment(linker, "/SECTION:.HOOKDATA,RWS")//linker directive

#pragma data_seg(".HOOKDATA32")//Shared data among all instances.
HHOOK g_hKbdHook32 = NULL;
#pragma data_seg()

#pragma comment(linker, "/SECTION:.HOOKDATA32,RWS")//linker directive

HINSTANCE g_hInstance = NULL;
HINSTANCE g_hInstance32 = NULL;
HWND hwndKeyFocused = NULL;


static HWND hwndOld;
static HWND hwndFocused;
static WNDPROC oldWindowProc;

BOOL APIENTRY
DllMain(
    HANDLE hModule,
    DWORD ul_reason_for_call,
    LPVOID lpReserved
    )
{
    DWORD dw;
    int iRet;
    HWND hwnd;
    BOOL boIs32bit;
    DWORD dwProcID;
    HANDLE hProcess;
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        dwProcID = GetCurrentProcessId();
        hProcess = OpenProcess(
                            PROCESS_VM_READ                 |
                            PROCESS_VM_WRITE                |
                            PROCESS_VM_OPERATION            |
                            PROCESS_CREATE_THREAD           |
                            PROCESS_QUERY_INFORMATION,
                            FALSE,
                            dwProcID
                            );

        IsWow64Process(
                    hProcess,
                    &boIs32bit
                    );
        if (TRUE == boIs32bit)
        {
            g_hInstance32 = (HINSTANCE)hModule;;
        }
        else
        {
            g_hInstance = (HINSTANCE)hModule;
        }

        hwnd = GetForegroundWindow();
        oldWindowProc =(WNDPROC) SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)HackingWndProc);
        dw = GetLastError();
        hwndOld = hwnd;
        break;

    case DLL_PROCESS_DETACH:
        SetWindowLongPtr(hwndOld, GWLP_WNDPROC, (LONG_PTR)oldWindowProc );
        break;

        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
            break;
    }

    g_hKbdHook = NULL;

    return TRUE;
}

BOOLEAN __stdcall
Init(
    )
{

    g_hKbdHook = NULL;

    g_hKbdHook = SetWindowsHookEx(WH_KEYBOARD, ProcessKey, g_hInstance, NULL);
    if(g_hKbdHook==NULL)
    {
        return FALSE;
    }

    return TRUE;
}

BOOLEAN __stdcall
Init32(
    )
{
    g_hKbdHook32 = NULL;

    g_hKbdHook32 = SetWindowsHookEx(WH_KEYBOARD, ProcessKey32, g_hInstance32, NULL);
    if(g_hKbdHook32 == NULL)
    {
        return FALSE;
    }

    return TRUE;
}

BOOLEAN __stdcall
DeInit(
    )
{
    UnhookWindowsHookEx(g_hKbdHook);
    if (NULL != g_hKbdHook32)
    {
        UnhookWindowsHookEx(g_hKbdHook32);
    }

    return TRUE;
}

LRESULT CALLBACK
ProcessKey(
    int ncode,
    WPARAM wparam,
    LPARAM lparam
    )
{
    return (CallNextHookEx(g_hKbdHook, ncode, wparam, lparam));//pass control to next hook in the hook chain.
}

LRESULT CALLBACK HackingWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    return CallWindowProc(
                        oldWindowProc,
                        hWnd,
                        message,
                        wParam,
                        lParam
                        ); 
}

LRESULT CALLBACK
ProcessKey32(
    int ncode,
    WPARAM wparam,
    LPARAM lparam
    )
{
    return (CallNextHookEx(g_hKbdHook32, ncode, wparam, lparam));//pass control to next hook in the hook chain.
}

enter code here

`

0 个答案:

没有答案