forked from stax76/run-hidden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-hidden.cpp
90 lines (72 loc) · 2.1 KB
/
run-hidden.cpp
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
#include <windows.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
std::wstring join(const std::vector<std::wstring>& elements, const std::wstring& separator)
{
if (!elements.empty())
{
std::wstringstream ss;
auto it = elements.cbegin();
while (true)
{
ss << *it++;
if (it != elements.cend())
ss << separator;
else
return ss.str();
}
}
return L"";
}
int APIENTRY wWinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
int count = 0;
LPWSTR* args;
std::vector<std::wstring> vec;
args = CommandLineToArgvW(GetCommandLineW(), &count);
for (int i = 1; i < count; i++)
{
bool hasSpace = wcsstr(args[i], L" ") != NULL;
if (hasSpace) {
std::wstring s;
s.append(L"\"");
s.append(args[i]);
s.append(L"\"");
vec.push_back(s);
}
else
vec.push_back(args[i]);
}
auto cmdlstr = join(vec, L" ");
LPWSTR cmdl = (LPWSTR)cmdlstr.c_str();
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
CreateProcess(
NULL, // No module name (use command line)
cmdl, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
CREATE_NO_WINDOW, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi); // Pointer to PROCESS_INFORMATION structure
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exit_code = 1;
GetExitCodeProcess(pi.hProcess, &exit_code);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return exit_code;
}