Skip to content

Commit 2a3f8d7

Browse files
authored
Update TimerResolutionModifier.cpp
1 parent 52d4af2 commit 2a3f8d7

File tree

1 file changed

+64
-43
lines changed

1 file changed

+64
-43
lines changed

TimerResolutionModifier.cpp

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
// TimerResolutionModifier.cpp
22
#include <Windows.h>
33
#include <timeapi.h>
4+
#include <string>
5+
#include <algorithm> // for std::transform
46

57
// 链接所需的库
68
#pragma comment(lib, "Winmm.lib")
7-
#pragma comment(lib, "User32.lib") // <-- 新增:链接 User32.lib 库以使用窗口相关的API
9+
#pragma comment(lib, "User32.lib")
810

9-
// 全局变量,用于线程控制和状态管理
11+
// 全局变量
1012
static volatile bool g_runThread = false;
1113
static volatile bool g_isTimerHigh = false;
1214
static HANDLE g_hThread = NULL;
1315

14-
// 监控线程的主函数
16+
// 监控线程的主函数 (这部分代码不变)
1517
DWORD WINAPI MonitorThread(LPVOID lpParam)
1618
{
1719
const DWORD currentProcessId = GetCurrentProcessId();
18-
1920
while (g_runThread)
2021
{
2122
HWND hForegroundWnd = GetForegroundWindow();
2223
bool isForeground = false;
23-
2424
if (hForegroundWnd)
2525
{
2626
DWORD foregroundProcessId = 0;
@@ -31,27 +31,15 @@ DWORD WINAPI MonitorThread(LPVOID lpParam)
3131
}
3232
}
3333

34-
if (isForeground)
35-
{
36-
if (!g_isTimerHigh)
37-
{
38-
if (timeBeginPeriod(1) == TIMERR_NOERROR)
39-
{
40-
g_isTimerHigh = true;
41-
}
34+
if (isForeground) {
35+
if (!g_isTimerHigh) {
36+
if (timeBeginPeriod(1) == TIMERR_NOERROR) g_isTimerHigh = true;
4237
}
43-
}
44-
else
45-
{
46-
if (g_isTimerHigh)
47-
{
48-
if (timeEndPeriod(1) == TIMERR_NOERROR)
49-
{
50-
g_isTimerHigh = false;
51-
}
38+
} else {
39+
if (g_isTimerHigh) {
40+
if (timeEndPeriod(1) == TIMERR_NOERROR) g_isTimerHigh = false;
5241
}
5342
}
54-
5543
Sleep(250);
5644
}
5745

@@ -60,43 +48,76 @@ DWORD WINAPI MonitorThread(LPVOID lpParam)
6048
timeEndPeriod(1);
6149
g_isTimerHigh = false;
6250
}
63-
6451
return 0;
6552
}
6653

67-
// 导出函数,以满足某些注入器的要求
68-
extern "C" __declspec(dllexport) void PlaceholderExport()
69-
{
70-
// Do nothing.
71-
}
54+
// 导出函数 (不变)
55+
extern "C" __declspec(dllexport) void PlaceholderExport() {}
7256

7357
// DLL入口点
74-
BOOL APIENTRY DllMain( HMODULE hModule,
75-
DWORD ul_reason_for_call,
76-
LPVOID lpReserved
77-
)
58+
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
7859
{
79-
switch (ul_reason_for_call)
60+
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
8061
{
81-
case DLL_PROCESS_ATTACH:
62+
// =======================================================================
63+
// ↓↓↓ 新增的黑名单检查逻辑 ↓↓↓
64+
// =======================================================================
65+
66+
// 1. 获取当前进程的可执行文件名
67+
wchar_t processPath[MAX_PATH];
68+
GetModuleFileNameW(NULL, processPath, MAX_PATH);
69+
70+
// 从完整路径中提取文件名 (例如 C:\Windows\explorer.exe -> explorer.exe)
71+
const wchar_t* processName = wcsrchr(processPath, L'\\');
72+
processName = (processName) ? processName + 1 : processPath;
73+
74+
// 2. 获取DLL的路径,并构建INI文件的路径
75+
wchar_t dllPath[MAX_PATH];
76+
GetModuleFileNameW(hModule, dllPath, MAX_PATH);
77+
wchar_t* lastSlash = wcsrchr(dllPath, L'\\');
78+
if (lastSlash) {
79+
*(lastSlash + 1) = L'\0'; // 截断文件名,只保留目录
80+
}
81+
std::wstring iniPath = dllPath;
82+
iniPath += L"blacklist.ini"; // 拼接成完整的INI文件路径
83+
84+
// 3. 在INI文件中检查进程名是否存在于[Blacklist]区域
85+
wchar_t valueBuffer[2]; // 只需要一个很小的缓冲区来确认键是否存在
86+
GetPrivateProfileStringW(
87+
L"Blacklist", // Section name
88+
processName, // Key name (the process executable)
89+
L"0", // Default value if key is not found
90+
valueBuffer, // Buffer to receive the value
91+
sizeof(valueBuffer) / sizeof(wchar_t),
92+
iniPath.c_str() // Full path to the INI file
93+
);
94+
95+
// 4. 如果读取到的值不是默认值"0",说明进程在黑名单中
96+
if (wcscmp(valueBuffer, L"0") != 0)
97+
{
98+
// 在黑名单中,立即返回,不执行任何操作
99+
return TRUE;
100+
}
101+
102+
// =======================================================================
103+
// ↑↑↑ 黑名单检查逻辑结束 ↑↑↑
104+
// =======================================================================
105+
106+
// 如果进程不在黑名单中,则执行原有逻辑
82107
DisableThreadLibraryCalls(hModule);
83108
g_runThread = true;
84109
g_hThread = CreateThread(NULL, 0, MonitorThread, NULL, 0, NULL);
85-
break;
86-
87-
case DLL_PROCESS_DETACH:
110+
}
111+
else if (ul_reason_for_call == DLL_PROCESS_DETACH)
112+
{
113+
// 清理逻辑 (不变)
88114
if (g_hThread)
89115
{
90116
g_runThread = false;
91117
WaitForSingleObject(g_hThread, 5000);
92118
CloseHandle(g_hThread);
93119
g_hThread = NULL;
94120
}
95-
break;
96-
97-
case DLL_THREAD_ATTACH:
98-
case DLL_THREAD_DETACH:
99-
break;
100121
}
101122
return TRUE;
102123
}

0 commit comments

Comments
 (0)