将 CALLBACK 从 dll 传递到 EXE

发布于 2024-10-30 18:18:26 字数 2821 浏览 6 评论 0原文

我有一个 DLL,用来挂接记事本并捕获击键。一切正常,但我想从我的 exe 中访问 KeyboardHook 回调。这样,每次触发 .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;
}

I have a DLL that I use to hook the Notepad and catch keystrokes. All works well, but I'd like to have access to the KeyboardHook Callback from within my exe. So that everytime the Callback in the .dll is triggered, it passes it's values to the .exe Is this possible?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

π浅易 2024-11-06 18:18:27

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

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

Use myCallbackRef with parameters inside KeyboardHook.

In your program
Call

InstallMyHook(MyCallBack);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文