HOOK笔记[2]
二、全局HOOK
I、编写DLL
注意:
1、SetWindowsHookEx最后两个参数的设置
GetModuleHandle("DLLHOOK")用来返回DLL模块句柄;也可以通过保存DllMain的hinstDLL得到DLL模块句柄;
DLLHOOK与工程名相同。
2、并不时一定要设置共享节
首先通过#pragma定义共享节,如:
#pragma data_seg("MySec")//MySec为节的名字
HWND hWnd=NULL;//这里必须初始化;
#pragma data_seg()
然后声明该节为共享节:
方法一:
#pragma comment(linker,"/section:MySec,RWS")
方法二:
在(也叫模块文件)中按如下写:
SEGMENTS
MySec READ WRITE SHARED
3、可以通过dumpbin /headers DLLHOOK.dll察看该共享节的设置
II、通过DEF文件发布接口函数
III、在目标工程中引入该DLL
注意:
1、在项目的配置中的LINKS选项中,导入库,如:
..\DLLHOOK\Debug\DLLHOOK.lib
当然位置可以自己改,也可以把该LIB文件放在该工程目录中的LIB文件夹中;
2、把DLL文件拷贝到该工程目录中
I、编写DLL
1 #include <windows.h>
2
3 HHOOK hMouseHook;
4 HHOOK hKeyHook;
5
6 #pragma data_seg("MySec")
7 HWND hWnd=NULL;
8 #pragma data_seg()
9
10 //#pragma comment(linker,"/section:MySec,RWS")
11 /*
12 HINSTANCE hInstance;
13 BOOL WINAPI DllMain(
14 HINSTANCE hinstDLL, // handle to the DLL module
15 DWORD fdwReason, // reason for calling function
16 LPVOID lpvReserved // reserved
17 )
18 {
19 hInstance = hinstDLL;
20 }
21 */
22
23 LRESULT CALLBACK MouseProc(
24 int nCode, // hook code
25 WPARAM wParam, // message identifier
26 LPARAM lParam // mouse coordinates
27 )
28 {
29 return 1;
30 }
31
32 LRESULT CALLBACK KeyboardProc(
33 int code, // hook code
34 WPARAM wParam, // virtual-key code
35 LPARAM lParam // keystroke-message information
36 )
37 {
38 if(VK_F2==wParam)
39 {
40 ::SendMessage(hWnd,WM_CLOSE,0,0);
41 UnhookWindowsHookEx(hKeyHook);
42 UnhookWindowsHookEx(hMouseHook);
43 }
44 return 1;
45 }
46
47 void setHook(HWND hwnd)
48 {
49 hWnd=hwnd;
50 hMouseHook=SetWindowsHookEx(WH_MOUSE,MouseProc,GetModuleHandle("DLLHOOK"),0);
51 hKeyHook=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("DLLHOOK"),0);
52 }
2
3 HHOOK hMouseHook;
4 HHOOK hKeyHook;
5
6 #pragma data_seg("MySec")
7 HWND hWnd=NULL;
8 #pragma data_seg()
9
10 //#pragma comment(linker,"/section:MySec,RWS")
11 /*
12 HINSTANCE hInstance;
13 BOOL WINAPI DllMain(
14 HINSTANCE hinstDLL, // handle to the DLL module
15 DWORD fdwReason, // reason for calling function
16 LPVOID lpvReserved // reserved
17 )
18 {
19 hInstance = hinstDLL;
20 }
21 */
22
23 LRESULT CALLBACK MouseProc(
24 int nCode, // hook code
25 WPARAM wParam, // message identifier
26 LPARAM lParam // mouse coordinates
27 )
28 {
29 return 1;
30 }
31
32 LRESULT CALLBACK KeyboardProc(
33 int code, // hook code
34 WPARAM wParam, // virtual-key code
35 LPARAM lParam // keystroke-message information
36 )
37 {
38 if(VK_F2==wParam)
39 {
40 ::SendMessage(hWnd,WM_CLOSE,0,0);
41 UnhookWindowsHookEx(hKeyHook);
42 UnhookWindowsHookEx(hMouseHook);
43 }
44 return 1;
45 }
46
47 void setHook(HWND hwnd)
48 {
49 hWnd=hwnd;
50 hMouseHook=SetWindowsHookEx(WH_MOUSE,MouseProc,GetModuleHandle("DLLHOOK"),0);
51 hKeyHook=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("DLLHOOK"),0);
52 }
注意:
1、SetWindowsHookEx最后两个参数的设置
GetModuleHandle("DLLHOOK")用来返回DLL模块句柄;也可以通过保存DllMain的hinstDLL得到DLL模块句柄;
DLLHOOK与工程名相同。
2、并不时一定要设置共享节
首先通过#pragma定义共享节,如:
#pragma data_seg("MySec")//MySec为节的名字
HWND hWnd=NULL;//这里必须初始化;
#pragma data_seg()
然后声明该节为共享节:
方法一:
#pragma comment(linker,"/section:MySec,RWS")
方法二:
在(也叫模块文件)中按如下写:
SEGMENTS
MySec READ WRITE SHARED
3、可以通过dumpbin /headers DLLHOOK.dll察看该共享节的设置
II、通过DEF文件发布接口函数
1 LIBRARY DLLHOOK
2 EXPORTS
3 setHook @2
4 SEGMENTS
5 MySec READ WRITE SHARED
注:可以先新建DEF文件,再引入工程中。2 EXPORTS
3 setHook @2
4 SEGMENTS
5 MySec READ WRITE SHARED
III、在目标工程中引入该DLL
1 _declspec(dllimport) void setHook(HWND hwnd);
2
3 BOOL CHOOKTESTDlg::OnInitDialog()
4 {
5 ..
6 // TODO: Add extra initialization here
7 int cxScreen,cyScreen;
8 cxScreen = GetSystemMetrics(SM_CXSCREEN);
9 cyScreen = GetSystemMetrics(SM_CYSCREEN);
10 SetWindowPos(&wndTopMost,0,0,cxScreen,cyScreen,SWP_SHOWWINDOW);
11 setHook(m_hWnd);
12 return TRUE; // return TRUE unless you set the focus to a control
13 }
2
3 BOOL CHOOKTESTDlg::OnInitDialog()
4 {
5 ..
6 // TODO: Add extra initialization here
7 int cxScreen,cyScreen;
8 cxScreen = GetSystemMetrics(SM_CXSCREEN);
9 cyScreen = GetSystemMetrics(SM_CYSCREEN);
10 SetWindowPos(&wndTopMost,0,0,cxScreen,cyScreen,SWP_SHOWWINDOW);
11 setHook(m_hWnd);
12 return TRUE; // return TRUE unless you set the focus to a control
13 }
注意:
1、在项目的配置中的LINKS选项中,导入库,如:
..\DLLHOOK\Debug\DLLHOOK.lib
当然位置可以自己改,也可以把该LIB文件放在该工程目录中的LIB文件夹中;
2、把DLL文件拷贝到该工程目录中