8
8
#define NT_SUCCESS (Status ) (((NTSTATUS)(Status)) >= 0 )
9
9
10
10
// =======================================================================
11
- // ↓↓↓ 核心改动:使用函数指针进行动态加载 ↓↓↓
11
+ // ↓↓↓ 终极解决方案:使用完全通用的名称来定义函数指针 ↓↓↓
12
12
// =======================================================================
13
- // 1. 定义一个与 NtSetTimerResolution 函数签名完全匹配的函数指针类型
14
- typedef NTSTATUS (NTAPI* pNtSetTimerResolution )(ULONG, BOOLEAN, PULONG);
13
+ // 1. 定义一个通用的函数指针类型
14
+ typedef NTSTATUS (NTAPI* pfnGenericTimerApi )(ULONG, BOOLEAN, PULONG);
15
15
16
- // 2. 创建一个全局的函数指针变量,用于保存获取到的函数地址
17
- pNtSetTimerResolution NtSetTimerResolution_p = nullptr ;
16
+ // 2. 创建一个全局的、使用通用名称的函数指针变量
17
+ pfnGenericTimerApi g_pfnSetTimerResolution = nullptr ;
18
18
// =======================================================================
19
19
20
20
// 全局变量
@@ -30,9 +30,9 @@ DWORD WINAPI MonitorThread(LPVOID lpParam)
30
30
31
31
while (g_runThread)
32
32
{
33
- // 3. 在调用前,必须检查函数指针是否有效
34
- if (!NtSetTimerResolution_p ) {
35
- Sleep (1000 ); // 如果指针无效,则等待并重试,或直接退出
33
+ // 3. 检查通用函数指针是否有效
34
+ if (!g_pfnSetTimerResolution ) {
35
+ Sleep (1000 );
36
36
continue ;
37
37
}
38
38
@@ -50,24 +50,24 @@ DWORD WINAPI MonitorThread(LPVOID lpParam)
50
50
51
51
if (isForeground) {
52
52
if (!g_isTimerHigh) {
53
- // 4. 通过函数指针来调用函数
54
- if (NT_SUCCESS (NtSetTimerResolution_p (5000 , TRUE , ¤tResolution))) {
53
+ // 4. 通过通用函数指针来调用函数
54
+ if (NT_SUCCESS (g_pfnSetTimerResolution (5000 , TRUE , ¤tResolution))) {
55
55
g_isTimerHigh = true ;
56
56
}
57
57
}
58
58
} else {
59
59
if (g_isTimerHigh) {
60
- if (NT_SUCCESS (NtSetTimerResolution_p (5000 , FALSE , ¤tResolution))) {
60
+ if (NT_SUCCESS (g_pfnSetTimerResolution (5000 , FALSE , ¤tResolution))) {
61
61
g_isTimerHigh = false ;
62
62
}
63
63
}
64
64
}
65
65
Sleep (250 );
66
66
}
67
67
68
- if (g_isTimerHigh && NtSetTimerResolution_p )
68
+ if (g_isTimerHigh && g_pfnSetTimerResolution )
69
69
{
70
- NtSetTimerResolution_p (5000 , FALSE , ¤tResolution);
70
+ g_pfnSetTimerResolution (5000 , FALSE , ¤tResolution);
71
71
g_isTimerHigh = false ;
72
72
}
73
73
return 0 ;
@@ -97,19 +97,17 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
97
97
if (wcscmp (valueBuffer, L" 0" ) != 0 ) { return TRUE ; }
98
98
99
99
// =======================================================================
100
- // ↓↓↓ 核心改动:在DLL加载时获取函数地址 ↓↓↓
100
+ // ↓↓↓ 使用通用名称的指针来获取函数地址 ↓↓↓
101
101
// =======================================================================
102
- // 获取 ntdll.dll 的句柄,它在所有进程中都已加载
103
102
HMODULE hNtdll = GetModuleHandleW (L" ntdll.dll" );
104
103
if (hNtdll)
105
104
{
106
- // 使用 GetProcAddress 获取函数地址,并存入函数指针
107
- NtSetTimerResolution_p = (pNtSetTimerResolution )GetProcAddress (hNtdll, " NtSetTimerResolution" );
105
+ // GetProcAddress 使用的是纯字符串,这部分不会有问题
106
+ g_pfnSetTimerResolution = (pfnGenericTimerApi )GetProcAddress (hNtdll, " NtSetTimerResolution" );
108
107
}
109
108
// =======================================================================
110
109
111
- // 只有成功获取到函数地址后,才创建监控线程
112
- if (NtSetTimerResolution_p)
110
+ if (g_pfnSetTimerResolution)
113
111
{
114
112
DisableThreadLibraryCalls (hModule);
115
113
g_runThread = true ;
0 commit comments