Передача CALLBACK из DLL в EXE - PullRequest
3 голосов
/ 06 апреля 2011

У меня есть DLL, которую я использую, чтобы подключить Блокнот и ловить нажатия клавиш. Все работает хорошо, но я хотел бы получить доступ к KeyboardHook Callback из моего exe. Чтобы каждый раз, когда вызывался обратный вызов в .dll, он передавал свои значения в .exe. Возможно ли это?

** HOOK.dll (hookDll.cpp) **

#include "windows.h"

#pragma data_seg (".SHARED")  
     HHOOK keyboardHook = 0;  
     HINSTANCE g_hInstance = 0;  
#pragma data_seg()  

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

extern "C" __declspec(dllexport) LRESULT CALLBACK KeyboardHook(int nCode, WPARAM wParam, LPARAM lParam)
{
    if(0 > nCode)
    return CallNextHookEx(keyboardHook, nCode, wParam, lParam);

    {
        MessageBox(NULL, L"Got Keyboard Event !", L"Event", 0);
    }

    return CallNextHookEx(keyboardHook, nCode, wParam, lParam);
}

extern "C" __declspec(dllexport) bool InstallKeyboardHook(unsigned long threadID)
{
    keyboardHook = SetWindowsHookEx(WH_KEYBOARD, &KeyboardHook, g_hInstance, threadID);
    return true;
}

extern "C" __declspec(dllexport) bool UnInstallKeyboardHook()
{
    UnhookWindowsHookEx(keyboardHook);  
    return true;
}

BOOL APIENTRY DllMain( HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved ) 
{     
switch (ul_reason_for_call){
    case DLL_PROCESS_ATTACH:
        g_hInstance  = (HINSTANCE) hModule;
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
}     
return TRUE; 
}

** Hook.exe (hook.h) **

#pragma once

#include "afxwin.h"

typedef bool(*InstallHook)(unsigned long);
typedef bool(*UnInstallHook)();
typedef LRESULT (*KeyboardHook)(int nCode, WPARAM wParam, LPARAM lParam);

LRESULT CALLBACK MyCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
    //Id like this to get called whenever the CALLBACK in the dll is called
    MessageBox(NULL, L"test", L"test", 0);
    return NULL;
}


class CTrayMeApp : public CWinApp
{
public:
CTrayMeApp(){};
~CTrayMeApp(){};

virtual BOOL InitInstance();
};

extern CTrayMeApp theApp;

** Hook.exe (hook.cpp) **

#include "hook.h"

CTrayMeApp theApp;

BOOL CTrayMeApp::InitInstance()
{
BOOL bReturn = FALSE;

CWinApp::InitInstance();

HMODULE hModule = LoadLibrary(_T("Hook.dll"));

//Get the function address which installs the keyboard events filter.
InstallHook fpKeyboardHook = (InstallHook) GetProcAddress(hModule,"InstallKeyboardHook");

//Install the Keyboard Event Hook (Filter).
fpKeyboardHook(GetTargetThreadIdFromWindow("Notepad", "Untitled - Notepad"));

//Install the Callback
KeyboardHook cpKeyboardHook = (KeyboardHook) GetProcAddress(hModule,"_KeyboardHook@12");

//OK, are callback is here, now how do we make use of it?
KeyboardHook(MyCallback); //of course this won't work, but it gives an idea of what I'm trying to do.

FreeLibrary(hModule);

Sleep(10000); //give us 10 seconds to test it out
return bReturn;
}

1 Ответ

4 голосов
/ 22 декабря 2011

Внутри вашей DLL:

LRESULT CALLBACK (*myCallbackRef)(int nCode, WPARAM wParam, LPARAM lParam);

extern "C" __declspec(dllexport) void InstallMyHook(LRESULT CALLBACK (*MyCallback)(int nCode, WPARAM wParam, LPARAM lParam))
{
   myCallbackRef = MyCallback;
}

Используйте myCallbackRef с параметрами внутри KeyboardHook.

В вашей программе Звоните

InstallMyHook(MyCallBack);
...