-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExeRedirector.cpp
More file actions
105 lines (79 loc) · 2.49 KB
/
Copy pathExeRedirector.cpp
File metadata and controls
105 lines (79 loc) · 2.49 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
#ifndef _UNICODE
#define _UNICODE
#endif
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "user32.lib")
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
)
{
wchar_t fileName[MAX_PATH];
DWORD dwError;
wchar_t errorMsg[MAX_PATH];
wchar_t* errorTitle = L"Redirector Error";
DWORD fileNameLen = GetModuleFileName(NULL, fileName, MAX_PATH);
if (fileNameLen == 0)
{
dwError = GetLastError();
swprintf_s(errorMsg, MAX_PATH, L"%s:0x%x", L"GetModuleFileName failed", dwError);
MessageBox(NULL, errorMsg, L"Redirector Error", MB_OK|MB_ICONERROR);
return 0;
}
wchar_t* configFileName = fileName;
wcscat_s(configFileName, MAX_PATH, L".config");
FILE* config = _wfopen(configFileName, L"r");
if (config == NULL)
{
dwError = GetLastError();
swprintf_s(errorMsg, MAX_PATH, L"%s:0x%x", L"Open config file failed", dwError);
MessageBox(NULL, errorMsg, L"Redirector Error", MB_OK|MB_ICONERROR);
return 0;
}
wchar_t configStr[MAX_PATH];
if (fgetws(configStr, MAX_PATH, config) == NULL)
{
dwError = GetLastError();
return 0;
}
// only the first line is used
wchar_t* firstLineEnd = wcschr(configStr, L'\n');
if (firstLineEnd != NULL)
{
*firstLineEnd = L'\0';
}
wchar_t wzCmdLine[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, lpCmdLine, -1, wzCmdLine, MAX_PATH);
wcscat(configStr, L" ");
wcscat(configStr, wzCmdLine);
STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0};
si.cb = sizeof(si);
BOOL success = CreateProcess(NULL,
configStr,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi);
if (!success)
{
dwError = GetLastError();
swprintf_s(errorMsg, MAX_PATH, L"%s:0x%x", L"Create new process failed", dwError);
MessageBox(NULL, errorMsg, L"Redirector Error", MB_OK|MB_ICONERROR);
return 0;
}
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}