Merge from amd64 branch:
[reactos.git] / 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 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "wincon.h"
33 #include "winnls.h"
34 #include "winternl.h"
35
36 #include "wine/test.h"
37
38 #define expect_eq_d(expected, actual) \
39 do { \
40 int value = (actual); \
41 ok((expected) == value, "Expected " #actual " to be %d (" #expected ") is %d\n", \
42 (expected), value); \
43 } while (0)
44 #define expect_eq_s(expected, actual) \
45 do { \
46 LPCSTR value = (actual); \
47 ok(lstrcmpA((expected), value) == 0, "Expected " #actual " to be L\"%s\" (" #expected ") is L\"%s\"\n", \
48 expected, value); \
49 } while (0)
50 #define expect_eq_ws_i(expected, actual) \
51 do { \
52 LPCWSTR value = (actual); \
53 ok(lstrcmpiW((expected), value) == 0, "Expected " #actual " to be L\"%s\" (" #expected ") is L\"%s\"\n", \
54 wine_dbgstr_w(expected), wine_dbgstr_w(value)); \
55 } while (0)
56
57 static HINSTANCE hkernel32;
58 static LPVOID (WINAPI *pVirtualAllocEx)(HANDLE, LPVOID, SIZE_T, DWORD, DWORD);
59 static BOOL (WINAPI *pVirtualFreeEx)(HANDLE, LPVOID, SIZE_T, DWORD);
60 static BOOL (WINAPI *pQueryFullProcessImageNameA)(HANDLE hProcess, DWORD dwFlags, LPSTR lpExeName, PDWORD lpdwSize);
61 static BOOL (WINAPI *pQueryFullProcessImageNameW)(HANDLE hProcess, DWORD dwFlags, LPWSTR lpExeName, PDWORD lpdwSize);
62
63 /* ############################### */
64 static char base[MAX_PATH];
65 static char selfname[MAX_PATH];
66 static char* exename;
67 static char resfile[MAX_PATH];
68
69 static int myARGC;
70 static char** myARGV;
71
72 /* As some environment variables get very long on Unix, we only test for
73 * the first 127 bytes.
74 * Note that increasing this value past 256 may exceed the buffer size
75 * limitations of the *Profile functions (at least on Wine).
76 */
77 #define MAX_LISTED_ENV_VAR 128
78
79 /* ---------------- portable memory allocation thingie */
80
81 static char memory[1024*256];
82 static char* memory_index = memory;
83
84 static char* grab_memory(size_t len)
85 {
86 char* ret = memory_index;
87 /* align on dword */
88 len = (len + 3) & ~3;
89 memory_index += len;
90 assert(memory_index <= memory + sizeof(memory));
91 return ret;
92 }
93
94 static void release_memory(void)
95 {
96 memory_index = memory;
97 }
98
99 /* ---------------- simplistic tool to encode/decode strings (to hide \ " ' and such) */
100
101 static const char* encodeA(const char* str)
102 {
103 char* ptr;
104 size_t len,i;
105
106 if (!str) return "";
107 len = strlen(str) + 1;
108 ptr = grab_memory(len * 2 + 1);
109 for (i = 0; i < len; i++)
110 sprintf(&ptr[i * 2], "%02x", (unsigned char)str[i]);
111 ptr[2 * len] = '\0';
112 return ptr;
113 }
114
115 static const char* encodeW(const WCHAR* str)
116 {
117 char* ptr;
118 size_t len,i;
119
120 if (!str) return "";
121 len = lstrlenW(str) + 1;
122 ptr = grab_memory(len * 4 + 1);
123 assert(ptr);
124 for (i = 0; i < len; i++)
125 sprintf(&ptr[i * 4], "%04x", (unsigned int)(unsigned short)str[i]);
126 ptr[4 * len] = '\0';
127 return ptr;
128 }
129
130 static unsigned decode_char(char c)
131 {
132 if (c >= '0' && c <= '9') return c - '0';
133 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
134 assert(c >= 'A' && c <= 'F');
135 return c - 'A' + 10;
136 }
137
138 static char* decodeA(const char* str)
139 {
140 char* ptr;
141 size_t len,i;
142
143 len = strlen(str) / 2;
144 if (!len--) return NULL;
145 ptr = grab_memory(len + 1);
146 for (i = 0; i < len; i++)
147 ptr[i] = (decode_char(str[2 * i]) << 4) | decode_char(str[2 * i + 1]);
148 ptr[len] = '\0';
149 return ptr;
150 }
151
152 /* This will be needed to decode Unicode strings saved by the child process
153 * when we test Unicode functions.
154 */
155 static WCHAR* decodeW(const char* str)
156 {
157 size_t len;
158 WCHAR* ptr;
159 int i;
160
161 len = strlen(str) / 4;
162 if (!len--) return NULL;
163 ptr = (WCHAR*)grab_memory(len * 2 + 1);
164 for (i = 0; i < len; i++)
165 ptr[i] = (decode_char(str[4 * i]) << 12) |
166 (decode_char(str[4 * i + 1]) << 8) |
167 (decode_char(str[4 * i + 2]) << 4) |
168 (decode_char(str[4 * i + 3]) << 0);
169 ptr[len] = '\0';
170 return ptr;
171 }
172
173 /******************************************************************
174 * init
175 *
176 * generates basic information like:
177 * base: absolute path to curr dir
178 * selfname: the way to reinvoke ourselves
179 * exename: executable without the path
180 * function-pointers, which are not implemented in all windows versions
181 */
182 static int init(void)
183 {
184 char *p;
185
186 myARGC = winetest_get_mainargs( &myARGV );
187 if (!GetCurrentDirectoryA(sizeof(base), base)) return 0;
188 strcpy(selfname, myARGV[0]);
189
190 /* Strip the path of selfname */
191 if ((p = strrchr(selfname, '\\')) != NULL) exename = p + 1;
192 else exename = selfname;
193
194 if ((p = strrchr(exename, '/')) != NULL) exename = p + 1;
195
196 hkernel32 = GetModuleHandleA("kernel32");
197 pVirtualAllocEx = (void *) GetProcAddress(hkernel32, "VirtualAllocEx");
198 pVirtualFreeEx = (void *) GetProcAddress(hkernel32, "VirtualFreeEx");
199 pQueryFullProcessImageNameA = (void *) GetProcAddress(hkernel32, "QueryFullProcessImageNameA");
200 pQueryFullProcessImageNameW = (void *) GetProcAddress(hkernel32, "QueryFullProcessImageNameW");
201 return 1;
202 }
203
204 /******************************************************************
205 * get_file_name
206 *
207 * generates an absolute file_name for temporary file
208 *
209 */
210 static void get_file_name(char* buf)
211 {
212 char path[MAX_PATH];
213
214 buf[0] = '\0';
215 GetTempPathA(sizeof(path), path);
216 GetTempFileNameA(path, "wt", 0, buf);
217 }
218
219 /******************************************************************
220 * static void childPrintf
221 *
222 */
223 static void childPrintf(HANDLE h, const char* fmt, ...)
224 {
225 va_list valist;
226 char buffer[1024+4*MAX_LISTED_ENV_VAR];
227 DWORD w;
228
229 va_start(valist, fmt);
230 vsprintf(buffer, fmt, valist);
231 va_end(valist);
232 WriteFile(h, buffer, strlen(buffer), &w, NULL);
233 }
234
235
236 /******************************************************************
237 * doChild
238 *
239 * output most of the information in the child process
240 */
241 static void doChild(const char* file, const char* option)
242 {
243 STARTUPINFOA siA;
244 STARTUPINFOW siW;
245 int i;
246 char* ptrA;
247 WCHAR* ptrW;
248 char bufA[MAX_PATH];
249 WCHAR bufW[MAX_PATH];
250 HANDLE hFile = CreateFileA(file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
251 BOOL ret;
252
253 if (hFile == INVALID_HANDLE_VALUE) return;
254
255 /* output of startup info (Ansi) */
256 GetStartupInfoA(&siA);
257 childPrintf(hFile,
258 "[StartupInfoA]\ncb=%08ld\nlpDesktop=%s\nlpTitle=%s\n"
259 "dwX=%lu\ndwY=%lu\ndwXSize=%lu\ndwYSize=%lu\n"
260 "dwXCountChars=%lu\ndwYCountChars=%lu\ndwFillAttribute=%lu\n"
261 "dwFlags=%lu\nwShowWindow=%u\n"
262 "hStdInput=%lu\nhStdOutput=%lu\nhStdError=%lu\n\n",
263 siA.cb, encodeA(siA.lpDesktop), encodeA(siA.lpTitle),
264 siA.dwX, siA.dwY, siA.dwXSize, siA.dwYSize,
265 siA.dwXCountChars, siA.dwYCountChars, siA.dwFillAttribute,
266 siA.dwFlags, siA.wShowWindow,
267 (DWORD_PTR)siA.hStdInput, (DWORD_PTR)siA.hStdOutput, (DWORD_PTR)siA.hStdError);
268
269 /* since GetStartupInfoW is only implemented in win2k,
270 * zero out before calling so we can notice the difference
271 */
272 memset(&siW, 0, sizeof(siW));
273 GetStartupInfoW(&siW);
274 childPrintf(hFile,
275 "[StartupInfoW]\ncb=%08ld\nlpDesktop=%s\nlpTitle=%s\n"
276 "dwX=%lu\ndwY=%lu\ndwXSize=%lu\ndwYSize=%lu\n"
277 "dwXCountChars=%lu\ndwYCountChars=%lu\ndwFillAttribute=%lu\n"
278 "dwFlags=%lu\nwShowWindow=%u\n"
279 "hStdInput=%lu\nhStdOutput=%lu\nhStdError=%lu\n\n",
280 siW.cb, encodeW(siW.lpDesktop), encodeW(siW.lpTitle),
281 siW.dwX, siW.dwY, siW.dwXSize, siW.dwYSize,
282 siW.dwXCountChars, siW.dwYCountChars, siW.dwFillAttribute,
283 siW.dwFlags, siW.wShowWindow,
284 (DWORD_PTR)siW.hStdInput, (DWORD_PTR)siW.hStdOutput, (DWORD_PTR)siW.hStdError);
285
286 /* Arguments */
287 childPrintf(hFile, "[Arguments]\nargcA=%d\n", myARGC);
288 for (i = 0; i < myARGC; i++)
289 {
290 childPrintf(hFile, "argvA%d=%s\n", i, encodeA(myARGV[i]));
291 }
292 childPrintf(hFile, "CommandLineA=%s\n", encodeA(GetCommandLineA()));
293
294 #if 0
295 int argcW;
296 WCHAR** argvW;
297
298 /* this is part of shell32... and should be tested there */
299 argvW = CommandLineToArgvW(GetCommandLineW(), &argcW);
300 for (i = 0; i < argcW; i++)
301 {
302 childPrintf(hFile, "argvW%d=%s\n", i, encodeW(argvW[i]));
303 }
304 #endif
305 childPrintf(hFile, "CommandLineW=%s\n\n", encodeW(GetCommandLineW()));
306
307 /* output of environment (Ansi) */
308 ptrA = GetEnvironmentStringsA();
309 if (ptrA)
310 {
311 char env_var[MAX_LISTED_ENV_VAR];
312
313 childPrintf(hFile, "[EnvironmentA]\n");
314 i = 0;
315 while (*ptrA)
316 {
317 lstrcpynA(env_var, ptrA, MAX_LISTED_ENV_VAR);
318 childPrintf(hFile, "env%d=%s\n", i, encodeA(env_var));
319 i++;
320 ptrA += strlen(ptrA) + 1;
321 }
322 childPrintf(hFile, "len=%d\n\n", i);
323 }
324
325 /* output of environment (Unicode) */
326 ptrW = GetEnvironmentStringsW();
327 if (ptrW)
328 {
329 WCHAR env_var[MAX_LISTED_ENV_VAR];
330
331 childPrintf(hFile, "[EnvironmentW]\n");
332 i = 0;
333 while (*ptrW)
334 {
335 lstrcpynW(env_var, ptrW, MAX_LISTED_ENV_VAR - 1);
336 env_var[MAX_LISTED_ENV_VAR - 1] = '\0';
337 childPrintf(hFile, "env%d=%s\n", i, encodeW(env_var));
338 i++;
339 ptrW += lstrlenW(ptrW) + 1;
340 }
341 childPrintf(hFile, "len=%d\n\n", i);
342 }
343
344 childPrintf(hFile, "[Misc]\n");
345 if (GetCurrentDirectoryA(sizeof(bufA), bufA))
346 childPrintf(hFile, "CurrDirA=%s\n", encodeA(bufA));
347 if (GetCurrentDirectoryW(sizeof(bufW) / sizeof(bufW[0]), bufW))
348 childPrintf(hFile, "CurrDirW=%s\n", encodeW(bufW));
349 childPrintf(hFile, "\n");
350
351 if (option && strcmp(option, "console") == 0)
352 {
353 CONSOLE_SCREEN_BUFFER_INFO sbi;
354 HANDLE hConIn = GetStdHandle(STD_INPUT_HANDLE);
355 HANDLE hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
356 DWORD modeIn, modeOut;
357
358 childPrintf(hFile, "[Console]\n");
359 if (GetConsoleScreenBufferInfo(hConOut, &sbi))
360 {
361 childPrintf(hFile, "SizeX=%d\nSizeY=%d\nCursorX=%d\nCursorY=%d\nAttributes=%d\n",
362 sbi.dwSize.X, sbi.dwSize.Y, sbi.dwCursorPosition.X, sbi.dwCursorPosition.Y, sbi.wAttributes);
363 childPrintf(hFile, "winLeft=%d\nwinTop=%d\nwinRight=%d\nwinBottom=%d\n",
364 sbi.srWindow.Left, sbi.srWindow.Top, sbi.srWindow.Right, sbi.srWindow.Bottom);
365 childPrintf(hFile, "maxWinWidth=%d\nmaxWinHeight=%d\n",
366 sbi.dwMaximumWindowSize.X, sbi.dwMaximumWindowSize.Y);
367 }
368 childPrintf(hFile, "InputCP=%d\nOutputCP=%d\n",
369 GetConsoleCP(), GetConsoleOutputCP());
370 if (GetConsoleMode(hConIn, &modeIn))
371 childPrintf(hFile, "InputMode=%ld\n", modeIn);
372 if (GetConsoleMode(hConOut, &modeOut))
373 childPrintf(hFile, "OutputMode=%ld\n", modeOut);
374
375 /* now that we have written all relevant information, let's change it */
376 SetLastError(0xdeadbeef);
377 ret = SetConsoleCP(1252);
378 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
379 {
380 win_skip("Setting the codepage is not implemented\n");
381 }
382 else
383 {
384 ok(ret, "Setting CP\n");
385 ok(SetConsoleOutputCP(1252), "Setting SB CP\n");
386 }
387
388 ret = SetConsoleMode(hConIn, modeIn ^ 1);
389 ok( ret, "Setting mode (%d)\n", GetLastError());
390 ret = SetConsoleMode(hConOut, modeOut ^ 1);
391 ok( ret, "Setting mode (%d)\n", GetLastError());
392 sbi.dwCursorPosition.X ^= 1;
393 sbi.dwCursorPosition.Y ^= 1;
394 ret = SetConsoleCursorPosition(hConOut, sbi.dwCursorPosition);
395 ok( ret, "Setting cursor position (%d)\n", GetLastError());
396 }
397 if (option && strcmp(option, "stdhandle") == 0)
398 {
399 HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
400 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
401
402 if (hStdIn != INVALID_HANDLE_VALUE || hStdOut != INVALID_HANDLE_VALUE)
403 {
404 char buf[1024];
405 DWORD r, w;
406
407 ok(ReadFile(hStdIn, buf, sizeof(buf), &r, NULL) && r > 0, "Reading message from input pipe\n");
408 childPrintf(hFile, "[StdHandle]\nmsg=%s\n\n", encodeA(buf));
409 ok(WriteFile(hStdOut, buf, r, &w, NULL) && w == r, "Writing message to output pipe\n");
410 }
411 }
412
413 if (option && strcmp(option, "exit_code") == 0)
414 {
415 childPrintf(hFile, "[ExitCode]\nvalue=%d\n\n", 123);
416 CloseHandle(hFile);
417 ExitProcess(123);
418 }
419
420 CloseHandle(hFile);
421 }
422
423 static char* getChildString(const char* sect, const char* key)
424 {
425 char buf[1024+4*MAX_LISTED_ENV_VAR];
426 char* ret;
427
428 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), resfile);
429 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
430 assert(!(strlen(buf) & 1));
431 ret = decodeA(buf);
432 return ret;
433 }
434
435 static WCHAR* getChildStringW(const char* sect, const char* key)
436 {
437 char buf[1024+4*MAX_LISTED_ENV_VAR];
438 WCHAR* ret;
439
440 GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), resfile);
441 if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
442 assert(!(strlen(buf) & 1));
443 ret = decodeW(buf);
444 return ret;
445 }
446
447 /* FIXME: this may be moved to the wtmain.c file, because it may be needed by
448 * others... (windows uses stricmp while Un*x uses strcasecmp...)
449 */
450 static int wtstrcasecmp(const char* p1, const char* p2)
451 {
452 char c1, c2;
453
454 c1 = c2 = '@';
455 while (c1 == c2 && c1)
456 {
457 c1 = *p1++; c2 = *p2++;
458 if (c1 != c2)
459 {
460 c1 = toupper(c1); c2 = toupper(c2);
461 }
462 }
463 return c1 - c2;
464 }
465
466 static int strCmp(const char* s1, const char* s2, BOOL sensitive)
467 {
468 if (!s1 && !s2) return 0;
469 if (!s2) return -1;
470 if (!s1) return 1;
471 return (sensitive) ? strcmp(s1, s2) : wtstrcasecmp(s1, s2);
472 }
473
474 static void ok_child_string( int line, const char *sect, const char *key,
475 const char *expect, int sensitive )
476 {
477 char* result = getChildString( sect, key );
478 ok_(__FILE__, line)( strCmp(result, expect, sensitive) == 0, "%s:%s expected '%s', got '%s'\n",
479 sect, key, expect ? expect : "(null)", result );
480 }
481
482 static void ok_child_stringWA( int line, const char *sect, const char *key,
483 const char *expect, int sensitive )
484 {
485 WCHAR* expectW;
486 CHAR* resultA;
487 DWORD len;
488 WCHAR* result = getChildStringW( sect, key );
489
490 len = MultiByteToWideChar( CP_ACP, 0, expect, -1, NULL, 0);
491 expectW = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
492 MultiByteToWideChar( CP_ACP, 0, expect, -1, expectW, len);
493
494 len = WideCharToMultiByte( CP_ACP, 0, result, -1, NULL, 0, NULL, NULL);
495 resultA = HeapAlloc(GetProcessHeap(),0,len*sizeof(CHAR));
496 WideCharToMultiByte( CP_ACP, 0, result, -1, resultA, len, NULL, NULL);
497
498 if (sensitive)
499 ok_(__FILE__, line)( lstrcmpW(result, expectW) == 0, "%s:%s expected '%s', got '%s'\n",
500 sect, key, expect ? expect : "(null)", resultA );
501 else
502 ok_(__FILE__, line)( lstrcmpiW(result, expectW) == 0, "%s:%s expected '%s', got '%s'\n",
503 sect, key, expect ? expect : "(null)", resultA );
504 HeapFree(GetProcessHeap(),0,expectW);
505 HeapFree(GetProcessHeap(),0,resultA);
506 }
507
508 #define okChildString(sect, key, expect) ok_child_string(__LINE__, (sect), (key), (expect), 1 )
509 #define okChildIString(sect, key, expect) ok_child_string(__LINE__, (sect), (key), (expect), 0 )
510 #define okChildStringWA(sect, key, expect) ok_child_stringWA(__LINE__, (sect), (key), (expect), 1 )
511
512 /* using !expect ensures that the test will fail if the sect/key isn't present
513 * in result file
514 */
515 #define okChildInt(sect, key, expect) \
516 do { \
517 UINT result = GetPrivateProfileIntA((sect), (key), !(expect), resfile); \
518 ok(result == expect, "%s:%s expected %u, but got %u\n", (sect), (key), (UINT)(expect), result); \
519 } while (0)
520
521 static void test_Startup(void)
522 {
523 char buffer[MAX_PATH];
524 PROCESS_INFORMATION info;
525 STARTUPINFOA startup,si;
526 static CHAR title[] = "I'm the title string",
527 desktop[] = "winsta0\\default",
528 empty[] = "";
529
530 /* let's start simplistic */
531 memset(&startup, 0, sizeof(startup));
532 startup.cb = sizeof(startup);
533 startup.dwFlags = STARTF_USESHOWWINDOW;
534 startup.wShowWindow = SW_SHOWNORMAL;
535
536 get_file_name(resfile);
537 sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
538 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
539 /* wait for child to terminate */
540 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
541 /* child process has changed result file, so let profile functions know about it */
542 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
543
544 GetStartupInfoA(&si);
545 okChildInt("StartupInfoA", "cb", startup.cb);
546 okChildString("StartupInfoA", "lpDesktop", si.lpDesktop);
547 okChildInt("StartupInfoA", "dwX", startup.dwX);
548 okChildInt("StartupInfoA", "dwY", startup.dwY);
549 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
550 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
551 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
552 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
553 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
554 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
555 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
556 release_memory();
557 assert(DeleteFileA(resfile) != 0);
558
559 /* not so simplistic now */
560 memset(&startup, 0, sizeof(startup));
561 startup.cb = sizeof(startup);
562 startup.dwFlags = STARTF_USESHOWWINDOW;
563 startup.wShowWindow = SW_SHOWNORMAL;
564 startup.lpTitle = title;
565 startup.lpDesktop = desktop;
566 startup.dwXCountChars = 0x12121212;
567 startup.dwYCountChars = 0x23232323;
568 startup.dwX = 0x34343434;
569 startup.dwY = 0x45454545;
570 startup.dwXSize = 0x56565656;
571 startup.dwYSize = 0x67676767;
572 startup.dwFillAttribute = 0xA55A;
573
574 get_file_name(resfile);
575 sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
576 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
577 /* wait for child to terminate */
578 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
579 /* child process has changed result file, so let profile functions know about it */
580 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
581
582 okChildInt("StartupInfoA", "cb", startup.cb);
583 okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
584 okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
585 okChildInt("StartupInfoA", "dwX", startup.dwX);
586 okChildInt("StartupInfoA", "dwY", startup.dwY);
587 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
588 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
589 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
590 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
591 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
592 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
593 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
594 release_memory();
595 assert(DeleteFileA(resfile) != 0);
596
597 /* not so simplistic now */
598 memset(&startup, 0, sizeof(startup));
599 startup.cb = sizeof(startup);
600 startup.dwFlags = STARTF_USESHOWWINDOW;
601 startup.wShowWindow = SW_SHOWNORMAL;
602 startup.lpTitle = title;
603 startup.lpDesktop = NULL;
604 startup.dwXCountChars = 0x12121212;
605 startup.dwYCountChars = 0x23232323;
606 startup.dwX = 0x34343434;
607 startup.dwY = 0x45454545;
608 startup.dwXSize = 0x56565656;
609 startup.dwYSize = 0x67676767;
610 startup.dwFillAttribute = 0xA55A;
611
612 get_file_name(resfile);
613 sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
614 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
615 /* wait for child to terminate */
616 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
617 /* child process has changed result file, so let profile functions know about it */
618 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
619
620 okChildInt("StartupInfoA", "cb", startup.cb);
621 okChildString("StartupInfoA", "lpDesktop", si.lpDesktop);
622 okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
623 okChildInt("StartupInfoA", "dwX", startup.dwX);
624 okChildInt("StartupInfoA", "dwY", startup.dwY);
625 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
626 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
627 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
628 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
629 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
630 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
631 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
632 release_memory();
633 assert(DeleteFileA(resfile) != 0);
634
635 /* not so simplistic now */
636 memset(&startup, 0, sizeof(startup));
637 startup.cb = sizeof(startup);
638 startup.dwFlags = STARTF_USESHOWWINDOW;
639 startup.wShowWindow = SW_SHOWNORMAL;
640 startup.lpTitle = title;
641 startup.lpDesktop = empty;
642 startup.dwXCountChars = 0x12121212;
643 startup.dwYCountChars = 0x23232323;
644 startup.dwX = 0x34343434;
645 startup.dwY = 0x45454545;
646 startup.dwXSize = 0x56565656;
647 startup.dwYSize = 0x67676767;
648 startup.dwFillAttribute = 0xA55A;
649
650 get_file_name(resfile);
651 sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
652 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
653 /* wait for child to terminate */
654 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
655 /* child process has changed result file, so let profile functions know about it */
656 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
657
658 okChildInt("StartupInfoA", "cb", startup.cb);
659 todo_wine okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
660 okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
661 okChildInt("StartupInfoA", "dwX", startup.dwX);
662 okChildInt("StartupInfoA", "dwY", startup.dwY);
663 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
664 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
665 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
666 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
667 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
668 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
669 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
670 release_memory();
671 assert(DeleteFileA(resfile) != 0);
672
673 /* not so simplistic now */
674 memset(&startup, 0, sizeof(startup));
675 startup.cb = sizeof(startup);
676 startup.dwFlags = STARTF_USESHOWWINDOW;
677 startup.wShowWindow = SW_SHOWNORMAL;
678 startup.lpTitle = NULL;
679 startup.lpDesktop = desktop;
680 startup.dwXCountChars = 0x12121212;
681 startup.dwYCountChars = 0x23232323;
682 startup.dwX = 0x34343434;
683 startup.dwY = 0x45454545;
684 startup.dwXSize = 0x56565656;
685 startup.dwYSize = 0x67676767;
686 startup.dwFillAttribute = 0xA55A;
687
688 get_file_name(resfile);
689 sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
690 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
691 /* wait for child to terminate */
692 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
693 /* child process has changed result file, so let profile functions know about it */
694 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
695
696 okChildInt("StartupInfoA", "cb", startup.cb);
697 okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
698 ok (startup.lpTitle == NULL || !strcmp(startup.lpTitle, selfname),
699 "StartupInfoA:lpTitle expected '%s' or null, got '%s'\n", selfname, startup.lpTitle);
700 okChildInt("StartupInfoA", "dwX", startup.dwX);
701 okChildInt("StartupInfoA", "dwY", startup.dwY);
702 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
703 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
704 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
705 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
706 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
707 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
708 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
709 release_memory();
710 assert(DeleteFileA(resfile) != 0);
711
712 /* not so simplistic now */
713 memset(&startup, 0, sizeof(startup));
714 startup.cb = sizeof(startup);
715 startup.dwFlags = STARTF_USESHOWWINDOW;
716 startup.wShowWindow = SW_SHOWNORMAL;
717 startup.lpTitle = empty;
718 startup.lpDesktop = desktop;
719 startup.dwXCountChars = 0x12121212;
720 startup.dwYCountChars = 0x23232323;
721 startup.dwX = 0x34343434;
722 startup.dwY = 0x45454545;
723 startup.dwXSize = 0x56565656;
724 startup.dwYSize = 0x67676767;
725 startup.dwFillAttribute = 0xA55A;
726
727 get_file_name(resfile);
728 sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
729 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
730 /* wait for child to terminate */
731 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
732 /* child process has changed result file, so let profile functions know about it */
733 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
734
735 okChildInt("StartupInfoA", "cb", startup.cb);
736 okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
737 todo_wine okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
738 okChildInt("StartupInfoA", "dwX", startup.dwX);
739 okChildInt("StartupInfoA", "dwY", startup.dwY);
740 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
741 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
742 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
743 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
744 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
745 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
746 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
747 release_memory();
748 assert(DeleteFileA(resfile) != 0);
749
750 /* not so simplistic now */
751 memset(&startup, 0, sizeof(startup));
752 startup.cb = sizeof(startup);
753 startup.dwFlags = STARTF_USESHOWWINDOW;
754 startup.wShowWindow = SW_SHOWNORMAL;
755 startup.lpTitle = empty;
756 startup.lpDesktop = empty;
757 startup.dwXCountChars = 0x12121212;
758 startup.dwYCountChars = 0x23232323;
759 startup.dwX = 0x34343434;
760 startup.dwY = 0x45454545;
761 startup.dwXSize = 0x56565656;
762 startup.dwYSize = 0x67676767;
763 startup.dwFillAttribute = 0xA55A;
764
765 get_file_name(resfile);
766 sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
767 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
768 /* wait for child to terminate */
769 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
770 /* child process has changed result file, so let profile functions know about it */
771 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
772
773 okChildInt("StartupInfoA", "cb", startup.cb);
774 todo_wine okChildString("StartupInfoA", "lpDesktop", startup.lpDesktop);
775 todo_wine okChildString("StartupInfoA", "lpTitle", startup.lpTitle);
776 okChildInt("StartupInfoA", "dwX", startup.dwX);
777 okChildInt("StartupInfoA", "dwY", startup.dwY);
778 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
779 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
780 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
781 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
782 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
783 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
784 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
785 release_memory();
786 assert(DeleteFileA(resfile) != 0);
787
788 /* TODO: test for A/W and W/A and W/W */
789 }
790
791 static void test_CommandLine(void)
792 {
793 char buffer[MAX_PATH], fullpath[MAX_PATH], *lpFilePart, *p;
794 char buffer2[MAX_PATH];
795 PROCESS_INFORMATION info;
796 STARTUPINFOA startup;
797 DWORD len;
798 BOOL ret;
799
800 memset(&startup, 0, sizeof(startup));
801 startup.cb = sizeof(startup);
802 startup.dwFlags = STARTF_USESHOWWINDOW;
803 startup.wShowWindow = SW_SHOWNORMAL;
804
805 /* the basics */
806 get_file_name(resfile);
807 sprintf(buffer, "%s tests/process.c %s \"C:\\Program Files\\my nice app.exe\"", selfname, resfile);
808 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
809 /* wait for child to terminate */
810 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
811 /* child process has changed result file, so let profile functions know about it */
812 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
813
814 okChildInt("Arguments", "argcA", 4);
815 okChildString("Arguments", "argvA3", "C:\\Program Files\\my nice app.exe");
816 okChildString("Arguments", "argvA4", NULL);
817 okChildString("Arguments", "CommandLineA", buffer);
818 release_memory();
819 assert(DeleteFileA(resfile) != 0);
820
821 memset(&startup, 0, sizeof(startup));
822 startup.cb = sizeof(startup);
823 startup.dwFlags = STARTF_USESHOWWINDOW;
824 startup.wShowWindow = SW_SHOWNORMAL;
825
826 /* from Frangois */
827 get_file_name(resfile);
828 sprintf(buffer, "%s tests/process.c %s \"a\\\"b\\\\\" c\\\" d", selfname, resfile);
829 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
830 /* wait for child to terminate */
831 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
832 /* child process has changed result file, so let profile functions know about it */
833 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
834
835 okChildInt("Arguments", "argcA", 6);
836 okChildString("Arguments", "argvA3", "a\"b\\");
837 okChildString("Arguments", "argvA4", "c\"");
838 okChildString("Arguments", "argvA5", "d");
839 okChildString("Arguments", "argvA6", NULL);
840 okChildString("Arguments", "CommandLineA", buffer);
841 release_memory();
842 assert(DeleteFileA(resfile) != 0);
843
844 /* Test for Bug1330 to show that XP doesn't change '/' to '\\' in argv[0]*/
845 get_file_name(resfile);
846 /* Use exename to avoid buffer containing things like 'C:' */
847 sprintf(buffer, "./%s tests/process.c %s \"a\\\"b\\\\\" c\\\" d", exename, resfile);
848 SetLastError(0xdeadbeef);
849 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
850 ok(ret, "CreateProcess (%s) failed : %d\n", buffer, GetLastError());
851 /* wait for child to terminate */
852 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
853 /* child process has changed result file, so let profile functions know about it */
854 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
855 sprintf(buffer, "./%s", exename);
856 okChildString("Arguments", "argvA0", buffer);
857 release_memory();
858 assert(DeleteFileA(resfile) != 0);
859
860 get_file_name(resfile);
861 /* Use exename to avoid buffer containing things like 'C:' */
862 sprintf(buffer, ".\\%s tests/process.c %s \"a\\\"b\\\\\" c\\\" d", exename, resfile);
863 SetLastError(0xdeadbeef);
864 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
865 ok(ret, "CreateProcess (%s) failed : %d\n", buffer, GetLastError());
866 /* wait for child to terminate */
867 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
868 /* child process has changed result file, so let profile functions know about it */
869 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
870 sprintf(buffer, ".\\%s", exename);
871 okChildString("Arguments", "argvA0", buffer);
872 release_memory();
873 assert(DeleteFileA(resfile) != 0);
874
875 get_file_name(resfile);
876 len = GetFullPathNameA(selfname, MAX_PATH, fullpath, &lpFilePart);
877 assert ( lpFilePart != 0);
878 *(lpFilePart -1 ) = 0;
879 p = strrchr(fullpath, '\\');
880 /* Use exename to avoid buffer containing things like 'C:' */
881 if (p) sprintf(buffer, "..%s/%s tests/process.c %s \"a\\\"b\\\\\" c\\\" d", p, exename, resfile);
882 else sprintf(buffer, "./%s tests/process.c %s \"a\\\"b\\\\\" c\\\" d", exename, resfile);
883 SetLastError(0xdeadbeef);
884 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
885 ok(ret, "CreateProcess (%s) failed : %d\n", buffer, GetLastError());
886 /* wait for child to terminate */
887 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
888 /* child process has changed result file, so let profile functions know about it */
889 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
890 if (p) sprintf(buffer, "..%s/%s", p, exename);
891 else sprintf(buffer, "./%s", exename);
892 okChildString("Arguments", "argvA0", buffer);
893 release_memory();
894 assert(DeleteFileA(resfile) != 0);
895
896 /* Using AppName */
897 get_file_name(resfile);
898 len = GetFullPathNameA(selfname, MAX_PATH, fullpath, &lpFilePart);
899 assert ( lpFilePart != 0);
900 *(lpFilePart -1 ) = 0;
901 p = strrchr(fullpath, '\\');
902 /* Use exename to avoid buffer containing things like 'C:' */
903 if (p) sprintf(buffer, "..%s/%s", p, exename);
904 else sprintf(buffer, "./%s", exename);
905 sprintf(buffer2, "dummy tests/process.c %s \"a\\\"b\\\\\" c\\\" d", resfile);
906 SetLastError(0xdeadbeef);
907 ret = CreateProcessA(buffer, buffer2, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
908 ok(ret, "CreateProcess (%s) failed : %d\n", buffer, GetLastError());
909 /* wait for child to terminate */
910 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
911 /* child process has changed result file, so let profile functions know about it */
912 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
913 sprintf(buffer, "tests/process.c %s", resfile);
914 okChildString("Arguments", "argvA0", "dummy");
915 okChildString("Arguments", "CommandLineA", buffer2);
916 okChildStringWA("Arguments", "CommandLineW", buffer2);
917 release_memory();
918 assert(DeleteFileA(resfile) != 0);
919
920 if (0) /* Test crashes on NT-based Windows. */
921 {
922 /* Test NULL application name and command line parameters. */
923 SetLastError(0xdeadbeef);
924 ret = CreateProcessA(NULL, NULL, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
925 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
926 ok(GetLastError() == ERROR_INVALID_PARAMETER,
927 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
928 }
929
930 buffer[0] = '\0';
931
932 /* Test empty application name parameter. */
933 SetLastError(0xdeadbeef);
934 ret = CreateProcessA(buffer, NULL, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
935 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
936 ok(GetLastError() == ERROR_PATH_NOT_FOUND ||
937 broken(GetLastError() == ERROR_FILE_NOT_FOUND) /* Win9x/WinME */ ||
938 broken(GetLastError() == ERROR_ACCESS_DENIED) /* Win98 */,
939 "Expected ERROR_PATH_NOT_FOUND, got %d\n", GetLastError());
940
941 buffer2[0] = '\0';
942
943 /* Test empty application name and command line parameters. */
944 SetLastError(0xdeadbeef);
945 ret = CreateProcessA(buffer, buffer2, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
946 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
947 ok(GetLastError() == ERROR_PATH_NOT_FOUND ||
948 broken(GetLastError() == ERROR_FILE_NOT_FOUND) /* Win9x/WinME */ ||
949 broken(GetLastError() == ERROR_ACCESS_DENIED) /* Win98 */,
950 "Expected ERROR_PATH_NOT_FOUND, got %d\n", GetLastError());
951
952 /* Test empty command line parameter. */
953 SetLastError(0xdeadbeef);
954 ret = CreateProcessA(NULL, buffer2, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
955 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
956 ok(GetLastError() == ERROR_FILE_NOT_FOUND ||
957 GetLastError() == ERROR_PATH_NOT_FOUND /* NT4 */ ||
958 GetLastError() == ERROR_BAD_PATHNAME /* Win98 */ ||
959 GetLastError() == ERROR_INVALID_PARAMETER /* Win7 */,
960 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
961
962 strcpy(buffer, "doesnotexist.exe");
963 strcpy(buffer2, "does not exist.exe");
964
965 /* Test nonexistent application name. */
966 SetLastError(0xdeadbeef);
967 ret = CreateProcessA(buffer, NULL, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
968 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
969 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
970
971 SetLastError(0xdeadbeef);
972 ret = CreateProcessA(buffer2, NULL, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
973 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
974 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
975
976 /* Test nonexistent command line parameter. */
977 SetLastError(0xdeadbeef);
978 ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
979 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
980 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
981
982 SetLastError(0xdeadbeef);
983 ret = CreateProcessA(NULL, buffer2, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
984 ok(!ret, "CreateProcessA unexpectedly succeeded\n");
985 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
986 }
987
988 static void test_Directory(void)
989 {
990 char buffer[MAX_PATH];
991 PROCESS_INFORMATION info;
992 STARTUPINFOA startup;
993 char windir[MAX_PATH];
994 static CHAR cmdline[] = "winver.exe";
995
996 memset(&startup, 0, sizeof(startup));
997 startup.cb = sizeof(startup);
998 startup.dwFlags = STARTF_USESHOWWINDOW;
999 startup.wShowWindow = SW_SHOWNORMAL;
1000
1001 /* the basics */
1002 get_file_name(resfile);
1003 sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
1004 GetWindowsDirectoryA( windir, sizeof(windir) );
1005 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, windir, &startup, &info), "CreateProcess\n");
1006 /* wait for child to terminate */
1007 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1008 /* child process has changed result file, so let profile functions know about it */
1009 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1010
1011 okChildIString("Misc", "CurrDirA", windir);
1012 release_memory();
1013 assert(DeleteFileA(resfile) != 0);
1014
1015 /* search PATH for the exe if directory is NULL */
1016 ok(CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
1017 ok(TerminateProcess(info.hProcess, 0), "Child process termination\n");
1018
1019 /* if any directory is provided, don't search PATH, error on bad directory */
1020 SetLastError(0xdeadbeef);
1021 memset(&info, 0, sizeof(info));
1022 ok(!CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0L,
1023 NULL, "non\\existent\\directory", &startup, &info), "CreateProcess\n");
1024 ok(GetLastError() == ERROR_DIRECTORY, "Expected ERROR_DIRECTORY, got %d\n", GetLastError());
1025 ok(!TerminateProcess(info.hProcess, 0), "Child process should not exist\n");
1026 }
1027
1028 static BOOL is_str_env_drive_dir(const char* str)
1029 {
1030 return str[0] == '=' && str[1] >= 'A' && str[1] <= 'Z' && str[2] == ':' &&
1031 str[3] == '=' && str[4] == str[1];
1032 }
1033
1034 /* compared expected child's environment (in gesA) from actual
1035 * environment our child got
1036 */
1037 static void cmpEnvironment(const char* gesA)
1038 {
1039 int i, clen;
1040 const char* ptrA;
1041 char* res;
1042 char key[32];
1043 BOOL found;
1044
1045 clen = GetPrivateProfileIntA("EnvironmentA", "len", 0, resfile);
1046
1047 /* now look each parent env in child */
1048 if ((ptrA = gesA) != NULL)
1049 {
1050 while (*ptrA)
1051 {
1052 for (i = 0; i < clen; i++)
1053 {
1054 sprintf(key, "env%d", i);
1055 res = getChildString("EnvironmentA", key);
1056 if (strncmp(ptrA, res, MAX_LISTED_ENV_VAR - 1) == 0)
1057 break;
1058 }
1059 found = i < clen;
1060 ok(found, "Parent-env string %s isn't in child process\n", ptrA);
1061
1062 ptrA += strlen(ptrA) + 1;
1063 release_memory();
1064 }
1065 }
1066 /* and each child env in parent */
1067 for (i = 0; i < clen; i++)
1068 {
1069 sprintf(key, "env%d", i);
1070 res = getChildString("EnvironmentA", key);
1071 if ((ptrA = gesA) != NULL)
1072 {
1073 while (*ptrA)
1074 {
1075 if (strncmp(res, ptrA, MAX_LISTED_ENV_VAR - 1) == 0)
1076 break;
1077 ptrA += strlen(ptrA) + 1;
1078 }
1079 if (!*ptrA) ptrA = NULL;
1080 }
1081
1082 if (!is_str_env_drive_dir(res))
1083 {
1084 found = ptrA != NULL;
1085 ok(found, "Child-env string %s isn't in parent process\n", res);
1086 }
1087 /* else => should also test we get the right per drive default directory here... */
1088 }
1089 }
1090
1091 static void test_Environment(void)
1092 {
1093 char buffer[MAX_PATH];
1094 PROCESS_INFORMATION info;
1095 STARTUPINFOA startup;
1096 char* child_env;
1097 int child_env_len;
1098 char* ptr;
1099 char* env;
1100 int slen;
1101
1102 memset(&startup, 0, sizeof(startup));
1103 startup.cb = sizeof(startup);
1104 startup.dwFlags = STARTF_USESHOWWINDOW;
1105 startup.wShowWindow = SW_SHOWNORMAL;
1106
1107 /* the basics */
1108 get_file_name(resfile);
1109 sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
1110 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
1111 /* wait for child to terminate */
1112 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1113 /* child process has changed result file, so let profile functions know about it */
1114 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1115
1116 cmpEnvironment(GetEnvironmentStringsA());
1117 release_memory();
1118 assert(DeleteFileA(resfile) != 0);
1119
1120 memset(&startup, 0, sizeof(startup));
1121 startup.cb = sizeof(startup);
1122 startup.dwFlags = STARTF_USESHOWWINDOW;
1123 startup.wShowWindow = SW_SHOWNORMAL;
1124
1125 /* the basics */
1126 get_file_name(resfile);
1127 sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
1128
1129 child_env_len = 0;
1130 ptr = GetEnvironmentStringsA();
1131 while(*ptr)
1132 {
1133 slen = strlen(ptr)+1;
1134 child_env_len += slen;
1135 ptr += slen;
1136 }
1137 /* Add space for additional environment variables */
1138 child_env_len += 256;
1139 child_env = HeapAlloc(GetProcessHeap(), 0, child_env_len);
1140
1141 ptr = child_env;
1142 sprintf(ptr, "=%c:=%s", 'C', "C:\\FOO\\BAR");
1143 ptr += strlen(ptr) + 1;
1144 strcpy(ptr, "PATH=C:\\WINDOWS;C:\\WINDOWS\\SYSTEM;C:\\MY\\OWN\\DIR");
1145 ptr += strlen(ptr) + 1;
1146 strcpy(ptr, "FOO=BAR");
1147 ptr += strlen(ptr) + 1;
1148 strcpy(ptr, "BAR=FOOBAR");
1149 ptr += strlen(ptr) + 1;
1150 /* copy all existing variables except:
1151 * - WINELOADER
1152 * - PATH (already set above)
1153 * - the directory definitions (=[A-Z]:=)
1154 */
1155 for (env = GetEnvironmentStringsA(); *env; env += strlen(env) + 1)
1156 {
1157 if (strncmp(env, "PATH=", 5) != 0 &&
1158 strncmp(env, "WINELOADER=", 11) != 0 &&
1159 !is_str_env_drive_dir(env))
1160 {
1161 strcpy(ptr, env);
1162 ptr += strlen(ptr) + 1;
1163 }
1164 }
1165 *ptr = '\0';
1166 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, child_env, NULL, &startup, &info), "CreateProcess\n");
1167 /* wait for child to terminate */
1168 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1169 /* child process has changed result file, so let profile functions know about it */
1170 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1171
1172 cmpEnvironment(child_env);
1173
1174 HeapFree(GetProcessHeap(), 0, child_env);
1175 release_memory();
1176 assert(DeleteFileA(resfile) != 0);
1177 }
1178
1179 static void test_SuspendFlag(void)
1180 {
1181 char buffer[MAX_PATH];
1182 PROCESS_INFORMATION info;
1183 STARTUPINFOA startup, us;
1184 DWORD exit_status;
1185
1186 /* let's start simplistic */
1187 memset(&startup, 0, sizeof(startup));
1188 startup.cb = sizeof(startup);
1189 startup.dwFlags = STARTF_USESHOWWINDOW;
1190 startup.wShowWindow = SW_SHOWNORMAL;
1191
1192 get_file_name(resfile);
1193 sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
1194 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &startup, &info), "CreateProcess\n");
1195
1196 ok(GetExitCodeThread(info.hThread, &exit_status) && exit_status == STILL_ACTIVE, "thread still running\n");
1197 Sleep(8000);
1198 ok(GetExitCodeThread(info.hThread, &exit_status) && exit_status == STILL_ACTIVE, "thread still running\n");
1199 ok(ResumeThread(info.hThread) == 1, "Resuming thread\n");
1200
1201 /* wait for child to terminate */
1202 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1203 /* child process has changed result file, so let profile functions know about it */
1204 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1205
1206 GetStartupInfoA(&us);
1207
1208 okChildInt("StartupInfoA", "cb", startup.cb);
1209 okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1210 ok (startup.lpTitle == NULL || !strcmp(startup.lpTitle, selfname),
1211 "StartupInfoA:lpTitle expected '%s' or null, got '%s'\n", selfname, startup.lpTitle);
1212 okChildInt("StartupInfoA", "dwX", startup.dwX);
1213 okChildInt("StartupInfoA", "dwY", startup.dwY);
1214 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1215 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1216 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1217 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1218 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1219 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1220 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1221 release_memory();
1222 assert(DeleteFileA(resfile) != 0);
1223 }
1224
1225 static void test_DebuggingFlag(void)
1226 {
1227 char buffer[MAX_PATH];
1228 void *processbase = NULL;
1229 PROCESS_INFORMATION info;
1230 STARTUPINFOA startup, us;
1231 DEBUG_EVENT de;
1232 unsigned dbg = 0;
1233
1234 /* let's start simplistic */
1235 memset(&startup, 0, sizeof(startup));
1236 startup.cb = sizeof(startup);
1237 startup.dwFlags = STARTF_USESHOWWINDOW;
1238 startup.wShowWindow = SW_SHOWNORMAL;
1239
1240 get_file_name(resfile);
1241 sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
1242 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &startup, &info), "CreateProcess\n");
1243
1244 /* get all startup events up to the entry point break exception */
1245 do
1246 {
1247 ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
1248 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
1249 if (!dbg)
1250 {
1251 ok(de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT,
1252 "first event: %d\n", de.dwDebugEventCode);
1253 processbase = de.u.CreateProcessInfo.lpBaseOfImage;
1254 }
1255 if (de.dwDebugEventCode != EXCEPTION_DEBUG_EVENT) dbg++;
1256 ok(de.dwDebugEventCode != LOAD_DLL_DEBUG_EVENT ||
1257 de.u.LoadDll.lpBaseOfDll != processbase, "got LOAD_DLL for main module\n");
1258 } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
1259
1260 ok(dbg, "I have seen a debug event\n");
1261 /* wait for child to terminate */
1262 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1263 /* child process has changed result file, so let profile functions know about it */
1264 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1265
1266 GetStartupInfoA(&us);
1267
1268 okChildInt("StartupInfoA", "cb", startup.cb);
1269 okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1270 ok (startup.lpTitle == NULL || !strcmp(startup.lpTitle, selfname),
1271 "StartupInfoA:lpTitle expected '%s' or null, got '%s'\n", selfname, startup.lpTitle);
1272 okChildInt("StartupInfoA", "dwX", startup.dwX);
1273 okChildInt("StartupInfoA", "dwY", startup.dwY);
1274 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1275 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1276 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1277 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1278 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1279 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1280 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1281 release_memory();
1282 assert(DeleteFileA(resfile) != 0);
1283 }
1284
1285 static BOOL is_console(HANDLE h)
1286 {
1287 return h != INVALID_HANDLE_VALUE && ((ULONG_PTR)h & 3) == 3;
1288 }
1289
1290 static void test_Console(void)
1291 {
1292 char buffer[MAX_PATH];
1293 PROCESS_INFORMATION info;
1294 STARTUPINFOA startup, us;
1295 SECURITY_ATTRIBUTES sa;
1296 CONSOLE_SCREEN_BUFFER_INFO sbi, sbiC;
1297 DWORD modeIn, modeOut, modeInC, modeOutC;
1298 DWORD cpIn, cpOut, cpInC, cpOutC;
1299 DWORD w;
1300 HANDLE hChildIn, hChildInInh, hChildOut, hChildOutInh, hParentIn, hParentOut;
1301 const char* msg = "This is a std-handle inheritance test.";
1302 unsigned msg_len;
1303 BOOL run_tests = TRUE;
1304
1305 memset(&startup, 0, sizeof(startup));
1306 startup.cb = sizeof(startup);
1307 startup.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
1308 startup.wShowWindow = SW_SHOWNORMAL;
1309
1310 sa.nLength = sizeof(sa);
1311 sa.lpSecurityDescriptor = NULL;
1312 sa.bInheritHandle = TRUE;
1313
1314 startup.hStdInput = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1315 startup.hStdOutput = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1316
1317 /* first, we need to be sure we're attached to a console */
1318 if (!is_console(startup.hStdInput) || !is_console(startup.hStdOutput))
1319 {
1320 /* we're not attached to a console, let's do it */
1321 AllocConsole();
1322 startup.hStdInput = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1323 startup.hStdOutput = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1324 }
1325 /* now verify everything's ok */
1326 ok(startup.hStdInput != INVALID_HANDLE_VALUE, "Opening ConIn\n");
1327 ok(startup.hStdOutput != INVALID_HANDLE_VALUE, "Opening ConOut\n");
1328 startup.hStdError = startup.hStdOutput;
1329
1330 ok(GetConsoleScreenBufferInfo(startup.hStdOutput, &sbi), "Getting sb info\n");
1331 ok(GetConsoleMode(startup.hStdInput, &modeIn) &&
1332 GetConsoleMode(startup.hStdOutput, &modeOut), "Getting console modes\n");
1333 cpIn = GetConsoleCP();
1334 cpOut = GetConsoleOutputCP();
1335
1336 get_file_name(resfile);
1337 sprintf(buffer, "%s tests/process.c %s console", selfname, resfile);
1338 ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
1339
1340 /* wait for child to terminate */
1341 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1342 /* child process has changed result file, so let profile functions know about it */
1343 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1344
1345 /* now get the modification the child has made, and resets parents expected values */
1346 ok(GetConsoleScreenBufferInfo(startup.hStdOutput, &sbiC), "Getting sb info\n");
1347 ok(GetConsoleMode(startup.hStdInput, &modeInC) &&
1348 GetConsoleMode(startup.hStdOutput, &modeOutC), "Getting console modes\n");
1349
1350 SetConsoleMode(startup.hStdInput, modeIn);
1351 SetConsoleMode(startup.hStdOutput, modeOut);
1352
1353 cpInC = GetConsoleCP();
1354 cpOutC = GetConsoleOutputCP();
1355
1356 /* Try to set invalid CP */
1357 SetLastError(0xdeadbeef);
1358 ok(!SetConsoleCP(0), "Shouldn't succeed\n");
1359 ok(GetLastError()==ERROR_INVALID_PARAMETER ||
1360 broken(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), /* win9x */
1361 "GetLastError: expecting %u got %u\n",
1362 ERROR_INVALID_PARAMETER, GetLastError());
1363 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1364 run_tests = FALSE;
1365
1366
1367 SetLastError(0xdeadbeef);
1368 ok(!SetConsoleOutputCP(0), "Shouldn't succeed\n");
1369 ok(GetLastError()==ERROR_INVALID_PARAMETER ||
1370 broken(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), /* win9x */
1371 "GetLastError: expecting %u got %u\n",
1372 ERROR_INVALID_PARAMETER, GetLastError());
1373
1374 SetConsoleCP(cpIn);
1375 SetConsoleOutputCP(cpOut);
1376
1377 GetStartupInfoA(&us);
1378
1379 okChildInt("StartupInfoA", "cb", startup.cb);
1380 okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1381 ok (startup.lpTitle == NULL || !strcmp(startup.lpTitle, selfname),
1382 "StartupInfoA:lpTitle expected '%s' or null, got '%s'\n", selfname, startup.lpTitle);
1383 okChildInt("StartupInfoA", "dwX", startup.dwX);
1384 okChildInt("StartupInfoA", "dwY", startup.dwY);
1385 okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1386 okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1387 okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1388 okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1389 okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1390 okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1391 okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1392
1393 /* check child correctly inherited the console */
1394 okChildInt("StartupInfoA", "hStdInput", (DWORD_PTR)startup.hStdInput);
1395 okChildInt("StartupInfoA", "hStdOutput", (DWORD_PTR)startup.hStdOutput);
1396 okChildInt("StartupInfoA", "hStdError", (DWORD_PTR)startup.hStdError);
1397 okChildInt("Console", "SizeX", (DWORD)sbi.dwSize.X);
1398 okChildInt("Console", "SizeY", (DWORD)sbi.dwSize.Y);
1399 okChildInt("Console", "CursorX", (DWORD)sbi.dwCursorPosition.X);
1400 okChildInt("Console", "CursorY", (DWORD)sbi.dwCursorPosition.Y);
1401 okChildInt("Console", "Attributes", sbi.wAttributes);
1402 okChildInt("Console", "winLeft", (DWORD)sbi.srWindow.Left);
1403 okChildInt("Console", "winTop", (DWORD)sbi.srWindow.Top);
1404 okChildInt("Console", "winRight", (DWORD)sbi.srWindow.Right);
1405 okChildInt("Console", "winBottom", (DWORD)sbi.srWindow.Bottom);
1406 okChildInt("Console", "maxWinWidth", (DWORD)sbi.dwMaximumWindowSize.X);
1407 okChildInt("Console", "maxWinHeight", (DWORD)sbi.dwMaximumWindowSize.Y);
1408 okChildInt("Console", "InputCP", cpIn);
1409 okChildInt("Console", "OutputCP", cpOut);
1410 okChildInt("Console", "InputMode", modeIn);
1411 okChildInt("Console", "OutputMode", modeOut);
1412
1413 if (run_tests)
1414 {
1415 ok(cpInC == 1252, "Wrong console CP (expected 1252 got %d/%d)\n", cpInC, cpIn);
1416 ok(cpOutC == 1252, "Wrong console-SB CP (expected 1252 got %d/%d)\n", cpOutC, cpOut);
1417 }
1418 else
1419 win_skip("Setting the codepage is not implemented\n");
1420
1421 ok(modeInC == (modeIn ^ 1), "Wrong console mode\n");
1422 ok(modeOutC == (modeOut ^ 1), "Wrong console-SB mode\n");
1423 trace("cursor position(X): %d/%d\n",sbi.dwCursorPosition.X, sbiC.dwCursorPosition.X);
1424 ok(sbiC.dwCursorPosition.Y == (sbi.dwCursorPosition.Y ^ 1), "Wrong cursor position\n");
1425
1426 release_memory();
1427 assert(DeleteFileA(resfile) != 0);
1428
1429 ok(CreatePipe(&hParentIn, &hChildOut, NULL, 0), "Creating parent-input pipe\n");
1430 ok(DuplicateHandle(GetCurrentProcess(), hChildOut, GetCurrentProcess(),
1431 &hChildOutInh, 0, TRUE, DUPLICATE_SAME_ACCESS),
1432 "Duplicating as inheritable child-output pipe\n");
1433 CloseHandle(hChildOut);
1434
1435 ok(CreatePipe(&hChildIn, &hParentOut, NULL, 0), "Creating parent-output pipe\n");
1436 ok(DuplicateHandle(GetCurrentProcess(), hChildIn, GetCurrentProcess(),
1437 &hChildInInh, 0, TRUE, DUPLICATE_SAME_ACCESS),
1438 "Duplicating as inheritable child-input pipe\n");
1439 CloseHandle(hChildIn);
1440
1441 memset(&startup, 0, sizeof(startup));
1442 startup.cb = sizeof(startup);
1443 startup.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
1444 startup.wShowWindow = SW_SHOWNORMAL;
1445 startup.hStdInput = hChildInInh;
1446 startup.hStdOutput = hChildOutInh;
1447 startup.hStdError = hChildOutInh;
1448
1449 get_file_name(resfile);
1450 sprintf(buffer, "%s tests/process.c %s stdhandle", selfname, resfile);
1451 ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &startup, &info), "CreateProcess\n");
1452 ok(CloseHandle(hChildInInh), "Closing handle\n");
1453 ok(CloseHandle(hChildOutInh), "Closing handle\n");
1454
1455 msg_len = strlen(msg) + 1;
1456 ok(WriteFile(hParentOut, msg, msg_len, &w, NULL), "Writing to child\n");
1457 ok(w == msg_len, "Should have written %u bytes, actually wrote %u\n", msg_len, w);
1458 memset(buffer, 0, sizeof(buffer));
1459 ok(ReadFile(hParentIn, buffer, sizeof(buffer), &w, NULL), "Reading from child\n");
1460 ok(strcmp(buffer, msg) == 0, "Should have received '%s'\n", msg);
1461
1462 /* wait for child to terminate */
1463 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1464 /* child process has changed result file, so let profile functions know about it */
1465 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1466
1467 okChildString("StdHandle", "msg", msg);
1468
1469 release_memory();
1470 assert(DeleteFileA(resfile) != 0);
1471 }
1472
1473 static void test_ExitCode(void)
1474 {
1475 char buffer[MAX_PATH];
1476 PROCESS_INFORMATION info;
1477 STARTUPINFOA startup;
1478 DWORD code;
1479
1480 /* let's start simplistic */
1481 memset(&startup, 0, sizeof(startup));
1482 startup.cb = sizeof(startup);
1483 startup.dwFlags = STARTF_USESHOWWINDOW;
1484 startup.wShowWindow = SW_SHOWNORMAL;
1485
1486 get_file_name(resfile);
1487 sprintf(buffer, "%s tests/process.c %s exit_code", selfname, resfile);
1488 ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
1489
1490 /* wait for child to terminate */
1491 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1492 /* child process has changed result file, so let profile functions know about it */
1493 WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1494
1495 ok(GetExitCodeProcess(info.hProcess, &code), "Getting exit code\n");
1496 okChildInt("ExitCode", "value", code);
1497
1498 release_memory();
1499 assert(DeleteFileA(resfile) != 0);
1500 }
1501
1502 static void test_OpenProcess(void)
1503 {
1504 HANDLE hproc;
1505 void *addr1;
1506 MEMORY_BASIC_INFORMATION info;
1507 SIZE_T dummy, read_bytes;
1508
1509 /* not exported in all windows versions */
1510 if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
1511 win_skip("VirtualAllocEx not found\n");
1512 return;
1513 }
1514
1515 /* without PROCESS_VM_OPERATION */
1516 hproc = OpenProcess(PROCESS_ALL_ACCESS & ~PROCESS_VM_OPERATION, FALSE, GetCurrentProcessId());
1517 ok(hproc != NULL, "OpenProcess error %d\n", GetLastError());
1518
1519 SetLastError(0xdeadbeef);
1520 addr1 = pVirtualAllocEx(hproc, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
1521 ok(!addr1, "VirtualAllocEx should fail\n");
1522 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1523 { /* Win9x */
1524 CloseHandle(hproc);
1525 win_skip("VirtualAllocEx not implemented\n");
1526 return;
1527 }
1528 ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1529
1530 read_bytes = 0xdeadbeef;
1531 SetLastError(0xdeadbeef);
1532 ok(ReadProcessMemory(hproc, test_OpenProcess, &dummy, sizeof(dummy), &read_bytes),
1533 "ReadProcessMemory error %d\n", GetLastError());
1534 ok(read_bytes == sizeof(dummy), "wrong read bytes %ld\n", read_bytes);
1535
1536 CloseHandle(hproc);
1537
1538 hproc = OpenProcess(PROCESS_VM_OPERATION, FALSE, GetCurrentProcessId());
1539 ok(hproc != NULL, "OpenProcess error %d\n", GetLastError());
1540
1541 addr1 = pVirtualAllocEx(hproc, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
1542 ok(addr1 != NULL, "VirtualAllocEx error %d\n", GetLastError());
1543
1544 /* without PROCESS_QUERY_INFORMATION */
1545 SetLastError(0xdeadbeef);
1546 ok(!VirtualQueryEx(hproc, addr1, &info, sizeof(info)),
1547 "VirtualQueryEx without PROCESS_QUERY_INFORMATION rights should fail\n");
1548 ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1549
1550 /* without PROCESS_VM_READ */
1551 read_bytes = 0xdeadbeef;
1552 SetLastError(0xdeadbeef);
1553 ok(!ReadProcessMemory(hproc, addr1, &dummy, sizeof(dummy), &read_bytes),
1554 "ReadProcessMemory without PROCESS_VM_READ rights should fail\n");
1555 ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1556 ok(read_bytes == 0, "wrong read bytes %ld\n", read_bytes);
1557
1558 CloseHandle(hproc);
1559
1560 hproc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
1561
1562 memset(&info, 0xcc, sizeof(info));
1563 ok(VirtualQueryEx(hproc, addr1, &info, sizeof(info)) == sizeof(info),
1564 "VirtualQueryEx error %d\n", GetLastError());
1565
1566 ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
1567 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
1568 ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
1569 ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
1570 ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
1571 /* NT reports Protect == 0 for a not committed memory block */
1572 ok(info.Protect == 0 /* NT */ ||
1573 info.Protect == PAGE_NOACCESS, /* Win9x */
1574 "%x != PAGE_NOACCESS\n", info.Protect);
1575 ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
1576
1577 SetLastError(0xdeadbeef);
1578 ok(!pVirtualFreeEx(hproc, addr1, 0, MEM_RELEASE),
1579 "VirtualFreeEx without PROCESS_VM_OPERATION rights should fail\n");
1580 ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1581
1582 CloseHandle(hproc);
1583
1584 ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
1585 }
1586
1587 static void test_GetProcessVersion(void)
1588 {
1589 static char cmdline[] = "winver.exe";
1590 PROCESS_INFORMATION pi;
1591 STARTUPINFOA si;
1592 DWORD ret;
1593
1594 SetLastError(0xdeadbeef);
1595 ret = GetProcessVersion(0);
1596 ok(ret, "GetProcessVersion error %u\n", GetLastError());
1597
1598 SetLastError(0xdeadbeef);
1599 ret = GetProcessVersion(GetCurrentProcessId());
1600 ok(ret, "GetProcessVersion error %u\n", GetLastError());
1601
1602 memset(&si, 0, sizeof(si));
1603 si.cb = sizeof(si);
1604 si.dwFlags = STARTF_USESHOWWINDOW;
1605 si.wShowWindow = SW_HIDE;
1606 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1607 SetLastError(0xdeadbeef);
1608 ok(ret, "CreateProcess error %u\n", GetLastError());
1609
1610 SetLastError(0xdeadbeef);
1611 ret = GetProcessVersion(pi.dwProcessId);
1612 ok(ret, "GetProcessVersion error %u\n", GetLastError());
1613
1614 SetLastError(0xdeadbeef);
1615 ret = TerminateProcess(pi.hProcess, 0);
1616 ok(ret, "TerminateProcess error %u\n", GetLastError());
1617
1618 CloseHandle(pi.hProcess);
1619 CloseHandle(pi.hThread);
1620 }
1621
1622 static void test_ProcessNameA(void)
1623 {
1624 #define INIT_STR "Just some words"
1625 DWORD length, size;
1626 CHAR buf[1024];
1627
1628 if (!pQueryFullProcessImageNameA)
1629 {
1630 win_skip("QueryFullProcessImageNameA unavailable (added in Windows Vista)\n");
1631 return;
1632 }
1633 /* get the buffer length without \0 terminator */
1634 length = 1024;
1635 expect_eq_d(TRUE, pQueryFullProcessImageNameA(GetCurrentProcess(), 0, buf, &length));
1636 expect_eq_d(length, lstrlenA(buf));
1637
1638 /* when the buffer is too small
1639 * - function fail with error ERROR_INSUFFICIENT_BUFFER
1640 * - the size variable is not modified
1641 * tested with the biggest too small size
1642 */
1643 size = length;
1644 sprintf(buf,INIT_STR);
1645 expect_eq_d(FALSE, pQueryFullProcessImageNameA(GetCurrentProcess(), 0, buf, &size));
1646 expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1647 expect_eq_d(length, size);
1648 expect_eq_s(INIT_STR, buf);
1649
1650 /* retest with smaller buffer size
1651 */
1652 size = 4;
1653 sprintf(buf,INIT_STR);
1654 expect_eq_d(FALSE, pQueryFullProcessImageNameA(GetCurrentProcess(), 0, buf, &size));
1655 expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1656 expect_eq_d(4, size);
1657 expect_eq_s(INIT_STR, buf);
1658
1659 /* this is a difference between the ascii and the unicode version
1660 * the unicode version crashes when the size is big enough to hold the result
1661 * ascii version throughs an error
1662 */
1663 size = 1024;
1664 expect_eq_d(FALSE, pQueryFullProcessImageNameA(GetCurrentProcess(), 0, NULL, &size));
1665 expect_eq_d(1024, size);
1666 expect_eq_d(ERROR_INVALID_PARAMETER, GetLastError());
1667 }
1668
1669 static void test_ProcessName(void)
1670 {
1671 HANDLE hSelf;
1672 WCHAR module_name[1024];
1673 WCHAR deviceW[] = {'\\','D', 'e','v','i','c','e',0};
1674 WCHAR buf[1024];
1675 DWORD size;
1676
1677 if (!pQueryFullProcessImageNameW)
1678 {
1679 win_skip("QueryFullProcessImageNameW unavailable (added in Windows Vista)\n");
1680 return;
1681 }
1682
1683 ok(GetModuleFileNameW(NULL, module_name, 1024), "GetModuleFileNameW(NULL, ...) failed\n");
1684
1685 /* GetCurrentProcess pseudo-handle */
1686 size = sizeof(buf) / sizeof(buf[0]);
1687 expect_eq_d(TRUE, pQueryFullProcessImageNameW(GetCurrentProcess(), 0, buf, &size));
1688 expect_eq_d(lstrlenW(buf), size);
1689 expect_eq_ws_i(buf, module_name);
1690
1691 hSelf = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
1692 /* Real handle */
1693 size = sizeof(buf) / sizeof(buf[0]);
1694 expect_eq_d(TRUE, pQueryFullProcessImageNameW(hSelf, 0, buf, &size));
1695 expect_eq_d(lstrlenW(buf), size);
1696 expect_eq_ws_i(buf, module_name);
1697
1698 /* Buffer too small */
1699 size = lstrlenW(module_name)/2;
1700 lstrcpyW(buf, deviceW);
1701 SetLastError(0xdeadbeef);
1702 expect_eq_d(FALSE, pQueryFullProcessImageNameW(hSelf, 0, buf, &size));
1703 expect_eq_d(lstrlenW(module_name)/2, size); /* size not changed(!) */
1704 expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1705 expect_eq_ws_i(deviceW, buf); /* buffer not changed */
1706
1707 /* Too small - not space for NUL terminator */
1708 size = lstrlenW(module_name);
1709 SetLastError(0xdeadbeef);
1710 expect_eq_d(FALSE, pQueryFullProcessImageNameW(hSelf, 0, buf, &size));
1711 expect_eq_d(lstrlenW(module_name), size); /* size not changed(!) */
1712 expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1713
1714 /* NULL buffer */
1715 size = 0;
1716 expect_eq_d(FALSE, pQueryFullProcessImageNameW(hSelf, 0, NULL, &size));
1717 expect_eq_d(0, size);
1718 expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1719
1720 /* native path */
1721 size = sizeof(buf) / sizeof(buf[0]);
1722 expect_eq_d(TRUE, pQueryFullProcessImageNameW(hSelf, PROCESS_NAME_NATIVE, buf, &size));
1723 expect_eq_d(lstrlenW(buf), size);
1724 ok(buf[0] == '\\', "NT path should begin with '\\'\n");
1725 todo_wine ok(memcmp(buf, deviceW, sizeof(WCHAR)*lstrlenW(deviceW)) == 0, "NT path should begin with \\Device\n");
1726
1727 /* Buffer too small */
1728 size = lstrlenW(module_name)/2;
1729 SetLastError(0xdeadbeef);
1730 lstrcpyW(buf, module_name);
1731 expect_eq_d(FALSE, pQueryFullProcessImageNameW(hSelf, 0, buf, &size));
1732 expect_eq_d(lstrlenW(module_name)/2, size); /* size not changed(!) */
1733 expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1734 expect_eq_ws_i(module_name, buf); /* buffer not changed */
1735
1736 CloseHandle(hSelf);
1737 }
1738
1739 static void test_Handles(void)
1740 {
1741 HANDLE handle = GetCurrentProcess();
1742 BOOL ret;
1743 DWORD code;
1744
1745 ok( handle == (HANDLE)~(ULONG_PTR)0 ||
1746 handle == (HANDLE)(ULONG_PTR)0x7fffffff /* win9x */,
1747 "invalid current process handle %p\n", handle );
1748 ret = GetExitCodeProcess( handle, &code );
1749 ok( ret, "GetExitCodeProcess failed err %u\n", GetLastError() );
1750 #ifdef _WIN64
1751 /* truncated handle */
1752 SetLastError( 0xdeadbeef );
1753 handle = (HANDLE)((ULONG_PTR)handle & ~0u);
1754 ret = GetExitCodeProcess( handle, &code );
1755 ok( !ret, "GetExitCodeProcess succeeded for %p\n", handle );
1756 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
1757 /* sign-extended handle */
1758 SetLastError( 0xdeadbeef );
1759 handle = (HANDLE)((LONG_PTR)(int)(ULONG_PTR)handle);
1760 ret = GetExitCodeProcess( handle, &code );
1761 ok( ret, "GetExitCodeProcess failed err %u\n", GetLastError() );
1762 /* invalid high-word */
1763 SetLastError( 0xdeadbeef );
1764 handle = (HANDLE)(((ULONG_PTR)handle & ~0u) + ((ULONG_PTR)1 << 32));
1765 ret = GetExitCodeProcess( handle, &code );
1766 ok( !ret, "GetExitCodeProcess succeeded for %p\n", handle );
1767 ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
1768 #endif
1769 }
1770
1771 START_TEST(process)
1772 {
1773 int b = init();
1774 ok(b, "Basic init of CreateProcess test\n");
1775 if (!b) return;
1776
1777 if (myARGC >= 3)
1778 {
1779 doChild(myARGV[2], (myARGC == 3) ? NULL : myARGV[3]);
1780 return;
1781 }
1782 test_Startup();
1783 test_CommandLine();
1784 test_Directory();
1785 test_Environment();
1786 test_SuspendFlag();
1787 test_DebuggingFlag();
1788 test_Console();
1789 test_ExitCode();
1790 test_OpenProcess();
1791 test_GetProcessVersion();
1792 test_ProcessNameA();
1793 test_ProcessName();
1794 test_Handles();
1795 /* things that can be tested:
1796 * lookup: check the way program to be executed is searched
1797 * handles: check the handle inheritance stuff (+sec options)
1798 * console: check if console creation parameters work
1799 */
1800 }