Skip to content

Commit a03b6cb

Browse files
authored
Update TimerResolutionModifier.cpp
1 parent 5fd1e7d commit a03b6cb

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

TimerResolutionModifier.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
99

1010
// =======================================================================
11-
// ↓↓↓ 核心改动:使用函数指针进行动态加载 ↓↓↓
11+
// ↓↓↓ 终极解决方案:使用完全通用的名称来定义函数指针 ↓↓↓
1212
// =======================================================================
13-
// 1. 定义一个与 NtSetTimerResolution 函数签名完全匹配的函数指针类型
14-
typedef NTSTATUS(NTAPI* pNtSetTimerResolution)(ULONG, BOOLEAN, PULONG);
13+
// 1. 定义一个通用的函数指针类型
14+
typedef NTSTATUS(NTAPI* pfnGenericTimerApi)(ULONG, BOOLEAN, PULONG);
1515

16-
// 2. 创建一个全局的函数指针变量,用于保存获取到的函数地址
17-
pNtSetTimerResolution NtSetTimerResolution_p = nullptr;
16+
// 2. 创建一个全局的、使用通用名称的函数指针变量
17+
pfnGenericTimerApi g_pfnSetTimerResolution = nullptr;
1818
// =======================================================================
1919

2020
// 全局变量
@@ -30,9 +30,9 @@ DWORD WINAPI MonitorThread(LPVOID lpParam)
3030

3131
while (g_runThread)
3232
{
33-
// 3. 在调用前,必须检查函数指针是否有效
34-
if (!NtSetTimerResolution_p) {
35-
Sleep(1000); // 如果指针无效,则等待并重试,或直接退出
33+
// 3. 检查通用函数指针是否有效
34+
if (!g_pfnSetTimerResolution) {
35+
Sleep(1000);
3636
continue;
3737
}
3838

@@ -50,24 +50,24 @@ DWORD WINAPI MonitorThread(LPVOID lpParam)
5050

5151
if (isForeground) {
5252
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))) {
5555
g_isTimerHigh = true;
5656
}
5757
}
5858
} else {
5959
if (g_isTimerHigh) {
60-
if (NT_SUCCESS(NtSetTimerResolution_p(5000, FALSE, ¤tResolution))) {
60+
if (NT_SUCCESS(g_pfnSetTimerResolution(5000, FALSE, ¤tResolution))) {
6161
g_isTimerHigh = false;
6262
}
6363
}
6464
}
6565
Sleep(250);
6666
}
6767

68-
if (g_isTimerHigh && NtSetTimerResolution_p)
68+
if (g_isTimerHigh && g_pfnSetTimerResolution)
6969
{
70-
NtSetTimerResolution_p(5000, FALSE, ¤tResolution);
70+
g_pfnSetTimerResolution(5000, FALSE, ¤tResolution);
7171
g_isTimerHigh = false;
7272
}
7373
return 0;
@@ -97,19 +97,17 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
9797
if (wcscmp(valueBuffer, L"0") != 0) { return TRUE; }
9898

9999
// =======================================================================
100-
// ↓↓↓ 核心改动:在DLL加载时获取函数地址 ↓↓↓
100+
// ↓↓↓ 使用通用名称的指针来获取函数地址 ↓↓↓
101101
// =======================================================================
102-
// 获取 ntdll.dll 的句柄,它在所有进程中都已加载
103102
HMODULE hNtdll = GetModuleHandleW(L"ntdll.dll");
104103
if (hNtdll)
105104
{
106-
// 使用 GetProcAddress 获取函数地址,并存入函数指针
107-
NtSetTimerResolution_p = (pNtSetTimerResolution)GetProcAddress(hNtdll, "NtSetTimerResolution");
105+
// GetProcAddress 使用的是纯字符串,这部分不会有问题
106+
g_pfnSetTimerResolution = (pfnGenericTimerApi)GetProcAddress(hNtdll, "NtSetTimerResolution");
108107
}
109108
// =======================================================================
110109

111-
// 只有成功获取到函数地址后,才创建监控线程
112-
if (NtSetTimerResolution_p)
110+
if (g_pfnSetTimerResolution)
113111
{
114112
DisableThreadLibraryCalls(hModule);
115113
g_runThread = true;

0 commit comments

Comments
 (0)