@@ -9,98 +9,64 @@ char exePath[MAX_PATH];
99DWORD GetGameProcessId ();
1010bool GetGameDirectory ();
1111void PrintGameDirectorySync ();
12- void KillGameProcess ();
13- void WaitForGameLaunch ();
1412
15-
16- int main (int argc, char * argv[])
13+ int main ()
1714{
18- if (argc <= 1 )
19- {
20- return -1 ;
21- }
22- char * arg = argv[1 ];
23- if (strcmp (arg, " -D" ) == 0 ) {
24- PrintGameDirectorySync ();
25- }
26- else if (strcmp (arg, " -K" ) == 0 ) {
27- KillGameProcess ();
28- }
29- else if (strcmp (arg, " -W" ) == 0 ) {
30- WaitForGameLaunch ();
31- }
32- return 0 ;
15+ PrintGameDirectorySync ();
16+ return 0 ;
3317}
3418
35- bool GetGameDirectory () {
36- DWORD gamePID = GetGameProcessId ();
37- if (gamePID != 0 ) {
38- HANDLE hProcess = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE , gamePID);
39- if (hProcess == NULL ) {
40- return false ;
41- }
42- memset (exePath,0 ,sizeof (exePath));
43- DWORD result = GetModuleFileNameExA (hProcess, NULL , exePath, MAX_PATH);
44- CloseHandle (hProcess);
45- return result != 0 ;
19+ bool GetGameDirectory ()
20+ {
21+ DWORD gamePID = GetGameProcessId ();
22+ if (gamePID != 0 )
23+ {
24+ HANDLE hProcess = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE , gamePID);
25+ if (hProcess == NULL )
26+ {
27+ return false ;
4628 }
47- return false ;
29+ memset (exePath, 0 , sizeof (exePath));
30+ DWORD result = GetModuleFileNameExA (hProcess, NULL , exePath, MAX_PATH);
31+ CloseHandle (hProcess);
32+ return result != 0 ;
33+ }
34+ return false ;
4835}
4936
50- void PrintGameDirectorySync () {
51- for (;;) {
52- if (GetGameDirectory ()) {
53- printf (" %s" , exePath);
54- break ;
55- }
56- Sleep (1000 );
37+ void PrintGameDirectorySync ()
38+ {
39+ for (;;)
40+ {
41+ if (GetGameDirectory ())
42+ {
43+ printf (" %s" , exePath);
44+ break ;
5745 }
46+ Sleep (1000 );
47+ }
5848}
5949
60- void WaitForGameLaunch () {
61- for (;;) {
62- if (GetGameProcessId ()!=0 ) {
63- break ;
50+ DWORD GetGameProcessId ()
51+ {
52+ DWORD processId = 0 ;
53+ HANDLE snapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0 );
54+ if (snapshot != INVALID_HANDLE_VALUE)
55+ {
56+ PROCESSENTRY32 processEntry;
57+ processEntry.dwSize = sizeof (PROCESSENTRY32);
58+ if (Process32First (snapshot, &processEntry))
59+ {
60+ do
61+ {
62+ if (wcscmp (processEntry.szExeFile , GAME_FILE_NAME) == 0 )
63+ {
64+ processId = processEntry.th32ProcessID ;
65+ break ;
6466 }
65- Sleep (1000 );
66- }
67- }
68-
69- DWORD GetGameProcessId () {
70- DWORD processId = 0 ;
71- HANDLE snapshot = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0 );
72- if (snapshot != INVALID_HANDLE_VALUE) {
73- PROCESSENTRY32 processEntry;
74- processEntry.dwSize = sizeof (PROCESSENTRY32);
75- if (Process32First (snapshot, &processEntry)) {
76- do {
77- if (wcscmp (processEntry.szExeFile , GAME_FILE_NAME) == 0 ) {
78- processId = processEntry.th32ProcessID ;
79- break ;
80- }
81- } while (Process32Next (snapshot, &processEntry));
82- }
83- CloseHandle (snapshot);
84- }
85- return processId;
86- }
87-
88- void KillGameProcess () {
89- // 获取游戏进程的PID
90- DWORD gamePID = GetGameProcessId ();
91- if (gamePID == 0 ) {
92- return ;
67+ } while (Process32Next (snapshot, &processEntry));
9368 }
94-
95- // 打开游戏进程
96- HANDLE hProcess = OpenProcess (PROCESS_TERMINATE, FALSE , gamePID);
97- if (hProcess == NULL ) {
98- return ;
99- }
100-
101- // 终止游戏进程
102- TerminateProcess (hProcess, 0 );
103-
104- // 关闭进程句柄
105- CloseHandle (hProcess);
106- }
69+ CloseHandle (snapshot);
70+ }
71+ return processId;
72+ }
0 commit comments