-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonAPI.cpp
More file actions
362 lines (283 loc) · 9.53 KB
/
CommonAPI.cpp
File metadata and controls
362 lines (283 loc) · 9.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include <memory>
#include "FileCtrl.h"
#include "SysCtrl.h"
#include "ProcCtrl.h"
#include "RegCtrl.h"
#include "ServiceCtrl.h"
#include "CommonAPI.h"
// =============================================================================================== //
// Registry Control API
// =============================================================================================== //
bool RegCtrl_API::GetRegistryA(HKEY hKey, const char* subKey, const char* valueName, char* pBuf, int nLenBuf)
{
std::string strData;
bool ret = false;
if (true == RegCtrl.GetRegistry(hKey, subKey, valueName, strData))
{
if (0 == strncpy_s(pBuf, nLenBuf, strData.c_str(), strData.length()))
{
ret = true;
}
}
return ret;
}
bool RegCtrl_API::GetRegistryW(HKEY hKey, const wchar_t* subKey, const wchar_t* valueName, wchar_t* pBuf, int nLenBuf)
{
std::wstring wstrData;
bool ret = false;
if (true == RegCtrl.GetRegistry(hKey, subKey, valueName, wstrData))
{
if (0 == wcsncpy_s(pBuf, nLenBuf, wstrData.c_str(), wstrData.length()))
{
ret = true;
}
}
return ret;
}
bool RegCtrl_API::SetRegistryA(HKEY hKey, const char * subKey, const char* valueName, const char* data)
{
return RegCtrl.SetRegistry(hKey, subKey, valueName, data);
}
bool RegCtrl_API::SetRegistryW(HKEY hKey, const wchar_t* subKey, const wchar_t* valueName, const wchar_t* data)
{
return RegCtrl.SetRegistry(hKey, subKey, valueName, data);
}
bool RegCtrl_API::DeleteRegistryA(HKEY hKey, const char* subKey, const char* valueName)
{
return RegCtrl.DeleteRegistry(hKey, subKey, valueName);
}
bool RegCtrl_API::DeleteRegistryW(HKEY hKey, const wchar_t* subKey, const wchar_t* valueName)
{
return RegCtrl.DeleteRegistry(hKey, subKey, valueName);
}
// =============================================================================================== //
// File Control API
// =============================================================================================== //
BOOL FileCtrl_API::CreateDirectory(const char* strPath)
{
return FileCtrl.CreateDirectory(strPath);
}
BOOL FileCtrl_API::FileExist(const char* strPath)
{
return FileCtrl.FileExists(strPath);
}
FileType FileCtrl_API::GetType(const char* strPath)
{
return FileCtrl.GetType(strPath);
}
BOOL FileCtrl_API::RemoveFIle(const char* strPath)
{
return FileCtrl.RemoveFile(strPath);
}
BOOL FileCtrl_API::CopyFile(const char* strSrc, const char* strDst)
{
return FileCtrl.CopyFile(strSrc, strDst);
}
BOOL FileCtrl_API::GetFiles(const char* strSrc, std::vector<std::string>* files)
{
return FileCtrl.GetFiles(strSrc, files);
}
BOOL FileCtrl_API::FileVersion(const char* pPath, char* pDst, int nLenDst)
{
BOOL nRet = FALSE;
std::string strPath(pPath);
std::string version;
if (FileCtrl.FileVersion(strPath, version))
{
if (0 == strncpy_s(const_cast<char*>(pDst), nLenDst, version.c_str(), version.length()))
nRet = TRUE;
}
return nRet;
}
BOOL FileCtrl_API::Is64BitFile(const char* pPath)
{
std::string strPath(pPath);
return FileCtrl.Is64BitFile(strPath);
}
// =============================================================================================== //
// System Control API
// =============================================================================================== //
BOOL SysCtrl_API::GetEnviroment(const char* key, const char* pBuf, int nBufSize)
{
BOOL ret = FALSE;
std::string value;
if (TRUE == SysCtrl.GetEnviroment(key, value))
{
strcpy_s(const_cast<char*>(pBuf), nBufSize, value.c_str());
}
return ret;
}
BOOL SysCtrl_API::SetEnviroment(const char* key, const char* value)
{
BOOL ret = FALSE;
if (TRUE == SysCtrl.SetEnviroment(key, value))
{
ret = TRUE;
}
return ret;
}
BOOL SysCtrl_API::GetSystemTimeEx(const char* pBuf, int nBufSize)
{
BOOL ret = FALSE;
std::string strTime;
if (TRUE == SysCtrl.GetSystemTime(strTime))
{
strcpy_s(const_cast<char*>(pBuf), nBufSize, strTime.c_str());
ret = TRUE;
}
return ret;
}
BOOL SysCtrl_API::IsProcessElevated()
{
return SysCtrl.IsProcessElevated();
}
BOOL SysCtrl_API::AddStartProgram(const char* pName, const char* pBinPath)
{
const int nLenName = strlen(pName);
const int nLenPath = strlen(pBinPath);
return RegCtrl.SetRegistry(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
StrCtrl.AnsiStringToWideString(std::string(pName, pName + nLenName)),
StrCtrl.AnsiStringToWideString(std::string(pBinPath, pBinPath + nLenPath)));
}
BOOL SysCtrl_API::DeleteStartProgram(const char* pName, int nLenName)
{
return RegCtrl.DeleteRegistry(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
StrCtrl.AnsiStringToWideString(std::string(pName, pName + nLenName)));
}
// =============================================================================================== //
// String Control API
// =============================================================================================== //
BOOL StrCtrl_API::AnsiStringToWideString(const char* pSrc, wchar_t* pDst, int nLenDst)
{
BOOL ret = FALSE;
auto wstrTemp = StrCtrl.AnsiStringToWideString(pSrc);
if (0 == wcsncpy_s(pDst, nLenDst, wstrTemp.c_str(), wstrTemp.length()))
{
ret = TRUE;
}
return ret;
}
BOOL StrCtrl_API::WideStringToAnsiString(const wchar_t* pSrc, char* pDst, int nLenDst)
{
BOOL ret = FALSE;
std::wstring wstrTemp(pSrc);
auto strTemp = StrCtrl.WideStringToAnsiString(wstrTemp);
if (0 == strncpy_s(pDst, nLenDst, strTemp.c_str(), strTemp.length()))
{
ret = TRUE;
}
return ret;
}
int StrCtrl_API::GetStringParsing(const WCHAR* pString, const WCHAR* pDelimiter, std::vector<std::wstring>* pVecString)
{
return StrCtrl.GetStringParsing(const_cast<wchar_t*>(pString), const_cast<wchar_t*>(pDelimiter), pVecString);
}
bool StrCtrl_API::GetRandomString(char* pBuf, int nLenBuf, int length)
{
if (length >= nLenBuf)
return false;
std::memset(pBuf, 0x00, sizeof(char) * nLenBuf);
std::string strRand = StrCtrl.GetRandomString(length);
if (strRand.length() == 0)
return false;
if (0 == strcpy_s(pBuf, nLenBuf, strRand.c_str())) return true;
return false;
}
bool StrCtrl_API::UTF8ToUTF16(const char* pSrc, wchar_t* pDst, int nLenDst)
{
std::wstring wstrUTF16 = StrCtrl.UTF8ToUTF16(pSrc);
errno_t err = wcscpy_s(pDst, nLenDst, wstrUTF16.c_str());
if (err == 0)
return true;
return false;
}
bool StrCtrl_API::UTF16ToUTF8(const wchar_t * pSrc, char * pDst, int nLenDst)
{
std::string strUTF8 = StrCtrl.UTF16ToUTF8(pSrc);
errno_t err = strcpy_s(pDst, nLenDst, strUTF8.c_str());
if (err == 0)
return true;
return false;
}
// =============================================================================================== //
// Process Control API
// =============================================================================================== //
bool ProcCtrl_API::StartProcess(const char* szPath, DWORD* pPID)
{
DWORD dwPID = 0;
bool bRet = ProcCtrl.StartProcess(StrCtrl.AnsiStringToWideString(szPath), dwPID);
if (bRet = true)
(*pPID) = dwPID;
return bRet;
}
bool ProcCtrl_API::StopProcess(const char* szProcessName)
{
return ProcCtrl.StopProcess(StrCtrl.AnsiStringToWideString(szProcessName));
}
bool ProcCtrl_API::IsProcessRunning(const char* szProcessName, DWORD* dwPID)
{
DWORD dwTemp = 0;
bool nRet = ProcCtrl.IsProcessRunning(StrCtrl.AnsiStringToWideString(szProcessName), dwTemp);
// 프로세스가 실행중이고, 매개변수 dwPID가 존재하는 경우
if (true == nRet && nullptr != dwPID)
{
(*dwPID) = dwTemp;
}
return nRet;
}
bool ProcCtrl_API::SetProcessPriority(const char* szProcName, DWORD dwPriority)
{
return ProcCtrl.SetProcessPriority(StrCtrl.AnsiStringToWideString(szProcName), dwPriority);
}
bool ProcCtrl_API::MonitorProcessResources(const char* szProcName, PROCESS_RESOURCE_USAGE* usage)
{
return ProcCtrl.MonitorProcessResources(StrCtrl.AnsiStringToWideString(szProcName), *usage);
}
int ProcCtrl_API::InjectDLL(const char* pDllPath, DWORD dwPID)
{
return ProcCtrl.InjectDLL(StrCtrl.AnsiStringToWideString(pDllPath), dwPID);
}
// =============================================================================================== //
// Service Control API
// =============================================================================================== //
bool ServiceCtrl_API::Create(const char* serviceName, const char* displayName, const char* binPath)
{
return ServiceCtrl.Create(const_cast<char*>(serviceName), const_cast<char*>(displayName), const_cast<char*>(binPath));
}
bool ServiceCtrl_API::IsRunning(const char* serviceName)
{
return ServiceCtrl.IsRunning(const_cast<char*>(serviceName));
}
bool ServiceCtrl_API::Start(const char* serviceName, bool force_admin)
{
return ServiceCtrl.Start(const_cast<char*>(serviceName), force_admin);
}
bool ServiceCtrl_API::Stop(const char* serviceName)
{
return ServiceCtrl.Stop(const_cast<char*>(serviceName));
}
bool ServiceCtrl_API::Restart(const char* serviceName)
{
return ServiceCtrl.Restart(const_cast<char*>(serviceName));
}
bool ServiceCtrl_API::Delete(const char* serviceName)
{
return ServiceCtrl.Stop(const_cast<char *>(serviceName));
}
void DebugPrintA(const char* pModuleName, const char* pFunctionName, const int nLine, const char* format, ...)
{
va_list argList;
va_start(argList, format);
// 버퍼 크기를 동적으로 할당
int bufferSize = vsnprintf(NULL, 0, format, argList) + 1;
char* buffer = (char*)malloc(bufferSize);
// 다시 가변 인자 목록을 초기화
va_end(argList);
va_start(argList, format);
vsnprintf(buffer, bufferSize, format, argList);
std::string message{ std::format("[{}|{}({})] ", pModuleName, pFunctionName, nLine) };
message.append(buffer, bufferSize);
OutputDebugStringA(message.c_str());
free(buffer);
va_end(argList);
}