-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArgxCreateProcessA.cpp
269 lines (222 loc) · 7.46 KB
/
ArgxCreateProcessA.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* ArgX - Better command line processing for Windows.
*
* Copyright (c) 2019 Alastair J. Houghton.
*
*/
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include <limits.h>
#include "ArgX.h"
#include "utils.hpp"
namespace {
LPCWSTR* ConvertArgv(DWORD dwArgc, LPCSTR* lpArgv) {
// Work out how much space we need for the Argv array
SIZE_T totalStringSize = 0;
for (DWORD n = 0; n < dwArgc; ++n) {
int ret = MultiByteToWideChar(CP_ACP, 0,
lpArgv[n], -1,
NULL, 0);
if (!ret)
return NULL;
totalStringSize += ret;
}
SIZE_T totalSize = sizeof(LPCWSTR) * dwArgc + sizeof(WCHAR) * totalStringSize;
LPCWSTR* lpArgvW = (LPCWSTR*)HeapAlloc(GetProcessHeap(), 0, totalSize);
if (!lpArgvW)
return NULL;
LPWSTR lpStr = (LPWSTR)(lpArgvW + dwArgc);
SIZE_T remainingStringSize = totalStringSize;
// Convert the Argv array
for (DWORD n = 0; n < dwArgc; ++n) {
lpArgvW[n] = lpStr;
int bufSiz = (remainingStringSize <= INT_MAX
? (int)remainingStringSize : INT_MAX);
int ret = MultiByteToWideChar(CP_ACP, 0,
lpArgv[n], -1,
lpStr, bufSiz);
if (!ret) {
DWORD dwErr = GetLastError();
HeapFree(GetProcessHeap(), 0, lpArgvW);
SetLastError(dwErr);
return NULL;
}
lpStr += ret;
remainingStringSize -= ret;
}
return lpArgvW;
}
void FreeConvertedArgv(LPCWSTR* lpArgv) {
if (lpArgv)
HeapFree(GetProcessHeap(), 0, lpArgv);
}
BOOL ConvertStartupInfo(DWORD dwCreationFlags,
LPSTARTUPINFOA lpStartupInfoA,
LPSTARTUPINFOEXW lpStartupInfoW) {
if (dwCreationFlags & EXTENDED_STARTUPINFO_PRESENT) {
if (lpStartupInfoA->cb != sizeof(STARTUPINFOEXA)) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
LPSTARTUPINFOEXA lpStartupInfoEx = (LPSTARTUPINFOEXA)lpStartupInfoA;
lpStartupInfoW->StartupInfo.cb = sizeof(*lpStartupInfoW);
lpStartupInfoW->lpAttributeList = lpStartupInfoEx->lpAttributeList;
} else {
lpStartupInfoW->StartupInfo.cb = sizeof(lpStartupInfoW->StartupInfo);
lpStartupInfoW->lpAttributeList = NULL;
}
if (lpStartupInfoA->lpDesktop) {
lpStartupInfoW->StartupInfo.lpDesktop
= argx_utils::ConvertMultiByteToWideString(lpStartupInfoA->lpDesktop);
if (!lpStartupInfoW->StartupInfo.lpDesktop)
return FALSE;
}
if (lpStartupInfoA->lpTitle) {
lpStartupInfoW->StartupInfo.lpTitle
= argx_utils::ConvertMultiByteToWideString(lpStartupInfoA->lpTitle);
if (!lpStartupInfoW->StartupInfo.lpTitle)
return FALSE;
}
lpStartupInfoW->StartupInfo.dwX = lpStartupInfoA->dwX;
lpStartupInfoW->StartupInfo.dwY = lpStartupInfoA->dwY;
lpStartupInfoW->StartupInfo.dwXSize = lpStartupInfoA->dwXSize;
lpStartupInfoW->StartupInfo.dwYSize = lpStartupInfoA->dwYSize;
lpStartupInfoW->StartupInfo.dwXCountChars = lpStartupInfoA->dwXCountChars;
lpStartupInfoW->StartupInfo.dwYCountChars = lpStartupInfoA->dwYCountChars;
lpStartupInfoW->StartupInfo.dwFillAttribute = lpStartupInfoA->dwFillAttribute;
lpStartupInfoW->StartupInfo.dwFlags = lpStartupInfoA->dwFlags;
lpStartupInfoW->StartupInfo.wShowWindow = lpStartupInfoA->wShowWindow;
lpStartupInfoW->StartupInfo.hStdInput = lpStartupInfoA->hStdInput;
lpStartupInfoW->StartupInfo.hStdOutput = lpStartupInfoA->hStdOutput;
lpStartupInfoW->StartupInfo.hStdError = lpStartupInfoA->hStdError;
return TRUE;
}
}
BOOL ARGXAPI
ArgxCreateProcessA(LPCSTR lpApplicationName,
LPCSTR* lpArgv,
DWORD dwArgc,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCSTR lpCurrentDirectory,
LPSTARTUPINFOA lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation)
{
// We don't defer to kernel32 for the -A version of this function.
// If you're using it and Microsoft's behaves differently, too bad.
// Don't use the -A version :-)
if (!lpArgv || !dwArgc) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
BOOL bResult = FALSE;
LPWSTR lpAppNameW = NULL;
LPWSTR lpCurrentDirW = NULL;
LPCWSTR* lpArgvW = NULL;
STARTUPINFOEXW startupInfo;
ZeroMemory(&startupInfo, sizeof(startupInfo));
if (lpApplicationName) {
lpAppNameW = argx_utils::ConvertMultiByteToWideString(lpApplicationName);
if (!lpAppNameW)
goto quickexit;
}
if (lpCurrentDirectory) {
lpCurrentDirW = argx_utils::ConvertMultiByteToWideString(lpCurrentDirectory);
if (!lpCurrentDirW)
goto quickexit;
}
lpArgvW = ConvertArgv(dwArgc, lpArgv);
if (!lpArgvW)
goto quickexit;
// Convert the STARTUPINFO structure
if (!ConvertStartupInfo(dwCreationFlags, lpStartupInfo, &startupInfo))
goto quickexit;
bResult = ArgxCreateProcessW(lpAppNameW,
lpArgvW,
dwArgc,
lpProcessAttributes,
lpThreadAttributes,
bInheritHandles,
dwCreationFlags,
lpEnvironment,
lpCurrentDirW,
(LPSTARTUPINFOW)&startupInfo,
lpProcessInformation);
quickexit:
DWORD dwErr = GetLastError();
argx_utils::FreeConvertedString(lpAppNameW);
argx_utils::FreeConvertedString(lpCurrentDirW);
FreeConvertedArgv(lpArgvW);
argx_utils::FreeConvertedString(startupInfo.StartupInfo.lpDesktop);
argx_utils::FreeConvertedString(startupInfo.StartupInfo.lpTitle);
SetLastError(dwErr);
return bResult;
}
BOOL ARGXAPI
ArgxCreateProcessAsUserA(HANDLE hToken,
LPCSTR lpApplicationName,
LPCSTR* lpArgv,
DWORD dwArgc,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCSTR lpCurrentDirectory,
LPSTARTUPINFOA lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation)
{
// We don't defer to kernel32 for the -A version of this function.
// If you're using it and Microsoft's behaves differently, too bad.
// Don't use the -A version :-)
if (!lpArgv || !dwArgc) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
BOOL bResult = FALSE;
LPWSTR lpAppNameW = NULL;
LPWSTR lpCurrentDirW = NULL;
LPCWSTR* lpArgvW = NULL;
STARTUPINFOEXW startupInfo;
ZeroMemory(&startupInfo, sizeof(startupInfo));
if (lpApplicationName) {
lpAppNameW = argx_utils::ConvertMultiByteToWideString(lpApplicationName);
if (!lpAppNameW)
goto quickexit;
}
if (lpCurrentDirectory) {
lpCurrentDirW = argx_utils::ConvertMultiByteToWideString(lpCurrentDirectory);
if (!lpCurrentDirW)
goto quickexit;
}
lpArgvW = ConvertArgv(dwArgc, lpArgv);
if (!lpArgvW)
goto quickexit;
// Convert the STARTUPINFO structure
if (!ConvertStartupInfo(dwCreationFlags, lpStartupInfo, &startupInfo))
goto quickexit;
bResult = ArgxCreateProcessAsUserW(hToken,
lpAppNameW,
lpArgvW,
dwArgc,
lpProcessAttributes,
lpThreadAttributes,
bInheritHandles,
dwCreationFlags,
lpEnvironment,
lpCurrentDirW,
(LPSTARTUPINFOW)&startupInfo,
lpProcessInformation);
quickexit:
DWORD dwErr = GetLastError();
argx_utils::FreeConvertedString(lpAppNameW);
argx_utils::FreeConvertedString(lpCurrentDirW);
FreeConvertedArgv(lpArgvW);
argx_utils::FreeConvertedString(startupInfo.StartupInfo.lpDesktop);
argx_utils::FreeConvertedString(startupInfo.StartupInfo.lpTitle);
SetLastError(dwErr);
return bResult;
}