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