1
1
// TimerResolutionModifier.cpp
2
2
#include < Windows.h>
3
3
#include < timeapi.h>
4
+ #include < string>
5
+ #include < algorithm> // for std::transform
4
6
5
7
// 链接所需的库
6
8
#pragma comment(lib, "Winmm.lib")
7
- #pragma comment(lib, "User32.lib") // <-- 新增:链接 User32.lib 库以使用窗口相关的API
9
+ #pragma comment(lib, "User32.lib")
8
10
9
- // 全局变量,用于线程控制和状态管理
11
+ // 全局变量
10
12
static volatile bool g_runThread = false ;
11
13
static volatile bool g_isTimerHigh = false ;
12
14
static HANDLE g_hThread = NULL ;
13
15
14
- // 监控线程的主函数
16
+ // 监控线程的主函数 (这部分代码不变)
15
17
DWORD WINAPI MonitorThread (LPVOID lpParam)
16
18
{
17
19
const DWORD currentProcessId = GetCurrentProcessId ();
18
-
19
20
while (g_runThread)
20
21
{
21
22
HWND hForegroundWnd = GetForegroundWindow ();
22
23
bool isForeground = false ;
23
-
24
24
if (hForegroundWnd)
25
25
{
26
26
DWORD foregroundProcessId = 0 ;
@@ -31,27 +31,15 @@ DWORD WINAPI MonitorThread(LPVOID lpParam)
31
31
}
32
32
}
33
33
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 ;
42
37
}
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 ;
52
41
}
53
42
}
54
-
55
43
Sleep (250 );
56
44
}
57
45
@@ -60,43 +48,76 @@ DWORD WINAPI MonitorThread(LPVOID lpParam)
60
48
timeEndPeriod (1 );
61
49
g_isTimerHigh = false ;
62
50
}
63
-
64
51
return 0 ;
65
52
}
66
53
67
- // 导出函数,以满足某些注入器的要求
68
- extern " C" __declspec(dllexport) void PlaceholderExport ()
69
- {
70
- // Do nothing.
71
- }
54
+ // 导出函数 (不变)
55
+ extern " C" __declspec(dllexport) void PlaceholderExport () {}
72
56
73
57
// 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)
78
59
{
79
- switch (ul_reason_for_call)
60
+ if (ul_reason_for_call == DLL_PROCESS_ATTACH )
80
61
{
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
+ // 如果进程不在黑名单中,则执行原有逻辑
82
107
DisableThreadLibraryCalls (hModule);
83
108
g_runThread = true ;
84
109
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
+ // 清理逻辑 (不变)
88
114
if (g_hThread)
89
115
{
90
116
g_runThread = false ;
91
117
WaitForSingleObject (g_hThread, 5000 );
92
118
CloseHandle (g_hThread);
93
119
g_hThread = NULL ;
94
120
}
95
- break ;
96
-
97
- case DLL_THREAD_ATTACH:
98
- case DLL_THREAD_DETACH:
99
- break ;
100
121
}
101
122
return TRUE ;
102
123
}
0 commit comments