66
77int wmain (int argc, wchar_t *argv[], wchar_t *envp[])
88{
9-
109 if (2 != argc) {
1110 ShowHelp ();
1211 int i;
12+ printf_s (" 1.start\n 2.stop\n 3.restart\n\n input:" );
1313 scanf_s (" %d" , &i);
1414 switch (i)
1515 {
@@ -22,124 +22,109 @@ int wmain(int argc, wchar_t *argv[], wchar_t *envp[])
2222 case 3 :
2323 RestartMonitor ();
2424 break ;
25+ default :
26+ ShowHelp ();
2527 }
2628 }
27- else if (CSTR_EQUAL == CompareString (LOCALE_SYSTEM_DEFAULT, LINGUISTIC_IGNORECASE, argv[1 ], -1 , L " start" , -1 ))
29+ else if (CSTR_EQUAL == CompareString (LOCALE_SYSTEM_DEFAULT, LINGUISTIC_IGNORECASE, argv[1 ], -1 , TEXT ( " start" ) , -1 ))
2830 {
2931 StartMonitor ();
3032 }
31- else if (CSTR_EQUAL == CompareString (LOCALE_SYSTEM_DEFAULT, LINGUISTIC_IGNORECASE, argv[1 ], -1 , L " stop" , -1 ))
33+ else if (CSTR_EQUAL == CompareString (LOCALE_SYSTEM_DEFAULT, LINGUISTIC_IGNORECASE, argv[1 ], -1 , TEXT ( " stop" ) , -1 ))
3234 {
3335 StopMonitor ();
3436 }
35- else if (CSTR_EQUAL == CompareString (LOCALE_SYSTEM_DEFAULT, LINGUISTIC_IGNORECASE, argv[1 ], -1 , L " restart" , -1 ))
37+ else if (CSTR_EQUAL == CompareString (LOCALE_SYSTEM_DEFAULT, LINGUISTIC_IGNORECASE, argv[1 ], -1 , TEXT ( " restart" ) , -1 ))
3638 {
3739 RestartMonitor ();
3840 }
3941 else
4042 {
41- OutputDebugString (L " Tooooooo Many\n " );
43+ OutputDebugString (TEXT ( " Tooooooo Many\n " ) );
4244 ShowHelp ();
4345 }
4446
4547 system (" PAUSE" );
4648 return 0 ;
4749}
4850
51+
4952void ShowHelp ()
5053{
51- OutputDebugString (L" Help: Monitor.exe start|stop|restart\n " );
52- // TODO: showhelp
54+ printf_s (" Help: Monitor.exe start|stop|restart\n " );
5355}
5456
57+
5558void StartMonitor ()
5659{
57- OutputDebugString (L " Do start \n " );
60+ OutputDebugString (TEXT ( " Do start \n " ) );
5861
5962 HANDLE hMutexSingleton = NULL ;
60- HANDLE hThreadLog = NULL ;
61- WCHAR szDllInjectPath[MAX_PATH];
6263
63- hMutexSingleton = CreateMutex (NULL , TRUE , L " APIHOOK_Monitor_Mutex_Singleton" );
64+ hMutexSingleton = CreateMutex (NULL , TRUE , TEXT ( " APIHOOK_Monitor_Mutex_Singleton" ) );
6465 if (ERROR_ALREADY_EXISTS == GetLastError ())
6566 {
66- OutputDebugString (L " ERROR_ALREADY_EXISTS\n " );
67+ OutputDebugString (TEXT ( " ERROR_ALREADY_EXISTS\n " ) );
6768 printf (" ERROR_ALREADY_EXISTS\n " );
6869 ShowHelp ();
6970 return ;
7071 }
7172 if (!hMutexSingleton)
7273 {
73- OutputDebugString (L" CreateMutex ERROR\n " );
74- return ;
75- }
76-
77- hSemaphoreInject = CreateSemaphore (NULL , 0 , 1 , L" APIHOOK_Monitor_Semaphore_Inject" );
78- if (!hSemaphoreInject)
79- {
80- OutputDebugString (L" CreateSemaphore ERROR\n " );
81- return ;
82- }
83-
84- // create LogThread
85- hThreadLog = CreateThread (NULL , 0 , LogThreadProc, NULL , 0 , NULL );
86- if (!hThreadLog)
87- {
88- OutputDebugString (L" CreateThread ERROR\n " );
74+ OutputDebugString (TEXT (" CreateMutex ERROR\n " ));
8975 return ;
9076 }
91- WaitForSingleObject (hSemaphoreInject, INFINITE);
9277
93- // create DllInject.exe process
94- STARTUPINFO si;
95- PROCESS_INFORMATION pi;
96- ZeroMemory (&si, sizeof (si));
97- ZeroMemory (&pi, sizeof (pi));
98- StringCbCopy (szDllInjectPath, MAX_PATH, szCurrentDirectory);
99- StringCbCat (szDllInjectPath, MAX_PATH, L" DllInject.exe" );
100- if (!CreateProcess (szDllInjectPath, NULL , NULL , NULL , FALSE , 0 , NULL , NULL , &si, &pi))
101- {
102- OutputDebugString (L" CreateProcess DllInject.exe ERROR\n " );
103- return ;
104- }
105- OutputDebugString (L" CreateProcess DllInject.exe\n " );
106- WaitForSingleObject (pi.hProcess , INFINITE);
107- OutputDebugString (L" DllInject.exe Finished\n " );
78+ Log ();
10879
109- WaitForSingleObject (hThreadLog, INFINITE);
11080 ReleaseMutex (hMutexSingleton);
11181 CloseHandle (hMutexSingleton);
11282 hMutexSingleton = NULL ;
113- CloseHandle (hSemaphoreInject);
114- hSemaphoreInject = NULL ;
83+
11584}
11685
86+
11787void StopMonitor ()
11888{
119- OutputDebugString (L" Do stop \n " );
89+ OutputDebugString (TEXT (" Do stop \n " ));
90+
91+ MyCreateProcess (TEXT (" DllUnInject.dll" ));
92+
93+ }
94+
95+
96+ void RestartMonitor ()
97+ {
98+ OutputDebugString (TEXT (" Do restart \n " ));
99+ StopMonitor ();
100+ StartMonitor ();
101+ }
102+
120103
121- WCHAR szDllUnInjectPath[MAX_PATH];
104+ void MyCreateProcess (LPCTSTR szProcessName)
105+ {
106+ TCHAR szCurrentDirectory[MAX_PATH];
107+ TCHAR szProcessPath[MAX_PATH];
122108 STARTUPINFO si;
123109 PROCESS_INFORMATION pi;
124110
125111 ZeroMemory (&si, sizeof (si));
126112 ZeroMemory (&pi, sizeof (pi));
127- StringCbCopy (szDllUnInjectPath, MAX_PATH, szCurrentDirectory);
128- StringCbCat (szDllUnInjectPath, MAX_PATH, L" DllUnInject.exe" );
129- if (!CreateProcess (szDllUnInjectPath, NULL , NULL , NULL , FALSE , 0 , NULL , NULL , &si, &pi))
113+ GetCurrentDirectory (MAX_PATH, szCurrentDirectory);
114+ StringCbCopy (szProcessPath, MAX_PATH, szCurrentDirectory);
115+ StringCbCat (szProcessPath, MAX_PATH, TEXT (" \\ " ));
116+ StringCbCat (szProcessPath, MAX_PATH, szProcessName);
117+ OutputDebugString (TEXT (" CreateProcess " ));
118+ OutputDebugString (szProcessPath);
119+ OutputDebugString (TEXT (" \n " ));
120+ if (!CreateProcess (szProcessPath, NULL , NULL , NULL , FALSE , 0 , NULL , NULL , &si, &pi))
130121 {
131- OutputDebugString (L" CreateProcess DllUnInject.exe ERROR\n " );
122+ OutputDebugString (TEXT (" CreateProcess " ));
123+ OutputDebugString (szProcessName);
124+ OutputDebugString (TEXT (" ERROR\n " ));
132125 return ;
133126 }
134- OutputDebugString (L" CreateProcess DllUnInject.exe\n " );
135127 WaitForSingleObject (pi.hProcess , INFINITE);
136- OutputDebugString (L" DllUnInject.exe Finished\n " );
137-
138- }
139-
140- void RestartMonitor ()
141- {
142- OutputDebugString (L" Do restart \n " );
143- StopMonitor ();
144- StartMonitor ();
128+ OutputDebugString (szProcessName);
129+ OutputDebugString (TEXT (" Finished\n " ));
145130}
0 commit comments