• 动态链接库


    键盘鼠标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 code
    	WPARAM wParam, // message identifier
    	LPARAM lParam   // mouse coordinates
    	)
    {
    	return 1;
    }
    */
    
    LRESULT CALLBACK KeyboardProc(
    	int code,       // hook code
    	WPARAM wParam, // virtual-key code
    	LPARAM lParam   // keystroke-message information
    	)
    {
    	if(VK_F7 == wParam)
    	{	
    		//UnhookWindowsHookEx(g_MouseHook);
    		UnhookWindowsHookEx(g_KeyBoardHook);
    		return 1;
    	}
    #ifndef _DEBUG
    	if(VK_F12 == wParam)
    	{	
    		return 1;
    	}
    #endif
    
    	if (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 __cdecl
    
    namespace Common {
    	namespace Hook {
    #pragma region Declaration
    
    		typedef 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 Definition
    		KeyHooker::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___





  • 相关阅读:
    PowerDesigner生成SQL的冒号设置
    Linux/Windows 一键获取当前目录及子目录下所有文件名脚本
    Target runtime jdk1.8.0_181 is not defined
    windows——任务计划程序
    12篇文章回顾总结
    《逆商》2月12日
    《终身成长》2月11日
    《心流》 什么才是真正的幸福
    《心流》 什么才是真正的幸福 2月7日
    《高效能人士的7个习惯》 2月3日
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671668.html
Copyright © 2020-2023  润新知