fltlib: Add a stub dll.
[wine] / dlls / kernel32 / tests / process.c
1 /*
2  * Unit test suite for CreateProcess function.
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        "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
960
961     strcpy(buffer, "doesnotexist.exe");
962     strcpy(buffer2, "does not exist.exe");
963
964     /* Test nonexistent application name. */
965     SetLastError(0xdeadbeef);
966     ret = CreateProcessA(buffer, NULL, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
967     ok(!ret, "CreateProcessA unexpectedly succeeded\n");
968     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
969
970     SetLastError(0xdeadbeef);
971     ret = CreateProcessA(buffer2, NULL, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
972     ok(!ret, "CreateProcessA unexpectedly succeeded\n");
973     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
974
975     /* Test nonexistent command line parameter. */
976     SetLastError(0xdeadbeef);
977     ret = CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
978     ok(!ret, "CreateProcessA unexpectedly succeeded\n");
979     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
980
981     SetLastError(0xdeadbeef);
982     ret = CreateProcessA(NULL, buffer2, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info);
983     ok(!ret, "CreateProcessA unexpectedly succeeded\n");
984     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
985 }
986
987 static void test_Directory(void)
988 {
989     char                buffer[MAX_PATH];
990     PROCESS_INFORMATION info;
991     STARTUPINFOA        startup;
992     char windir[MAX_PATH];
993     static CHAR cmdline[] = "winver.exe";
994
995     memset(&startup, 0, sizeof(startup));
996     startup.cb = sizeof(startup);
997     startup.dwFlags = STARTF_USESHOWWINDOW;
998     startup.wShowWindow = SW_SHOWNORMAL;
999
1000     /* the basics */
1001     get_file_name(resfile);
1002     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
1003     GetWindowsDirectoryA( windir, sizeof(windir) );
1004     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, windir, &startup, &info), "CreateProcess\n");
1005     /* wait for child to terminate */
1006     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1007     /* child process has changed result file, so let profile functions know about it */
1008     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1009
1010     okChildIString("Misc", "CurrDirA", windir);
1011     release_memory();
1012     assert(DeleteFileA(resfile) != 0);
1013
1014     /* search PATH for the exe if directory is NULL */
1015     ok(CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
1016     ok(TerminateProcess(info.hProcess, 0), "Child process termination\n");
1017
1018     /* if any directory is provided, don't search PATH, error on bad directory */
1019     SetLastError(0xdeadbeef);
1020     memset(&info, 0, sizeof(info));
1021     ok(!CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0L,
1022                        NULL, "non\\existent\\directory", &startup, &info), "CreateProcess\n");
1023     ok(GetLastError() == ERROR_DIRECTORY, "Expected ERROR_DIRECTORY, got %d\n", GetLastError());
1024     ok(!TerminateProcess(info.hProcess, 0), "Child process should not exist\n");
1025 }
1026
1027 static BOOL is_str_env_drive_dir(const char* str)
1028 {
1029     return str[0] == '=' && str[1] >= 'A' && str[1] <= 'Z' && str[2] == ':' &&
1030         str[3] == '=' && str[4] == str[1];
1031 }
1032
1033 /* compared expected child's environment (in gesA) from actual
1034  * environment our child got
1035  */
1036 static void cmpEnvironment(const char* gesA)
1037 {
1038     int                 i, clen;
1039     const char*         ptrA;
1040     char*               res;
1041     char                key[32];
1042     BOOL                found;
1043
1044     clen = GetPrivateProfileIntA("EnvironmentA", "len", 0, resfile);
1045     
1046     /* now look each parent env in child */
1047     if ((ptrA = gesA) != NULL)
1048     {
1049         while (*ptrA)
1050         {
1051             for (i = 0; i < clen; i++)
1052             {
1053                 sprintf(key, "env%d", i);
1054                 res = getChildString("EnvironmentA", key);
1055                 if (strncmp(ptrA, res, MAX_LISTED_ENV_VAR - 1) == 0)
1056                     break;
1057             }
1058             found = i < clen;
1059             ok(found, "Parent-env string %s isn't in child process\n", ptrA);
1060             
1061             ptrA += strlen(ptrA) + 1;
1062             release_memory();
1063         }
1064     }
1065     /* and each child env in parent */
1066     for (i = 0; i < clen; i++)
1067     {
1068         sprintf(key, "env%d", i);
1069         res = getChildString("EnvironmentA", key);
1070         if ((ptrA = gesA) != NULL)
1071         {
1072             while (*ptrA)
1073             {
1074                 if (strncmp(res, ptrA, MAX_LISTED_ENV_VAR - 1) == 0)
1075                     break;
1076                 ptrA += strlen(ptrA) + 1;
1077             }
1078             if (!*ptrA) ptrA = NULL;
1079         }
1080
1081         if (!is_str_env_drive_dir(res))
1082         {
1083             found = ptrA != NULL;
1084             ok(found, "Child-env string %s isn't in parent process\n", res);
1085         }
1086         /* else => should also test we get the right per drive default directory here... */
1087     }
1088 }
1089
1090 static void test_Environment(void)
1091 {
1092     char                buffer[MAX_PATH];
1093     PROCESS_INFORMATION info;
1094     STARTUPINFOA        startup;
1095     char*               child_env;
1096     int                 child_env_len;
1097     char*               ptr;
1098     char*               env;
1099     int                 slen;
1100
1101     memset(&startup, 0, sizeof(startup));
1102     startup.cb = sizeof(startup);
1103     startup.dwFlags = STARTF_USESHOWWINDOW;
1104     startup.wShowWindow = SW_SHOWNORMAL;
1105
1106     /* the basics */
1107     get_file_name(resfile);
1108     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
1109     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
1110     /* wait for child to terminate */
1111     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1112     /* child process has changed result file, so let profile functions know about it */
1113     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1114     
1115     cmpEnvironment(GetEnvironmentStringsA());
1116     release_memory();
1117     assert(DeleteFileA(resfile) != 0);
1118
1119     memset(&startup, 0, sizeof(startup));
1120     startup.cb = sizeof(startup);
1121     startup.dwFlags = STARTF_USESHOWWINDOW;
1122     startup.wShowWindow = SW_SHOWNORMAL;
1123
1124     /* the basics */
1125     get_file_name(resfile);
1126     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
1127     
1128     child_env_len = 0;
1129     ptr = GetEnvironmentStringsA();
1130     while(*ptr)
1131     {
1132         slen = strlen(ptr)+1;
1133         child_env_len += slen;
1134         ptr += slen;
1135     }
1136     /* Add space for additional environment variables */
1137     child_env_len += 256;
1138     child_env = HeapAlloc(GetProcessHeap(), 0, child_env_len);
1139     
1140     ptr = child_env;
1141     sprintf(ptr, "=%c:=%s", 'C', "C:\\FOO\\BAR");
1142     ptr += strlen(ptr) + 1;
1143     strcpy(ptr, "PATH=C:\\WINDOWS;C:\\WINDOWS\\SYSTEM;C:\\MY\\OWN\\DIR");
1144     ptr += strlen(ptr) + 1;
1145     strcpy(ptr, "FOO=BAR");
1146     ptr += strlen(ptr) + 1;
1147     strcpy(ptr, "BAR=FOOBAR");
1148     ptr += strlen(ptr) + 1;
1149     /* copy all existing variables except:
1150      * - WINELOADER
1151      * - PATH (already set above)
1152      * - the directory definitions (=[A-Z]:=)
1153      */
1154     for (env = GetEnvironmentStringsA(); *env; env += strlen(env) + 1)
1155     {
1156         if (strncmp(env, "PATH=", 5) != 0 &&
1157             strncmp(env, "WINELOADER=", 11) != 0 &&
1158             !is_str_env_drive_dir(env))
1159         {
1160             strcpy(ptr, env);
1161             ptr += strlen(ptr) + 1;
1162         }
1163     }
1164     *ptr = '\0';
1165     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, child_env, NULL, &startup, &info), "CreateProcess\n");
1166     /* wait for child to terminate */
1167     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1168     /* child process has changed result file, so let profile functions know about it */
1169     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1170     
1171     cmpEnvironment(child_env);
1172
1173     HeapFree(GetProcessHeap(), 0, child_env);
1174     release_memory();
1175     assert(DeleteFileA(resfile) != 0);
1176 }
1177
1178 static  void    test_SuspendFlag(void)
1179 {
1180     char                buffer[MAX_PATH];
1181     PROCESS_INFORMATION info;
1182     STARTUPINFOA       startup, us;
1183     DWORD               exit_status;
1184
1185     /* let's start simplistic */
1186     memset(&startup, 0, sizeof(startup));
1187     startup.cb = sizeof(startup);
1188     startup.dwFlags = STARTF_USESHOWWINDOW;
1189     startup.wShowWindow = SW_SHOWNORMAL;
1190
1191     get_file_name(resfile);
1192     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
1193     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &startup, &info), "CreateProcess\n");
1194
1195     ok(GetExitCodeThread(info.hThread, &exit_status) && exit_status == STILL_ACTIVE, "thread still running\n");
1196     Sleep(8000);
1197     ok(GetExitCodeThread(info.hThread, &exit_status) && exit_status == STILL_ACTIVE, "thread still running\n");
1198     ok(ResumeThread(info.hThread) == 1, "Resuming thread\n");
1199
1200     /* wait for child to terminate */
1201     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1202     /* child process has changed result file, so let profile functions know about it */
1203     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1204
1205     GetStartupInfoA(&us);
1206
1207     okChildInt("StartupInfoA", "cb", startup.cb);
1208     okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1209     ok (startup.lpTitle == NULL || !strcmp(startup.lpTitle, selfname),
1210         "StartupInfoA:lpTitle expected '%s' or null, got '%s'\n", selfname, startup.lpTitle);
1211     okChildInt("StartupInfoA", "dwX", startup.dwX);
1212     okChildInt("StartupInfoA", "dwY", startup.dwY);
1213     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1214     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1215     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1216     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1217     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1218     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1219     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1220     release_memory();
1221     assert(DeleteFileA(resfile) != 0);
1222 }
1223
1224 static  void    test_DebuggingFlag(void)
1225 {
1226     char                buffer[MAX_PATH];
1227     void               *processbase = NULL;
1228     PROCESS_INFORMATION info;
1229     STARTUPINFOA       startup, us;
1230     DEBUG_EVENT         de;
1231     unsigned            dbg = 0;
1232
1233     /* let's start simplistic */
1234     memset(&startup, 0, sizeof(startup));
1235     startup.cb = sizeof(startup);
1236     startup.dwFlags = STARTF_USESHOWWINDOW;
1237     startup.wShowWindow = SW_SHOWNORMAL;
1238
1239     get_file_name(resfile);
1240     sprintf(buffer, "%s tests/process.c %s", selfname, resfile);
1241     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &startup, &info), "CreateProcess\n");
1242
1243     /* get all startup events up to the entry point break exception */
1244     do 
1245     {
1246         ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
1247         ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
1248         if (!dbg)
1249         {
1250             ok(de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT,
1251                "first event: %d\n", de.dwDebugEventCode);
1252             processbase = de.u.CreateProcessInfo.lpBaseOfImage;
1253         }
1254         if (de.dwDebugEventCode != EXCEPTION_DEBUG_EVENT) dbg++;
1255         ok(de.dwDebugEventCode != LOAD_DLL_DEBUG_EVENT ||
1256            de.u.LoadDll.lpBaseOfDll != processbase, "got LOAD_DLL for main module\n");
1257     } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
1258
1259     ok(dbg, "I have seen a debug event\n");
1260     /* wait for child to terminate */
1261     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1262     /* child process has changed result file, so let profile functions know about it */
1263     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1264
1265     GetStartupInfoA(&us);
1266
1267     okChildInt("StartupInfoA", "cb", startup.cb);
1268     okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1269     ok (startup.lpTitle == NULL || !strcmp(startup.lpTitle, selfname),
1270         "StartupInfoA:lpTitle expected '%s' or null, got '%s'\n", selfname, startup.lpTitle);
1271     okChildInt("StartupInfoA", "dwX", startup.dwX);
1272     okChildInt("StartupInfoA", "dwY", startup.dwY);
1273     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1274     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1275     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1276     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1277     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1278     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1279     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1280     release_memory();
1281     assert(DeleteFileA(resfile) != 0);
1282 }
1283
1284 static BOOL is_console(HANDLE h)
1285 {
1286     return h != INVALID_HANDLE_VALUE && ((ULONG_PTR)h & 3) == 3;
1287 }
1288
1289 static void test_Console(void)
1290 {
1291     char                buffer[MAX_PATH];
1292     PROCESS_INFORMATION info;
1293     STARTUPINFOA       startup, us;
1294     SECURITY_ATTRIBUTES sa;
1295     CONSOLE_SCREEN_BUFFER_INFO  sbi, sbiC;
1296     DWORD               modeIn, modeOut, modeInC, modeOutC;
1297     DWORD               cpIn, cpOut, cpInC, cpOutC;
1298     DWORD               w;
1299     HANDLE              hChildIn, hChildInInh, hChildOut, hChildOutInh, hParentIn, hParentOut;
1300     const char*         msg = "This is a std-handle inheritance test.";
1301     unsigned            msg_len;
1302     BOOL                run_tests = TRUE;
1303
1304     memset(&startup, 0, sizeof(startup));
1305     startup.cb = sizeof(startup);
1306     startup.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
1307     startup.wShowWindow = SW_SHOWNORMAL;
1308
1309     sa.nLength = sizeof(sa);
1310     sa.lpSecurityDescriptor = NULL;
1311     sa.bInheritHandle = TRUE;
1312
1313     startup.hStdInput = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1314     startup.hStdOutput = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1315
1316     /* first, we need to be sure we're attached to a console */
1317     if (!is_console(startup.hStdInput) || !is_console(startup.hStdOutput))
1318     {
1319         /* we're not attached to a console, let's do it */
1320         AllocConsole();
1321         startup.hStdInput = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1322         startup.hStdOutput = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, &sa, OPEN_EXISTING, 0, 0);
1323     }
1324     /* now verify everything's ok */
1325     ok(startup.hStdInput != INVALID_HANDLE_VALUE, "Opening ConIn\n");
1326     ok(startup.hStdOutput != INVALID_HANDLE_VALUE, "Opening ConOut\n");
1327     startup.hStdError = startup.hStdOutput;
1328
1329     ok(GetConsoleScreenBufferInfo(startup.hStdOutput, &sbi), "Getting sb info\n");
1330     ok(GetConsoleMode(startup.hStdInput, &modeIn) && 
1331        GetConsoleMode(startup.hStdOutput, &modeOut), "Getting console modes\n");
1332     cpIn = GetConsoleCP();
1333     cpOut = GetConsoleOutputCP();
1334
1335     get_file_name(resfile);
1336     sprintf(buffer, "%s tests/process.c %s console", selfname, resfile);
1337     ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
1338
1339     /* wait for child to terminate */
1340     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1341     /* child process has changed result file, so let profile functions know about it */
1342     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1343
1344     /* now get the modification the child has made, and resets parents expected values */
1345     ok(GetConsoleScreenBufferInfo(startup.hStdOutput, &sbiC), "Getting sb info\n");
1346     ok(GetConsoleMode(startup.hStdInput, &modeInC) && 
1347        GetConsoleMode(startup.hStdOutput, &modeOutC), "Getting console modes\n");
1348
1349     SetConsoleMode(startup.hStdInput, modeIn);
1350     SetConsoleMode(startup.hStdOutput, modeOut);
1351
1352     cpInC = GetConsoleCP();
1353     cpOutC = GetConsoleOutputCP();
1354
1355     /* Try to set invalid CP */
1356     SetLastError(0xdeadbeef);
1357     ok(!SetConsoleCP(0), "Shouldn't succeed\n");
1358     ok(GetLastError()==ERROR_INVALID_PARAMETER ||
1359        broken(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), /* win9x */
1360        "GetLastError: expecting %u got %u\n",
1361        ERROR_INVALID_PARAMETER, GetLastError());
1362     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1363         run_tests = FALSE;
1364
1365
1366     SetLastError(0xdeadbeef);
1367     ok(!SetConsoleOutputCP(0), "Shouldn't succeed\n");
1368     ok(GetLastError()==ERROR_INVALID_PARAMETER ||
1369        broken(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED), /* win9x */
1370        "GetLastError: expecting %u got %u\n",
1371        ERROR_INVALID_PARAMETER, GetLastError());
1372
1373     SetConsoleCP(cpIn);
1374     SetConsoleOutputCP(cpOut);
1375
1376     GetStartupInfoA(&us);
1377
1378     okChildInt("StartupInfoA", "cb", startup.cb);
1379     okChildString("StartupInfoA", "lpDesktop", us.lpDesktop);
1380     ok (startup.lpTitle == NULL || !strcmp(startup.lpTitle, selfname),
1381         "StartupInfoA:lpTitle expected '%s' or null, got '%s'\n", selfname, startup.lpTitle);
1382     okChildInt("StartupInfoA", "dwX", startup.dwX);
1383     okChildInt("StartupInfoA", "dwY", startup.dwY);
1384     okChildInt("StartupInfoA", "dwXSize", startup.dwXSize);
1385     okChildInt("StartupInfoA", "dwYSize", startup.dwYSize);
1386     okChildInt("StartupInfoA", "dwXCountChars", startup.dwXCountChars);
1387     okChildInt("StartupInfoA", "dwYCountChars", startup.dwYCountChars);
1388     okChildInt("StartupInfoA", "dwFillAttribute", startup.dwFillAttribute);
1389     okChildInt("StartupInfoA", "dwFlags", startup.dwFlags);
1390     okChildInt("StartupInfoA", "wShowWindow", startup.wShowWindow);
1391
1392     /* check child correctly inherited the console */
1393     okChildInt("StartupInfoA", "hStdInput", (DWORD_PTR)startup.hStdInput);
1394     okChildInt("StartupInfoA", "hStdOutput", (DWORD_PTR)startup.hStdOutput);
1395     okChildInt("StartupInfoA", "hStdError", (DWORD_PTR)startup.hStdError);
1396     okChildInt("Console", "SizeX", (DWORD)sbi.dwSize.X);
1397     okChildInt("Console", "SizeY", (DWORD)sbi.dwSize.Y);
1398     okChildInt("Console", "CursorX", (DWORD)sbi.dwCursorPosition.X);
1399     okChildInt("Console", "CursorY", (DWORD)sbi.dwCursorPosition.Y);
1400     okChildInt("Console", "Attributes", sbi.wAttributes);
1401     okChildInt("Console", "winLeft", (DWORD)sbi.srWindow.Left);
1402     okChildInt("Console", "winTop", (DWORD)sbi.srWindow.Top);
1403     okChildInt("Console", "winRight", (DWORD)sbi.srWindow.Right);
1404     okChildInt("Console", "winBottom", (DWORD)sbi.srWindow.Bottom);
1405     okChildInt("Console", "maxWinWidth", (DWORD)sbi.dwMaximumWindowSize.X);
1406     okChildInt("Console", "maxWinHeight", (DWORD)sbi.dwMaximumWindowSize.Y);
1407     okChildInt("Console", "InputCP", cpIn);
1408     okChildInt("Console", "OutputCP", cpOut);
1409     okChildInt("Console", "InputMode", modeIn);
1410     okChildInt("Console", "OutputMode", modeOut);
1411
1412     if (run_tests)
1413     {
1414         ok(cpInC == 1252, "Wrong console CP (expected 1252 got %d/%d)\n", cpInC, cpIn);
1415         ok(cpOutC == 1252, "Wrong console-SB CP (expected 1252 got %d/%d)\n", cpOutC, cpOut);
1416     }
1417     else
1418         win_skip("Setting the codepage is not implemented\n");
1419
1420     ok(modeInC == (modeIn ^ 1), "Wrong console mode\n");
1421     ok(modeOutC == (modeOut ^ 1), "Wrong console-SB mode\n");
1422     trace("cursor position(X): %d/%d\n",sbi.dwCursorPosition.X, sbiC.dwCursorPosition.X);
1423     ok(sbiC.dwCursorPosition.Y == (sbi.dwCursorPosition.Y ^ 1), "Wrong cursor position\n");
1424
1425     release_memory();
1426     assert(DeleteFileA(resfile) != 0);
1427
1428     ok(CreatePipe(&hParentIn, &hChildOut, NULL, 0), "Creating parent-input pipe\n");
1429     ok(DuplicateHandle(GetCurrentProcess(), hChildOut, GetCurrentProcess(), 
1430                        &hChildOutInh, 0, TRUE, DUPLICATE_SAME_ACCESS),
1431        "Duplicating as inheritable child-output pipe\n");
1432     CloseHandle(hChildOut);
1433  
1434     ok(CreatePipe(&hChildIn, &hParentOut, NULL, 0), "Creating parent-output pipe\n");
1435     ok(DuplicateHandle(GetCurrentProcess(), hChildIn, GetCurrentProcess(), 
1436                        &hChildInInh, 0, TRUE, DUPLICATE_SAME_ACCESS),
1437        "Duplicating as inheritable child-input pipe\n");
1438     CloseHandle(hChildIn); 
1439     
1440     memset(&startup, 0, sizeof(startup));
1441     startup.cb = sizeof(startup);
1442     startup.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
1443     startup.wShowWindow = SW_SHOWNORMAL;
1444     startup.hStdInput = hChildInInh;
1445     startup.hStdOutput = hChildOutInh;
1446     startup.hStdError = hChildOutInh;
1447
1448     get_file_name(resfile);
1449     sprintf(buffer, "%s tests/process.c %s stdhandle", selfname, resfile);
1450     ok(CreateProcessA(NULL, buffer, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &startup, &info), "CreateProcess\n");
1451     ok(CloseHandle(hChildInInh), "Closing handle\n");
1452     ok(CloseHandle(hChildOutInh), "Closing handle\n");
1453
1454     msg_len = strlen(msg) + 1;
1455     ok(WriteFile(hParentOut, msg, msg_len, &w, NULL), "Writing to child\n");
1456     ok(w == msg_len, "Should have written %u bytes, actually wrote %u\n", msg_len, w);
1457     memset(buffer, 0, sizeof(buffer));
1458     ok(ReadFile(hParentIn, buffer, sizeof(buffer), &w, NULL), "Reading from child\n");
1459     ok(strcmp(buffer, msg) == 0, "Should have received '%s'\n", msg);
1460
1461     /* wait for child to terminate */
1462     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1463     /* child process has changed result file, so let profile functions know about it */
1464     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1465
1466     okChildString("StdHandle", "msg", msg);
1467
1468     release_memory();
1469     assert(DeleteFileA(resfile) != 0);
1470 }
1471
1472 static  void    test_ExitCode(void)
1473 {
1474     char                buffer[MAX_PATH];
1475     PROCESS_INFORMATION info;
1476     STARTUPINFOA        startup;
1477     DWORD               code;
1478
1479     /* let's start simplistic */
1480     memset(&startup, 0, sizeof(startup));
1481     startup.cb = sizeof(startup);
1482     startup.dwFlags = STARTF_USESHOWWINDOW;
1483     startup.wShowWindow = SW_SHOWNORMAL;
1484
1485     get_file_name(resfile);
1486     sprintf(buffer, "%s tests/process.c %s exit_code", selfname, resfile);
1487     ok(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info), "CreateProcess\n");
1488
1489     /* wait for child to terminate */
1490     ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
1491     /* child process has changed result file, so let profile functions know about it */
1492     WritePrivateProfileStringA(NULL, NULL, NULL, resfile);
1493
1494     ok(GetExitCodeProcess(info.hProcess, &code), "Getting exit code\n");
1495     okChildInt("ExitCode", "value", code);
1496
1497     release_memory();
1498     assert(DeleteFileA(resfile) != 0);
1499 }
1500
1501 static void test_OpenProcess(void)
1502 {
1503     HANDLE hproc;
1504     void *addr1;
1505     MEMORY_BASIC_INFORMATION info;
1506     SIZE_T dummy, read_bytes;
1507
1508     /* not exported in all windows versions */
1509     if ((!pVirtualAllocEx) || (!pVirtualFreeEx)) {
1510         win_skip("VirtualAllocEx not found\n");
1511         return;
1512     }
1513
1514     /* without PROCESS_VM_OPERATION */
1515     hproc = OpenProcess(PROCESS_ALL_ACCESS & ~PROCESS_VM_OPERATION, FALSE, GetCurrentProcessId());
1516     ok(hproc != NULL, "OpenProcess error %d\n", GetLastError());
1517
1518     SetLastError(0xdeadbeef);
1519     addr1 = pVirtualAllocEx(hproc, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
1520     ok(!addr1, "VirtualAllocEx should fail\n");
1521     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1522     {   /* Win9x */
1523         CloseHandle(hproc);
1524         win_skip("VirtualAllocEx not implemented\n");
1525         return;
1526     }
1527     ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1528
1529     read_bytes = 0xdeadbeef;
1530     SetLastError(0xdeadbeef);
1531     ok(ReadProcessMemory(hproc, test_OpenProcess, &dummy, sizeof(dummy), &read_bytes),
1532        "ReadProcessMemory error %d\n", GetLastError());
1533     ok(read_bytes == sizeof(dummy), "wrong read bytes %ld\n", read_bytes);
1534
1535     CloseHandle(hproc);
1536
1537     hproc = OpenProcess(PROCESS_VM_OPERATION, FALSE, GetCurrentProcessId());
1538     ok(hproc != NULL, "OpenProcess error %d\n", GetLastError());
1539
1540     addr1 = pVirtualAllocEx(hproc, 0, 0xFFFC, MEM_RESERVE, PAGE_NOACCESS);
1541     ok(addr1 != NULL, "VirtualAllocEx error %d\n", GetLastError());
1542
1543     /* without PROCESS_QUERY_INFORMATION */
1544     SetLastError(0xdeadbeef);
1545     ok(!VirtualQueryEx(hproc, addr1, &info, sizeof(info)),
1546        "VirtualQueryEx without PROCESS_QUERY_INFORMATION rights should fail\n");
1547     ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1548
1549     /* without PROCESS_VM_READ */
1550     read_bytes = 0xdeadbeef;
1551     SetLastError(0xdeadbeef);
1552     ok(!ReadProcessMemory(hproc, addr1, &dummy, sizeof(dummy), &read_bytes),
1553        "ReadProcessMemory without PROCESS_VM_READ rights should fail\n");
1554     ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1555     ok(read_bytes == 0, "wrong read bytes %ld\n", read_bytes);
1556
1557     CloseHandle(hproc);
1558
1559     hproc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
1560
1561     memset(&info, 0xcc, sizeof(info));
1562     ok(VirtualQueryEx(hproc, addr1, &info, sizeof(info)) == sizeof(info),
1563        "VirtualQueryEx error %d\n", GetLastError());
1564
1565     ok(info.BaseAddress == addr1, "%p != %p\n", info.BaseAddress, addr1);
1566     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
1567     ok(info.AllocationProtect == PAGE_NOACCESS, "%x != PAGE_NOACCESS\n", info.AllocationProtect);
1568     ok(info.RegionSize == 0x10000, "%lx != 0x10000\n", info.RegionSize);
1569     ok(info.State == MEM_RESERVE, "%x != MEM_RESERVE\n", info.State);
1570     /* NT reports Protect == 0 for a not committed memory block */
1571     ok(info.Protect == 0 /* NT */ ||
1572        info.Protect == PAGE_NOACCESS, /* Win9x */
1573         "%x != PAGE_NOACCESS\n", info.Protect);
1574     ok(info.Type == MEM_PRIVATE, "%x != MEM_PRIVATE\n", info.Type);
1575
1576     SetLastError(0xdeadbeef);
1577     ok(!pVirtualFreeEx(hproc, addr1, 0, MEM_RELEASE),
1578        "VirtualFreeEx without PROCESS_VM_OPERATION rights should fail\n");
1579     ok(GetLastError() == ERROR_ACCESS_DENIED, "wrong error %d\n", GetLastError());
1580
1581     CloseHandle(hproc);
1582
1583     ok(VirtualFree(addr1, 0, MEM_RELEASE), "VirtualFree failed\n");
1584 }
1585
1586 static void test_GetProcessVersion(void)
1587 {
1588     static char cmdline[] = "winver.exe";
1589     PROCESS_INFORMATION pi;
1590     STARTUPINFOA si;
1591     DWORD ret;
1592
1593     SetLastError(0xdeadbeef);
1594     ret = GetProcessVersion(0);
1595     ok(ret, "GetProcessVersion error %u\n", GetLastError());
1596
1597     SetLastError(0xdeadbeef);
1598     ret = GetProcessVersion(GetCurrentProcessId());
1599     ok(ret, "GetProcessVersion error %u\n", GetLastError());
1600
1601     memset(&si, 0, sizeof(si));
1602     si.cb = sizeof(si);
1603     si.dwFlags = STARTF_USESHOWWINDOW;
1604     si.wShowWindow = SW_HIDE;
1605     ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1606     SetLastError(0xdeadbeef);
1607     ok(ret, "CreateProcess error %u\n", GetLastError());
1608
1609     SetLastError(0xdeadbeef);
1610     ret = GetProcessVersion(pi.dwProcessId);
1611     ok(ret, "GetProcessVersion error %u\n", GetLastError());
1612
1613     SetLastError(0xdeadbeef);
1614     ret = TerminateProcess(pi.hProcess, 0);
1615     ok(ret, "TerminateProcess error %u\n", GetLastError());
1616
1617     CloseHandle(pi.hProcess);
1618     CloseHandle(pi.hThread);
1619 }
1620
1621 static void test_ProcessNameA(void)
1622 {
1623 #define INIT_STR "Just some words"
1624     DWORD length, size;
1625     CHAR buf[1024];
1626
1627     if (!pQueryFullProcessImageNameA)
1628     {
1629         win_skip("QueryFullProcessImageNameA unavailable (added in Windows Vista)\n");
1630         return;
1631     }
1632     /* get the buffer length without \0 terminator */
1633     length = 1024;
1634     expect_eq_d(TRUE, pQueryFullProcessImageNameA(GetCurrentProcess(), 0, buf, &length));
1635     expect_eq_d(length, lstrlenA(buf));
1636
1637     /*  when the buffer is too small
1638      *  - function fail with error ERROR_INSUFFICIENT_BUFFER
1639      *  - the size variable is not modified
1640      * tested with the biggest too small size
1641      */
1642     size = length;
1643     sprintf(buf,INIT_STR);
1644     expect_eq_d(FALSE, pQueryFullProcessImageNameA(GetCurrentProcess(), 0, buf, &size));
1645     expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1646     expect_eq_d(length, size);
1647     expect_eq_s(INIT_STR, buf);
1648
1649     /* retest with smaller buffer size
1650      */
1651     size = 4;
1652     sprintf(buf,INIT_STR);
1653     expect_eq_d(FALSE, pQueryFullProcessImageNameA(GetCurrentProcess(), 0, buf, &size));
1654     expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1655     expect_eq_d(4, size);
1656     expect_eq_s(INIT_STR, buf);
1657
1658     /* this is a difference between the ascii and the unicode version
1659      * the unicode version crashes when the size is big enough to hold the result
1660      * ascii version throughs an error
1661      */
1662     size = 1024;
1663     expect_eq_d(FALSE, pQueryFullProcessImageNameA(GetCurrentProcess(), 0, NULL, &size));
1664     expect_eq_d(1024, size);
1665     expect_eq_d(ERROR_INVALID_PARAMETER, GetLastError());
1666 }
1667
1668 static void test_ProcessName(void)
1669 {
1670     HANDLE hSelf;
1671     WCHAR module_name[1024];
1672     WCHAR deviceW[] = {'\\','D', 'e','v','i','c','e',0};
1673     WCHAR buf[1024];
1674     DWORD size;
1675
1676     if (!pQueryFullProcessImageNameW)
1677     {
1678         win_skip("QueryFullProcessImageNameW unavailable (added in Windows Vista)\n");
1679         return;
1680     }
1681
1682     ok(GetModuleFileNameW(NULL, module_name, 1024), "GetModuleFileNameW(NULL, ...) failed\n");
1683
1684     /* GetCurrentProcess pseudo-handle */
1685     size = sizeof(buf) / sizeof(buf[0]);
1686     expect_eq_d(TRUE, pQueryFullProcessImageNameW(GetCurrentProcess(), 0, buf, &size));
1687     expect_eq_d(lstrlenW(buf), size);
1688     expect_eq_ws_i(buf, module_name);
1689
1690     hSelf = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
1691     /* Real handle */
1692     size = sizeof(buf) / sizeof(buf[0]);
1693     expect_eq_d(TRUE, pQueryFullProcessImageNameW(hSelf, 0, buf, &size));
1694     expect_eq_d(lstrlenW(buf), size);
1695     expect_eq_ws_i(buf, module_name);
1696
1697     /* Buffer too small */
1698     size = lstrlenW(module_name)/2;
1699     lstrcpyW(buf, deviceW);
1700     SetLastError(0xdeadbeef);
1701     expect_eq_d(FALSE, pQueryFullProcessImageNameW(hSelf, 0, buf, &size));
1702     expect_eq_d(lstrlenW(module_name)/2, size);  /* size not changed(!) */
1703     expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1704     expect_eq_ws_i(deviceW, buf);  /* buffer not changed */
1705
1706     /* Too small - not space for NUL terminator */
1707     size = lstrlenW(module_name);
1708     SetLastError(0xdeadbeef);
1709     expect_eq_d(FALSE, pQueryFullProcessImageNameW(hSelf, 0, buf, &size));
1710     expect_eq_d(lstrlenW(module_name), size);  /* size not changed(!) */
1711     expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1712
1713     /* NULL buffer */
1714     size = 0;
1715     expect_eq_d(FALSE, pQueryFullProcessImageNameW(hSelf, 0, NULL, &size));
1716     expect_eq_d(0, size);
1717     expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1718
1719     /* native path */
1720     size = sizeof(buf) / sizeof(buf[0]);
1721     expect_eq_d(TRUE, pQueryFullProcessImageNameW(hSelf, PROCESS_NAME_NATIVE, buf, &size));
1722     expect_eq_d(lstrlenW(buf), size);
1723     ok(buf[0] == '\\', "NT path should begin with '\\'\n");
1724     todo_wine ok(memcmp(buf, deviceW, sizeof(WCHAR)*lstrlenW(deviceW)) == 0, "NT path should begin with \\Device\n");
1725
1726     /* Buffer too small */
1727     size = lstrlenW(module_name)/2;
1728     SetLastError(0xdeadbeef);
1729     lstrcpyW(buf, module_name);
1730     expect_eq_d(FALSE, pQueryFullProcessImageNameW(hSelf, 0, buf, &size));
1731     expect_eq_d(lstrlenW(module_name)/2, size);  /* size not changed(!) */
1732     expect_eq_d(ERROR_INSUFFICIENT_BUFFER, GetLastError());
1733     expect_eq_ws_i(module_name, buf);  /* buffer not changed */
1734
1735     CloseHandle(hSelf);
1736 }
1737
1738 static void test_Handles(void)
1739 {
1740     HANDLE handle = GetCurrentProcess();
1741     BOOL ret;
1742     DWORD code;
1743
1744     ok( handle == (HANDLE)~(ULONG_PTR)0 ||
1745         handle == (HANDLE)(ULONG_PTR)0x7fffffff /* win9x */,
1746         "invalid current process handle %p\n", handle );
1747     ret = GetExitCodeProcess( handle, &code );
1748     ok( ret, "GetExitCodeProcess failed err %u\n", GetLastError() );
1749 #ifdef _WIN64
1750     /* truncated handle */
1751     SetLastError( 0xdeadbeef );
1752     handle = (HANDLE)((ULONG_PTR)handle & ~0u);
1753     ret = GetExitCodeProcess( handle, &code );
1754     ok( !ret, "GetExitCodeProcess succeeded for %p\n", handle );
1755     ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
1756     /* sign-extended handle */
1757     SetLastError( 0xdeadbeef );
1758     handle = (HANDLE)((LONG_PTR)(int)(ULONG_PTR)handle);
1759     ret = GetExitCodeProcess( handle, &code );
1760     ok( ret, "GetExitCodeProcess failed err %u\n", GetLastError() );
1761     /* invalid high-word */
1762     SetLastError( 0xdeadbeef );
1763     handle = (HANDLE)(((ULONG_PTR)handle & ~0u) + ((ULONG_PTR)1 << 32));
1764     ret = GetExitCodeProcess( handle, &code );
1765     ok( !ret, "GetExitCodeProcess succeeded for %p\n", handle );
1766     ok( GetLastError() == ERROR_INVALID_HANDLE, "wrong error %u\n", GetLastError() );
1767 #endif
1768 }
1769
1770 START_TEST(process)
1771 {
1772     int b = init();
1773     ok(b, "Basic init of CreateProcess test\n");
1774     if (!b) return;
1775
1776     if (myARGC >= 3)
1777     {
1778         doChild(myARGV[2], (myARGC == 3) ? NULL : myARGV[3]);
1779         return;
1780     }
1781     test_Startup();
1782     test_CommandLine();
1783     test_Directory();
1784     test_Environment();
1785     test_SuspendFlag();
1786     test_DebuggingFlag();
1787     test_Console();
1788     test_ExitCode();
1789     test_OpenProcess();
1790     test_GetProcessVersion();
1791     test_ProcessNameA();
1792     test_ProcessName();
1793     test_Handles();
1794     /* things that can be tested:
1795      *  lookup:         check the way program to be executed is searched
1796      *  handles:        check the handle inheritance stuff (+sec options)
1797      *  console:        check if console creation parameters work
1798      */
1799 }