-
Notifications
You must be signed in to change notification settings - Fork 2
/
Hooks.cpp
94 lines (82 loc) · 1.77 KB
/
Hooks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//编译为64位
#include <Windows.h>
#include <stdio.h>
typedef BOOL(*SetHook)();
typedef BOOL(*UnHook)();
BOOL GlobalHook(BOOL isSet) {
HMODULE hModule = LoadLibrary(L"NotepadHook.dll");
if (!hModule)
{
printf("LoadLibrary Error:%d", GetLastError());
return FALSE;
}
if (isSet == TRUE)
{
SetHook sethook = (SetHook)GetProcAddress(hModule, "SetGlobalHook");
if (!sethook)
{
printf("GetProcAddress SetGlobalHook Error:%d", GetLastError());
return FALSE;
}
sethook();
return TRUE;
}
else
{
UnHook unhook = (UnHook)GetProcAddress(hModule, "UnsetGlobalHook");
if (!unhook)
{
printf("GetProcAddress UnsetGlobalHook Error:%d", GetLastError());
return FALSE;
}
unhook();
return TRUE;
}
}
int main(int argc, char* argv[]) {
//延迟释放:就是把dll编译进资源文件里面,在执行的时候释放
HRSRC hRsrc = FindResource(0, (LPCWSTR)101, L"MYDLL");
if (!hRsrc)
{
printf("FindResource Error:%d", GetLastError());
return FALSE;
}
DWORD dwSize = SizeofResource(NULL, hRsrc);
if (!dwSize)
{
printf("SizeofResource Error:%d", GetLastError());
return FALSE;
}
HGLOBAL hGlobal = LoadResource(NULL, hRsrc);
if (!hGlobal)
{
printf("LoadResource Error:%d", GetLastError());
return FALSE;
}
LPVOID lpVoid = LockResource(hGlobal);
if (!lpVoid)
{
printf("LockResource Error:%d", GetLastError());
return FALSE;
}
FILE* fp = NULL;
fopen_s(&fp, "NotepadHook.dll", "wb+");
fwrite(lpVoid, sizeof(char), dwSize, fp);
fclose(fp);
BOOL bRet = NULL;
bRet = GlobalHook(TRUE);
if (!bRet)
{
printf("SetGlobalHook Error:%d", GetLastError());
return FALSE;
}
system("pause");
bRet = GlobalHook(FALSE);
if (!bRet)
{
printf("UnSetGlobalHook Error:%d", GetLastError());
return FALSE;
}
system("pause");
return 0;
}