[KERNEL32_WINETEST] Add a PCH.
[reactos.git] / modules / rostests / winetests / kernel32 / process.c
1 /*
2 * Unit test suite for process functions
3 *
4 * Copyright 2002 Eric Pouech
5 * Copyright 2006 Dmitry Timoshkov
6 * Copyright 2014 Michael Müller
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #include "precomp.h"
24
25 /* PROCESS_ALL_ACCESS in Vista+ PSDKs is incompatible with older Windows versions */
26 #define PROCESS_ALL_ACCESS_NT4 (PROCESS_ALL_ACCESS & ~0xf000)
27 /* THREAD_ALL_ACCESS in Vista+ PSDKs is incompatible with older Windows versions */
28 #define THREAD_ALL_ACCESS_NT4 (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3ff)
29
30 #define expect_eq_d(expected, actual) \
31 do { \
32 int value = (actual); \
33 ok((expected) == value, "Expected " #actual " to be %d (" #expected ") is %d\n", \
34 (expected), value); \
35 } while (0)
36 #define expect_eq_s(expected, actual) \
37 do { \
38 LPCSTR value = (actual); \
39 ok(lstrcmpA((expected), value) == 0, "Expected " #actual " to be L\"%s\" (" #expected ") is L\"%s\"\n", \
40 expected, value); \
41 } while (0)
42 #define expect_eq_ws_i(expected, actual) \
43 do { \
44 LPCWSTR value = (actual); \
45 ok(lstrcmpiW((expected), value) == 0, "Expected " #actual " to be L\"%s\" (" #expected ") is L\"%s\"\n", \
46 wine_dbgstr_w(expected), wine_dbgstr_w(value)); \
47 } while (0)
48
49 static HINSTANCE hkernel32, hntdll;
50 static void (WINAPI *pGetNativeSystemInfo)(LPSYSTEM_INFO);
51 static BOOL (WINAPI *pGetSystemRegistryQuota)(PDWORD, PDWORD);
52 static BOOL (WINAPI *pIsWow64Process)(HANDLE,PBOOL);
53 static LPVOID (WINAPI *pVirtualAllocEx)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD);
54 static BOOL (WINAPI *pVirtualFreeEx)(HANDLE, LPVOID, SIZE_T, DWORD);
55 static BOOL (WINAPI *pQueryFullProcessImageNameA)(HANDLE hProcess, DWORD dwFlags, LPSTR lpExeName, PDWORD lpdwSize);
56 static BOOL (WINAPI *pQueryFullProcessImageNameW)(HANDLE hProcess, DWORD dwFlags, LPWSTR lpExeName, PDWORD lpdwSize);
57 static DWORD (WINAPI *pK32GetProcessImageFileNameA)(HANDLE,LPSTR,DWORD);
58 static HANDLE (WINAPI *pCreateJobObjectW)(LPSECURITY_ATTRIBUTES sa, LPCWSTR name);
59 static BOOL (WINAPI *pAssignProcessToJobObject)(HANDLE job, HANDLE process);
60 static BOOL (WINAPI *pIsProcessInJob)(HANDLE process, HANDLE job, PBOOL result);
61 static BOOL (WINAPI *pTerminateJobObject)(HANDLE job, UINT exit_code);
62 static BOOL (WINAPI *pQueryInformationJobObject)(HANDLE job, JOBOBJECTINFOCLASS class, LPVOID info, DWORD len, LPDWORD ret_len);
63 static BOOL (WINAPI *pSetInformationJobObject)(HANDLE job, JOBOBJECTINFOCLASS class, LPVOID info, DWORD len);
64 static HANDLE (WINAPI *pCreateIoCompletionPort)(HANDLE file, HANDLE existing_port, ULONG_PTR key, DWORD threads);
65 static BOOL (WINAPI *pGetNumaProcessorNode)(UCHAR, PUCHAR);
66 static NTSTATUS (WINAPI *pNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
67 static BOOL (WINAPI *pProcessIdToSessionId)(DWORD,DWORD*);
68 static DWORD (WINAPI *pWTSGetActiveConsoleSessionId)(void);
69 static HANDLE (WINAPI *pCreateToolhelp32Snapshot)(DWORD, DWORD);
70 static BOOL (WINAPI *pProcess32First)(HANDLE, PROCESSENTRY32*);
71 static BOOL (WINAPI *pProcess32Next)(HANDLE, PROCESSENTRY32*);
72 static BOOL (WINAPI *pThread32First)(HANDLE, THREADENTRY32*);
73 static BOOL (WINAPI *pThread32Next)(HANDLE, THREADENTRY32*);
74 static BOOL (WINAPI *pGetLogicalProcessorInformationEx)(LOGICAL_PROCESSOR_RELATIONSHIP,SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*,DWORD*);
75 static SIZE_T (WINAPI *pGetLargePageMinimum)(void);
76 static BOOL (WINAPI *pInitializeProcThreadAttributeList)(struct _PROC_THREAD_ATTRIBUTE_LIST*, DWORD, DWORD, SIZE_T*);
77 static BOOL (WINAPI *pUpdateProcThreadAttribute)(struct _PROC_THREAD_ATTRIBUTE_LIST*, DWORD, DWORD_PTR, void *,SIZE_T,void*,SIZE_T*);
78 static void (WINAPI *pDeleteProcThreadAttributeList)(struct _PROC_THREAD_ATTRIBUTE_LIST*);
79 static DWORD (WINAPI *pGetActiveProcessorCount)(WORD);
80
81 /* ############################### */
82 static char base[MAX_PATH];
83 static char selfname[MAX_PATH];
84 static char* exename;
85 static char resfile[MAX_PATH];
86
87 static int myARGC;
88 static char** myARGV;
89
90 /* As some environment variables get very long on Unix, we only test for
91 * the first 127 bytes.
92 * Note that increasing this value past 256 may exceed the buffer size
93 * limitations of the *Profile functions (at least on Wine).
94 */
95 #define MAX_LISTED_ENV_VAR 128
96
97 /* ---------------- portable memory allocation thingie */
98
99 static char memory[1024*256];
100 static char* memory_index = memory;
101
102 static char* grab_memory(size_t len)
103 {
104 char* ret = memory_index;
105 /* align on dword */
106 len = (len + 3) & ~3;
107 memory_index += len;
108 assert(memory_index <= memory + sizeof(memory));
109 return ret;
110 }
111
112 static void release_memory(void)
113 {
114 memory_index = memory;
115 }
116
117 /* ---------------- simplistic tool to encode/decode strings (to hide \ " ' and such) */
118
119 static const char* encodeA(const char* str)
120 {
121 char* ptr;
122 size_t len,i;
123
124 if (!str) return "";
125 len = strlen(str) + 1;
126 ptr = grab_memory(len * 2 + 1);
127 for (i = 0; i < len; i++)
128 sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
129 ptr[2 * len] = '\0';
130 return ptr;
131 }
132
133 static const char* encodeW(const WCHAR* str)
134 {
135 char* ptr;
136 size_t len,i;
137
138 if (!str) return "";
139 len = lstrlenW(str) + 1;
140 ptr = grab_memory(len * 4 + 1);
141 assert(ptr);
142 for (i = 0; i < len; i++)
143 sprintf(&ptr[i * 4], "%04x", (unsigned int)(unsigned short)str[i]);
144 ptr[4 * len] = '\0';
145 return ptr;
146 }
147
148 static unsigned decode_char(char c)
149 {
150 if (c >= '0' && c <= '9') return c - '0';
151 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
152 assert(c >= 'A' && c <= 'F');
153 return c - 'A' + 10;
154 }
155
156 static char* decodeA(const char* str)
157 {
158 char* ptr;
159 size_t len,i;
160
161 len = strlen(str) / 2;
162 if (!len--) return NULL;
163 ptr = grab_memory(len + 1);
164 for (i = 0; i < len; i++)
165 ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
166 ptr[len] = '\0';
167 return ptr;
168 }
169
170 /* This will be needed to decode Unicode strings saved by the child process
171 * when we test Unicode functions.
172 */
173 static WCHAR* decodeW(const char* str)
174 {
175 size_t len;
176 WCHAR* ptr;
177 int i;
178
179 len = strlen(str) / 4;
180 if (!len--) return NULL;
181 ptr = (WCHAR*)grab_memory(len * 2 + 1);
182 for (i = 0; i < len; i++)
183 ptr[i] = (decode_char(str[4 * i]) << 12) |
184 (decode_char(str[4 * i + 1]) << 8) |
185 (decode_char(str[4 * i + 2]) << 4) |
186 (decode_char(str[4 * i + 3]) << 0);
187 ptr[len] = '\0';
188 return ptr;
189 }
190
191 /******************************************************************
192 * init
193 *
194 * generates basic information like:
195 * base: absolute path to curr dir
196 * selfname: the way to reinvoke ourselves
197 * exename: executable without the path
198 * function-pointers, which are not implemented in all windows versions
199 */
200 static BOOL init(void)
201 {
202 char *p;
203
204 myARGC = winetest_get_mainargs( &myARGV );
205 if (!GetCurrentDirectoryA(sizeof(base), base)) return FALSE;
206 strcpy(selfname, myARGV[0]);
207
208 /* Strip the path of selfname */
209 if ((p = strrchr(selfname, '\\')) != NULL) exename = p + 1;
210 else exename = selfname;
211
212 if ((p = strrchr(exename, '/')) != NULL) exename = p + 1;
213
214 hkernel32 = GetModuleHandleA("kernel32");
215 hntdll = GetModuleHandleA("ntdll.dll");
216
217 pNtQueryInformationProcess = (void *)GetProcAddress(hntdll, "NtQueryInformationProcess");
218
219 pGetNativeSystemInfo = (void *) GetProcAddress(hkernel32, "GetNativeSystemInfo");
220 pGetSystemRegistryQuota = (void *) GetProcAddress(hkernel32, "GetSystemRegistryQuota");
221 pIsWow64Process = (void *) GetProcAddress(hkernel32, "IsWow64Process");
222 pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
223 pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
224 pQueryFullProcessImageNameA = (void *) GetProcAddress(hkernel32, "QueryFullProcessImageNameA");
225 pQueryFullProcessImageNameW = (void *) GetProcAddress(hkernel32, "QueryFullProcessImageNameW");
226 pK32GetProcessImageFileNameA = (void *) GetProcAddress(hkernel32, "K32GetProcessImageFileNameA");
227 pCreateJobObjectW = (void *)GetProcAddress(hkernel32, "CreateJobObjectW");
228 pAssignProcessToJobObject = (void *)GetProcAddress(hkernel32, "AssignProcessToJobObject");
229 pIsProcessInJob = (void *)GetProcAddress(hkernel32, "IsProcessInJob");
230 pTerminateJobObject = (void *)GetProcAddress(hkernel32, "TerminateJobObject");
231 pQueryInformationJobObject = (void *)GetProcAddress(hkernel32, "QueryInformationJobObject");
232 pSetInformationJobObject = (void *)GetProcAddress(hkernel32, "SetInformationJobObject");
233 pCreateIoCompletionPort = (void *)GetProcAddress(hkernel32, "CreateIoCompletionPort");
234 pGetNumaProcessorNode = (void *)GetProcAddress(hkernel32, "GetNumaProcessorNode");
235 pProcessIdToSessionId = (void *)GetProcAddress(hkernel32, "ProcessIdToSessionId");
236 pWTSGetActiveConsoleSessionId = (void *)GetProcAddress(hkernel32, "WTSGetActiveConsoleSessionId");
237 pCreateToolhelp32Snapshot = (void *)GetProcAddress(hkernel32, "CreateToolhelp32Snapshot");
238 pProcess32First = (void *)GetProcAddress(hkernel32, "Process32First");
239 pProcess32Next = (void *)GetProcAddress(hkernel32, "Process32Next");
240 pThread32First = (void *)GetProcAddress(hkernel32, "Thread32First");
241 pThread32Next = (void *)GetProcAddress(hkernel32, "Thread32Next");
242 pGetLogicalProcessorInformationEx = (void *)GetProcAddress(hkernel32, "GetLogicalProcessorInformationEx");
243 pGetLargePageMinimum = (void *)GetProcAddress(hkernel32, "GetLargePageMinimum");
244 pInitializeProcThreadAttributeList = (void *)GetProcAddress(hkernel32, "InitializeProcThreadAttributeList");
245 pUpdateProcThreadAttribute = (void *)GetProcAddress(hkernel32, "UpdateProcThreadAttribute");
246 pDeleteProcThreadAttributeList = (void *)GetProcAddress(hkernel32, "DeleteProcThreadAttributeList");
247 pGetActiveProcessorCount = (void *)GetProcAddress(hkernel32, "GetActiveProcessorCount");
248
249 return TRUE;
250 }
251
252 /******************************************************************
253 * get_file_name
254 *
255 * generates an absolute file_name for temporary file
256 *
257 */
258 static void get_file_name(char* buf)
259 {
260 char path[MAX_PATH];
261
262 buf[0] = '\0';
263 GetTempPathA(sizeof(path), path);
264 GetTempFileNameA(path, "wt", 0, buf);
265 }
266
267 /******************************************************************
268 * static void childPrintf
269 *
270 */
271 static void WINETEST_PRINTF_ATTR(2,3) childPrintf(HANDLE h, const char* fmt, ...)
272 {
273 va_list valist;
274 char buffer[1024+4*MAX_LISTED_ENV_VAR];
275 DWORD w;
276
277 va_start(valist, fmt);
278 vsprintf(buffer, fmt, valist);
279 va_end(valist);
280 WriteFile(h, buffer, strlen(buffer), &w, NULL);
281 }
282
283
284 /******************************************************************
285 * doChild
286 *
287 * output most of the information in the child process
288 */
289 static void doChild(const char* file, const char* option)
290 {
291 RTL_USER_PROCESS_PARAMETERS *params = NtCurrentTeb()->Peb->ProcessParameters;
292 STARTUPINFOA siA;
293 STARTUPINFOW siW;
294 int i;
295 char *ptrA, *ptrA_save;
296 WCHAR *ptrW, *ptrW_save;
297 char bufA[MAX_PATH];
298 WCHAR bufW[MAX_PATH];
299 HANDLE hFile = CreateFileA(file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
300 HANDLE snapshot;
301 PROCESSENTRY32 pe;
302 BOOL ret;
303
304 if (hFile == INVALID_HANDLE_VALUE) return;
305
306 /* output of startup info (Ansi) */
307 GetStartupInfoA(&siA);
308 childPrintf(hFile,
309 "[StartupInfoA]\ncb=%08u\nlpDesktop=%s\nlpTitle=%s\n"
310 "dwX=%u\ndwY=%u\ndwXSize=%u\ndwYSize=%u\n"
311 "dwXCountChars=%u\ndwYCountChars=%u\ndwFillAttribute=%u\n"
312 "dwFlags=%u\nwShowWindow=%u\n"
313 "hStdInput=%lu\nhStdOutput=%lu\nhStdError=%lu\n\n",
314 siA.cb, encodeA(siA.lpDesktop), encodeA(siA.lpTitle),
315 siA.dwX, siA.dwY, siA.dwXSize, siA.dwYSize,
316 siA.dwXCountChars, siA.dwYCountChars, siA.dwFillAttribute,
317 siA.dwFlags, siA.wShowWindow,
318 (DWORD_PTR)siA.hStdInput, (DWORD_PTR)siA.hStdOutput, (DWORD_PTR)siA.hStdError);
319
320 /* check the console handles in the TEB */
321 childPrintf(hFile, "[TEB]\nhStdInput=%lu\nhStdOutput=%lu\nhStdError=%lu\n\n",
322 (DWORD_PTR)params->hStdInput, (DWORD_PTR)params->hStdOutput,
323 (DWORD_PTR)params->hStdError);
324
325 /* since GetStartupInfoW is only implemented in win2k,
326 * zero out before calling so we can notice the difference
327 */
328 memset(&siW, 0, sizeof(siW));
329 GetStartupInfoW(&siW);
330 childPrintf(hFile,
331 "[StartupInfoW]\ncb=%08u\nlpDesktop=%s\nlpTitle=%s\n"
332 "dwX=%u\ndwY=%u\ndwXSize=%u\ndwYSize=%u\n"
333 "dwXCountChars=%u\ndwYCountChars=%u\ndwFillAttribute=%u\n"
334 "dwFlags=%u\nwShowWindow=%u\n"
335 "hStdInput=%lu\nhStdOutput=%lu\nhStdError=%lu\n\n",
336 siW.cb, encodeW(siW.lpDesktop), encodeW(siW.lpTitle),
337 siW.dwX, siW.dwY, siW.dwXSize, siW.dwYSize,
338 siW.dwXCountChars, siW.dwYCountChars, siW.dwFillAttribute,
339 siW.dwFlags, siW.wShowWindow,
340 (DWORD_PTR)siW.hStdInput, (DWORD_PTR)siW.hStdOutput, (DWORD_PTR)siW.hStdError);
341
342 /* Arguments */
343 childPrintf(hFile, "[Arguments]\nargcA=%d\n", myARGC);
344 for (i = 0; i < myARGC; i++)
345 {
346 childPrintf(hFile, "argvA%d=%s\n", i, encodeA(myARGV[i]));
347 }
348 childPrintf(hFile, "CommandLineA=%s\n", encodeA(GetCommandLineA()));
349 childPrintf(hFile, "CommandLineW=%s\n\n", encodeW(GetCommandLineW()));
350
351 /* output toolhelp information */
352 snapshot = pCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
353 ok(snapshot != INVALID_HANDLE_VALUE, "CreateToolhelp32Snapshot failed %u\n", GetLastError());
354 memset(&pe, 0, sizeof(pe));
355 pe.dwSize = sizeof(pe);
356 if (pProcess32First(snapshot, &pe))
357 {
358 while (pe.th32ProcessID != GetCurrentProcessId())
359 if (!pProcess32Next(snapshot, &pe)) break;
360 }
361 CloseHandle(snapshot);
362 ok(pe.th32ProcessID == GetCurrentProcessId(), "failed to find current process in snapshot\n");
363 childPrintf(hFile,
364 "[Toolhelp]\ncntUsage=%u\nth32DefaultHeapID=%lu\n"
365 "th32ModuleID=%u\ncntThreads=%u\nth32ParentProcessID=%u\n"
366 "pcPriClassBase=%u\ndwFlags=%u\nszExeFile=%s\n\n",
367 pe.cntUsage, pe.th32DefaultHeapID, pe.th32ModuleID,
368 pe.cntThreads, pe.th32ParentProcessID, pe.pcPriClassBase,
369 pe.dwFlags, encodeA(pe.szExeFile));
370
371 /* output of environment (Ansi) */
372 ptrA_save = ptrA = GetEnvironmentStringsA();
373 if (ptrA)
374 {
375 char env_var[MAX_LISTED_ENV_VAR];
376
377 childPrintf(hFile, "[EnvironmentA]\n");
378 i = 0;
379 while (*ptrA)
380 {
381 lstrcpynA(env_var, ptrA, MAX_LISTED_ENV_VAR);
382 childPrintf(hFile, "env%d=%s\n", i, encodeA(env_var));
383 i++;
384 ptrA += strlen(ptrA) + 1;
385 }
386 childPrintf(hFile, "len=%d\n\n", i);
387 FreeEnvironmentStringsA(ptrA_save);
388 }
389
390 /* output of environment (Unicode) */
391 ptrW_save = ptrW = GetEnvironmentStringsW();
392 if (ptrW)
393 {
394 WCHAR env_var[MAX_LISTED_ENV_VAR];
395
396 childPrintf(hFile, "[EnvironmentW]\n");
397 i = 0;
398 while (*ptrW)
399 {
400 lstrcpynW(env_var, ptrW, MAX_LISTED_ENV_VAR - 1);
401 env_var[MAX_LISTED_ENV_VAR - 1] = '\0';
402 childPrintf(hFile, "env%d=%s\n", i, encodeW(env_var));
403 i++;
404 ptrW += lstrlenW(ptrW) + 1;
405 }
406 childPrintf(hFile, "len=%d\n\n", i);
407 FreeEnvironmentStringsW(ptrW_save);
408 }
409
410 childPrintf(hFile, "[Misc]\n");
411 if (GetCurrentDirectoryA(sizeof(bufA), bufA))
412 childPrintf(hFile, "CurrDirA=%s\n", encodeA(bufA));
413 if (GetCurrentDirectoryW(sizeof(bufW) / sizeof(bufW[0]), bufW))
414 childPrintf(hFile, "CurrDirW=%s\n", encodeW(bufW));
415 childPrintf(hFile, "\n");
416
417 if (option && strcmp(option, "console") == 0)
418 {
419 CONSOLE_SCREEN_BUFFER_INFO sbi;
420 HANDLE hConIn = GetStdHandle(STD_INPUT_HANDLE);
421 HANDLE hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
422 DWORD modeIn, modeOut;
423
424 childPrintf(hFile, "[Console]\n");
425 if (GetConsoleScreenBufferInfo(hConOut, &sbi))
426 {
427 childPrintf(hFile, "SizeX=%d\nSizeY=%d\nCursorX=%d\nCursorY=%d\nAttributes=%d\n",
428 sbi.dwSize.X, sbi.dwSize.Y, sbi.dwCursorPosition.X, sbi.dwCursorPosition.Y, sbi.wAttributes);
429 childPrintf(hFile, "winLeft=%d\nwinTop=%d\nwinRight=%d\nwinBottom=%d\n",
430 sbi.srWindow.Left, sbi.srWindow.Top, sbi.srWindow.Right, sbi.srWindow.Bottom);
431 childPrintf(hFile, "maxWinWidth=%d\nmaxWinHeight=%d\n",
432 sbi.dwMaximumWindowSize.X, sbi.dwMaximumWindowSize.Y);
433 }
434 childPrintf(hFile, "InputCP=%d\nOutputCP=%d\n",
435 GetConsoleCP(), GetConsoleOutputCP());
436 if (GetConsoleMode(hConIn, &modeIn))
437 childPrintf(hFile, "InputMode=%u\n", modeIn);
438 if (GetConsoleMode(hConOut, &modeOut))
439 childPrintf(hFile, "OutputMode=%u\n", modeOut);
440
441 /* now that we have written all relevant information, let's change it */
442 SetLastError(0xdeadbeef);
443 ret = SetConsoleCP(1252);
444 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
445 {
446 win_skip("Setting the codepage is not implemented\n");
447 }
448 else
449 {
450 ok(ret, "Setting CP\n");
451 ok(SetConsoleOutputCP(1252), "Setting SB CP\n");
452 }
453
454 ret = SetConsoleMode(hConIn, modeIn ^ 1);
455 ok( ret, "Setting mode (%d)\n", GetLastError());
456 ret = SetConsoleMode(hConOut, modeOut ^ 1);
457 ok( ret, "Setting mode (%d)\n", GetLastError());
458 sbi.dwCursorPosition.X ^= 1;
459 sbi.dwCursorPosition.Y ^= 1;
460 ret = SetConsoleCursorPosition(hConOut, sbi.dwCursorPosition);
461 ok( ret, "Setting cursor position (%d)\n", GetLastError());
462 }
463 if (option && strcmp(option, "stdhandle") == 0)
464 {
465 HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
466 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
467
468 if (hStdIn != INVALID_HANDLE_VALUE || hStdOut != INVALID_HANDLE_VALUE)
469 {
470 char buf[1024];
471 DWORD r, w;
472
473 ok(ReadFile(hStdIn, buf, sizeof(buf), &r, NULL) && r > 0, "Reading message from input pipe\n");
474 childPrintf(hFile, "[StdHandle]\nmsg=%s\n\n", encodeA(buf));
475 ok(WriteFile(hStdOut, buf, r, &w, NULL) && w == r, "Writing message to output pipe\n");
476 }
477 }
478
479 if (option && strcmp(option, "exit_code") == 0)
480 {
481 childPrintf(hFile, "[ExitCode]\nvalue=%d\n\n", 123);
482 CloseHandle(hFile);
483 ExitProcess(123);
484 }
485
486 CloseHandle(hFile);
487 }
488
489 static char* getChildString(const char* sect, const char* key)
490 {
491 char buf[1024+4*MAX_LISTED_ENV_VAR];
492 char* ret;
493
494 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), resfile);
495 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
496 assert(!(strlen(buf) & 1));
497 ret = decodeA(buf);
498 return ret;
499 }
500
501 static WCHAR* getChildStringW(const char* sect, const char* key)
502 {
503 char buf[1024+4*MAX_LISTED_ENV_VAR];
504 WCHAR* ret;
505
506 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), resfile);
507 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
508 assert(!(strlen(buf) & 1));
509 ret = decodeW(buf);
510 return ret;
511 }
512
513 /* FIXME: this may be moved to the wtmain.c file, because it may be needed by
514 * others... (windows uses stricmp while Un*x uses strcasecmp...)
515 */
516 static int wtstrcasecmp(const char* p1, const char* p2)
517 {
518 char c1, c2;
519
520 c1 = c2 = '@';
521 while (c1 == c2 && c1)
522 {
523 c1 = *p1++; c2 = *p2++;
524 if (c1 != c2)
525 {
526 c1 = toupper(c1); c2 = toupper(c2);
527 }
528 }
529 return c1 - c2;
530 }
531
532 static int strCmp(const char* s1, const char* s2, BOOL sensitive)
533 {
534 if (!s1 && !s2) return 0;
535 if (!s2) return -1;
536 if (!s1) return 1;
537 return (sensitive) ? strcmp(s1, s2) : wtstrcasecmp(s1, s2);
538 }
539
540 static void ok_child_string( int line, const char *sect, const char *key,
541 const char *expect, int sensitive )
542 {
543 char* result = getChildString( sect, key );
544 ok_(__FILE__, line)( strCmp(result, expect, sensitive) == 0, "%s:%s expected '%s', got '%s'\n",
545 sect, key, expect ? expect : "(null)", result );
546 }
547
548 static void ok_child_stringWA( int line, const char *sect, const char *key,
549 const char *expect, int sensitive )
550 {
551 WCHAR* expectW;
552 CHAR* resultA;
553 DWORD len;
554 WCHAR* result = getChildStringW( sect, key );
555
556 len = MultiByteToWideChar( CP_ACP, 0, expect, -1, NULL, 0);
557 expectW = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
558 MultiByteToWideChar( CP_ACP, 0, expect, -1, expectW, len);
559
560 len = WideCharToMultiByte( CP_ACP, 0, result, -1, NULL, 0, NULL, NULL);
561 resultA = HeapAlloc(GetProcessHeap(),0,len*sizeof(CHAR));
562 WideCharToMultiByte( CP_ACP, 0, result, -1, resultA, len, NULL, NULL);
563
564 if (sensitive)
565 ok_(__FILE__, line)( lstrcmpW(result, expectW) == 0, "%s:%s expected '%s', got '%s'\n",
566 sect, key, expect ? expect : "(null)", resultA );
567 else
568 ok_(__FILE__, line)( lstrcmpiW(result, expectW) == 0, "%s:%s expected '%s', got '%s'\n",
569 sect, key, expect ? expect : "(null)", resultA );
570 HeapFree(GetProcessHeap(),0,expectW);
571 HeapFree(GetProcessHeap(),0,resultA);
572 }
573
574 static void ok_child_int( int line, const char *sect, const char *key, UINT expect )
575 {
576 UINT result = GetPrivateProfileIntA( sect, key, !expect, resfile );
577 ok_(__FILE__, line)( result == expect, "%s:%s expected %u, but got %u\n", sect, key, expect, result );
578 }
579
580 #define okChildString(sect, key, expect) ok_child_string(__LINE__, (sect), (key), (expect), 1 )
581 #define okChildIString(sect, key, expect) ok_child_string(__LINE__, (sect), (key), (expect), 0 )
582 #define okChildStringWA(sect, key, expect) ok_child_stringWA(__LINE__, (sect), (key), (expect), 1 )
583 #define okChildInt(sect, key, expect) ok_child_int(__LINE__, (sect), (key), (expect))
584
585 static void test_Startup(void)
586 {
587 char buffer[MAX_PATH];
588 PROCESS_INFORMATION info;
589 STARTUPINFOA startup,si;
590 char *result;
591 static CHAR title[] = "I'm the title string",
592 desktop[] = "winsta0\\default",
593 empty[] = "";
594
595 /* let's start simplistic */
596 memset(&startup, 0, sizeof(startup));
597 startup.cb = sizeof(startup);
598 startup.dwFlags = STARTF_USESHOWWINDOW;
599 startup.wShowWindow = SW_SHOWNORMAL;
600
601 get_file_name(resfile);
602 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\"", selfname, resfile);
603 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
604 /* wait for child to terminate */
605 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
606 /* child process has changed result file, so let profile functions know about it */
607 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
608
609 GetStartupInfoA(&si);
610 okChildInt("StartupInfoA", "cb", startup.cb);
611 okChildString("StartupInfoA", "lpDesktop", si.lpDesktop);
612 okChildInt("StartupInfoA", "dwX", startup.dwX);
613 okChildInt("StartupInfoA", "dwY", startup.dwY);
614 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
615 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
616 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
617 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
618 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
619 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
620 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
621 release_memory();
622 DeleteFileA(resfile);
623
624 /* not so simplistic now */
625 memset(&startup, 0, sizeof(startup));
626 startup.cb = sizeof(startup);
627 startup.dwFlags = STARTF_USESHOWWINDOW;
628 startup.wShowWindow = SW_SHOWNORMAL;
629 startup.lpTitle = title;
630 startup.lpDesktop = desktop;
631 startup.dwXCountChars = 0x12121212;
632 startup.dwYCountChars = 0x23232323;
633 startup.dwX = 0x34343434;
634 startup.dwY = 0x45454545;
635 startup.dwXSize = 0x56565656;
636 startup.dwYSize = 0x67676767;
637 startup.dwFillAttribute = 0xA55A;
638
639 get_file_name(resfile);
640 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\"", selfname, resfile);
641 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
642 /* wait for child to terminate */
643 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
644 /* child process has changed result file, so let profile functions know about it */
645 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
646
647 okChildInt("StartupInfoA", "cb", startup.cb);
648 okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
649 okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
650 okChildInt("StartupInfoA", "dwX", startup.dwX);
651 okChildInt("StartupInfoA", "dwY", startup.dwY);
652 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
653 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
654 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
655 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
656 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
657 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
658 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
659 release_memory();
660 DeleteFileA(resfile);
661
662 /* not so simplistic now */
663 memset(&startup, 0, sizeof(startup));
664 startup.cb = sizeof(startup);
665 startup.dwFlags = STARTF_USESHOWWINDOW;
666 startup.wShowWindow = SW_SHOWNORMAL;
667 startup.lpTitle = title;
668 startup.lpDesktop = NULL;
669 startup.dwXCountChars = 0x12121212;
670 startup.dwYCountChars = 0x23232323;
671 startup.dwX = 0x34343434;
672 startup.dwY = 0x45454545;
673 startup.dwXSize = 0x56565656;
674 startup.dwYSize = 0x67676767;
675 startup.dwFillAttribute = 0xA55A;
676
677 get_file_name(resfile);
678 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\"", selfname, resfile);
679 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
680 /* wait for child to terminate */
681 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
682 /* child process has changed result file, so let profile functions know about it */
683 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
684
685 okChildInt("StartupInfoA", "cb", startup.cb);
686 okChildString("StartupInfoA", "lpDesktop", si.lpDesktop);
687 okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
688 okChildInt("StartupInfoA", "dwX", startup.dwX);
689 okChildInt("StartupInfoA", "dwY", startup.dwY);
690 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
691 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
692 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
693 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
694 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
695 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
696 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
697 release_memory();
698 DeleteFileA(resfile);
699
700 /* not so simplistic now */
701 memset(&startup, 0, sizeof(startup));
702 startup.cb = sizeof(startup);
703 startup.dwFlags = STARTF_USESHOWWINDOW;
704 startup.wShowWindow = SW_SHOWNORMAL;
705 startup.lpTitle = title;
706 startup.lpDesktop = empty;
707 startup.dwXCountChars = 0x12121212;
708 startup.dwYCountChars = 0x23232323;
709 startup.dwX = 0x34343434;
710 startup.dwY = 0x45454545;
711 startup.dwXSize = 0x56565656;
712 startup.dwYSize = 0x67676767;
713 startup.dwFillAttribute = 0xA55A;
714
715 get_file_name(resfile);
716 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\"", selfname, resfile);
717 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
718 /* wait for child to terminate */
719 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
720 /* child process has changed result file, so let profile functions know about it */
721 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
722
723 okChildInt("StartupInfoA", "cb", startup.cb);
724 okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
725 okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
726 okChildInt("StartupInfoA", "dwX", startup.dwX);
727 okChildInt("StartupInfoA", "dwY", startup.dwY);
728 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
729 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
730 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
731 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
732 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
733 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
734 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
735 release_memory();
736 DeleteFileA(resfile);
737
738 /* not so simplistic now */
739 memset(&startup, 0, sizeof(startup));
740 startup.cb = sizeof(startup);
741 startup.dwFlags = STARTF_USESHOWWINDOW;
742 startup.wShowWindow = SW_SHOWNORMAL;
743 startup.lpTitle = NULL;
744 startup.lpDesktop = desktop;
745 startup.dwXCountChars = 0x12121212;
746 startup.dwYCountChars = 0x23232323;
747 startup.dwX = 0x34343434;
748 startup.dwY = 0x45454545;
749 startup.dwXSize = 0x56565656;
750 startup.dwYSize = 0x67676767;
751 startup.dwFillAttribute = 0xA55A;
752
753 get_file_name(resfile);
754 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\"", selfname, resfile);
755 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
756 /* wait for child to terminate */
757 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
758 /* child process has changed result file, so let profile functions know about it */
759 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
760
761 okChildInt("StartupInfoA", "cb", startup.cb);
762 okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
763 result = getChildString( "StartupInfoA", "lpTitle" );
764 ok( broken(!result) || (result && !strCmp( result, selfname, 0 )),
765 "expected '%s' or null, got '%s'\n", selfname, result );
766 okChildInt("StartupInfoA", "dwX", startup.dwX);
767 okChildInt("StartupInfoA", "dwY", startup.dwY);
768 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
769 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
770 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
771 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
772 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
773 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
774 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
775 release_memory();
776 DeleteFileA(resfile);
777
778 /* not so simplistic now */
779 memset(&startup, 0, sizeof(startup));
780 startup.cb = sizeof(startup);
781 startup.dwFlags = STARTF_USESHOWWINDOW;
782 startup.wShowWindow = SW_SHOWNORMAL;
783 startup.lpTitle = empty;
784 startup.lpDesktop = desktop;
785 startup.dwXCountChars = 0x12121212;
786 startup.dwYCountChars = 0x23232323;
787 startup.dwX = 0x34343434;
788 startup.dwY = 0x45454545;
789 startup.dwXSize = 0x56565656;
790 startup.dwYSize = 0x67676767;
791 startup.dwFillAttribute = 0xA55A;
792
793 get_file_name(resfile);
794 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\"", selfname, resfile);
795 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
796 /* wait for child to terminate */
797 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
798 /* child process has changed result file, so let profile functions know about it */
799 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
800
801 okChildInt("StartupInfoA", "cb", startup.cb);
802 okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
803 okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
804 okChildInt("StartupInfoA", "dwX", startup.dwX);
805 okChildInt("StartupInfoA", "dwY", startup.dwY);
806 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
807 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
808 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
809 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
810 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
811 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
812 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
813 release_memory();
814 DeleteFileA(resfile);
815
816 /* not so simplistic now */
817 memset(&startup, 0, sizeof(startup));
818 startup.cb = sizeof(startup);
819 startup.dwFlags = STARTF_USESHOWWINDOW;
820 startup.wShowWindow = SW_SHOWNORMAL;
821 startup.lpTitle = empty;
822 startup.lpDesktop = empty;
823 startup.dwXCountChars = 0x12121212;
824 startup.dwYCountChars = 0x23232323;
825 startup.dwX = 0x34343434;
826 startup.dwY = 0x45454545;
827 startup.dwXSize = 0x56565656;
828 startup.dwYSize = 0x67676767;
829 startup.dwFillAttribute = 0xA55A;
830
831 get_file_name(resfile);
832 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\"", selfname, resfile);
833 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
834 /* wait for child to terminate */
835 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
836 /* child process has changed result file, so let profile functions know about it */
837 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
838
839 okChildInt("StartupInfoA", "cb", startup.cb);
840 okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
841 okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
842 okChildInt("StartupInfoA", "dwX", startup.dwX);
843 okChildInt("StartupInfoA", "dwY", startup.dwY);
844 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
845 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
846 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
847 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
848 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
849 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
850 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
851 release_memory();
852 DeleteFileA(resfile);
853
854 /* TODO: test for A/W and W/A and W/W */
855 }
856
857 static void test_CommandLine(void)
858 {
859 char buffer[MAX_PATH], fullpath[MAX_PATH], *lpFilePart, *p;
860 char buffer2[MAX_PATH];
861 PROCESS_INFORMATION info;
862 STARTUPINFOA startup;
863 BOOL ret;
864
865 memset(&startup, 0, sizeof(startup));
866 startup.cb = sizeof(startup);
867 startup.dwFlags = STARTF_USESHOWWINDOW;
868 startup.wShowWindow = SW_SHOWNORMAL;
869
870 /* the basics */
871 get_file_name(resfile);
872 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\" \"C:\\Program Files\\my nice app.exe\"", selfname, resfile);
873 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
874 /* wait for child to terminate */
875 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
876 /* child process has changed result file, so let profile functions know about it */
877 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
878
879 okChildInt("Arguments", "argcA", 5);
880 okChildString("Arguments", "argvA4", "C:\\Program Files\\my nice app.exe");
881 okChildString("Arguments", "argvA5", NULL);
882 okChildString("Arguments", "CommandLineA", buffer);
883 release_memory();
884 DeleteFileA(resfile);
885
886 memset(&startup, 0, sizeof(startup));
887 startup.cb = sizeof(startup);
888 startup.dwFlags = STARTF_USESHOWWINDOW;
889 startup.wShowWindow = SW_SHOWNORMAL;
890
891 /* from François */
892 get_file_name(resfile);
893 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\" \"a\\\"b\\\\\" c\\\" d", selfname, resfile);
894 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
895 /* wait for child to terminate */
896 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
897 /* child process has changed result file, so let profile functions know about it */
898 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
899
900 okChildInt("Arguments", "argcA", 7);
901 okChildString("Arguments", "argvA4", "a\"b\\");
902 okChildString("Arguments", "argvA5", "c\"");
903 okChildString("Arguments", "argvA6", "d");
904 okChildString("Arguments", "argvA7", NULL);
905 okChildString("Arguments", "CommandLineA", buffer);
906 release_memory();
907 DeleteFileA(resfile);
908
909 /* Test for Bug1330 to show that XP doesn't change '/' to '\\' in argv[0]*/
910 get_file_name(resfile);
911 /* Use exename to avoid buffer containing things like 'C:' */
912 sprintf(buffer, "./%s tests/process.c dump \"%s\" \"a\\\"b\\\\\" c\\\" d", exename, resfile);
913 SetLastError(0xdeadbeef);
914 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
915 ok(ret, "CreateProcess (%s) failed : %d\n", buffer, GetLastError());
916 /* wait for child to terminate */
917 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
918 /* child process has changed result file, so let profile functions know about it */
919 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
920 sprintf(buffer, "./%s", exename);
921 okChildString("Arguments", "argvA0", buffer);
922 release_memory();
923 DeleteFileA(resfile);
924
925 get_file_name(resfile);
926 /* Use exename to avoid buffer containing things like 'C:' */
927 sprintf(buffer, ".\\%s tests/process.c dump \"%s\" \"a\\\"b\\\\\" c\\\" d", exename, resfile);
928 SetLastError(0xdeadbeef);
929 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
930 ok(ret, "CreateProcess (%s) failed : %d\n", buffer, GetLastError());
931 /* wait for child to terminate */
932 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
933 /* child process has changed result file, so let profile functions know about it */
934 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
935 sprintf(buffer, ".\\%s", exename);
936 okChildString("Arguments", "argvA0", buffer);
937 release_memory();
938 DeleteFileA(resfile);
939
940 get_file_name(resfile);
941 GetFullPathNameA(selfname, MAX_PATH, fullpath, &lpFilePart);
942 assert ( lpFilePart != 0);
943 *(lpFilePart -1 ) = 0;
944 p = strrchr(fullpath, '\\');
945 /* Use exename to avoid buffer containing things like 'C:' */
946 if (p) sprintf(buffer, "..%s/%s tests/process.c dump \"%s\" \"a\\\"b\\\\\" c\\\" d", p, exename, resfile);
947 else sprintf(buffer, "./%s tests/process.c dump \"%s\" \"a\\\"b\\\\\" c\\\" d", exename, resfile);
948 SetLastError(0xdeadbeef);
949 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
950 ok(ret, "CreateProcess (%s) failed : %d\n", buffer, GetLastError());
951 /* wait for child to terminate */
952 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
953 /* child process has changed result file, so let profile functions know about it */
954 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
955 if (p) sprintf(buffer, "..%s/%s", p, exename);
956 else sprintf(buffer, "./%s", exename);
957 okChildString("Arguments", "argvA0", buffer);
958 release_memory();
959 DeleteFileA(resfile);
960
961 /* Using AppName */
962 get_file_name(resfile);
963 GetFullPathNameA(selfname, MAX_PATH, fullpath, &lpFilePart);
964 assert ( lpFilePart != 0);
965 *(lpFilePart -1 ) = 0;
966 p = strrchr(fullpath, '\\');
967 /* Use exename to avoid buffer containing things like 'C:' */
968 if (p) sprintf(buffer, "..%s/%s", p, exename);
969 else sprintf(buffer, "./%s", exename);
970 sprintf(buffer2, "dummy tests/process.c dump \"%s\" \"a\\\"b\\\\\" c\\\" d", resfile);
971 SetLastError(0xdeadbeef);
972 ret = CreateProcessA(buffer, buffer2, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
973 ok(ret, "CreateProcess (%s) failed : %d\n", buffer, GetLastError());
974 /* wait for child to terminate */
975 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
976 /* child process has changed result file, so let profile functions know about it */
977 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
978 sprintf(buffer, "tests/process.c dump %s", resfile);
979 okChildString("Arguments", "argvA0", "dummy");
980 okChildString("Arguments", "CommandLineA", buffer2);
981 okChildStringWA("Arguments", "CommandLineW", buffer2);
982 release_memory();
983 DeleteFileA(resfile);
984
985 if (0) /* Test crashes on NT-based Windows. */
986 {
987 /* Test NULL application name and command line parameters. */
988 SetLastError(0xdeadbeef);
989 ret = CreateProcessA(NULL, NULL, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
990 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
991 ok(GetLastError() == ERROR_INVALID_PARAMETER,
992 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
993 }
994
995 buffer[0] = '\0';
996
997 /* Test empty application name parameter. */
998 SetLastError(0xdeadbeef);
999 ret = CreateProcessA(buffer, NULL, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
1000 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
1001 ok(GetLastError() == ERROR_PATH_NOT_FOUND ||
1002 broken(GetLastError() == ERROR_FILE_NOT_FOUND) /* Win9x/WinME */ ||
1003 broken(GetLastError() == ERROR_ACCESS_DENIED) /* Win98 */,
1004 "Expected ERROR_PATH_NOT_FOUND, got %d\n", GetLastError());
1005
1006 buffer2[0] = '\0';
1007
1008 /* Test empty application name and command line parameters. */
1009 SetLastError(0xdeadbeef);
1010 ret = CreateProcessA(buffer, buffer2, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
1011 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
1012 ok(GetLastError() == ERROR_PATH_NOT_FOUND ||
1013 broken(GetLastError() == ERROR_FILE_NOT_FOUND) /* Win9x/WinME */ ||
1014 broken(GetLastError() == ERROR_ACCESS_DENIED) /* Win98 */,
1015 "Expected ERROR_PATH_NOT_FOUND, got %d\n", GetLastError());
1016
1017 /* Test empty command line parameter. */
1018 SetLastError(0xdeadbeef);
1019 ret = CreateProcessA(NULL, buffer2, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
1020 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
1021 ok(GetLastError() == ERROR_FILE_NOT_FOUND ||
1022 GetLastError() == ERROR_PATH_NOT_FOUND /* NT4 */ ||
1023 GetLastError() == ERROR_BAD_PATHNAME /* Win98 */ ||
1024 GetLastError() == ERROR_INVALID_PARAMETER /* Win7 */,
1025 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
1026
1027 strcpy(buffer, "doesnotexist.exe");
1028 strcpy(buffer2, "does not exist.exe");
1029
1030 /* Test nonexistent application name. */
1031 SetLastError(0xdeadbeef);
1032 ret = CreateProcessA(buffer, NULL, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
1033 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
1034 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
1035
1036 SetLastError(0xdeadbeef);
1037 ret = CreateProcessA(buffer2, NULL, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
1038 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
1039 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
1040
1041 /* Test nonexistent command line parameter. */
1042 SetLastError(0xdeadbeef);
1043 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
1044 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
1045 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
1046
1047 SetLastError(0xdeadbeef);
1048 ret = CreateProcessA(NULL, buffer2, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
1049 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
1050 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
1051 }
1052
1053 static void test_Directory(void)
1054 {
1055 char buffer[MAX_PATH];
1056 PROCESS_INFORMATION info;
1057 STARTUPINFOA startup;
1058 char windir[MAX_PATH];
1059 static CHAR cmdline[] = "winver.exe";
1060
1061 memset(&startup, 0, sizeof(startup));
1062 startup.cb = sizeof(startup);
1063 startup.dwFlags = STARTF_USESHOWWINDOW;
1064 startup.wShowWindow = SW_SHOWNORMAL;
1065
1066 /* the basics */
1067 get_file_name(resfile);
1068 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\"", selfname, resfile);
1069 GetWindowsDirectoryA( windir, sizeof(windir) );
1070 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, windir, &startup, &info), "CreateProcess\n");
1071 /* wait for child to terminate */
1072 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1073 /* child process has changed result file, so let profile functions know about it */
1074 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1075
1076 okChildIString("Misc", "CurrDirA", windir);
1077 release_memory();
1078 DeleteFileA(resfile);
1079
1080 /* search PATH for the exe if directory is NULL */
1081 ok(CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
1082 ok(TerminateProcess(info.hProcess, 0), "Child process termination\n");
1083
1084 /* if any directory is provided, don't search PATH, error on bad directory */
1085 SetLastError(0xdeadbeef);
1086 memset(&info, 0, sizeof(info));
1087 ok(!CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0L,
1088 NULL, "non\\existent\\directory", &startup, &info), "CreateProcess\n");
1089 ok(GetLastError() == ERROR_DIRECTORY, "Expected ERROR_DIRECTORY, got %d\n", GetLastError());
1090 ok(!TerminateProcess(info.hProcess, 0), "Child process should not exist\n");
1091 }
1092
1093 static void test_Toolhelp(void)
1094 {
1095 char buffer[MAX_PATH];
1096 STARTUPINFOA startup;
1097 PROCESS_INFORMATION info;
1098 HANDLE process, thread, snapshot;
1099 PROCESSENTRY32 pe;
1100 THREADENTRY32 te;
1101 DWORD ret;
1102 int i;
1103
1104 memset(&startup, 0, sizeof(startup));
1105 startup.cb = sizeof(startup);
1106 startup.dwFlags = STARTF_USESHOWWINDOW;
1107 startup.wShowWindow = SW_SHOWNORMAL;
1108
1109 get_file_name(resfile);
1110 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\"", selfname, resfile);
1111 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess failed\n");
1112 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1113 CloseHandle(info.hProcess);
1114 CloseHandle(info.hThread);
1115
1116 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1117 okChildInt("Toolhelp", "cntUsage", 0);
1118 okChildInt("Toolhelp", "th32DefaultHeapID", 0);
1119 okChildInt("Toolhelp", "th32ModuleID", 0);
1120 okChildInt("Toolhelp", "th32ParentProcessID", GetCurrentProcessId());
1121 /* pcPriClassBase differs between Windows versions (either 6 or 8) */
1122 okChildInt("Toolhelp", "dwFlags", 0);
1123
1124 release_memory();
1125 DeleteFileA(resfile);
1126
1127 get_file_name(resfile);
1128 sprintf(buffer, "\"%s\" tests/process.c nested \"%s\"", selfname, resfile);
1129 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess failed\n");
1130 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1131
1132 process = OpenProcess(PROCESS_ALL_ACCESS_NT4, FALSE, info.dwProcessId);
1133 ok(process != NULL, "OpenProcess failed %u\n", GetLastError());
1134 CloseHandle(process);
1135
1136 CloseHandle(info.hProcess);
1137 CloseHandle(info.hThread);
1138
1139 for (i = 0; i < 20; i++)
1140 {
1141 SetLastError(0xdeadbeef);
1142 process = OpenProcess(PROCESS_ALL_ACCESS_NT4, FALSE, info.dwProcessId);
1143 ok(process || GetLastError() == ERROR_INVALID_PARAMETER, "OpenProcess failed %u\n", GetLastError());
1144 if (!process) break;
1145 CloseHandle(process);
1146 Sleep(100);
1147 }
1148 /* The following test fails randomly on some Windows versions, but Gothic 2 depends on it */
1149 ok(i < 20 || broken(i == 20), "process object not released\n");
1150
1151 snapshot = pCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
1152 ok(snapshot != INVALID_HANDLE_VALUE, "CreateToolhelp32Snapshot failed %u\n", GetLastError());
1153 memset(&pe, 0, sizeof(pe));
1154 pe.dwSize = sizeof(pe);
1155 if (pProcess32First(snapshot, &pe))
1156 {
1157 while (pe.th32ParentProcessID != info.dwProcessId)
1158 if (!pProcess32Next(snapshot, &pe)) break;
1159 }
1160 CloseHandle(snapshot);
1161 ok(pe.th32ParentProcessID == info.dwProcessId, "failed to find nested child process\n");
1162
1163 process = OpenProcess(PROCESS_ALL_ACCESS_NT4, FALSE, pe.th32ProcessID);
1164 ok(process != NULL, "OpenProcess failed %u\n", GetLastError());
1165
1166 snapshot = pCreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
1167 ok(snapshot != INVALID_HANDLE_VALUE, "CreateToolhelp32Snapshot failed %u\n", GetLastError());
1168 memset(&te, 0, sizeof(te));
1169 te.dwSize = sizeof(te);
1170 if (pThread32First(snapshot, &te))
1171 {
1172 while (te.th32OwnerProcessID != pe.th32ProcessID)
1173 if (!pThread32Next(snapshot, &te)) break;
1174 }
1175 CloseHandle(snapshot);
1176 ok(te.th32OwnerProcessID == pe.th32ProcessID, "failed to find suspended thread\n");
1177
1178 thread = OpenThread(THREAD_ALL_ACCESS_NT4, FALSE, te.th32ThreadID);
1179 ok(thread != NULL, "OpenThread failed %u\n", GetLastError());
1180 ret = ResumeThread(thread);
1181 ok(ret == 1, "expected 1, got %u\n", ret);
1182 CloseHandle(thread);
1183
1184 ok(WaitForSingleObject(process, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1185 CloseHandle(process);
1186
1187 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1188 okChildInt("Toolhelp", "cntUsage", 0);
1189 okChildInt("Toolhelp", "th32DefaultHeapID", 0);
1190 okChildInt("Toolhelp", "th32ModuleID", 0);
1191 okChildInt("Toolhelp", "th32ParentProcessID", info.dwProcessId);
1192 /* pcPriClassBase differs between Windows versions (either 6 or 8) */
1193 okChildInt("Toolhelp", "dwFlags", 0);
1194
1195 release_memory();
1196 DeleteFileA(resfile);
1197 }
1198
1199 static BOOL is_str_env_drive_dir(const char* str)
1200 {
1201 return str[0] == '=' && str[1] >= 'A' && str[1] <= 'Z' && str[2] == ':' &&
1202 str[3] == '=' && str[4] == str[1];
1203 }
1204
1205 /* compared expected child's environment (in gesA) from actual
1206 * environment our child got
1207 */
1208 static void cmpEnvironment(const char* gesA)
1209 {
1210 int i, clen;
1211 const char* ptrA;
1212 char* res;
1213 char key[32];
1214 BOOL found;
1215
1216 clen = GetPrivateProfileIntA("EnvironmentA", "len", 0, resfile);
1217
1218 /* now look each parent env in child */
1219 if ((ptrA = gesA) != NULL)
1220 {
1221 while (*ptrA)
1222 {
1223 for (i = 0; i < clen; i++)
1224 {
1225 sprintf(key, "env%d", i);
1226 res = getChildString("EnvironmentA", key);
1227 if (strncmp(ptrA, res, MAX_LISTED_ENV_VAR - 1) == 0)
1228 break;
1229 }
1230 found = i < clen;
1231 ok(found, "Parent-env string %s isn't in child process\n", ptrA);
1232
1233 ptrA += strlen(ptrA) + 1;
1234 release_memory();
1235 }
1236 }
1237 /* and each child env in parent */
1238 for (i = 0; i < clen; i++)
1239 {
1240 sprintf(key, "env%d", i);
1241 res = getChildString("EnvironmentA", key);
1242 if ((ptrA = gesA) != NULL)
1243 {
1244 while (*ptrA)
1245 {
1246 if (strncmp(res, ptrA, MAX_LISTED_ENV_VAR - 1) == 0)
1247 break;
1248 ptrA += strlen(ptrA) + 1;
1249 }
1250 if (!*ptrA) ptrA = NULL;
1251 }
1252
1253 if (!is_str_env_drive_dir(res))
1254 {
1255 found = ptrA != NULL;
1256 ok(found, "Child-env string %s isn't in parent process\n", res);
1257 }
1258 /* else => should also test we get the right per drive default directory here... */
1259 }
1260 }
1261
1262 static void test_Environment(void)
1263 {
1264 char buffer[MAX_PATH];
1265 PROCESS_INFORMATION info;
1266 STARTUPINFOA startup;
1267 char *child_env;
1268 int child_env_len;
1269 char *ptr;
1270 char *ptr2;
1271 char *env;
1272 int slen;
1273
1274 memset(&startup, 0, sizeof(startup));
1275 startup.cb = sizeof(startup);
1276 startup.dwFlags = STARTF_USESHOWWINDOW;
1277 startup.wShowWindow = SW_SHOWNORMAL;
1278
1279 /* the basics */
1280 get_file_name(resfile);
1281 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\"", selfname, resfile);
1282 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
1283 /* wait for child to terminate */
1284 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1285 /* child process has changed result file, so let profile functions know about it */
1286 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1287
1288 env = GetEnvironmentStringsA();
1289 cmpEnvironment(env);
1290 release_memory();
1291 DeleteFileA(resfile);
1292
1293 memset(&startup, 0, sizeof(startup));
1294 startup.cb = sizeof(startup);
1295 startup.dwFlags = STARTF_USESHOWWINDOW;
1296 startup.wShowWindow = SW_SHOWNORMAL;
1297
1298 /* the basics */
1299 get_file_name(resfile);
1300 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\"", selfname, resfile);
1301
1302 child_env_len = 0;
1303 ptr = env;
1304 while(*ptr)
1305 {
1306 slen = strlen(ptr)+1;
1307 child_env_len += slen;
1308 ptr += slen;
1309 }
1310 /* Add space for additional environment variables */
1311 child_env_len += 256;
1312 child_env = HeapAlloc(GetProcessHeap(), 0, child_env_len);
1313
1314 ptr = child_env;
1315 sprintf(ptr, "=%c:=%s", 'C', "C:\\FOO\\BAR");
1316 ptr += strlen(ptr) + 1;
1317 strcpy(ptr, "PATH=C:\\WINDOWS;C:\\WINDOWS\\SYSTEM;C:\\MY\\OWN\\DIR");
1318 ptr += strlen(ptr) + 1;
1319 strcpy(ptr, "FOO=BAR");
1320 ptr += strlen(ptr) + 1;
1321 strcpy(ptr, "BAR=FOOBAR");
1322 ptr += strlen(ptr) + 1;
1323 /* copy all existing variables except:
1324 * - WINELOADER
1325 * - PATH (already set above)
1326 * - the directory definitions (=[A-Z]:=)
1327 */
1328 for (ptr2 = env; *ptr2; ptr2 += strlen(ptr2) + 1)
1329 {
1330 if (strncmp(ptr2, "PATH=", 5) != 0 &&
1331 strncmp(ptr2, "WINELOADER=", 11) != 0 &&
1332 !is_str_env_drive_dir(ptr2))
1333 {
1334 strcpy(ptr, ptr2);
1335 ptr += strlen(ptr) + 1;
1336 }
1337 }
1338 *ptr = '\0';
1339 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, child_env, NULL, &startup, &info), "CreateProcess\n");
1340 /* wait for child to terminate */
1341 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1342 /* child process has changed result file, so let profile functions know about it */
1343 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1344
1345 cmpEnvironment(child_env);
1346
1347 HeapFree(GetProcessHeap(), 0, child_env);
1348 FreeEnvironmentStringsA(env);
1349 release_memory();
1350 DeleteFileA(resfile);
1351 }
1352
1353 static void test_SuspendFlag(void)
1354 {
1355 char buffer[MAX_PATH];
1356 PROCESS_INFORMATION info;
1357 STARTUPINFOA startup, us;
1358 DWORD exit_status;
1359 char *result;
1360
1361 /* let's start simplistic */
1362 memset(&startup, 0, sizeof(startup));
1363 startup.cb = sizeof(startup);
1364 startup.dwFlags = STARTF_USESHOWWINDOW;
1365 startup.wShowWindow = SW_SHOWNORMAL;
1366
1367 get_file_name(resfile);
1368 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\"", selfname, resfile);
1369 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &startup, &info), "CreateProcess\n");
1370
1371 ok(GetExitCodeThread(info.hThread, &exit_status) && exit_status == STILL_ACTIVE, "thread still running\n");
1372 Sleep(8000);
1373 ok(GetExitCodeThread(info.hThread, &exit_status) && exit_status == STILL_ACTIVE, "thread still running\n");
1374 ok(ResumeThread(info.hThread) == 1, "Resuming thread\n");
1375
1376 /* wait for child to terminate */
1377 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1378 /* child process has changed result file, so let profile functions know about it */
1379 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1380
1381 GetStartupInfoA(&us);
1382
1383 okChildInt("StartupInfoA", "cb", startup.cb);
1384 okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1385 result = getChildString( "StartupInfoA", "lpTitle" );
1386 ok( broken(!result) || (result && !strCmp( result, selfname, 0 )),
1387 "expected '%s' or null, got '%s'\n", selfname, result );
1388 okChildInt("StartupInfoA", "dwX", startup.dwX);
1389 okChildInt("StartupInfoA", "dwY", startup.dwY);
1390 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1391 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1392 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1393 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1394 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1395 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1396 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1397 release_memory();
1398 DeleteFileA(resfile);
1399 }
1400
1401 static void test_DebuggingFlag(void)
1402 {
1403 char buffer[MAX_PATH];
1404 void *processbase = NULL;
1405 PROCESS_INFORMATION info;
1406 STARTUPINFOA startup, us;
1407 DEBUG_EVENT de;
1408 unsigned dbg = 0;
1409 char *result;
1410
1411 /* let's start simplistic */
1412 memset(&startup, 0, sizeof(startup));
1413 startup.cb = sizeof(startup);
1414 startup.dwFlags = STARTF_USESHOWWINDOW;
1415 startup.wShowWindow = SW_SHOWNORMAL;
1416
1417 get_file_name(resfile);
1418 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\"", selfname, resfile);
1419 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &startup, &info), "CreateProcess\n");
1420
1421 /* get all startup events up to the entry point break exception */
1422 do
1423 {
1424 ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
1425 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
1426 if (!dbg)
1427 {
1428 ok(de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT,
1429 "first event: %d\n", de.dwDebugEventCode);
1430 processbase = de.u.CreateProcessInfo.lpBaseOfImage;
1431 }
1432 if (de.dwDebugEventCode != EXCEPTION_DEBUG_EVENT) dbg++;
1433 ok(de.dwDebugEventCode != LOAD_DLL_DEBUG_EVENT ||
1434 de.u.LoadDll.lpBaseOfDll != processbase, "got LOAD_DLL for main module\n");
1435 } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
1436
1437 ok(dbg, "I have seen a debug event\n");
1438 /* wait for child to terminate */
1439 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1440 /* child process has changed result file, so let profile functions know about it */
1441 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1442
1443 GetStartupInfoA(&us);
1444
1445 okChildInt("StartupInfoA", "cb", startup.cb);
1446 okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1447 result = getChildString( "StartupInfoA", "lpTitle" );
1448 ok( broken(!result) || (result && !strCmp( result, selfname, 0 )),
1449 "expected '%s' or null, got '%s'\n", selfname, result );
1450 okChildInt("StartupInfoA", "dwX", startup.dwX);
1451 okChildInt("StartupInfoA", "dwY", startup.dwY);
1452 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1453 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1454 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1455 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1456 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1457 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1458 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1459 release_memory();
1460 DeleteFileA(resfile);
1461 }
1462
1463 static BOOL is_console(HANDLE h)
1464 {
1465 return h != INVALID_HANDLE_VALUE && ((ULONG_PTR)h & 3) == 3;
1466 }
1467
1468 static void test_Console(void)
1469 {
1470 char buffer[MAX_PATH];
1471 PROCESS_INFORMATION info;
1472 STARTUPINFOA startup, us;
1473 SECURITY_ATTRIBUTES sa;
1474 CONSOLE_SCREEN_BUFFER_INFO sbi, sbiC;
1475 DWORD modeIn, modeOut, modeInC, modeOutC;
1476 DWORD cpIn, cpOut, cpInC, cpOutC;
1477 DWORD w;
1478 HANDLE hChildIn, hChildInInh, hChildOut, hChildOutInh, hParentIn, hParentOut;
1479 const char* msg = "This is a std-handle inheritance test.";
1480 unsigned msg_len;
1481 BOOL run_tests = TRUE;
1482 char *result;
1483
1484 memset(&startup, 0, sizeof(startup));
1485 startup.cb = sizeof(startup);
1486 startup.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
1487 startup.wShowWindow = SW_SHOWNORMAL;
1488
1489 sa.nLength = sizeof(sa);
1490 sa.lpSecurityDescriptor = NULL;
1491 sa.bInheritHandle = TRUE;
1492
1493 startup.hStdInput = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1494 startup.hStdOutput = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1495
1496 /* first, we need to be sure we're attached to a console */
1497 if (!is_console(startup.hStdInput) || !is_console(startup.hStdOutput))
1498 {
1499 /* we're not attached to a console, let's do it */
1500 AllocConsole();
1501 startup.hStdInput = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1502 startup.hStdOutput = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1503 }
1504 /* now verify everything's ok */
1505 ok(startup.hStdInput != INVALID_HANDLE_VALUE, "Opening ConIn\n");
1506 ok(startup.hStdOutput != INVALID_HANDLE_VALUE, "Opening ConOut\n");
1507 startup.hStdError = startup.hStdOutput;
1508
1509 ok(GetConsoleScreenBufferInfo(startup.hStdOutput, &sbi), "Getting sb info\n");
1510 ok(GetConsoleMode(startup.hStdInput, &modeIn), "Getting console in mode\n");
1511 ok(GetConsoleMode(startup.hStdOutput, &modeOut), "Getting console out mode\n");
1512 cpIn = GetConsoleCP();
1513 cpOut = GetConsoleOutputCP();
1514
1515 get_file_name(resfile);
1516 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\" console", selfname, resfile);
1517 ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
1518
1519 /* wait for child to terminate */
1520 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1521 /* child process has changed result file, so let profile functions know about it */
1522 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1523
1524 /* now get the modification the child has made, and resets parents expected values */
1525 ok(GetConsoleScreenBufferInfo(startup.hStdOutput, &sbiC), "Getting sb info\n");
1526 ok(GetConsoleMode(startup.hStdInput, &modeInC), "Getting console in mode\n");
1527 ok(GetConsoleMode(startup.hStdOutput, &modeOutC), "Getting console out mode\n");
1528
1529 SetConsoleMode(startup.hStdInput, modeIn);
1530 SetConsoleMode(startup.hStdOutput, modeOut);
1531
1532 cpInC = GetConsoleCP();
1533 cpOutC = GetConsoleOutputCP();
1534
1535 /* Try to set invalid CP */
1536 SetLastError(0xdeadbeef);
1537 ok(!SetConsoleCP(0), "Shouldn't succeed\n");
1538 ok(GetLastError()==ERROR_INVALID_PARAMETER ||
1539 broken(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), /* win9x */
1540 "GetLastError: expecting %u got %u\n",
1541 ERROR_INVALID_PARAMETER, GetLastError());
1542 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1543 run_tests = FALSE;
1544
1545
1546 SetLastError(0xdeadbeef);
1547 ok(!SetConsoleOutputCP(0), "Shouldn't succeed\n");
1548 ok(GetLastError()==ERROR_INVALID_PARAMETER ||
1549 broken(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), /* win9x */
1550 "GetLastError: expecting %u got %u\n",
1551 ERROR_INVALID_PARAMETER, GetLastError());
1552
1553 SetConsoleCP(cpIn);
1554 SetConsoleOutputCP(cpOut);
1555
1556 GetStartupInfoA(&us);
1557
1558 okChildInt("StartupInfoA", "cb", startup.cb);
1559 okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1560 result = getChildString( "StartupInfoA", "lpTitle" );
1561 ok( broken(!result) || (result && !strCmp( result, selfname, 0 )),
1562 "expected '%s' or null, got '%s'\n", selfname, result );
1563 okChildInt("StartupInfoA", "dwX", startup.dwX);
1564 okChildInt("StartupInfoA", "dwY", startup.dwY);
1565 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1566 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1567 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1568 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1569 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1570 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1571 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1572
1573 /* check child correctly inherited the console */
1574 okChildInt("StartupInfoA", "hStdInput", (DWORD_PTR)startup.hStdInput);
1575 okChildInt("StartupInfoA", "hStdOutput", (DWORD_PTR)startup.hStdOutput);
1576 okChildInt("StartupInfoA", "hStdError", (DWORD_PTR)startup.hStdError);
1577 okChildInt("Console", "SizeX", (DWORD)sbi.dwSize.X);
1578 okChildInt("Console", "SizeY", (DWORD)sbi.dwSize.Y);
1579 okChildInt("Console", "CursorX", (DWORD)sbi.dwCursorPosition.X);
1580 okChildInt("Console", "CursorY", (DWORD)sbi.dwCursorPosition.Y);
1581 okChildInt("Console", "Attributes", sbi.wAttributes);
1582 okChildInt("Console", "winLeft", (DWORD)sbi.srWindow.Left);
1583 okChildInt("Console", "winTop", (DWORD)sbi.srWindow.Top);
1584 okChildInt("Console", "winRight", (DWORD)sbi.srWindow.Right);
1585 okChildInt("Console", "winBottom", (DWORD)sbi.srWindow.Bottom);
1586 okChildInt("Console", "maxWinWidth", (DWORD)sbi.dwMaximumWindowSize.X);
1587 okChildInt("Console", "maxWinHeight", (DWORD)sbi.dwMaximumWindowSize.Y);
1588 okChildInt("Console", "InputCP", cpIn);
1589 okChildInt("Console", "OutputCP", cpOut);
1590 okChildInt("Console", "InputMode", modeIn);
1591 okChildInt("Console", "OutputMode", modeOut);
1592
1593 if (run_tests)
1594 {
1595 ok(cpInC == 1252, "Wrong console CP (expected 1252 got %d/%d)\n", cpInC, cpIn);
1596 ok(cpOutC == 1252, "Wrong console-SB CP (expected 1252 got %d/%d)\n", cpOutC, cpOut);
1597 }
1598 else
1599 win_skip("Setting the codepage is not implemented\n");
1600
1601 ok(modeInC == (modeIn ^ 1), "Wrong console mode\n");
1602 ok(modeOutC == (modeOut ^ 1), "Wrong console-SB mode\n");
1603 trace("cursor position(X): %d/%d\n",sbi.dwCursorPosition.X, sbiC.dwCursorPosition.X);
1604 ok(sbiC.dwCursorPosition.Y == (sbi.dwCursorPosition.Y ^ 1), "Wrong cursor position\n");
1605
1606 release_memory();
1607 DeleteFileA(resfile);
1608
1609 ok(CreatePipe(&hParentIn, &hChildOut, NULL, 0), "Creating parent-input pipe\n");
1610 ok(DuplicateHandle(GetCurrentProcess(), hChildOut, GetCurrentProcess(),
1611 &hChildOutInh, 0, TRUE, DUPLICATE_SAME_ACCESS),
1612 "Duplicating as inheritable child-output pipe\n");
1613 CloseHandle(hChildOut);
1614
1615 ok(CreatePipe(&hChildIn, &hParentOut, NULL, 0), "Creating parent-output pipe\n");
1616 ok(DuplicateHandle(GetCurrentProcess(), hChildIn, GetCurrentProcess(),
1617 &hChildInInh, 0, TRUE, DUPLICATE_SAME_ACCESS),
1618 "Duplicating as inheritable child-input pipe\n");
1619 CloseHandle(hChildIn);
1620
1621 memset(&startup, 0, sizeof(startup));
1622 startup.cb = sizeof(startup);
1623 startup.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
1624 startup.wShowWindow = SW_SHOWNORMAL;
1625 startup.hStdInput = hChildInInh;
1626 startup.hStdOutput = hChildOutInh;
1627 startup.hStdError = hChildOutInh;
1628
1629 get_file_name(resfile);
1630 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\" stdhandle", selfname, resfile);
1631 ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &startup, &info), "CreateProcess\n");
1632 ok(CloseHandle(hChildInInh), "Closing handle\n");
1633 ok(CloseHandle(hChildOutInh), "Closing handle\n");
1634
1635 msg_len = strlen(msg) + 1;
1636 ok(WriteFile(hParentOut, msg, msg_len, &w, NULL), "Writing to child\n");
1637 ok(w == msg_len, "Should have written %u bytes, actually wrote %u\n", msg_len, w);
1638 memset(buffer, 0, sizeof(buffer));
1639 ok(ReadFile(hParentIn, buffer, sizeof(buffer), &w, NULL), "Reading from child\n");
1640 ok(strcmp(buffer, msg) == 0, "Should have received '%s'\n", msg);
1641
1642 /* wait for child to terminate */
1643 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1644 /* child process has changed result file, so let profile functions know about it */
1645 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1646
1647 okChildString("StdHandle", "msg", msg);
1648
1649 release_memory();
1650 DeleteFileA(resfile);
1651 }
1652
1653 static void test_ExitCode(void)
1654 {
1655 char buffer[MAX_PATH];
1656 PROCESS_INFORMATION info;
1657 STARTUPINFOA startup;
1658 DWORD code;
1659
1660 /* let's start simplistic */
1661 memset(&startup, 0, sizeof(startup));
1662 startup.cb = sizeof(startup);
1663 startup.dwFlags = STARTF_USESHOWWINDOW;
1664 startup.wShowWindow = SW_SHOWNORMAL;
1665
1666 get_file_name(resfile);
1667 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\" exit_code", selfname, resfile);
1668 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
1669
1670 /* wait for child to terminate */
1671 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1672 /* child process has changed result file, so let profile functions know about it */
1673 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1674
1675 ok(GetExitCodeProcess(info.hProcess, &code), "Getting exit code\n");
1676 okChildInt("ExitCode", "value", code);
1677
1678 release_memory();
1679 DeleteFileA(resfile);
1680 }
1681
1682 static void test_OpenProcess(void)
1683 {
1684 HANDLE hproc;
1685 void *addr1;
1686 MEMORY_BASIC_INFORMATION info;
1687 SIZE_T dummy, read_bytes;
1688 BOOL ret;
1689
1690 /* not exported in all windows versions */
1691 if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
1692 win_skip("VirtualAllocEx not found\n");
1693 return;
1694 }
1695
1696 /* without PROCESS_VM_OPERATION */
1697 hproc = OpenProcess(PROCESS_ALL_ACCESS_NT4 & ~PROCESS_VM_OPERATION, FALSE, GetCurrentProcessId());
1698 ok(hproc != NULL, "OpenProcess error %d\n", GetLastError());
1699
1700 SetLastError(0xdeadbeef);
1701 addr1 = pVirtualAllocEx(hproc, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
1702 ok(!addr1, "VirtualAllocEx should fail\n");
1703 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1704 { /* Win9x */
1705 CloseHandle(hproc);
1706 win_skip("VirtualAllocEx not implemented\n");
1707 return;
1708 }
1709 ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1710
1711 read_bytes = 0xdeadbeef;
1712 SetLastError(0xdeadbeef);
1713 ret = ReadProcessMemory(hproc, test_OpenProcess, &dummy, sizeof(dummy), &read_bytes);
1714 ok(ret, "ReadProcessMemory error %d\n", GetLastError());
1715 ok(read_bytes == sizeof(dummy), "wrong read bytes %ld\n", read_bytes);
1716
1717 CloseHandle(hproc);
1718
1719 hproc = OpenProcess(PROCESS_VM_OPERATION, FALSE, GetCurrentProcessId());
1720 ok(hproc != NULL, "OpenProcess error %d\n", GetLastError());
1721
1722 addr1 = pVirtualAllocEx(hproc, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
1723 ok(addr1 != NULL, "VirtualAllocEx error %d\n", GetLastError());
1724
1725 /* without PROCESS_QUERY_INFORMATION */
1726 SetLastError(0xdeadbeef);
1727 ok(!VirtualQueryEx(hproc, addr1, &info, sizeof(info)),
1728 "VirtualQueryEx without PROCESS_QUERY_INFORMATION rights should fail\n");
1729 ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1730
1731 /* without PROCESS_VM_READ */
1732 read_bytes = 0xdeadbeef;
1733 SetLastError(0xdeadbeef);
1734 ok(!ReadProcessMemory(hproc, addr1, &dummy, sizeof(dummy), &read_bytes),
1735 "ReadProcessMemory without PROCESS_VM_READ rights should fail\n");
1736 ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1737 ok(read_bytes == 0, "wrong read bytes %ld\n", read_bytes);
1738
1739 CloseHandle(hproc);
1740
1741 hproc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
1742
1743 memset(&info, 0xcc, sizeof(info));
1744 read_bytes = VirtualQueryEx(hproc, addr1, &info, sizeof(info));
1745 ok(read_bytes == sizeof(info), "VirtualQueryEx error %d\n", GetLastError());
1746
1747 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
1748 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
1749 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
1750 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
1751 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
1752 /* NT reports Protect == 0 for a not committed memory block */
1753 ok(info.Protect == 0 /* NT */ ||
1754 info.Protect == PAGE_NOACCESS, /* Win9x */
1755 "%x != PAGE_NOACCESS\n", info.Protect);
1756 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
1757
1758 SetLastError(0xdeadbeef);
1759 ok(!pVirtualFreeEx(hproc, addr1, 0, MEM_RELEASE),
1760 "VirtualFreeEx without PROCESS_VM_OPERATION rights should fail\n");
1761 ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1762
1763 CloseHandle(hproc);
1764
1765 hproc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, GetCurrentProcessId());
1766 if (hproc)
1767 {
1768 SetLastError(0xdeadbeef);
1769 memset(&info, 0xcc, sizeof(info));
1770 read_bytes = VirtualQueryEx(hproc, addr1, &info, sizeof(info));
1771 if (read_bytes) /* win8 */
1772 {
1773 ok(read_bytes == sizeof(info), "VirtualQueryEx error %d\n", GetLastError());
1774 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
1775 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
1776 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
1777 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
1778 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
1779 ok(info.Protect == 0, "%x != PAGE_NOACCESS\n", info.Protect);
1780 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
1781 }
1782 else /* before win8 */
1783 ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1784
1785 SetLastError(0xdeadbeef);
1786 ok(!pVirtualFreeEx(hproc, addr1, 0, MEM_RELEASE),
1787 "VirtualFreeEx without PROCESS_VM_OPERATION rights should fail\n");
1788 ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1789
1790 CloseHandle(hproc);
1791 }
1792
1793 ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
1794 }
1795
1796 static void test_GetProcessVersion(void)
1797 {
1798 static char cmdline[] = "winver.exe";
1799 PROCESS_INFORMATION pi;
1800 STARTUPINFOA si;
1801 DWORD ret;
1802
1803 SetLastError(0xdeadbeef);
1804 ret = GetProcessVersion(0);
1805 ok(ret, "GetProcessVersion error %u\n", GetLastError());
1806
1807 SetLastError(0xdeadbeef);
1808 ret = GetProcessVersion(GetCurrentProcessId());
1809 ok(ret, "GetProcessVersion error %u\n", GetLastError());
1810
1811 memset(&si, 0, sizeof(si));
1812 si.cb = sizeof(si);
1813 si.dwFlags = STARTF_USESHOWWINDOW;
1814 si.wShowWindow = SW_HIDE;
1815 SetLastError(0xdeadbeef);
1816 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1817 ok(ret, "CreateProcess error %u\n", GetLastError());
1818
1819 SetLastError(0xdeadbeef);
1820 ret = GetProcessVersion(pi.dwProcessId);
1821 ok(ret, "GetProcessVersion error %u\n", GetLastError());
1822
1823 SetLastError(0xdeadbeef);
1824 ret = TerminateProcess(pi.hProcess, 0);
1825 ok(ret, "TerminateProcess error %u\n", GetLastError());
1826
1827 CloseHandle(pi.hProcess);
1828 CloseHandle(pi.hThread);
1829 }
1830
1831 static void test_GetProcessImageFileNameA(void)
1832 {
1833 DWORD rc;
1834 CHAR process[MAX_PATH];
1835 static const char harddisk[] = "\\Device\\HarddiskVolume";
1836
1837 if (!pK32GetProcessImageFileNameA)
1838 {
1839 win_skip("K32GetProcessImageFileNameA is unavailable\n");
1840 return;
1841 }
1842
1843 /* callers must guess the buffer size */
1844 SetLastError(0xdeadbeef);
1845 rc = pK32GetProcessImageFileNameA(GetCurrentProcess(), NULL, 0);
1846 ok(!rc && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1847 "K32GetProcessImageFileNameA(no buffer): returned %u, le=%u\n", rc, GetLastError());
1848
1849 *process = '\0';
1850 rc = pK32GetProcessImageFileNameA(GetCurrentProcess(), process, sizeof(process));
1851 expect_eq_d(rc, lstrlenA(process));
1852 if (strncmp(process, harddisk, lstrlenA(harddisk)))
1853 {
1854 todo_wine win_skip("%s is probably on a network share, skipping tests\n", process);
1855 return;
1856 }
1857
1858 if (!pQueryFullProcessImageNameA)
1859 win_skip("QueryFullProcessImageNameA unavailable (added in Windows Vista)\n");
1860 else
1861 {
1862 CHAR image[MAX_PATH];
1863 DWORD length;
1864
1865 length = sizeof(image);
1866 expect_eq_d(TRUE, pQueryFullProcessImageNameA(GetCurrentProcess(), PROCESS_NAME_NATIVE, image, &length));
1867 expect_eq_d(length, lstrlenA(image));
1868 ok(lstrcmpiA(process, image) == 0, "expected '%s' to be equal to '%s'\n", process, image);
1869 }
1870 }
1871
1872 static void test_QueryFullProcessImageNameA(void)
1873 {
1874 #define INIT_STR "Just some words"
1875 DWORD length, size;
1876 CHAR buf[MAX_PATH], module[MAX_PATH];
1877
1878 if (!pQueryFullProcessImageNameA)
1879 {
1880 win_skip("QueryFullProcessImageNameA unavailable (added in Windows Vista)\n");
1881 return;
1882 }
1883
1884 *module = '\0';
1885 SetLastError(0); /* old Windows don't reset it on success */
1886 size = GetModuleFileNameA(NULL, module, sizeof(module));
1887 ok(size && GetLastError() != ERROR_INSUFFICIENT_BUFFER, "GetModuleFileName failed: %u le=%u\n", size, GetLastError());
1888
1889 /* get the buffer length without \0 terminator */
1890 length = sizeof(buf);
1891 expect_eq_d(TRUE, pQueryFullProcessImageNameA(GetCurrentProcess(), 0, buf, &length));
1892 expect_eq_d(length, lstrlenA(buf));
1893 ok((buf[0] == '\\' && buf[1] == '\\') ||
1894 lstrcmpiA(buf, module) == 0, "expected %s to match %s\n", buf, module);
1895
1896 /* when the buffer is too small
1897 * - function fail with error ERROR_INSUFFICIENT_BUFFER
1898 * - the size variable is not modified
1899 * tested with the biggest too small size
1900 */
1901 size = length;
1902 sprintf(buf,INIT_STR);
1903 expect_eq_d(FALSE, pQueryFullProcessImageNameA(GetCurrentProcess(), 0, buf, &size));
1904 expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1905 expect_eq_d(length, size);
1906 expect_eq_s(INIT_STR, buf);
1907
1908 /* retest with smaller buffer size
1909 */
1910 size = 4;
1911 sprintf(buf,INIT_STR);
1912 expect_eq_d(FALSE, pQueryFullProcessImageNameA(GetCurrentProcess(), 0, buf, &size));
1913 expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1914 expect_eq_d(4, size);
1915 expect_eq_s(INIT_STR, buf);
1916
1917 /* this is a difference between the ascii and the unicode version
1918 * the unicode version crashes when the size is big enough to hold
1919 * the result while the ascii version throws an error
1920 */
1921 size = 1024;
1922 expect_eq_d(FALSE, pQueryFullProcessImageNameA(GetCurrentProcess(), 0, NULL, &size));
1923 expect_eq_d(1024, size);
1924 expect_eq_d(ERROR_INVALID_PARAMETER, GetLastError());
1925 }
1926
1927 static void test_QueryFullProcessImageNameW(void)
1928 {
1929 HANDLE hSelf;
1930 WCHAR module_name[1024], device[1024];
1931 WCHAR deviceW[] = {'\\','D', 'e','v','i','c','e',0};
1932 WCHAR buf[1024];
1933 DWORD size, len;
1934
1935 if (!pQueryFullProcessImageNameW)
1936 {
1937 win_skip("QueryFullProcessImageNameW unavailable (added in Windows Vista)\n");
1938 return;
1939 }
1940
1941 ok(GetModuleFileNameW(NULL, module_name, 1024), "GetModuleFileNameW(NULL, ...) failed\n");
1942
1943 /* GetCurrentProcess pseudo-handle */
1944 size = sizeof(buf) / sizeof(buf[0]);
1945 expect_eq_d(TRUE, pQueryFullProcessImageNameW(GetCurrentProcess(), 0, buf, &size));
1946 expect_eq_d(lstrlenW(buf), size);
1947 expect_eq_ws_i(buf, module_name);
1948
1949 hSelf = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, GetCurrentProcessId());
1950 /* Real handle */
1951 size = sizeof(buf) / sizeof(buf[0]);
1952 expect_eq_d(TRUE, pQueryFullProcessImageNameW(hSelf, 0, buf, &size));
1953 expect_eq_d(lstrlenW(buf), size);
1954 expect_eq_ws_i(buf, module_name);
1955
1956 /* Buffer too small */
1957 size = lstrlenW(module_name)/2;
1958 lstrcpyW(buf, deviceW);
1959 SetLastError(0xdeadbeef);
1960 expect_eq_d(FALSE, pQueryFullProcessImageNameW(hSelf, 0, buf, &size));
1961 expect_eq_d(lstrlenW(module_name)/2, size); /* size not changed(!) */
1962 expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1963 expect_eq_ws_i(deviceW, buf); /* buffer not changed */
1964
1965 /* Too small - not space for NUL terminator */
1966 size = lstrlenW(module_name);
1967 SetLastError(0xdeadbeef);
1968 expect_eq_d(FALSE, pQueryFullProcessImageNameW(hSelf, 0, buf, &size));
1969 expect_eq_d(lstrlenW(module_name), size); /* size not changed(!) */
1970 expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1971
1972 /* NULL buffer */
1973 size = 0;
1974 expect_eq_d(FALSE, pQueryFullProcessImageNameW(hSelf, 0, NULL, &size));
1975 expect_eq_d(0, size);
1976 expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1977
1978 /* Buffer too small */
1979 size = lstrlenW(module_name)/2;
1980 SetLastError(0xdeadbeef);
1981 lstrcpyW(buf, module_name);
1982 expect_eq_d(FALSE, pQueryFullProcessImageNameW(hSelf, 0, buf, &size));
1983 expect_eq_d(lstrlenW(module_name)/2, size); /* size not changed(!) */
1984 expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1985 expect_eq_ws_i(module_name, buf); /* buffer not changed */
1986
1987
1988 /* native path */
1989 size = sizeof(buf) / sizeof(buf[0]);
1990 expect_eq_d(TRUE, pQueryFullProcessImageNameW(hSelf, PROCESS_NAME_NATIVE, buf, &size));
1991 expect_eq_d(lstrlenW(buf), size);
1992 ok(buf[0] == '\\', "NT path should begin with '\\'\n");
1993 ok(memcmp(buf, deviceW, sizeof(WCHAR)*lstrlenW(deviceW)) == 0, "NT path should begin with \\Device\n");
1994
1995 module_name[2] = '\0';
1996 *device = '\0';
1997 size = QueryDosDeviceW(module_name, device, sizeof(device)/sizeof(device[0]));
1998 ok(size, "QueryDosDeviceW failed: le=%u\n", GetLastError());
1999 len = lstrlenW(device);
2000 ok(size >= len+2, "expected %d to be greater than %d+2 = strlen(%s)\n", size, len, wine_dbgstr_w(device));
2001
2002 if (size >= lstrlenW(buf))
2003 {
2004 ok(0, "expected %s\\ to match the start of %s\n", wine_dbgstr_w(device), wine_dbgstr_w(buf));
2005 }
2006 else
2007 {
2008 ok(buf[len] == '\\', "expected '%c' to be a '\\' in %s\n", buf[len], wine_dbgstr_w(module_name));
2009 buf[len] = '\0';
2010 ok(lstrcmpiW(device, buf) == 0, "expected %s to match %s\n", wine_dbgstr_w(device), wine_dbgstr_w(buf));
2011 ok(lstrcmpiW(module_name+3, buf+len+1) == 0, "expected '%s' to match '%s'\n", wine_dbgstr_w(module_name+3), wine_dbgstr_w(buf+len+1));
2012 }
2013
2014 CloseHandle(hSelf);
2015 }
2016
2017 static void test_Handles(void)
2018 {
2019 HANDLE handle = GetCurrentProcess();
2020 HANDLE h2, h3;
2021 BOOL ret;
2022 DWORD code;
2023
2024 ok( handle == (HANDLE)~(ULONG_PTR)0 ||
2025 handle == (HANDLE)(ULONG_PTR)0x7fffffff /* win9x */,
2026 "invalid current process handle %p\n", handle );
2027 ret = GetExitCodeProcess( handle, &code );
2028 ok( ret, "GetExitCodeProcess failed err %u\n", GetLastError() );
2029 #ifdef _WIN64
2030 /* truncated handle */
2031 SetLastError( 0xdeadbeef );
2032 handle = (HANDLE)((ULONG_PTR)handle & ~0u);
2033 ret = GetExitCodeProcess( handle, &code );
2034 ok( !ret, "GetExitCodeProcess succeeded for %p\n", handle );
2035 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2036 /* sign-extended handle */
2037 SetLastError( 0xdeadbeef );
2038 handle = (HANDLE)((LONG_PTR)(int)(ULONG_PTR)handle);
2039 ret = GetExitCodeProcess( handle, &code );
2040 ok( ret, "GetExitCodeProcess failed err %u\n", GetLastError() );
2041 /* invalid high-word */
2042 SetLastError( 0xdeadbeef );
2043 handle = (HANDLE)(((ULONG_PTR)handle & ~0u) + ((ULONG_PTR)1 << 32));
2044 ret = GetExitCodeProcess( handle, &code );
2045 ok( !ret, "GetExitCodeProcess succeeded for %p\n", handle );
2046 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
2047 #endif
2048
2049 handle = GetStdHandle( STD_ERROR_HANDLE );
2050 ok( handle != 0, "handle %p\n", handle );
2051 DuplicateHandle( GetCurrentProcess(), handle, GetCurrentProcess(), &h3,
2052 0, TRUE, DUPLICATE_SAME_ACCESS );
2053 SetStdHandle( STD_ERROR_HANDLE, h3 );
2054 CloseHandle( (HANDLE)STD_ERROR_HANDLE );
2055 h2 = GetStdHandle( STD_ERROR_HANDLE );
2056 ok( h2 == 0 ||
2057 broken( h2 == h3) || /* nt4, w2k */
2058 broken( h2 == INVALID_HANDLE_VALUE), /* win9x */
2059 "wrong handle %p/%p\n", h2, h3 );
2060 SetStdHandle( STD_ERROR_HANDLE, handle );
2061 }
2062
2063 static void test_IsWow64Process(void)
2064 {
2065 PROCESS_INFORMATION pi;
2066 STARTUPINFOA si;
2067 DWORD ret;
2068 BOOL is_wow64;
2069 static char cmdline[] = "C:\\Program Files\\Internet Explorer\\iexplore.exe";
2070 static char cmdline_wow64[] = "C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe";
2071
2072 if (!pIsWow64Process)
2073 {
2074 skip("IsWow64Process is not available\n");
2075 return;
2076 }
2077
2078 memset(&si, 0, sizeof(si));
2079 si.cb = sizeof(si);
2080 si.dwFlags = STARTF_USESHOWWINDOW;
2081 si.wShowWindow = SW_HIDE;
2082 ret = CreateProcessA(NULL, cmdline_wow64, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2083 if (ret)
2084 {
2085 trace("Created process %s\n", cmdline_wow64);
2086 is_wow64 = FALSE;
2087 ret = pIsWow64Process(pi.hProcess, &is_wow64);
2088 ok(ret, "IsWow64Process failed.\n");
2089 ok(is_wow64, "is_wow64 returned FALSE.\n");
2090
2091 ret = TerminateProcess(pi.hProcess, 0);
2092 ok(ret, "TerminateProcess error\n");
2093
2094 CloseHandle(pi.hProcess);
2095 CloseHandle(pi.hThread);
2096 }
2097
2098 memset(&si, 0, sizeof(si));
2099 si.cb = sizeof(si);
2100 si.dwFlags = STARTF_USESHOWWINDOW;
2101 si.wShowWindow = SW_HIDE;
2102 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2103 if (ret)
2104 {
2105 trace("Created process %s\n", cmdline);
2106 is_wow64 = TRUE;
2107 ret = pIsWow64Process(pi.hProcess, &is_wow64);
2108 ok(ret, "IsWow64Process failed.\n");
2109 ok(!is_wow64, "is_wow64 returned TRUE.\n");
2110
2111 ret = TerminateProcess(pi.hProcess, 0);
2112 ok(ret, "TerminateProcess error\n");
2113
2114 CloseHandle(pi.hProcess);
2115 CloseHandle(pi.hThread);
2116 }
2117 }
2118
2119 static void test_SystemInfo(void)
2120 {
2121 SYSTEM_INFO si, nsi;
2122 BOOL is_wow64;
2123
2124 if (!pGetNativeSystemInfo)
2125 {
2126 win_skip("GetNativeSystemInfo is not available\n");
2127 return;
2128 }
2129
2130 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 )) is_wow64 = FALSE;
2131
2132 GetSystemInfo(&si);
2133 pGetNativeSystemInfo(&nsi);
2134 if (is_wow64)
2135 {
2136 if (S(U(si)).wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
2137 {
2138 ok(S(U(nsi)).wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64,
2139 "Expected PROCESSOR_ARCHITECTURE_AMD64, got %d\n",
2140 S(U(nsi)).wProcessorArchitecture);
2141 ok(nsi.dwProcessorType == PROCESSOR_AMD_X8664,
2142 "Expected PROCESSOR_AMD_X8664, got %d\n",
2143 nsi.dwProcessorType);
2144 }
2145 }
2146 else
2147 {
2148 ok(S(U(si)).wProcessorArchitecture == S(U(nsi)).wProcessorArchitecture,
2149 "Expected no difference for wProcessorArchitecture, got %d and %d\n",
2150 S(U(si)).wProcessorArchitecture, S(U(nsi)).wProcessorArchitecture);
2151 ok(si.dwProcessorType == nsi.dwProcessorType,
2152 "Expected no difference for dwProcessorType, got %d and %d\n",
2153 si.dwProcessorType, nsi.dwProcessorType);
2154 }
2155 }
2156
2157 static void test_RegistryQuota(void)
2158 {
2159 BOOL ret;
2160 DWORD max_quota, used_quota;
2161
2162 if (!pGetSystemRegistryQuota)
2163 {
2164 win_skip("GetSystemRegistryQuota is not available\n");
2165 return;
2166 }
2167
2168 ret = pGetSystemRegistryQuota(NULL, NULL);
2169 ok(ret == TRUE,
2170 "Expected GetSystemRegistryQuota to return TRUE, got %d\n", ret);
2171
2172 ret = pGetSystemRegistryQuota(&max_quota, NULL);
2173 ok(ret == TRUE,
2174 "Expected GetSystemRegistryQuota to return TRUE, got %d\n", ret);
2175
2176 ret = pGetSystemRegistryQuota(NULL, &used_quota);
2177 ok(ret == TRUE,
2178 "Expected GetSystemRegistryQuota to return TRUE, got %d\n", ret);
2179
2180 ret = pGetSystemRegistryQuota(&max_quota, &used_quota);
2181 ok(ret == TRUE,
2182 "Expected GetSystemRegistryQuota to return TRUE, got %d\n", ret);
2183 }
2184
2185 static void test_TerminateProcess(void)
2186 {
2187 static char cmdline[] = "winver.exe";
2188 PROCESS_INFORMATION pi;
2189 STARTUPINFOA si;
2190 DWORD ret;
2191 HANDLE dummy, thread;
2192
2193 memset(&si, 0, sizeof(si));
2194 si.cb = sizeof(si);
2195 SetLastError(0xdeadbeef);
2196 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
2197 ok(ret, "CreateProcess error %u\n", GetLastError());
2198
2199 SetLastError(0xdeadbeef);
2200 thread = CreateRemoteThread(pi.hProcess, NULL, 0, (void *)0xdeadbeef, NULL, CREATE_SUSPENDED, &ret);
2201 ok(thread != 0, "CreateRemoteThread error %d\n", GetLastError());
2202
2203 /* create a not closed thread handle duplicate in the target process */
2204 SetLastError(0xdeadbeef);
2205 ret = DuplicateHandle(GetCurrentProcess(), thread, pi.hProcess, &dummy,
2206 0, FALSE, DUPLICATE_SAME_ACCESS);
2207 ok(ret, "DuplicateHandle error %u\n", GetLastError());
2208
2209 SetLastError(0xdeadbeef);
2210 ret = TerminateThread(thread, 0);
2211 ok(ret, "TerminateThread error %u\n", GetLastError());
2212 CloseHandle(thread);
2213
2214 SetLastError(0xdeadbeef);
2215 ret = TerminateProcess(pi.hProcess, 0);
2216 ok(ret, "TerminateProcess error %u\n", GetLastError());
2217
2218 CloseHandle(pi.hProcess);
2219 CloseHandle(pi.hThread);
2220 }
2221
2222 static void test_DuplicateHandle(void)
2223 {
2224 char path[MAX_PATH], file_name[MAX_PATH];
2225 HANDLE f, fmin, out;
2226 DWORD info;
2227 BOOL r;
2228
2229 r = DuplicateHandle(GetCurrentProcess(), GetCurrentProcess(),
2230 GetCurrentProcess(), &out, 0, FALSE,
2231 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
2232 ok(r, "DuplicateHandle error %u\n", GetLastError());
2233 r = GetHandleInformation(out, &info);
2234 ok(r, "GetHandleInformation error %u\n", GetLastError());
2235 ok(info == 0, "info = %x\n", info);
2236 ok(out != GetCurrentProcess(), "out = GetCurrentProcess()\n");
2237 CloseHandle(out);
2238
2239 r = DuplicateHandle(GetCurrentProcess(), GetCurrentProcess(),
2240 GetCurrentProcess(), &out, 0, TRUE,
2241 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
2242 ok(r, "DuplicateHandle error %u\n", GetLastError());
2243 r = GetHandleInformation(out, &info);
2244 ok(r, "GetHandleInformation error %u\n", GetLastError());
2245 ok(info == HANDLE_FLAG_INHERIT, "info = %x\n", info);
2246 ok(out != GetCurrentProcess(), "out = GetCurrentProcess()\n");
2247 CloseHandle(out);
2248
2249 GetTempPathA(MAX_PATH, path);
2250 GetTempFileNameA(path, "wt", 0, file_name);
2251 f = CreateFileA(file_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
2252 if (f == INVALID_HANDLE_VALUE)
2253 {
2254 ok(0, "could not create %s\n", file_name);
2255 return;
2256 }
2257
2258 r = DuplicateHandle(GetCurrentProcess(), f, GetCurrentProcess(), &out,
2259 0, FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
2260 ok(r, "DuplicateHandle error %u\n", GetLastError());
2261 ok(f == out, "f != out\n");
2262 r = GetHandleInformation(out, &info);
2263 ok(r, "GetHandleInformation error %u\n", GetLastError());
2264 ok(info == 0, "info = %x\n", info);
2265
2266 r = DuplicateHandle(GetCurrentProcess(), f, GetCurrentProcess(), &out,
2267 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
2268 ok(r, "DuplicateHandle error %u\n", GetLastError());
2269 ok(f == out, "f != out\n");
2270 r = GetHandleInformation(out, &info);
2271 ok(r, "GetHandleInformation error %u\n", GetLastError());
2272 ok(info == HANDLE_FLAG_INHERIT, "info = %x\n", info);
2273
2274 r = SetHandleInformation(f, HANDLE_FLAG_PROTECT_FROM_CLOSE, HANDLE_FLAG_PROTECT_FROM_CLOSE);
2275 ok(r, "SetHandleInformation error %u\n", GetLastError());
2276 r = DuplicateHandle(GetCurrentProcess(), f, GetCurrentProcess(), &out,
2277 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
2278 ok(r, "DuplicateHandle error %u\n", GetLastError());
2279 ok(f != out, "f == out\n");
2280 r = GetHandleInformation(out, &info);
2281 ok(r, "GetHandleInformation error %u\n", GetLastError());
2282 ok(info == HANDLE_FLAG_INHERIT, "info = %x\n", info);
2283 r = SetHandleInformation(f, HANDLE_FLAG_PROTECT_FROM_CLOSE, 0);
2284 ok(r, "SetHandleInformation error %u\n", GetLastError());
2285
2286 /* Test if DuplicateHandle allocates first free handle */
2287 if (f > out)
2288 {
2289 fmin = out;
2290 }
2291 else
2292 {
2293 fmin = f;
2294 f = out;
2295 }
2296 CloseHandle(fmin);
2297 r = DuplicateHandle(GetCurrentProcess(), f, GetCurrentProcess(), &out,
2298 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
2299 ok(r, "DuplicateHandle error %u\n", GetLastError());
2300 ok(f == out, "f != out\n");
2301 CloseHandle(out);
2302 DeleteFileA(file_name);
2303
2304 f = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
2305 if (!is_console(f))
2306 {
2307 skip("DuplicateHandle on console handle\n");
2308 CloseHandle(f);
2309 return;
2310 }
2311
2312 r = DuplicateHandle(GetCurrentProcess(), f, GetCurrentProcess(), &out,
2313 0, FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
2314 ok(r, "DuplicateHandle error %u\n", GetLastError());
2315 todo_wine ok(f != out, "f == out\n");
2316 CloseHandle(out);
2317 }
2318
2319 #define test_completion(a, b, c, d, e) _test_completion(__LINE__, a, b, c, d, e)
2320 static void _test_completion(int line, HANDLE port, DWORD ekey, ULONG_PTR evalue, ULONG_PTR eoverlapped, DWORD wait)
2321 {
2322 LPOVERLAPPED overlapped;
2323 ULONG_PTR value;
2324 DWORD key;
2325 BOOL ret;
2326
2327 ret = GetQueuedCompletionStatus(port, &key, &value, &overlapped, wait);
2328
2329 ok_(__FILE__, line)(ret, "GetQueuedCompletionStatus: %x\n", GetLastError());
2330 if (ret)
2331 {
2332 ok_(__FILE__, line)(key == ekey, "unexpected key %x\n", key);
2333 ok_(__FILE__, line)(value == evalue, "unexpected value %p\n", (void *)value);
2334 ok_(__FILE__, line)(overlapped == (LPOVERLAPPED)eoverlapped, "unexpected overlapped %p\n", overlapped);
2335 }
2336 }
2337
2338 #define create_process(cmd, pi) _create_process(__LINE__, cmd, pi)
2339 static void _create_process(int line, const char *command, LPPROCESS_INFORMATION pi)
2340 {
2341 BOOL ret;
2342 char buffer[MAX_PATH];
2343 STARTUPINFOA si = {0};
2344
2345 sprintf(buffer, "\"%s\" tests/process.c %s", selfname, command);
2346
2347 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, pi);
2348 ok_(__FILE__, line)(ret, "CreateProcess error %u\n", GetLastError());
2349 }
2350
2351
2352 static void test_IsProcessInJob(void)
2353 {
2354 HANDLE job, job2;
2355 PROCESS_INFORMATION pi;
2356 BOOL ret, out;
2357 DWORD dwret;
2358
2359 if (!pIsProcessInJob)
2360 {
2361 win_skip("IsProcessInJob not available.\n");
2362 return;
2363 }
2364
2365 job = pCreateJobObjectW(NULL, NULL);
2366 ok(job != NULL, "CreateJobObject error %u\n", GetLastError());
2367
2368 job2 = pCreateJobObjectW(NULL, NULL);
2369 ok(job2 != NULL, "CreateJobObject error %u\n", GetLastError());
2370
2371 create_process("wait", &pi);
2372
2373 out = TRUE;
2374 ret = pIsProcessInJob(pi.hProcess, job, &out);
2375 ok(ret, "IsProcessInJob error %u\n", GetLastError());
2376 ok(!out, "IsProcessInJob returned out=%u\n", out);
2377
2378 out = TRUE;
2379 ret = pIsProcessInJob(pi.hProcess, job2, &out);
2380 ok(ret, "IsProcessInJob error %u\n", GetLastError());
2381 ok(!out, "IsProcessInJob returned out=%u\n", out);
2382
2383 out = TRUE;
2384 ret = pIsProcessInJob(pi.hProcess, NULL, &out);
2385 ok(ret, "IsProcessInJob error %u\n", GetLastError());
2386 ok(!out, "IsProcessInJob returned out=%u\n", out);
2387
2388 ret = pAssignProcessToJobObject(job, pi.hProcess);
2389 ok(ret, "AssignProcessToJobObject error %u\n", GetLastError());
2390
2391 out = FALSE;
2392 ret = pIsProcessInJob(pi.hProcess, job, &out);
2393 ok(ret, "IsProcessInJob error %u\n", GetLastError());
2394 ok(out, "IsProcessInJob returned out=%u\n", out);
2395
2396 out = TRUE;
2397 ret = pIsProcessInJob(pi.hProcess, job2, &out);
2398 ok(ret, "IsProcessInJob error %u\n", GetLastError());
2399 ok(!out, "IsProcessInJob returned out=%u\n", out);
2400
2401 out = FALSE;
2402 ret = pIsProcessInJob(pi.hProcess, NULL, &out);
2403 ok(ret, "IsProcessInJob error %u\n", GetLastError());
2404 ok(out, "IsProcessInJob returned out=%u\n", out);
2405
2406 TerminateProcess(pi.hProcess, 0);
2407
2408 dwret = WaitForSingleObject(pi.hProcess, 1000);
2409 ok(dwret == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", dwret);
2410
2411 out = FALSE;
2412 ret = pIsProcessInJob(pi.hProcess, job, &out);
2413 ok(ret, "IsProcessInJob error %u\n", GetLastError());
2414 ok(out, "IsProcessInJob returned out=%u\n", out);
2415
2416 CloseHandle(pi.hProcess);
2417 CloseHandle(pi.hThread);
2418 CloseHandle(job);
2419 CloseHandle(job2);
2420 }
2421
2422 static void test_TerminateJobObject(void)
2423 {
2424 HANDLE job;
2425 PROCESS_INFORMATION pi;
2426 BOOL ret;
2427 DWORD dwret;
2428
2429 job = pCreateJobObjectW(NULL, NULL);
2430 ok(job != NULL, "CreateJobObject error %u\n", GetLastError());
2431
2432 create_process("wait", &pi);
2433
2434 ret = pAssignProcessToJobObject(job, pi.hProcess);
2435 ok(ret, "AssignProcessToJobObject error %u\n", GetLastError());
2436
2437 ret = pTerminateJobObject(job, 123);
2438 ok(ret, "TerminateJobObject error %u\n", GetLastError());
2439
2440 dwret = WaitForSingleObject(pi.hProcess, 1000);
2441 ok(dwret == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", dwret);
2442 if (dwret == WAIT_TIMEOUT) TerminateProcess(pi.hProcess, 0);
2443
2444 ret = GetExitCodeProcess(pi.hProcess, &dwret);
2445 ok(ret, "GetExitCodeProcess error %u\n", GetLastError());
2446 ok(dwret == 123 || broken(dwret == 0) /* randomly fails on Win 2000 / XP */,
2447 "wrong exitcode %u\n", dwret);
2448
2449 CloseHandle(pi.hProcess);
2450 CloseHandle(pi.hThread);
2451
2452 /* Test adding an already terminated process to a job object */
2453 create_process("exit", &pi);
2454
2455 dwret = WaitForSingleObject(pi.hProcess, 1000);
2456 ok(dwret == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", dwret);
2457
2458 SetLastError(0xdeadbeef);
2459 ret = pAssignProcessToJobObject(job, pi.hProcess);
2460 ok(!ret, "AssignProcessToJobObject unexpectedly succeeded\n");
2461 expect_eq_d(ERROR_ACCESS_DENIED, GetLastError());
2462
2463 CloseHandle(pi.hProcess);
2464 CloseHandle(pi.hThread);
2465
2466 CloseHandle(job);
2467 }
2468
2469 static void test_QueryInformationJobObject(void)
2470 {
2471 char buf[sizeof(JOBOBJECT_BASIC_PROCESS_ID_LIST) + sizeof(ULONG_PTR) * 4];
2472 PJOBOBJECT_BASIC_PROCESS_ID_LIST pid_list = (JOBOBJECT_BASIC_PROCESS_ID_LIST *)buf;
2473 JOBOBJECT_EXTENDED_LIMIT_INFORMATION ext_limit_info;
2474 JOBOBJECT_BASIC_LIMIT_INFORMATION *basic_limit_info = &ext_limit_info.BasicLimitInformation;
2475 DWORD dwret, ret_len;
2476 PROCESS_INFORMATION pi[2];
2477 HANDLE job;
2478 BOOL ret;
2479
2480 job = pCreateJobObjectW(NULL, NULL);
2481 ok(job != NULL, "CreateJobObject error %u\n", GetLastError());
2482
2483 /* Only active processes are returned */
2484 create_process("exit", &pi[0]);
2485 ret = pAssignProcessToJobObject(job, pi[0].hProcess);
2486 ok(ret, "AssignProcessToJobObject error %u\n", GetLastError());
2487 dwret = WaitForSingleObject(pi[0].hProcess, 1000);
2488 ok(dwret == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", dwret);
2489
2490 CloseHandle(pi[0].hProcess);
2491 CloseHandle(pi[0].hThread);
2492
2493 create_process("wait", &pi[0]);
2494 ret = pAssignProcessToJobObject(job, pi[0].hProcess);
2495 ok(ret, "AssignProcessToJobObject error %u\n", GetLastError());
2496
2497 create_process("wait", &pi[1]);
2498 ret = pAssignProcessToJobObject(job, pi[1].hProcess);
2499 ok(ret, "AssignProcessToJobObject error %u\n", GetLastError());
2500
2501 SetLastError(0xdeadbeef);
2502 ret = QueryInformationJobObject(job, JobObjectBasicProcessIdList, pid_list,
2503 FIELD_OFFSET(JOBOBJECT_BASIC_PROCESS_ID_LIST, ProcessIdList), &ret_len);
2504 ok(!ret, "QueryInformationJobObject expected failure\n");
2505 expect_eq_d(ERROR_BAD_LENGTH, GetLastError());
2506
2507 SetLastError(0xdeadbeef);
2508 memset(buf, 0, sizeof(buf));
2509 pid_list->NumberOfAssignedProcesses = 42;
2510 pid_list->NumberOfProcessIdsInList = 42;
2511 ret = QueryInformationJobObject(job, JobObjectBasicProcessIdList, pid_list,
2512 FIELD_OFFSET(JOBOBJECT_BASIC_PROCESS_ID_LIST, ProcessIdList[1]), &ret_len);
2513 todo_wine
2514 ok(!ret, "QueryInformationJobObject expected failure\n");
2515 todo_wine
2516 expect_eq_d(ERROR_MORE_DATA, GetLastError());
2517 if (ret)
2518 {
2519 todo_wine
2520 expect_eq_d(42, pid_list->NumberOfAssignedProcesses);
2521 todo_wine
2522 expect_eq_d(42, pid_list->NumberOfProcessIdsInList);
2523 }
2524
2525 memset(buf, 0, sizeof(buf));
2526 ret = pQueryInformationJobObject(job, JobObjectBasicProcessIdList, pid_list, sizeof(buf), &ret_len);
2527 ok(ret, "QueryInformationJobObject error %u\n", GetLastError());
2528 if(ret)
2529 {
2530 if (pid_list->NumberOfAssignedProcesses == 3) /* Win 8 */
2531 win_skip("Number of assigned processes broken on Win 8\n");
2532 else
2533 {
2534 ULONG_PTR *list = pid_list->ProcessIdList;
2535
2536 todo_wine
2537 ok(ret_len == FIELD_OFFSET(JOBOBJECT_BASIC_PROCESS_ID_LIST, ProcessIdList[2]),
2538 "QueryInformationJobObject returned ret_len=%u\n", ret_len);
2539
2540 todo_wine
2541 expect_eq_d(2, pid_list->NumberOfAssignedProcesses);
2542 todo_wine
2543 expect_eq_d(2, pid_list->NumberOfProcessIdsInList);
2544 todo_wine
2545 expect_eq_d(pi[0].dwProcessId, list[0]);
2546 todo_wine
2547 expect_eq_d(pi[1].dwProcessId, list[1]);
2548 }
2549 }
2550
2551 /* test JobObjectBasicLimitInformation */
2552 ret = pQueryInformationJobObject(job, JobObjectBasicLimitInformation, basic_limit_info,
2553 sizeof(*basic_limit_info) - 1, &ret_len);
2554 ok(!ret, "QueryInformationJobObject expected failure\n");
2555 expect_eq_d(ERROR_BAD_LENGTH, GetLastError());
2556
2557 ret_len = 0xdeadbeef;
2558 memset(basic_limit_info, 0x11, sizeof(*basic_limit_info));
2559 ret = pQueryInformationJobObject(job, JobObjectBasicLimitInformation, basic_limit_info,
2560 sizeof(*basic_limit_info), &ret_len);
2561 ok(ret, "QueryInformationJobObject error %u\n", GetLastError());
2562 ok(ret_len == sizeof(*basic_limit_info), "QueryInformationJobObject returned ret_len=%u\n", ret_len);
2563 expect_eq_d(0, basic_limit_info->LimitFlags);
2564
2565 /* test JobObjectExtendedLimitInformation */
2566 ret = pQueryInformationJobObject(job, JobObjectExtendedLimitInformation, &ext_limit_info,
2567 sizeof(ext_limit_info) - 1, &ret_len);
2568 ok(!ret, "QueryInformationJobObject expected failure\n");
2569 expect_eq_d(ERROR_BAD_LENGTH, GetLastError());
2570
2571 ret_len = 0xdeadbeef;
2572 memset(&ext_limit_info, 0x11, sizeof(ext_limit_info));
2573 ret = pQueryInformationJobObject(job, JobObjectExtendedLimitInformation, &ext_limit_info,
2574 sizeof(ext_limit_info), &ret_len);
2575 ok(ret, "QueryInformationJobObject error %u\n", GetLastError());
2576 ok(ret_len == sizeof(ext_limit_info), "QueryInformationJobObject returned ret_len=%u\n", ret_len);
2577 expect_eq_d(0, basic_limit_info->LimitFlags);
2578
2579 TerminateProcess(pi[0].hProcess, 0);
2580 CloseHandle(pi[0].hProcess);
2581 CloseHandle(pi[0].hThread);
2582
2583 TerminateProcess(pi[1].hProcess, 0);
2584 CloseHandle(pi[1].hProcess);
2585 CloseHandle(pi[1].hThread);
2586
2587 CloseHandle(job);
2588 }
2589
2590 static void test_CompletionPort(void)
2591 {
2592 JOBOBJECT_ASSOCIATE_COMPLETION_PORT port_info;
2593 PROCESS_INFORMATION pi;
2594 HANDLE job, port;
2595 DWORD dwret;
2596 BOOL ret;
2597
2598 job = pCreateJobObjectW(NULL, NULL);
2599 ok(job != NULL, "CreateJobObject error %u\n", GetLastError());
2600
2601 port = pCreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1);
2602 ok(port != NULL, "CreateIoCompletionPort error %u\n", GetLastError());
2603
2604 port_info.CompletionKey = job;
2605 port_info.CompletionPort = port;
2606 ret = pSetInformationJobObject(job, JobObjectAssociateCompletionPortInformation, &port_info, sizeof(port_info));
2607 ok(ret, "SetInformationJobObject error %u\n", GetLastError());
2608
2609 create_process("wait", &pi);
2610
2611 ret = pAssignProcessToJobObject(job, pi.hProcess);
2612 ok(ret, "AssignProcessToJobObject error %u\n", GetLastError());
2613
2614 test_completion(port, JOB_OBJECT_MSG_NEW_PROCESS, (DWORD_PTR)job, pi.dwProcessId, 0);
2615
2616 TerminateProcess(pi.hProcess, 0);
2617 dwret = WaitForSingleObject(pi.hProcess, 1000);
2618 ok(dwret == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", dwret);
2619
2620 test_completion(port, JOB_OBJECT_MSG_EXIT_PROCESS, (DWORD_PTR)job, pi.dwProcessId, 0);
2621 test_completion(port, JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO, (DWORD_PTR)job, 0, 100);
2622
2623 CloseHandle(pi.hProcess);
2624 CloseHandle(pi.hThread);
2625 CloseHandle(job);
2626 CloseHandle(port);
2627 }
2628
2629 static void test_KillOnJobClose(void)
2630 {
2631 JOBOBJECT_EXTENDED_LIMIT_INFORMATION limit_info;
2632 PROCESS_INFORMATION pi;
2633 DWORD dwret;
2634 HANDLE job;
2635 BOOL ret;
2636
2637 job = pCreateJobObjectW(NULL, NULL);
2638 ok(job != NULL, "CreateJobObject error %u\n", GetLastError());
2639
2640 limit_info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
2641 ret = pSetInformationJobObject(job, JobObjectExtendedLimitInformation, &limit_info, sizeof(limit_info));
2642 if (!ret && GetLastError() == ERROR_INVALID_PARAMETER)
2643 {
2644 win_skip("Kill on job close limit not available\n");
2645 return;
2646 }
2647 ok(ret, "SetInformationJobObject error %u\n", GetLastError());
2648
2649 create_process("wait", &pi);
2650
2651 ret = pAssignProcessToJobObject(job, pi.hProcess);
2652 ok(ret, "AssignProcessToJobObject error %u\n", GetLastError());
2653
2654 CloseHandle(job);
2655
2656 dwret = WaitForSingleObject(pi.hProcess, 1000);
2657 ok(dwret == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", dwret);
2658 if (dwret == WAIT_TIMEOUT) TerminateProcess(pi.hProcess, 0);
2659
2660 CloseHandle(pi.hProcess);
2661 CloseHandle(pi.hThread);
2662 }
2663
2664 static void test_WaitForJobObject(void)
2665 {
2666 HANDLE job;
2667 PROCESS_INFORMATION pi;
2668 BOOL ret;
2669 DWORD dwret;
2670
2671 /* test waiting for a job object when the process is killed */
2672 job = pCreateJobObjectW(NULL, NULL);
2673 ok(job != NULL, "CreateJobObject error %u\n", GetLastError());
2674
2675 dwret = WaitForSingleObject(job, 100);
2676 ok(dwret == WAIT_TIMEOUT, "WaitForSingleObject returned %u\n", dwret);
2677
2678 create_process("wait", &pi);
2679
2680 ret = pAssignProcessToJobObject(job, pi.hProcess);
2681 ok(ret, "AssignProcessToJobObject error %u\n", GetLastError());
2682
2683 dwret = WaitForSingleObject(job, 100);
2684 ok(dwret == WAIT_TIMEOUT, "WaitForSingleObject returned %u\n", dwret);
2685
2686 ret = pTerminateJobObject(job, 123);
2687 ok(ret, "TerminateJobObject error %u\n", GetLastError());
2688
2689 dwret = WaitForSingleObject(job, 500);
2690 ok(dwret == WAIT_OBJECT_0 || broken(dwret == WAIT_TIMEOUT),
2691 "WaitForSingleObject returned %u\n", dwret);
2692
2693 if (dwret == WAIT_TIMEOUT) /* Win 2000/XP */
2694 {
2695 #ifdef __REACTOS__
2696 if (!ret)
2697 {
2698 ok(0, "HACK: Killing process to speed up the test\n");
2699 TerminateProcess(pi.hProcess, 0);
2700 }
2701 #endif
2702 CloseHandle(pi.hProcess);
2703 CloseHandle(pi.hThread);
2704 CloseHandle(job);
2705 win_skip("TerminateJobObject doesn't signal job, skipping tests\n");
2706 return;
2707 }
2708
2709 /* the object is not reset immediately */
2710 dwret = WaitForSingleObject(job, 100);
2711 ok(dwret == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", dwret);
2712
2713 CloseHandle(pi.hProcess);
2714 CloseHandle(pi.hThread);
2715
2716 /* creating a new process doesn't reset the signalled state */
2717 create_process("wait", &pi);
2718
2719 ret = pAssignProcessToJobObject(job, pi.hProcess);
2720 ok(ret, "AssignProcessToJobObject error %u\n", GetLastError());
2721
2722 dwret = WaitForSingleObject(job, 100);
2723 ok(dwret == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", dwret);
2724
2725 ret = pTerminateJobObject(job, 123);
2726 ok(ret, "TerminateJobObject error %u\n", GetLastError());
2727
2728 CloseHandle(pi.hProcess);
2729 CloseHandle(pi.hThread);
2730
2731 CloseHandle(job);
2732
2733 /* repeat the test, but this time the process terminates properly */
2734 job = pCreateJobObjectW(NULL, NULL);
2735 ok(job != NULL, "CreateJobObject error %u\n", GetLastError());
2736
2737 dwret = WaitForSingleObject(job, 100);
2738 ok(dwret == WAIT_TIMEOUT, "WaitForSingleObject returned %u\n", dwret);
2739
2740 create_process("exit", &pi);
2741
2742 ret = pAssignProcessToJobObject(job, pi.hProcess);
2743 ok(ret, "AssignProcessToJobObject error %u\n", GetLastError());
2744
2745 dwret = WaitForSingleObject(job, 100);
2746 ok(dwret == WAIT_TIMEOUT, "WaitForSingleObject returned %u\n", dwret);
2747
2748 CloseHandle(pi.hProcess);
2749 CloseHandle(pi.hThread);
2750 CloseHandle(job);
2751 }
2752
2753 static HANDLE test_AddSelfToJob(void)
2754 {
2755 HANDLE job;
2756 BOOL ret;
2757
2758 job = pCreateJobObjectW(NULL, NULL);
2759 ok(job != NULL, "CreateJobObject error %u\n", GetLastError());
2760
2761 ret = pAssignProcessToJobObject(job, GetCurrentProcess());
2762 ok(ret, "AssignProcessToJobObject error %u\n", GetLastError());
2763
2764 return job;
2765 }
2766
2767 static void test_jobInheritance(HANDLE job)
2768 {
2769 char buffer[MAX_PATH];
2770 PROCESS_INFORMATION pi;
2771 STARTUPINFOA si = {0};
2772 DWORD dwret;
2773 BOOL ret, out;
2774
2775 if (!pIsProcessInJob)
2776 {
2777 win_skip("IsProcessInJob not available.\n");
2778 return;
2779 }
2780
2781 sprintf(buffer, "\"%s\" tests/process.c %s", selfname, "exit");
2782
2783 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2784 ok(ret, "CreateProcessA error %u\n", GetLastError());
2785
2786 out = FALSE;
2787 ret = pIsProcessInJob(pi.hProcess, job, &out);
2788 ok(ret, "IsProcessInJob error %u\n", GetLastError());
2789 ok(out, "IsProcessInJob returned out=%u\n", out);
2790
2791 dwret = WaitForSingleObject(pi.hProcess, 1000);
2792 ok(dwret == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", dwret);
2793
2794 CloseHandle(pi.hProcess);
2795 CloseHandle(pi.hThread);
2796 }
2797
2798 static void test_BreakawayOk(HANDLE job)
2799 {
2800 JOBOBJECT_EXTENDED_LIMIT_INFORMATION limit_info;
2801 PROCESS_INFORMATION pi;
2802 STARTUPINFOA si = {0};
2803 char buffer[MAX_PATH];
2804 BOOL ret, out;
2805 DWORD dwret;
2806
2807 if (!pIsProcessInJob)
2808 {
2809 win_skip("IsProcessInJob not available.\n");
2810 return;
2811 }
2812
2813 sprintf(buffer, "\"%s\" tests/process.c %s", selfname, "exit");
2814
2815 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, CREATE_BREAKAWAY_FROM_JOB, NULL, NULL, &si, &pi);
2816 ok(!ret, "CreateProcessA expected failure\n");
2817 expect_eq_d(ERROR_ACCESS_DENIED, GetLastError());
2818
2819 if (ret)
2820 {
2821 TerminateProcess(pi.hProcess, 0);
2822
2823 dwret = WaitForSingleObject(pi.hProcess, 1000);
2824 ok(dwret == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", dwret);
2825
2826 CloseHandle(pi.hProcess);
2827 CloseHandle(pi.hThread);
2828 }
2829
2830 limit_info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_BREAKAWAY_OK;
2831 ret = pSetInformationJobObject(job, JobObjectExtendedLimitInformation, &limit_info, sizeof(limit_info));
2832 ok(ret, "SetInformationJobObject error %u\n", GetLastError());
2833
2834 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, CREATE_BREAKAWAY_FROM_JOB, NULL, NULL, &si, &pi);
2835 ok(ret, "CreateProcessA error %u\n", GetLastError());
2836
2837 ret = pIsProcessInJob(pi.hProcess, job, &out);
2838 ok(ret, "IsProcessInJob error %u\n", GetLastError());
2839 ok(!out, "IsProcessInJob returned out=%u\n", out);
2840
2841 dwret = WaitForSingleObject(pi.hProcess, 1000);
2842 ok(dwret == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", dwret);
2843
2844 CloseHandle(pi.hProcess);
2845 CloseHandle(pi.hThread);
2846
2847 limit_info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK;
2848 ret = pSetInformationJobObject(job, JobObjectExtendedLimitInformation, &limit_info, sizeof(limit_info));
2849 ok(ret, "SetInformationJobObject error %u\n", GetLastError());
2850
2851 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2852 ok(ret, "CreateProcess error %u\n", GetLastError());
2853
2854 ret = pIsProcessInJob(pi.hProcess, job, &out);
2855 ok(ret, "IsProcessInJob error %u\n", GetLastError());
2856 ok(!out, "IsProcessInJob returned out=%u\n", out);
2857
2858 dwret = WaitForSingleObject(pi.hProcess, 1000);
2859 ok(dwret == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", dwret);
2860
2861 CloseHandle(pi.hProcess);
2862 CloseHandle(pi.hThread);
2863
2864 /* unset breakaway ok */
2865 limit_info.BasicLimitInformation.LimitFlags = 0;
2866 ret = pSetInformationJobObject(job, JobObjectExtendedLimitInformation, &limit_info, sizeof(limit_info));
2867 ok(ret, "SetInformationJobObject error %u\n", GetLastError());
2868 }
2869
2870 static void test_StartupNoConsole(void)
2871 {
2872 #ifndef _WIN64
2873 char buffer[MAX_PATH];
2874 STARTUPINFOA startup;
2875 PROCESS_INFORMATION info;
2876
2877 memset(&startup, 0, sizeof(startup));
2878 startup.cb = sizeof(startup);
2879 startup.dwFlags = STARTF_USESHOWWINDOW;
2880 startup.wShowWindow = SW_SHOWNORMAL;
2881 get_file_name(resfile);
2882 sprintf(buffer, "\"%s\" tests/process.c dump \"%s\"", selfname, resfile);
2883 ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &startup,
2884 &info), "CreateProcess\n");