动态链接库

来源:互联网 发布:技术研究工程师 知乎 编辑:程序博客网 时间:2024/06/13 00:46

键盘鼠标HOOK Demo

1,------------------------ 导出代码

// keyHooker.cpp : 定义 DLL 应用程序的导出函数。//#include "stdafx.h"#include "windows.h"HINSTANCE h_st;HHOOK g_KeyBoardHook;//HHOOK g_MouseHook;HWND g_wnd;/*LRESULT CALLBACK MouseProc(int nCode,      // hook codeWPARAM wParam, // message identifierLPARAM lParam   // mouse coordinates){return 1;}*/LRESULT CALLBACK KeyboardProc(int code,       // hook codeWPARAM wParam, // virtual-key codeLPARAM lParam   // keystroke-message information){if(VK_F7 == wParam){//UnhookWindowsHookEx(g_MouseHook);UnhookWindowsHookEx(g_KeyBoardHook);return 1;}#ifndef _DEBUGif(VK_F12 == wParam){return 1;}#endifif (VK_ESCAPE == wParam){return 1;}return CallNextHookEx(g_KeyBoardHook, code, wParam, lParam);}void SetHook(HWND hwnd){g_wnd=hwnd;//g_MouseHook=SetWindowsHookEx(WH_MOUSE,MouseProc,GetModuleHandle(L"keyHooker"),0);g_KeyBoardHook=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle(L"keyHooker"),0);}

2, 动态导入代码

#ifndef ___KEYHOOKER_H___#define ___KEYHOOKER_H___#include "stdafx.h"#include <cassert>#include <WinBase.h>#define KEYAPI __cdeclnamespace Common {namespace Hook {#pragma region Declarationtypedef void (KEYAPI *Proc_SetHook)(HWND h);class KeyHooker {public:void SetHook(HWND h);private:Proc_SetHook pSetHook;// dll 句柄HINSTANCE _hmsc;KeyHooker();virtual ~KeyHooker();friend class std::auto_ptr<KeyHooker>;static std::auto_ptr<KeyHooker> _instance;public:static KeyHooker* Inst() {if (0 == _instance.get()){  _instance.reset(new KeyHooker);  }  return _instance.get(); }};  // end of KeyHooker#pragma endregion#pragma region DefinitionKeyHooker::KeyHooker() {HINSTANCE hmscdll = LoadLibraryEx(_T("./keyHooker.dll"),NULL,0);assert(hmscdll != NULL);_hmsc = hmscdll;}KeyHooker::~KeyHooker() {if (_hmsc != NULL) {FreeLibrary(_hmsc);}}std::auto_ptr<KeyHooker> KeyHooker::_instance; void KeyHooker::SetHook(HWND h) {pSetHook = (Proc_SetHook)GetProcAddress(_hmsc,"SetHook");assert(pSetHook != NULL);return pSetHook(h);}#pragma endregion}}#endif // end of define ___KEYHOOKER_H___





4 0
原创粉丝点击