Skip to content

Commit 8dafc1e

Browse files
authored
Add files via upload
1 parent 255a723 commit 8dafc1e

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

TimerResolutionModifier.cpp

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@
77
// 手动定义 NT_SUCCESS 宏
88
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
99

10-
// =======================================================================
11-
// ↓↓↓ 终极解决方案:使用完全通用的名称来定义函数指针 ↓↓↓
12-
// =======================================================================
13-
// 1. 定义一个通用的函数指针类型
10+
// 定义函数指针类型
1411
typedef NTSTATUS(NTAPI* pfnGenericTimerApi)(ULONG, BOOLEAN, PULONG);
1512

16-
// 2. 创建一个全局的、使用通用名称的函数指针变量
17-
pfnGenericTimerApi g_pfnSetTimerResolution = nullptr;
18-
// =======================================================================
19-
2013
// 全局变量
14+
pfnGenericTimerApi g_pfnSetTimerResolution = nullptr;
2115
static volatile bool g_runThread = false;
2216
static volatile bool g_isTimerHigh = false;
2317
static HANDLE g_hThread = NULL;
2418

19+
// =======================================================================
20+
// ↓↓↓ 新增的全局变量,用于保存从INI读取的精度值 ↓↓↓
21+
// =======================================================================
22+
static ULONG g_targetResolution = 5000; // 默认值为 0.5ms (5000 * 100ns)
23+
// =======================================================================
24+
25+
2526
// 监控线程的主函数
2627
DWORD WINAPI MonitorThread(LPVOID lpParam)
2728
{
@@ -30,7 +31,6 @@ DWORD WINAPI MonitorThread(LPVOID lpParam)
3031

3132
while (g_runThread)
3233
{
33-
// 3. 检查通用函数指针是否有效
3434
if (!g_pfnSetTimerResolution) {
3535
Sleep(1000);
3636
continue;
@@ -50,14 +50,16 @@ DWORD WINAPI MonitorThread(LPVOID lpParam)
5050

5151
if (isForeground) {
5252
if (!g_isTimerHigh) {
53-
// 4. 通过通用函数指针来调用函数
54-
if (NT_SUCCESS(g_pfnSetTimerResolution(5000, TRUE, &currentResolution))) {
53+
// =======================================================================
54+
// ↓↓↓ 修改:使用配置好的精度值 ↓↓↓
55+
// =======================================================================
56+
if (NT_SUCCESS(g_pfnSetTimerResolution(g_targetResolution, TRUE, &currentResolution))) {
5557
g_isTimerHigh = true;
5658
}
5759
}
5860
} else {
5961
if (g_isTimerHigh) {
60-
if (NT_SUCCESS(g_pfnSetTimerResolution(5000, FALSE, &currentResolution))) {
62+
if (NT_SUCCESS(g_pfnSetTimerResolution(g_targetResolution, FALSE, &currentResolution))) {
6163
g_isTimerHigh = false;
6264
}
6365
}
@@ -67,7 +69,7 @@ DWORD WINAPI MonitorThread(LPVOID lpParam)
6769

6870
if (g_isTimerHigh && g_pfnSetTimerResolution)
6971
{
70-
g_pfnSetTimerResolution(5000, FALSE, &currentResolution);
72+
g_pfnSetTimerResolution(g_targetResolution, FALSE, &currentResolution);
7173
g_isTimerHigh = false;
7274
}
7375
return 0;
@@ -97,16 +99,30 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
9799
if (wcscmp(valueBuffer, L"0") != 0) { return TRUE; }
98100

99101
// =======================================================================
100-
// ↓↓↓ 使用通用名称的指针来获取函数地址 ↓↓↓
102+
// ↓↓↓ 新增:在黑名单检查后,读取INI配置,仅执行一次 ↓↓↓
103+
// =======================================================================
104+
// 使用 GetPrivateProfileIntW 读取整数值,如果键不存在,则使用第三个参数作为默认值
105+
g_targetResolution = GetPrivateProfileIntW(
106+
L"Settings", // Section name
107+
L"Resolution", // Key name
108+
5000, // Default value (0.5ms)
109+
iniPath.c_str() // Full path to the INI file
110+
);
111+
112+
// 做一个简单的安全检查,防止用户输入0
113+
if (g_targetResolution == 0) {
114+
g_targetResolution = 5000;
115+
}
101116
// =======================================================================
117+
118+
// 获取函数地址 (保持不变)
102119
HMODULE hNtdll = GetModuleHandleW(L"ntdll.dll");
103120
if (hNtdll)
104121
{
105-
// GetProcAddress 使用的是纯字符串,这部分不会有问题
106122
g_pfnSetTimerResolution = (pfnGenericTimerApi)GetProcAddress(hNtdll, "NtSetTimerResolution");
107123
}
108-
// =======================================================================
109124

125+
// 启动线程 (保持不变)
110126
if (g_pfnSetTimerResolution)
111127
{
112128
DisableThreadLibraryCalls(hModule);
@@ -116,6 +132,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
116132
}
117133
else if (ul_reason_for_call == DLL_PROCESS_DETACH)
118134
{
135+
// 清理逻辑 (保持不变)
119136
if (g_hThread)
120137
{
121138
g_runThread = false;

0 commit comments

Comments
 (0)