Skip to content

Commit aa1feca

Browse files
Monitor reconstructed
LogThread was canceled add a sleep into log method, cost little CPU resource now
1 parent 167b4f6 commit aa1feca

File tree

7 files changed

+108
-135
lines changed

7 files changed

+108
-135
lines changed
Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "stdafx.h"
2-
#include "LogThread.h"
2+
#include "Log.h"
33

4-
DWORD WINAPI LogThreadProc(LPVOID lpParameter)
4+
void Log()
55
{
6-
OutputDebugString(L"This in LogThread\n");
6+
OutputDebugString(TEXT("This in LogThread\n"));
77

88
HANDLE hSemaphoreStatus = NULL;
99
HANDLE hMailslotLog = NULL;
@@ -13,45 +13,46 @@ DWORD WINAPI LogThreadProc(LPVOID lpParameter)
1313
DWORD dwFileSize = MAX_FILE_SIZE+1;
1414
DWORD dwWrittenSize = 0;
1515
BOOL isStopped = FALSE;
16-
WCHAR szReadBuf[MAX_LOG_SIZE];
17-
WCHAR szCurrentLogPath[MAX_PATH];
16+
TCHAR szReadBuf[MAX_LOG_SIZE];
17+
TCHAR szCurrentLogPath[MAX_PATH];
18+
TCHAR szCurrentDirectory[MAX_PATH];
1819

19-
//Get Monitor.exe directory, in order to read config file
20-
GetModuleDirectory(szCurrentDirectory);
20+
//Get log directory, in order to read config file
21+
GetCurrentDirectory(MAX_PATH, szCurrentDirectory);
2122
StringCbCopy(szCurrentLogPath, MAX_PATH, szCurrentDirectory);
22-
StringCbCat(szCurrentLogPath, MAX_PATH, L"Logs\\");
23+
StringCbCat(szCurrentLogPath, MAX_PATH, TEXT("\\Logs\\"));
2324
if (CreateDirectory(szCurrentLogPath, NULL))
2425
{
25-
OutputDebugString(L"Create New Log Directory\n");
26+
OutputDebugString(TEXT("Create New Log Directory\n"));
2627
}
2728
else if (ERROR_ALREADY_EXISTS == GetLastError())
2829
{
29-
OutputDebugString(L"Log Directory Exists\n");
30+
OutputDebugString(TEXT("Log Directory Exists\n"));
3031
}
3132
else
3233
{
33-
OutputDebugString(L"CreateDirectory ERROR\n");
34-
return 0;
34+
OutputDebugString(TEXT("CreateDirectory ERROR\n"));
35+
return;
3536
}
3637

3738
//create status semaphore
38-
hSemaphoreStatus = CreateSemaphore(NULL, 0, 1, L"APIHOOK_Monitor_Semaphore_Status");
39+
hSemaphoreStatus = CreateSemaphore(NULL, 0, 1, TEXT("APIHOOK_Monitor_Semaphore_Status"));
3940
if (!hSemaphoreStatus)
4041
{
41-
OutputDebugString(L"CreateSemaphore ERROR\n");
42-
return 0;
42+
OutputDebugString(TEXT("CreateSemaphore ERROR\n"));
43+
return;
4344
}
4445

4546
//create mailslot
46-
hMailslotLog = CreateMailslot(L"\\\\.\\mailslot\\APIHOOK\\Monitor\\Log", 0, 0, NULL);
47+
hMailslotLog = CreateMailslot(TEXT("\\\\.\\mailslot\\APIHOOK\\Monitor\\Log"), 0, 0, NULL);
4748
if (INVALID_HANDLE_VALUE == hMailslotLog)
4849
{
49-
OutputDebugString(L"CreateMailslot ERROR\n");
50-
return 0;
50+
OutputDebugString(TEXT("CreateMailslot ERROR\n"));
51+
return;
5152
}
5253

5354
//it is time to create the new process DllInject.exe
54-
ReleaseSemaphore(hSemaphoreInject, 1, NULL);
55+
MyCreateProcess(TEXT("DllInject.exe"));
5556

5657
//read from slot and write to file
5758
while (!isStopped || 1 < dwMessageRemain)
@@ -65,7 +66,7 @@ DWORD WINAPI LogThreadProc(LPVOID lpParameter)
6566
{
6667
//read
6768
ReadFile(hMailslotLog, szReadBuf, dwMessageSize, NULL, NULL);
68-
szReadBuf[dwMessageSize/sizeof(WCHAR)] = L'\0';
69+
szReadBuf[dwMessageSize/sizeof(WCHAR)] = TEXT('\0');
6970
OutputDebugString(szReadBuf);
7071

7172
//use new file or not
@@ -75,44 +76,49 @@ DWORD WINAPI LogThreadProc(LPVOID lpParameter)
7576
{
7677
CloseHandle(hLogFile);
7778
}
78-
RefreshFileName(szCurrentLogPath);
79+
RefreshFileName(szCurrentLogPath, szCurrentDirectory);
7980
hLogFile = CreateFile(szCurrentLogPath, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
8081
if (INVALID_HANDLE_VALUE == hLogFile)
8182
{
82-
OutputDebugString(L"CreateFile ERROR\n");
83-
return 0;
83+
OutputDebugString(TEXT("CreateFile ERROR\n"));
84+
return;
8485
}
8586
dwFileSize = 0;
8687
}
8788
//do write
8889
WriteFile(hLogFile, szReadBuf, dwMessageSize, &dwWrittenSize, NULL);
8990
if (dwMessageSize != dwWrittenSize)
9091
{
91-
OutputDebugString(L"WriteFile ERROR\n");
92-
return 0;
92+
OutputDebugString(TEXT("WriteFile ERROR\n"));
93+
return;
9394
}
9495
dwFileSize += dwMessageSize;
9596
}
97+
if (!isStopped && dwMessageRemain <= 1)
98+
{
99+
Sleep(1000);
100+
OutputDebugString(TEXT("Sleep Here\n"));
101+
}
96102
}
97103

