将 CALLBACK 从 dll 传递到 EXE
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 DLL 内部:
使用
myCallbackRef
以及KeyboardHook
内的参数。在你的程序中
称呼
Inside your DLL:
Use
myCallbackRef
with parameters insideKeyboardHook
.In your program
Call