Skip to content

Commit 09ad4bf

Browse files
committed
[REFACT] Use CreateProcessW
1 parent 5a36c60 commit 09ad4bf

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

dll_injector/main.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,23 @@ HANDLE create_new_process(IN std::wstring exe_path, IN std::wstring cmd, OUT PR
5959
{
6060
std::wstring full_cmd = std::wstring(exe_path) + L" " + std::wstring(cmd);
6161

62-
std::string exe_str(exe_path.begin(), exe_path.end());
63-
std::string cmd_str(full_cmd.begin(), full_cmd.end());
64-
std::cout << "Command: " << cmd_str << std::endl;
62+
const size_t buf_len = (full_cmd.length() + 1) * sizeof(wchar_t);
63+
wchar_t* cmd_str = new wchar_t[buf_len];
64+
if (cmd_str) {
65+
memset(cmd_str, 0, buf_len);
66+
memcpy(cmd_str, full_cmd.c_str(), buf_len);
67+
}
6568

66-
STARTUPINFOA si = { 0 };
67-
si.cb = sizeof(STARTUPINFOA);
69+
STARTUPINFOW si = { 0 };
70+
si.cb = sizeof(STARTUPINFOW);
6871
si.dwFlags = STARTF_USESHOWWINDOW;
6972
si.wShowWindow = SW_SHOW;
7073

74+
HANDLE pHndl = NULL;
7175
memset(&pi, 0, sizeof(PROCESS_INFORMATION));
72-
if (!CreateProcessA(
73-
exe_str.c_str(),
74-
(LPSTR)cmd_str.c_str(),
76+
if (CreateProcessW(
77+
exe_path.c_str(),
78+
cmd_str,
7579
NULL, //lpProcessAttributes
7680
NULL, //lpThreadAttributes
7781
FALSE, //bInheritHandles
@@ -82,12 +86,15 @@ HANDLE create_new_process(IN std::wstring exe_path, IN std::wstring cmd, OUT PR
8286
&pi //lpProcessInformation
8387
))
8488
{
89+
pHndl = pi.hProcess;
90+
}
8591
#ifdef _DEBUG
92+
else {
8693
std::cerr << "[ERROR] CreateProcess failed, Error = " << GetLastError() << std::endl;
87-
#endif
88-
return NULL;
8994
}
90-
return pi.hProcess;
95+
#endif
96+
delete[]cmd_str;
97+
return pHndl;
9198
}
9299

93100

0 commit comments

Comments
 (0)