-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
66 lines (61 loc) · 1.9 KB
/
main.cpp
File metadata and controls
66 lines (61 loc) · 1.9 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
#include <Windows.h>
__forceinline void ZeroMem(void* buf, size_t sz)
{
for (size_t i = 0; i < sz; ++i)
((BYTE*)buf)[i] = 0;
}
int wWinMainCRTStartup()
{
LPWSTR CmdLine = GetCommandLine();
// Remove the main executable from command line
bool InsideQuotes = false;
while ((*CmdLine > L' ') || (*CmdLine && InsideQuotes))
{
// Flip the InsideQuotes if current character is a double quote
if (*CmdLine == L'"')
InsideQuotes = !InsideQuotes;
++CmdLine;
}
// Skip past any white space preceeding the second token.
while (*CmdLine && (*CmdLine <= L' '))
++CmdLine;
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMem(&pi, sizeof(pi));
ZeroMem(&si, sizeof(si));
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
if (CreateProcess(NULL, CmdLine, NULL, NULL, TRUE, CREATE_UNICODE_ENVIRONMENT | CREATE_NO_WINDOW, NULL, NULL, &si, &pi) == 0)
{
DWORD err = GetLastError();
const LPCWSTR MsgPrefix = L"Failed to run command:\n";
LPWSTR MsgError;
LPWSTR MsgResult;
size_t MsgPrefixLen = lstrlenW(MsgPrefix);
size_t MsgErrorLen = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
(LPWSTR)&MsgError,
0,
NULL
);
size_t BufSize = (MsgPrefixLen + MsgErrorLen + 1) * sizeof(WCHAR);
MsgResult = (LPWSTR)LocalAlloc(LMEM_ZEROINIT, BufSize);
lstrcpyW(MsgResult, MsgPrefix);
lstrcpyW(MsgResult + MsgPrefixLen, MsgError);
MessageBox(NULL, MsgResult, L"Console Hider", MB_ICONERROR | MB_OK);
LocalFree(MsgResult);
LocalFree(MsgError);
ExitProcess(1);
}
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
ExitProcess(0);
}