2011-04-05 81 views
2

我有一個DLL,用於鉤住記事本並捕獲按鍵。一切運作良好,但我想從我的exe文件中訪問KeyboardHook回調。因此,每次在.dll中的回調被觸發時,它都會將其值傳遞給.exe這可能嗎?將DLL從CALL傳遞到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(鉤。 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,"[email protected]"); 

//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; 
} 
+0

這看起來回答。你還需要什麼? – 2011-04-10 21:10:50

回答

3

內,您的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); 
+0

作品完美,謝謝 – aquawicket 2012-06-04 01:30:09