6
6
7
7
int wmain (int argc, wchar_t *argv[], wchar_t *envp[])
8
8
{
9
-
10
9
if (2 != argc) {
11
10
ShowHelp ();
12
11
int i;
12
+ printf_s (" 1.start\n 2.stop\n 3.restart\n\n input:" );
13
13
scanf_s (" %d" , &i);
14
14
switch (i)
15
15
{
@@ -22,124 +22,109 @@ int wmain(int argc, wchar_t *argv[], wchar_t *envp[])
22
22
case 3 :
23
23
RestartMonitor ();
24
24
break ;
25
+ default :
26
+ ShowHelp ();
25
27
}
26
28
}
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 ))
28
30
{
29
31
StartMonitor ();
30
32
}
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 ))
32
34
{
33
35
StopMonitor ();
34
36
}
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 ))
36
38
{
37
39
RestartMonitor ();
38
40
}
39
41
else
40
42
{
41
- OutputDebugString (L " Tooooooo Many\n " );
43
+ OutputDebugString (TEXT ( " Tooooooo Many\n " ) );
42
44
ShowHelp ();
43
45
}
44
46
45
47
system (" PAUSE" );
46
48
return 0 ;
47
49
}
48
50
51
+
49
52
void ShowHelp ()
50
53
{
51
- OutputDebugString (L" Help: Monitor.exe start|stop|restart\n " );
52
- // TODO: showhelp
54
+ printf_s (" Help: Monitor.exe start|stop|restart\n " );
53
55
}
54
56
57
+
55
58
void StartMonitor ()
56
59
{
57
- OutputDebugString (L " Do start \n " );
60
+ OutputDebugString (TEXT ( " Do start \n " ) );
58
61
59
62
HANDLE hMutexSingleton = NULL ;
60
- HANDLE hThreadLog = NULL ;
61
- WCHAR szDllInjectPath[MAX_PATH];
62
63
63
- hMutexSingleton = CreateMutex (NULL , TRUE , L " APIHOOK_Monitor_Mutex_Singleton" );
64
+ hMutexSingleton = CreateMutex (NULL , TRUE , TEXT ( " APIHOOK_Monitor_Mutex_Singleton" ) );
64
65
if (ERROR_ALREADY_EXISTS == GetLastError ())
65
66
{
66
- OutputDebugString (L " ERROR_ALREADY_EXISTS\n " );
67
+ OutputDebugString (TEXT ( " ERROR_ALREADY_EXISTS\n " ) );
67
68
printf (" ERROR_ALREADY_EXISTS\n " );
68
69
ShowHelp ();
69
70
return ;
70
71
}
71
72
if (!hMutexSingleton)
72
73
{
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 " ));
89
75
return ;
90
76
}
91
- WaitForSingleObject (hSemaphoreInject, INFINITE);
92
77
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 ();
108
79
109
- WaitForSingleObject (hThreadLog, INFINITE);
110
80
ReleaseMutex (hMutexSingleton);
111
81
CloseHandle (hMutexSingleton);
112
82
hMutexSingleton = NULL ;
113
- CloseHandle (hSemaphoreInject);
114
- hSemaphoreInject = NULL ;
83
+
115
84
}
116
85
86
+
117
87
void StopMonitor ()
118
88
{
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
+
120
103
121
- WCHAR szDllUnInjectPath[MAX_PATH];
104
+ void MyCreateProcess (LPCTSTR szProcessName)
105
+ {
106
+ TCHAR szCurrentDirectory[MAX_PATH];
107
+ TCHAR szProcessPath[MAX_PATH];
122
108
STARTUPINFO si;
123
109
PROCESS_INFORMATION pi ;
124
110
125
111
ZeroMemory (&si, sizeof (si));
126
112
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 ))
130
121
{
131
- OutputDebugString (L" CreateProcess DllUnInject.exe ERROR\n " );
122
+ OutputDebugString (TEXT (" CreateProcess " ));
123
+ OutputDebugString (szProcessName);
124
+ OutputDebugString (TEXT (" ERROR\n " ));
132
125
return ;
133
126
}
134
- OutputDebugString (L" CreateProcess DllUnInject.exe\n " );
135
127
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 " ));
145
130
}
0 commit comments