-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerResolution.cpp
More file actions
265 lines (241 loc) · 10.3 KB
/
TimerResolution.cpp
File metadata and controls
265 lines (241 loc) · 10.3 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// TimerResolutionModifier.cpp
#include <Windows.h>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <string.h> // For _wcsicmp and _wcsnicmp
#pragma comment(lib, "User32.lib")
// 手动定义 NT_SUCCESS 宏
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
// 定义函数指针类型
typedef NTSTATUS(NTAPI* pfnGenericTimerApi)(ULONG, BOOLEAN, PULONG);
typedef DWORD(WINAPI* pfnSetThreadIdealProcessor)(HANDLE, DWORD);
// 全局变量
pfnGenericTimerApi g_pfnSetTimerResolution = nullptr;
pfnSetThreadIdealProcessor g_pfnSetThreadIdealProcessor = nullptr;
static volatile bool g_runThread = false;
static volatile bool g_isTimerHigh = false;
static HANDLE g_hThread = NULL;
static DWORD g_dwThreadId = 0;
static ULONG g_targetResolution = 5000;
static bool g_isSpecialProcess = false;
static DWORD g_checkInterval = 10;
static int g_checkMode = 1;
static DWORD g_currentProcessId = 0; // 缓存自身进程ID
// 检查进程名是否在INI的某个区域中 (健壮版本)
bool IsProcessInList(const wchar_t* section, const wchar_t* processName, const std::wstring& iniPath)
{
const DWORD bufferSize = 8192;
wchar_t buffer[bufferSize];
DWORD bytesRead = GetPrivateProfileSectionW(section, buffer, bufferSize, iniPath.c_str());
if (bytesRead == 0) return false;
size_t processNameLen = wcslen(processName);
for (const wchar_t* p = buffer; *p; p += wcslen(p) + 1) {
const wchar_t* equalsSign = wcschr(p, L'=');
size_t lenToCompare = (equalsSign != nullptr) ? (equalsSign - p) : wcslen(p);
if (lenToCompare == processNameLen && _wcsnicmp(p, processName, processNameLen) == 0) {
return true;
}
}
return false;
}
// 解析亲和性设置字符串
DWORD_PTR ParseAffinityMask(const std::wstring& affinityStr)
{
DWORD_PTR mask = 0;
if (affinityStr.empty()) return 0;
std::wstringstream ss(affinityStr);
std::wstring item;
while (std::getline(ss, item, L',')) {
size_t hyphenPos = item.find(L'-');
if (hyphenPos != std::wstring::npos) {
try {
int start = std::stoi(item.substr(0, hyphenPos));
int end = std::stoi(item.substr(hyphenPos + 1));
if (start >= 0 && end >= start && end < sizeof(DWORD_PTR) * 8) {
for (int i = start; i <= end; ++i) mask |= (static_cast<DWORD_PTR>(1) << i);
}
} catch (...) {}
} else {
try {
int core = std::stoi(item);
if (core >= 0 && core < sizeof(DWORD_PTR) * 8) mask |= (static_cast<DWORD_PTR>(1) << core);
} catch (...) {}
}
}
return mask;
}
// WinEventHook 回调函数
void CALLBACK WinEventProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
if (event == EVENT_SYSTEM_FOREGROUND && hwnd) {
DWORD foregroundProcessId = 0;
GetWindowThreadProcessId(hwnd, &foregroundProcessId);
ULONG currentResolution;
// 使用缓存的进程ID进行比较
if (foregroundProcessId == g_currentProcessId) {
if (!g_isTimerHigh && g_pfnSetTimerResolution) {
if (NT_SUCCESS(g_pfnSetTimerResolution(g_targetResolution, TRUE, ¤tResolution))) g_isTimerHigh = true;
}
} else {
if (g_isTimerHigh && g_pfnSetTimerResolution) {
if (NT_SUCCESS(g_pfnSetTimerResolution(g_targetResolution, FALSE, ¤tResolution))) g_isTimerHigh = false;
}
}
}
}
// Hook线程的主函数
DWORD WINAPI HookThread(LPVOID lpParam)
{
HWINEVENTHOOK hHook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, NULL, WinEventProc, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
WinEventProc(hHook, EVENT_SYSTEM_FOREGROUND, GetForegroundWindow(), 0, 0, 0, 0);
MSG msg;
while (g_runThread && GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (hHook) UnhookWinEvent(hHook);
if (g_isTimerHigh && g_pfnSetTimerResolution) {
ULONG currentResolution;
g_pfnSetTimerResolution(g_targetResolution, FALSE, ¤tResolution);
g_isTimerHigh = false;
}
return 0;
}
// 监控线程的主函数 (轮询模式)
DWORD WINAPI MonitorThread(LPVOID lpParam)
{
ULONG currentResolution;
while (g_runThread) {
if (!g_pfnSetTimerResolution) { Sleep(1000); continue; }
HWND hForegroundWnd = GetForegroundWindow();
bool isForeground = false;
if (hForegroundWnd) {
DWORD foregroundProcessId = 0;
GetWindowThreadProcessId(hForegroundWnd, &foregroundProcessId);
// 使用缓存的进程ID进行比较
if (foregroundProcessId == g_currentProcessId) isForeground = true;
}
if (isForeground) {
if (!g_isTimerHigh) {
if (NT_SUCCESS(g_pfnSetTimerResolution(g_targetResolution, TRUE, ¤tResolution))) g_isTimerHigh = true;
}
} else {
if (g_isTimerHigh) {
if (NT_SUCCESS(g_pfnSetTimerResolution(g_targetResolution, FALSE, ¤tResolution))) g_isTimerHigh = false;
}
}
Sleep(g_checkInterval * 1000);
}
if (g_isTimerHigh && g_pfnSetTimerResolution) {
ULONG currentResolution;
g_pfnSetTimerResolution(g_targetResolution, FALSE, ¤tResolution);
g_isTimerHigh = false;
}
return 0;
}
// 导出函数
extern "C" __declspec(dllexport) void PlaceholderExport() {}
// DLL入口点
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
{
wchar_t processPath[MAX_PATH];
GetModuleFileNameW(NULL, processPath, MAX_PATH);
const wchar_t* processName = wcsrchr(processPath, L'\\');
processName = (processName) ? processName + 1 : processPath;
wchar_t dllPath[MAX_PATH];
GetModuleFileNameW(hModule, dllPath, MAX_PATH);
wchar_t* lastSlash = wcsrchr(dllPath, L'\\');
if (lastSlash) *(lastSlash + 1) = L'\0';
std::wstring iniPath = dllPath;
iniPath += L"Config.ini";
// =======================================================================
// 新增区域 1: 添加全局禁用选项
// =======================================================================
if (GetPrivateProfileIntW(L"Settings", L"Disable", 0, iniPath.c_str()) == 1) {
return TRUE; // 如果Disable=1, 则完全禁用DLL功能
}
// =======================================================================
// 新增区域 2: 缓存进程ID
// =======================================================================
g_currentProcessId = GetCurrentProcessId();
// 黑名单检查
if (IsProcessInList(L"BlackList", processName, iniPath)) {
return TRUE;
}
g_targetResolution = GetPrivateProfileIntW(L"Settings", L"Resolution", 5000, iniPath.c_str());
if (g_targetResolution == 0) g_targetResolution = 5000;
g_checkMode = GetPrivateProfileIntW(L"Settings", L"CheckMode", 1, iniPath.c_str());
g_checkInterval = GetPrivateProfileIntW(L"Settings", L"CheckInterval", 10, iniPath.c_str());
if (g_checkInterval < 1) g_checkInterval = 1;
HMODULE hNtdll = GetModuleHandleW(L"ntdll.dll");
if (hNtdll) {
g_pfnSetTimerResolution = (pfnGenericTimerApi)GetProcAddress(hNtdll, "NtSetTimerResolution");
}
if (!g_pfnSetTimerResolution) return TRUE;
// =======================================================================
// 修改区域: 将 "PersistentProcesses" 改为 "WhiteList"
// =======================================================================
if (IsProcessInList(L"WhiteList", processName, iniPath))
{
g_isSpecialProcess = true;
ULONG currentResolution;
g_pfnSetTimerResolution(g_targetResolution, TRUE, ¤tResolution);
}
else
{
g_isSpecialProcess = false;
DisableThreadLibraryCalls(hModule);
g_runThread = true;
if (g_checkMode == 2) {
g_hThread = CreateThread(NULL, 0, MonitorThread, NULL, 0, &g_dwThreadId);
} else {
g_hThread = CreateThread(NULL, 0, HookThread, NULL, 0, &g_dwThreadId);
}
if (g_hThread != NULL)
{
wchar_t affinityBuffer[256];
GetPrivateProfileStringW(L"Settings", L"Affinity", L"", affinityBuffer, 256, iniPath.c_str());
std::wstring affinityStr(affinityBuffer);
if (!affinityStr.empty()) {
DWORD_PTR affinityMask = ParseAffinityMask(affinityStr);
if (affinityMask > 0) SetThreadAffinityMask(g_hThread, affinityMask);
}
int idealCore = GetPrivateProfileIntW(L"Settings", L"IdealCore", -1, iniPath.c_str());
if (idealCore >= 0)
{
HMODULE hKernel32 = GetModuleHandleW(L"kernel32.dll");
if (hKernel32) {
g_pfnSetThreadIdealProcessor = (pfnSetThreadIdealProcessor)GetProcAddress(hKernel32, "SetThreadIdealProcessor");
if (g_pfnSetThreadIdealProcessor) {
g_pfnSetThreadIdealProcessor(g_hThread, static_cast<DWORD>(idealCore));
}
}
}
}
}
}
else if (ul_reason_for_call == DLL_PROCESS_DETACH)
{
g_runThread = false;
if (g_isSpecialProcess) {
if (g_pfnSetTimerResolution) {
ULONG currentResolution;
g_pfnSetTimerResolution(g_targetResolution, FALSE, ¤tResolution);
}
} else {
if (g_hThread) {
if (g_checkMode != 2 && g_dwThreadId != 0) {
PostThreadMessageW(g_dwThreadId, WM_QUIT, 0, 0);
}
WaitForSingleObject(g_hThread, 1000);
CloseHandle(g_hThread);
g_hThread = NULL;
}
}
}
return TRUE;
}