转载:http://blog.csdn.net/easysec/article/details/8833457
转载:http://www.vckbase.com/module/articleContent.php?id=567&title=
用VS创建一个空的动态库(dll)工程ShieldScreen
ShieldScreen.h文件
1 #ifndef _SHIELDSCREEN_H_ 2 #define _SHIELDSCREEN_H_ 3 #include <windows.h> 4 5 #ifdef _cplusplus 6 extern "C" 7 { 8 #endif 9 void WINAPI UnInstallLaunchEv();//安装钩子 10 void WINAPI InstallLaunchEv();//去掉钩子 11 #ifdef _cplusplus 12 }; 13 #endif 14 15 #endif
ShieldScreen.cpp文件
1 // ShieldScreen.cpp : Defines the exported functions for the DLL application. 2 // 3 4 #include "stdafx.h" 5 #include <stdio.h> 6 7 HHOOK Hook; 8 9 LRESULT CALLBACK LauncherHook(int nCode, WPARAM wParam, LPARAM lParam); 10 void strerror(DWORD errno); 11 void savelog(const char* s); 12 extern HMODULE hHookDll; 13 14 15 _declspec(dllexport) void WINAPI UnInstallLaunchEv()//dll的导出函数 16 { 17 UnhookWindowsHookEx(Hook); 18 } 19 20 _declspec(dllexport)void WINAPI InstallLaunchEv()//dll的导出函数 21 { 22 Hook = SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)LauncherHook,hHookDll,0); 23 24 if (Hook == NULL) 25 { 26 strerror(GetLastError()); 27 } 28 } 29 30 LRESULT CALLBACK LauncherHook(int nCode, WPARAM wParam, LPARAM lParam) 31 { 32 KBDLLHOOKSTRUCT *Key_Info = (KBDLLHOOKSTRUCT*)lParam; 33 34 if (nCode == HC_ACTION) 35 { 36 if ( WM_KEYDOWN == wParam || WM_SYSKEYDOWN == wParam ) 37 { 38 BOOL b_lctrl = ::GetAsyncKeyState(VK_LCONTROL) ;//如果左边Ctrl按下 39 BOOL b_rctrl = ::GetAsyncKeyState(VK_RCONTROL) ;//如果右边Ctrl按下 40 BOOL b_lAlt = ::GetAsyncKeyState(VK_LMENU) ;//如果左边Alt按下 41 BOOL b_rAlt = ::GetAsyncKeyState(VK_RMENU) ;//如果右边Alt按下 42 43 if ( Key_Info->vkCode == VK_SNAPSHOT ) 44 { 45 return TRUE; 46 } 47 else if (b_lctrl && b_lAlt) 48 { 49 switch (wParam) 50 { 51 case 41: //(‘A‘) 52 break; 53 default: 54 break; 55 } 56 return TRUE; 57 } 58 else if (b_rctrl && b_rAlt) 59 { 60 switch (wParam) 61 { 62 case 41://(‘A‘) 63 break; 64 default: 65 break; 66 } 67 return TRUE; 68 } 69 else if (b_lAlt) 70 { 71 switch (wParam) 72 { 73 case 41://(‘A‘) 74 break; 75 default: 76 break; 77 } 78 return TRUE; 79 } 80 else if (b_rAlt) 81 { 82 switch (wParam) 83 { 84 case 41://(‘A‘) 85 break; 86 default: 87 break; 88 } 89 return TRUE; 90 } 91 } 92 } 93 94 return CallNextHookEx(Hook,nCode,wParam,lParam); 95 } 96 97 98 void savelog(const char* s)//向D盘根目录下的my.log文件写log信息 99 { 100 FILE* p; 101 errno_t err= fopen_s(&p,"D:\\my.log", "a+");//在d盘根目录下生成my.log文件 102 fputs(s,p);//fputs是一种函数,具有的功能是向指定的文件写入一个字符串(不自动写入字符串结束标记符‘\0’)。成功写入一个字符串后,文件的位置指针会自动后移,函数返回值为非负整数 103 fclose(p); 104 } 105 106 void strerror(DWORD errno)//根据GetLastError()返回值,将错误信息转化为中文,写入my.log文件 107 { 108 void *lpMsgBuf; 109 FormatMessageA( 110 FORMAT_MESSAGE_ALLOCATE_BUFFER | 111 FORMAT_MESSAGE_FROM_SYSTEM | 112 FORMAT_MESSAGE_IGNORE_INSERTS, 113 NULL, 114 errno, 115 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language 116 (char *) &lpMsgBuf, 117 0, 118 NULL 119 ); 120 121 savelog((const char *)lpMsgBuf); 122 // Free the buffer. 123 LocalFree( lpMsgBuf ); 124 }
在dllmain文件中加入红色字体代码
// dllmain.cpp : Defines the entry point for the DLL application. #include "stdafx.h" HMODULE hHookDll;//定义一个全局的HMODULE供SetWindowsHookEx函数使用 BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { hHookDll = hModule; switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; }
使用拦截dll,在你的程序中导入ShieldScreen.h文件,把ShieldScreen.dll放到 exe目录下,把ShieldScreen.lib放到工程目录下,同时加上导入库pragma comment(lib, "ShieldScreen.lib");
在初始化函数处
InstallLaunchEv();//安装钩子
在程序退出处
UnInstallLaunchEv();//取消钩子