Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DLL.cpp : Defines the exported functions for the DLL.
- //
- #include "pch.h"
- #include "framework.h"
- #include "DLL.h"
- #define DLLEXPORT extern "C" __declspec (dllexport)
- // GlobalHook_Test.cpp : define the export functions of DLL application .
- extern HMODULE g_hDllModule; //save the Dll handle for calling by SetWindowsHookEx()
- // to share the same memory with the host process
- #pragma data_seg("mydata")
- //define a static variable g_hHook of the type HHook, initialize it by NULL
- HHOOK g_hHook = NULL;
- #pragma data_seg()
- #pragma comment(linker, "/SECTION:mydata,RWS")
- //The Hook Callback Function GetMsgProc
- LRESULT CALLBACK GetMsgProc(
- int code,
- WPARAM wParam,
- LPARAM lParam)
- {
- //add something here for debug
- // 1. show some messages
- int msgboxID = MessageBox(
- NULL,
- (LPCWSTR)L"Hello World",
- (LPCWSTR)L"123",
- NULL
- );
- //Pass the the next hook function if there is a hook function
- // to do ...
- return ::CallNextHookEx(g_hHook, code, wParam, lParam);
- }
- // Set the global hook
- DLLEXPORT BOOL WINAPI SetGlobalHook()
- {
- //2. Use SetWindowsHookEx(p1,p2,p3,p4) to bind the
- // keyboard message p1=WH_KEYBOARD with p2=(HOOKPROC)GetMsgProc, and p3 is the Dll handle, p4 is an int value
- g_hHook = SetWindowsHookEx(WH_KEYBOARD, &GetMsgProc, g_hDllModule, NULL);
- //to do ...
- //3. you can also add mouse messages here
- //to do ...
- if (NULL == g_hHook) // failure
- {
- return FALSE;
- }
- return TRUE; //success
- }
- // Unload the global hook
- DLLEXPORT BOOL WINAPI UnsetGlobalHook()
- {
- if (g_hHook)
- {
- //4. you cna also Unhook the Windows Hook "g_hHaook" here with UnHookWindowsHookEx(p1) by
- // letting equal p1 to g_hHook
- //to do ...
- //4. you cna also Unhook the Windows Hook "g_hHook" here with UnHookWindowsHookEx(p1) by
- UnhookWindowsHookEx(g_hHook);
- MessageBox(
- NULL,
- (LPCWSTR)L"Unhooked successfuly",
- (LPCWSTR)L"Unsetgloabl Hook",
- NULL
- );
- }
- return TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement