-
Notifications
You must be signed in to change notification settings - Fork 3
/
code-windows.c
55 lines (48 loc) · 1.47 KB
/
code-windows.c
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
#define WIN32_LEAN_AND_MEAN
#include <shlwapi.h>
#include <windows.h>
static const WCHAR *NextArgument(const WCHAR *commandLine) {
if (*commandLine == '"') {
commandLine++;
while (*commandLine) {
if (*commandLine++ == '"') {
break;
}
}
} else {
while (*commandLine && *commandLine != ' ' && *commandLine != '\t') {
commandLine++;
}
}
while (*commandLine == ' ' || *commandLine == '\t') {
commandLine++;
}
return commandLine;
}
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline,
int cmdshow) {
WCHAR *commandLine = StrDupW(NextArgument(GetCommandLineW()));
STARTUPINFOW si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
if (CreateProcessW(
/*lpApplicationName=*/NULL, commandLine,
/*lpProcessAttributes=*/NULL,
/*lpThreadAttributes=*/NULL,
/*bInheritHandles=*/FALSE,
/*dwCreationFlags=*/0,
/*lpEnvironment=*/NULL,
/*lpCurrentDirectory=*/NULL, &si, &pi)) {
return 0;
}
DWORD error = GetLastError();
WCHAR *message;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
/*lpSource=*/NULL, /*dwMessageId=*/error,
/*dwLanguageId=*/0, (WCHAR *)&message, /*nSize=*/0,
/*Arguments=*/NULL);
MessageBoxW(NULL, message, L"code.exe", MB_OK | MB_ICONERROR);
return error;
}