98104
CloseHandle(hLogFile);
99105
hLogFile = NULL;
100106
CloseHandle(hSemaphoreStatus);
101107
hSemaphoreStatus = NULL;
102108

103-
OutputDebugString(L"LogThread Finish\n");
109+
OutputDebugString(TEXT("LogThread Finish\n"));
104110

105-
return 0;
111+
return;
106112
}
107113

108114

109-
VOID RefreshFileName(PWCHAR szCurrentLogPath)
115+
void RefreshFileName(PTCHAR szCurrentLogPath, LPCTSTR szCurrentDirectory)
110116
{
111117
SYSTEMTIME st;
112118
GetLocalTime(&st);
113-
WCHAR szBuf[MAX_PATH];
119+
TCHAR szBuf[MAX_PATH];
114120
swprintf_s(szBuf,
115-
L"Logs\\ALIHOOK_Monitor_Log_%hu_%hu_%hu_%hu_%hu_%hu_%hu.txt",
121+
TEXT("\\Logs\\ALIHOOK_Monitor_Log_%hu_%hu_%hu_%hu_%hu_%hu_%hu.txt"),
116122
st.wYear,
117123
st.wMonth,
118124
st.wDay,
@@ -123,27 +129,4 @@ VOID RefreshFileName(PWCHAR szCurrentLogPath)
123129
);
124130
StringCbCopy(szCurrentLogPath, MAX_PATH, szCurrentDirectory);
125131
StringCbCat(szCurrentLogPath, MAX_PATH, szBuf);
126-
}
127-
128-
129-
VOID GetModuleDirectory(PWCHAR szCurrentDirectory)
130-
{
131-
DWORD dwCurDirPathLen;
132-
dwCurDirPathLen = GetModuleFileName(NULL, szCurrentDirectory, MAX_PATH);
133-
if (!dwCurDirPathLen)
134-
{
135-
OutputDebugString(L"GetModuleFileName ERROR\n");
136-
return;
137-
}
138-
SIZE_T i = 0;
139-
StringCbLengthW(szCurrentDirectory, MAX_PATH, &i);
140-
if (0 == i)
141-
{
142-
OutputDebugString(L"GetModuleFileName ERROR\n");
143-
return;
144-
}
145-
for (; i > 0 && L'\\' != szCurrentDirectory[i - 1]; i--) {}
146-
szCurrentDirectory[i] = L'\0';
147-
OutputDebugString(szCurrentDirectory);
148-
OutputDebugString(L"\n");
149132
}

APIHOOK/Monitor/Log.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#ifndef __LOG__
4+
#define __LOG__
5+
#include "Monitor.h"
6+
#endif // !__LOG__
7+
8+
9+
#define MAX_LOG_SIZE 260
10+
#define MAX_FILE_SIZE 1024 * 1024
11+
12+
13+
void Log();
14+
void RefreshFileName(PTCHAR szCurrentLogPath, LPCTSTR szCurrentDirectory);

APIHOOK/Monitor/LogThread.h

Lines changed: 0 additions & 11 deletions
This file was deleted.

APIHOOK/Monitor/Monitor.cpp

Lines changed: 48 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
int 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\n2.stop\n3.restart\n\ninput:");
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+
4952
void 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+
5558
void 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+
11787
void 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

Comments
 (0)