4 * Copyright 1996, 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
31 #include "wine/winbase16.h"
32 #include "wine/winuser16.h"
40 #include "kernel_private.h"
41 #include "wine/server.h"
42 #include "wine/unicode.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(process);
46 WINE_DECLARE_DEBUG_CHANNEL(server);
47 WINE_DECLARE_DEBUG_CHANNEL(relay);
49 static UINT process_error_mode;
51 static HANDLE main_exe_file;
52 static DWORD shutdown_flags = 0;
53 static DWORD shutdown_priority = 0x280;
54 static DWORD process_dword;
55 static BOOL oem_file_apis;
57 static unsigned int server_startticks;
58 int main_create_flags = 0;
61 #define PDB32_DEBUGGED 0x0001 /* Process is being debugged */
62 #define PDB32_WIN16_PROC 0x0008 /* Win16 process */
63 #define PDB32_DOS_PROC 0x0010 /* Dos process */
64 #define PDB32_CONSOLE_PROC 0x0020 /* Console process */
65 #define PDB32_FILE_APIS_OEM 0x0040 /* File APIs are OEM */
66 #define PDB32_WIN32S_PROC 0x8000 /* Win32s process */
68 static const WCHAR comW[] = {'c','o','m',0};
69 static const WCHAR batW[] = {'b','a','t',0};
70 static const WCHAR winevdmW[] = {'w','i','n','e','v','d','m','.','e','x','e',0};
72 extern void SHELL_LoadRegistry(void);
73 extern void VERSION_Init( const WCHAR *appname );
74 extern void MODULE_InitLoadPath(void);
76 /***********************************************************************
79 inline static int contains_path( LPCWSTR name )
81 return ((*name && (name[1] == ':')) || strchrW(name, '/') || strchrW(name, '\\'));
85 /***************************************************************************
88 * Get the path of a builtin module when the native file does not exist.
90 static BOOL get_builtin_path( const WCHAR *libname, const WCHAR *ext, WCHAR *filename, UINT size )
93 WCHAR sysdir[MAX_PATH];
94 UINT len = GetSystemDirectoryW( sysdir, MAX_PATH );
96 if (contains_path( libname ))
98 if (RtlGetFullPathName_U( libname, size * sizeof(WCHAR),
99 filename, &file_part ) > size * sizeof(WCHAR))
100 return FALSE; /* too long */
102 if (strncmpiW( filename, sysdir, len ) || filename[len] != '\\')
104 while (filename[len] == '\\') len++;
105 if (filename != file_part) return FALSE;
109 if (strlenW(libname) + len + 2 >= size) return FALSE; /* too long */
110 memcpy( filename, sysdir, len * sizeof(WCHAR) );
111 file_part = filename + len;
112 if (file_part > filename && file_part[-1] != '\\') *file_part++ = '\\';
113 strcpyW( file_part, libname );
115 if (ext && !strchrW( file_part, '.' ))
117 if (file_part + strlenW(file_part) + strlenW(ext) + 1 > filename + size)
118 return FALSE; /* too long */
119 strcatW( file_part, ext );
125 /***********************************************************************
126 * open_builtin_exe_file
128 * Open an exe file for a builtin exe.
130 static void *open_builtin_exe_file( const WCHAR *name, char *error, int error_size,
131 int test_only, int *file_exists )
133 char exename[MAX_PATH];
137 if ((p = strrchrW( name, '/' ))) name = p + 1;
138 if ((p = strrchrW( name, '\\' ))) name = p + 1;
140 /* we don't want to depend on the current codepage here */
141 len = strlenW( name ) + 1;
142 if (len >= sizeof(exename)) return NULL;
143 for (i = 0; i < len; i++)
145 if (name[i] > 127) return NULL;
146 exename[i] = (char)name[i];
147 if (exename[i] >= 'A' && exename[i] <= 'Z') exename[i] += 'a' - 'A';
149 return wine_dll_load_main_exe( exename, error, error_size, test_only, file_exists );
153 /***********************************************************************
156 * Open a specific exe file, taking load order into account.
157 * Returns the file handle or 0 for a builtin exe.
159 static HANDLE open_exe_file( const WCHAR *name )
161 enum loadorder_type loadorder[LOADORDER_NTYPES];
162 WCHAR buffer[MAX_PATH];
166 TRACE("looking for %s\n", debugstr_w(name) );
168 if ((handle = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ,
169 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
171 /* file doesn't exist, check for builtin */
172 if (!contains_path( name )) goto error;
173 if (!get_builtin_path( name, NULL, buffer, sizeof(buffer) )) goto error;
177 MODULE_GetLoadOrderW( loadorder, NULL, name, TRUE );
179 for(i = 0; i < LOADORDER_NTYPES; i++)
181 if (loadorder[i] == LOADORDER_INVALID) break;
185 TRACE( "Trying native exe %s\n", debugstr_w(name) );
186 if (handle != INVALID_HANDLE_VALUE) return handle;
189 TRACE( "Trying built-in exe %s\n", debugstr_w(name) );
190 open_builtin_exe_file( name, NULL, 0, 1, &file_exists );
193 if (handle != INVALID_HANDLE_VALUE) CloseHandle(handle);
200 if (handle != INVALID_HANDLE_VALUE) CloseHandle(handle);
203 SetLastError( ERROR_FILE_NOT_FOUND );
204 return INVALID_HANDLE_VALUE;
208 /***********************************************************************
211 * Open an exe file, and return the full name and file handle.
212 * Returns FALSE if file could not be found.
213 * If file exists but cannot be opened, returns TRUE and set handle to INVALID_HANDLE_VALUE.
214 * If file is a builtin exe, returns TRUE and sets handle to 0.
216 static BOOL find_exe_file( const WCHAR *name, WCHAR *buffer, int buflen, HANDLE *handle )
218 static const WCHAR exeW[] = {'.','e','x','e',0};
220 enum loadorder_type loadorder[LOADORDER_NTYPES];
223 TRACE("looking for %s\n", debugstr_w(name) );
225 if (!SearchPathW( NULL, name, exeW, buflen, buffer, NULL ) &&
226 !get_builtin_path( name, exeW, buffer, buflen ))
228 /* no builtin found, try native without extension in case it is a Unix app */
230 if (SearchPathW( NULL, name, NULL, buflen, buffer, NULL ))
232 TRACE( "Trying native/Unix binary %s\n", debugstr_w(buffer) );
233 if ((*handle = CreateFileW( buffer, GENERIC_READ, FILE_SHARE_READ,
234 NULL, OPEN_EXISTING, 0, 0 )) != INVALID_HANDLE_VALUE)
240 MODULE_GetLoadOrderW( loadorder, NULL, buffer, TRUE );
242 for(i = 0; i < LOADORDER_NTYPES; i++)
244 if (loadorder[i] == LOADORDER_INVALID) break;
248 TRACE( "Trying native exe %s\n", debugstr_w(buffer) );
249 if ((*handle = CreateFileW( buffer, GENERIC_READ, FILE_SHARE_READ,
250 NULL, OPEN_EXISTING, 0, 0 )) != INVALID_HANDLE_VALUE)
252 if (GetLastError() != ERROR_FILE_NOT_FOUND) return TRUE;
255 TRACE( "Trying built-in exe %s\n", debugstr_w(buffer) );
256 open_builtin_exe_file( buffer, NULL, 0, 1, &file_exists );
267 SetLastError( ERROR_FILE_NOT_FOUND );
272 /**********************************************************************
275 * Load a PE format EXE file.
277 static HMODULE load_pe_exe( const WCHAR *name, HANDLE file )
279 IMAGE_NT_HEADERS *nt;
282 OBJECT_ATTRIBUTES attr;
287 attr.Length = sizeof(attr);
288 attr.RootDirectory = 0;
289 attr.ObjectName = NULL;
291 attr.SecurityDescriptor = NULL;
292 attr.SecurityQualityOfService = NULL;
295 if (NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
296 &attr, &size, 0, SEC_IMAGE, file ) != STATUS_SUCCESS)
300 if (NtMapViewOfSection( mapping, GetCurrentProcess(), &module, 0, 0, &size, &len,
301 ViewShare, 0, PAGE_READONLY ) != STATUS_SUCCESS)
307 nt = RtlImageNtHeader( module );
308 if (nt->OptionalHeader.AddressOfEntryPoint)
310 if (!RtlImageRvaToSection( nt, module, nt->OptionalHeader.AddressOfEntryPoint ))
311 MESSAGE("VIRUS WARNING: PE module %s has an invalid entrypoint (0x%08lx) "
312 "outside all sections (possibly infected by Tchernobyl/SpaceFiller virus)!\n",
313 debugstr_w(name), nt->OptionalHeader.AddressOfEntryPoint );
316 drive_type = GetDriveTypeW( name );
317 /* don't keep the file handle open on removable media */
318 if (drive_type == DRIVE_REMOVABLE || drive_type == DRIVE_CDROM)
320 CloseHandle( main_exe_file );
327 /***********************************************************************
330 * Build the Win32 environment from the Unix environment
332 static BOOL build_initial_environment(void)
334 extern char **environ;
340 /* Compute the total size of the Unix environment */
341 for (e = environ; *e; e++)
343 if (!memcmp(*e, "PATH=", 5)) continue;
344 size += MultiByteToWideChar( CP_UNIXCP, 0, *e, -1, NULL, 0 );
346 size *= sizeof(WCHAR);
348 /* Now allocate the environment */
349 if (NtAllocateVirtualMemory(NtCurrentProcess(), &ptr, 0, &size,
350 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) != STATUS_SUCCESS)
353 NtCurrentTeb()->Peb->ProcessParameters->Environment = p = ptr;
354 endptr = p + size / sizeof(WCHAR);
356 /* And fill it with the Unix environment */
357 for (e = environ; *e; e++)
360 /* skip Unix PATH and store WINEPATH as PATH */
361 if (!memcmp(str, "PATH=", 5)) continue;
362 if (!memcmp(str, "WINEPATH=", 9 )) str += 4;
363 MultiByteToWideChar( CP_UNIXCP, 0, str, -1, p, endptr - p );
371 /***********************************************************************
374 * Set the Wine library Unicode argv global variables.
376 static void set_library_wargv( char **argv )
383 for (argc = 0; argv[argc]; argc++)
384 total += MultiByteToWideChar( CP_UNIXCP, 0, argv[argc], -1, NULL, 0 );
386 wargv = RtlAllocateHeap( GetProcessHeap(), 0,
387 total * sizeof(WCHAR) + (argc + 1) * sizeof(*wargv) );
388 p = (WCHAR *)(wargv + argc + 1);
389 for (argc = 0; argv[argc]; argc++)
391 DWORD reslen = MultiByteToWideChar( CP_UNIXCP, 0, argv[argc], -1, p, total );
397 __wine_main_wargv = wargv;
401 /***********************************************************************
404 * Build the command line of a process from the argv array.
406 * Note that it does NOT necessarily include the file name.
407 * Sometimes we don't even have any command line options at all.
409 * We must quote and escape characters so that the argv array can be rebuilt
410 * from the command line:
411 * - spaces and tabs must be quoted
413 * - quotes must be escaped
415 * - if '\'s are followed by a '"', they must be doubled and followed by '\"',
416 * resulting in an odd number of '\' followed by a '"'
419 * - '\'s that are not followed by a '"' can be left as is
423 static BOOL build_command_line( WCHAR **argv )
428 RTL_USER_PROCESS_PARAMETERS* rupp = NtCurrentTeb()->Peb->ProcessParameters;
430 if (rupp->CommandLine.Buffer) return TRUE; /* already got it from the server */
433 for (arg = argv; *arg; arg++)
435 int has_space,bcount;
441 if( !*a ) has_space=1;
446 if (*a==' ' || *a=='\t') {
448 } else if (*a=='"') {
449 /* doubling of '\' preceeding a '"',
450 * plus escaping of said '"'
458 len+=(a-*arg)+1 /* for the separating space */;
460 len+=2; /* for the quotes */
463 if (!(rupp->CommandLine.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR))))
466 p = rupp->CommandLine.Buffer;
467 rupp->CommandLine.Length = (len - 1) * sizeof(WCHAR);
468 rupp->CommandLine.MaximumLength = len * sizeof(WCHAR);
469 for (arg = argv; *arg; arg++)
471 int has_space,has_quote;
474 /* Check for quotes and spaces in this argument */
475 has_space=has_quote=0;
477 if( !*a ) has_space=1;
479 if (*a==' ' || *a=='\t') {
483 } else if (*a=='"') {
491 /* Now transfer it to the command line */
508 /* Double all the '\\' preceeding this '"', plus one */
509 for (i=0;i<=bcount;i++)
521 while ((*p=*x++)) p++;
527 if (p > rupp->CommandLine.Buffer)
528 p--; /* remove last space */
535 /* make sure the unicode string doesn't point beyond the end pointer */
536 static inline void fix_unicode_string( UNICODE_STRING *str, char *end_ptr )
538 if ((char *)str->Buffer >= end_ptr)
540 str->Length = str->MaximumLength = 0;
544 if ((char *)str->Buffer + str->MaximumLength > end_ptr)
546 str->MaximumLength = (end_ptr - (char *)str->Buffer) & ~(sizeof(WCHAR) - 1);
548 if (str->Length >= str->MaximumLength)
550 if (str->MaximumLength >= sizeof(WCHAR))
551 str->Length = str->MaximumLength - sizeof(WCHAR);
553 str->Length = str->MaximumLength = 0;
558 /***********************************************************************
559 * init_user_process_params
561 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
563 static RTL_USER_PROCESS_PARAMETERS *init_user_process_params( size_t info_size )
568 RTL_USER_PROCESS_PARAMETERS *params;
571 if ((status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, NULL, &size,
572 MEM_COMMIT, PAGE_READWRITE )) != STATUS_SUCCESS)
575 SERVER_START_REQ( get_startup_info )
577 wine_server_set_reply( req, ptr, info_size );
578 wine_server_call( req );
579 info_size = wine_server_reply_size( reply );
584 params->Size = info_size;
585 params->AllocationSize = size;
587 /* make sure the strings are valid */
588 fix_unicode_string( ¶ms->CurrentDirectoryName, (char *)info_size );
589 fix_unicode_string( ¶ms->DllPath, (char *)info_size );
590 fix_unicode_string( ¶ms->ImagePathName, (char *)info_size );
591 fix_unicode_string( ¶ms->CommandLine, (char *)info_size );
592 fix_unicode_string( ¶ms->WindowTitle, (char *)info_size );
593 fix_unicode_string( ¶ms->Desktop, (char *)info_size );
594 fix_unicode_string( ¶ms->ShellInfo, (char *)info_size );
595 fix_unicode_string( ¶ms->RuntimeInfo, (char *)info_size );
597 return RtlNormalizeProcessParams( params );
601 /***********************************************************************
604 * Main process initialisation code
606 static BOOL process_init( char *argv[] )
609 size_t info_size = 0;
610 RTL_USER_PROCESS_PARAMETERS *params;
611 PEB *peb = NtCurrentTeb()->Peb;
612 HANDLE hstdin, hstdout, hstderr;
616 setlocale(LC_CTYPE,"");
618 /* Retrieve startup info from the server */
619 SERVER_START_REQ( init_process )
621 req->ldt_copy = &wine_ldt_copy;
622 if ((ret = !wine_server_call_err( req )))
624 main_exe_file = reply->exe_file;
625 main_create_flags = reply->create_flags;
626 info_size = reply->info_size;
627 server_startticks = reply->server_start;
628 hstdin = reply->hstdin;
629 hstdout = reply->hstdout;
630 hstderr = reply->hstderr;
634 if (!ret) return FALSE;
638 params = peb->ProcessParameters;
640 /* This is wine specific: we have no parent (we're started from unix)
641 * so, create a simple console with bare handles to unix stdio
642 * input & output streams (aka simple console)
644 wine_server_fd_to_handle( 0, GENERIC_READ|SYNCHRONIZE, TRUE, ¶ms->hStdInput );
645 wine_server_fd_to_handle( 1, GENERIC_WRITE|SYNCHRONIZE, TRUE, ¶ms->hStdOutput );
646 wine_server_fd_to_handle( 2, GENERIC_WRITE|SYNCHRONIZE, TRUE, ¶ms->hStdError );
648 /* <hack: to be changed later on> */
649 params->CurrentDirectoryName.Length = 3 * sizeof(WCHAR);
650 params->CurrentDirectoryName.MaximumLength = RtlGetLongestNtPathLength() * sizeof(WCHAR);
651 params->CurrentDirectoryName.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, params->CurrentDirectoryName.MaximumLength);
652 params->CurrentDirectoryName.Buffer[0] = 'C';
653 params->CurrentDirectoryName.Buffer[1] = ':';
654 params->CurrentDirectoryName.Buffer[2] = '\\';
655 params->CurrentDirectoryName.Buffer[3] = '\0';
656 /* </hack: to be changed later on> */
660 if (!(params = init_user_process_params( info_size ))) return FALSE;
661 peb->ProcessParameters = params;
663 /* convert value from server:
664 * + 0 => INVALID_HANDLE_VALUE
665 * + console handle need to be mapped
668 hstdin = INVALID_HANDLE_VALUE;
669 else if (VerifyConsoleIoHandle(console_handle_map(hstdin)))
670 hstdin = console_handle_map(hstdin);
673 hstdout = INVALID_HANDLE_VALUE;
674 else if (VerifyConsoleIoHandle(console_handle_map(hstdout)))
675 hstdout = console_handle_map(hstdout);
678 hstderr = INVALID_HANDLE_VALUE;
679 else if (VerifyConsoleIoHandle(console_handle_map(hstderr)))
680 hstderr = console_handle_map(hstderr);
682 params->hStdInput = hstdin;
683 params->hStdOutput = hstdout;
684 params->hStdError = hstderr;
687 /* Copy the parent environment */
688 if (!build_initial_environment()) return FALSE;
690 /* Parse command line arguments */
691 OPTIONS_ParseOptions( !info_size ? argv : NULL );
693 /* initialise DOS drives */
694 if (!DRIVE_Init()) return FALSE;
696 /* initialise DOS directories */
697 if (!DIR_Init()) return FALSE;
699 /* registry initialisation */
700 SHELL_LoadRegistry();
702 /* global boot finished, the rest is process-local */
703 SERVER_START_REQ( boot_done )
705 req->debug_level = TRACE_ON(server);
706 wine_server_call( req );
714 /***********************************************************************
717 * Startup routine of a new process. Runs on the new process stack.
719 static void start_process( void *arg )
723 LdrInitializeThunk( main_exe_file, CreateFileW, 0, 0 );
725 __EXCEPT(UnhandledExceptionFilter)
727 TerminateThread( GetCurrentThread(), GetExceptionCode() );
733 /***********************************************************************
736 * Wine initialisation: load and start the main exe file.
738 void __wine_kernel_init(void)
740 WCHAR *main_exe_name, *p;
742 DWORD stack_size = 0;
744 PEB *peb = NtCurrentTeb()->Peb;
746 /* Initialize everything */
747 if (!process_init( __wine_main_argv )) exit(1);
749 __wine_main_argv++; /* remove argv[0] (wine itself) */
752 if (!(main_exe_name = peb->ProcessParameters->ImagePathName.Buffer))
754 WCHAR buffer[MAX_PATH];
755 WCHAR exe_nameW[MAX_PATH];
757 if (!__wine_main_argv[0]) OPTIONS_Usage();
759 /* FIXME: locale info not loaded yet */
760 MultiByteToWideChar( CP_UNIXCP, 0, __wine_main_argv[0], -1, exe_nameW, MAX_PATH );
761 if (!find_exe_file( exe_nameW, buffer, MAX_PATH, &main_exe_file ))
763 MESSAGE( "wine: cannot find '%s'\n", __wine_main_argv[0] );
766 if (main_exe_file == INVALID_HANDLE_VALUE)
768 MESSAGE( "wine: cannot open %s\n", debugstr_w(main_exe_name) );
771 RtlCreateUnicodeString( &peb->ProcessParameters->ImagePathName, buffer );
772 main_exe_name = peb->ProcessParameters->ImagePathName.Buffer;
775 TRACE( "starting process name=%s file=%p argv[0]=%s\n",
776 debugstr_w(main_exe_name), main_exe_file, debugstr_a(__wine_main_argv[0]) );
778 MODULE_InitLoadPath();
779 VERSION_Init( main_exe_name );
781 if (!main_exe_file) /* no file handle -> Winelib app */
783 TRACE( "starting Winelib app %s\n", debugstr_w(main_exe_name) );
784 if (open_builtin_exe_file( main_exe_name, error, sizeof(error), 0, &file_exists ))
786 MESSAGE( "wine: cannot open builtin library for %s: %s\n",
787 debugstr_w(main_exe_name), error );
791 switch( MODULE_GetBinaryType( main_exe_file ))
794 TRACE( "starting Win32 binary %s\n", debugstr_w(main_exe_name) );
795 if ((peb->ImageBaseAddress = load_pe_exe( main_exe_name, main_exe_file )))
797 MESSAGE( "wine: could not load %s as Win32 binary\n", debugstr_w(main_exe_name) );
800 MESSAGE( "wine: %s is a DLL, not an executable\n", debugstr_w(main_exe_name) );
803 /* check for .com extension */
804 if (!(p = strrchrW( main_exe_name, '.' )) || strcmpiW( p, comW ))
806 MESSAGE( "wine: cannot determine executable type for %s\n",
807 debugstr_w(main_exe_name) );
813 TRACE( "starting Win16/DOS binary %s\n", debugstr_w(main_exe_name) );
814 CloseHandle( main_exe_file );
818 __wine_main_argv[0] = "winevdm.exe";
819 if (open_builtin_exe_file( winevdmW, error, sizeof(error), 0, &file_exists ))
821 MESSAGE( "wine: trying to run %s, cannot open builtin library for 'winevdm.exe': %s\n",
822 debugstr_w(main_exe_name), error );
825 MESSAGE( "wine: %s is an OS/2 binary, not supported\n", debugstr_w(main_exe_name) );
827 case BINARY_UNIX_EXE:
828 MESSAGE( "wine: %s is a Unix binary, not supported\n", debugstr_w(main_exe_name) );
830 case BINARY_UNIX_LIB:
832 DOS_FULL_NAME full_name;
834 TRACE( "starting Winelib app %s\n", debugstr_w(main_exe_name) );
835 CloseHandle( main_exe_file );
837 if (DOSFS_GetFullName( main_exe_name, TRUE, &full_name ) &&
838 wine_dlopen( full_name.long_name, RTLD_NOW, error, sizeof(error) ))
840 static const WCHAR soW[] = {'.','s','o',0};
841 if ((p = strrchrW( main_exe_name, '.' )) && !strcmpW( p, soW ))
844 /* update the unicode string */
845 RtlInitUnicodeString( &peb->ProcessParameters->ImagePathName, main_exe_name );
849 MESSAGE( "wine: could not load %s: %s\n", debugstr_w(main_exe_name), error );
855 /* build command line */
856 set_library_wargv( __wine_main_argv );
857 if (!build_command_line( __wine_main_wargv )) goto error;
859 stack_size = RtlImageNtHeader(peb->ImageBaseAddress)->OptionalHeader.SizeOfStackReserve;
861 /* allocate main thread stack */
862 if (!THREAD_InitStack( NtCurrentTeb(), stack_size )) goto error;
864 /* switch to the new stack */
865 wine_switch_to_stack( start_process, NULL, NtCurrentTeb()->Tib.StackBase );
868 ExitProcess( GetLastError() );
872 /***********************************************************************
875 * Build an argv array from a command-line.
876 * 'reserved' is the number of args to reserve before the first one.
878 static char **build_argv( const WCHAR *cmdlineW, int reserved )
882 char *arg,*s,*d,*cmdline;
883 int in_quotes,bcount,len;
885 len = WideCharToMultiByte( CP_UNIXCP, 0, cmdlineW, -1, NULL, 0, NULL, NULL );
886 if (!(cmdline = malloc(len))) return NULL;
887 WideCharToMultiByte( CP_UNIXCP, 0, cmdlineW, -1, cmdline, len, NULL, NULL );
894 if (*s=='\0' || ((*s==' ' || *s=='\t') && !in_quotes)) {
897 /* skip the remaining spaces */
898 while (*s==' ' || *s=='\t') {
905 } else if (*s=='\\') {
906 /* '\', count them */
908 } else if ((*s=='"') && ((bcount & 1)==0)) {
910 in_quotes=!in_quotes;
913 /* a regular character */
918 argv=malloc(argc*sizeof(*argv));
927 if ((*s==' ' || *s=='\t') && !in_quotes) {
928 /* Close the argument and copy it */
932 /* skip the remaining spaces */
935 } while (*s==' ' || *s=='\t');
937 /* Start with a new argument */
940 } else if (*s=='\\') {
944 } else if (*s=='"') {
946 if ((bcount & 1)==0) {
947 /* Preceeded by an even number of '\', this is half that
948 * number of '\', plus a '"' which we discard.
952 in_quotes=!in_quotes;
954 /* Preceeded by an odd number of '\', this is half that
955 * number of '\' followed by a '"'
963 /* a regular character */
978 /***********************************************************************
981 * Build the environment of a new child process.
983 static char **build_envp( const WCHAR *envW, const WCHAR *extra_envW )
987 char *env, *extra_env = NULL;
988 int count = 0, length;
992 for (p = extra_envW; *p; count++) p += strlenW(p) + 1;
994 length = WideCharToMultiByte( CP_UNIXCP, 0, extra_envW, p - extra_envW,
995 NULL, 0, NULL, NULL );
996 if ((extra_env = malloc( length )))
997 WideCharToMultiByte( CP_UNIXCP, 0, extra_envW, p - extra_envW,
998 extra_env, length, NULL, NULL );
1000 for (p = envW; *p; count++) p += strlenW(p) + 1;
1002 length = WideCharToMultiByte( CP_UNIXCP, 0, envW, p - envW, NULL, 0, NULL, NULL );
1003 if (!(env = malloc( length ))) return NULL;
1004 WideCharToMultiByte( CP_UNIXCP, 0, envW, p - envW, env, length, NULL, NULL );
1008 if ((envp = malloc( count * sizeof(*envp) )))
1010 extern char **environ;
1011 char **envptr = envp;
1012 char **unixptr = environ;
1015 /* first the extra strings */
1016 if (extra_env) for (p = extra_env; *p; p += strlen(p) + 1) *envptr++ = p;
1017 /* then put PATH, HOME and WINEPREFIX from the unix env */
1018 for (unixptr = environ; unixptr && *unixptr; unixptr++)
1019 if (!memcmp( *unixptr, "PATH=", 5 ) ||
1020 !memcmp( *unixptr, "HOME=", 5 ) ||
1021 !memcmp( *unixptr, "WINEPREFIX=", 11 )) *envptr++ = *unixptr;
1022 /* now put the Windows environment strings */
1023 for (p = env; *p; p += strlen(p) + 1)
1025 if (!memcmp( p, "PATH=", 5 )) /* store PATH as WINEPATH */
1027 char *winepath = malloc( strlen(p) + 5 );
1028 strcpy( winepath, "WINE" );
1029 strcpy( winepath + 4, p );
1030 *envptr++ = winepath;
1032 else if (memcmp( p, "HOME=", 5 ) &&
1033 memcmp( p, "WINEPATH=", 9 ) &&
1034 memcmp( p, "WINEPREFIX=", 11 )) *envptr++ = p;
1042 /***********************************************************************
1045 * Locate the Wine binary to exec for a new Win32 process.
1047 static void exec_wine_binary( char **argv, char **envp )
1049 const char *path, *pos, *ptr;
1051 /* first, try for a WINELOADER environment variable */
1052 argv[0] = getenv("WINELOADER");
1054 execve( argv[0], argv, envp );
1056 /* next, try bin directory */
1057 argv[0] = BINDIR "/wine";
1058 execve( argv[0], argv, envp );
1060 /* now try the path of argv0 of the current binary */
1061 if ((path = wine_get_argv0_path()))
1063 if (!(argv[0] = malloc( strlen(path) + sizeof("wine") ))) return;
1064 strcpy( argv[0], path );
1065 strcat( argv[0], "wine" );
1066 execve( argv[0], argv, envp );
1070 /* now search in the Unix path */
1071 if ((path = getenv( "PATH" )))
1073 if (!(argv[0] = malloc( strlen(path) + 6 ))) return;
1077 while (*pos == ':') pos++;
1079 if (!(ptr = strchr( pos, ':' ))) ptr = pos + strlen(pos);
1080 memcpy( argv[0], pos, ptr - pos );
1081 strcpy( argv[0] + (ptr - pos), "/wine" );
1082 execve( argv[0], argv, envp );
1090 /***********************************************************************
1093 * Fork and exec a new Unix binary, checking for errors.
1095 static int fork_and_exec( const char *filename, const WCHAR *cmdline,
1096 const WCHAR *env, const char *newdir )
1101 if (!env) env = GetEnvironmentStringsW();
1108 fcntl( fd[1], F_SETFD, 1 ); /* set close on exec */
1109 if (!(pid = fork())) /* child */
1111 char **argv = build_argv( cmdline, 0 );
1112 char **envp = build_envp( env, NULL );
1115 /* Reset signals that we previously set to SIG_IGN */
1116 signal( SIGPIPE, SIG_DFL );
1117 signal( SIGCHLD, SIG_DFL );
1119 if (newdir) chdir(newdir);
1121 if (argv && envp) execve( filename, argv, envp );
1123 write( fd[1], &err, sizeof(err) );
1127 if ((pid != -1) && (read( fd[0], &err, sizeof(err) ) > 0)) /* exec failed */
1132 if (pid == -1) FILE_SetDosError();
1138 /***********************************************************************
1139 * create_user_params
1141 static RTL_USER_PROCESS_PARAMETERS *create_user_params( LPCWSTR filename, LPCWSTR cmdline,
1142 const STARTUPINFOW *startup )
1144 RTL_USER_PROCESS_PARAMETERS *params;
1145 UNICODE_STRING image_str, cmdline_str, desktop, title;
1147 WCHAR buffer[MAX_PATH];
1149 if (GetLongPathNameW( filename, buffer, MAX_PATH ))
1150 RtlInitUnicodeString( &image_str, buffer );
1152 RtlInitUnicodeString( &image_str, filename );
1154 RtlInitUnicodeString( &cmdline_str, cmdline );
1155 if (startup->lpDesktop) RtlInitUnicodeString( &desktop, startup->lpDesktop );
1156 if (startup->lpTitle) RtlInitUnicodeString( &title, startup->lpTitle );
1158 status = RtlCreateProcessParameters( ¶ms, &image_str, NULL, NULL, &cmdline_str, NULL,
1159 startup->lpTitle ? &title : NULL,
1160 startup->lpDesktop ? &desktop : NULL,
1162 if (status != STATUS_SUCCESS)
1164 SetLastError( RtlNtStatusToDosError(status) );
1168 params->Environment = NULL; /* we pass it through the Unix environment */
1169 params->hStdInput = startup->hStdInput;
1170 params->hStdOutput = startup->hStdOutput;
1171 params->hStdError = startup->hStdError;
1172 params->dwX = startup->dwX;
1173 params->dwY = startup->dwY;
1174 params->dwXSize = startup->dwXSize;
1175 params->dwYSize = startup->dwYSize;
1176 params->dwXCountChars = startup->dwXCountChars;
1177 params->dwYCountChars = startup->dwYCountChars;
1178 params->dwFillAttribute = startup->dwFillAttribute;
1179 params->dwFlags = startup->dwFlags;
1180 params->wShowWindow = startup->wShowWindow;
1185 /***********************************************************************
1188 * Create a new process. If hFile is a valid handle we have an exe
1189 * file, otherwise it is a Winelib app.
1191 static BOOL create_process( HANDLE hFile, LPCWSTR filename, LPWSTR cmd_line, LPWSTR env,
1192 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1193 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1194 LPPROCESS_INFORMATION info, LPCSTR unixdir )
1196 BOOL ret, success = FALSE;
1197 HANDLE process_info;
1198 RTL_USER_PROCESS_PARAMETERS *params;
1199 WCHAR *extra_env = NULL;
1208 env = GetEnvironmentStringsW();
1209 extra_env = DRIVE_BuildEnv();
1212 if (!(params = create_user_params( filename, cmd_line, startup )))
1214 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1218 /* create the synchronization pipes */
1220 if (pipe( startfd ) == -1)
1223 RtlDestroyProcessParameters( params );
1224 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1227 if (pipe( execfd ) == -1)
1230 close( startfd[0] );
1231 close( startfd[1] );
1232 RtlDestroyProcessParameters( params );
1233 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1236 fcntl( execfd[1], F_SETFD, 1 ); /* set close on exec */
1238 /* create the child process */
1240 if (!(pid = fork())) /* child */
1242 char **argv = build_argv( cmd_line, 1 );
1243 char **envp = build_envp( env, extra_env );
1245 close( startfd[1] );
1248 /* wait for parent to tell us to start */
1249 if (read( startfd[0], &dummy, 1 ) != 1) _exit(1);
1251 close( startfd[0] );
1252 /* Reset signals that we previously set to SIG_IGN */
1253 signal( SIGPIPE, SIG_DFL );
1254 signal( SIGCHLD, SIG_DFL );
1256 if (unixdir) chdir(unixdir);
1258 if (argv && envp) exec_wine_binary( argv, envp );
1261 write( execfd[1], &err, sizeof(err) );
1265 /* this is the parent */
1267 close( startfd[0] );
1269 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1272 close( startfd[1] );
1275 RtlDestroyProcessParameters( params );
1279 /* create the process on the server side */
1281 SERVER_START_REQ( new_process )
1283 req->inherit_all = inherit;
1284 req->create_flags = flags;
1285 req->unix_pid = pid;
1286 req->exe_file = hFile;
1287 if (startup->dwFlags & STARTF_USESTDHANDLES)
1289 req->hstdin = startup->hStdInput;
1290 req->hstdout = startup->hStdOutput;
1291 req->hstderr = startup->hStdError;
1295 req->hstdin = GetStdHandle( STD_INPUT_HANDLE );
1296 req->hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
1297 req->hstderr = GetStdHandle( STD_ERROR_HANDLE );
1300 if ((flags & (CREATE_NEW_CONSOLE | DETACHED_PROCESS)) != 0)
1302 /* this is temporary (for console handles). We have no way to control that the handle is invalid in child process otherwise */
1303 if (is_console_handle(req->hstdin)) req->hstdin = INVALID_HANDLE_VALUE;
1304 if (is_console_handle(req->hstdout)) req->hstdout = INVALID_HANDLE_VALUE;
1305 if (is_console_handle(req->hstderr)) req->hstderr = INVALID_HANDLE_VALUE;
1309 if (is_console_handle(req->hstdin)) req->hstdin = console_handle_unmap(req->hstdin);
1310 if (is_console_handle(req->hstdout)) req->hstdout = console_handle_unmap(req->hstdout);
1311 if (is_console_handle(req->hstderr)) req->hstderr = console_handle_unmap(req->hstderr);
1314 wine_server_add_data( req, params, params->Size );
1315 ret = !wine_server_call_err( req );
1316 process_info = reply->info;
1320 RtlDestroyProcessParameters( params );
1323 close( startfd[1] );
1328 /* tell child to start and wait for it to exec */
1330 write( startfd[1], &dummy, 1 );
1331 close( startfd[1] );
1333 if (read( execfd[0], &err, sizeof(err) ) > 0) /* exec failed */
1338 CloseHandle( process_info );
1342 /* wait for the new process info to be ready */
1344 WaitForSingleObject( process_info, INFINITE );
1345 SERVER_START_REQ( get_new_process_info )
1347 req->info = process_info;
1348 req->pinherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
1349 req->tinherit = (tsa && (tsa->nLength >= sizeof(*tsa)) && tsa->bInheritHandle);
1350 if ((ret = !wine_server_call_err( req )))
1352 info->dwProcessId = (DWORD)reply->pid;
1353 info->dwThreadId = (DWORD)reply->tid;
1354 info->hProcess = reply->phandle;
1355 info->hThread = reply->thandle;
1356 success = reply->success;
1361 if (ret && !success) /* new process failed to start */
1364 if (GetExitCodeProcess( info->hProcess, &exitcode )) SetLastError( exitcode );
1365 CloseHandle( info->hThread );
1366 CloseHandle( info->hProcess );
1369 CloseHandle( process_info );
1374 /***********************************************************************
1375 * create_vdm_process
1377 * Create a new VDM process for a 16-bit or DOS application.
1379 static BOOL create_vdm_process( LPCWSTR filename, LPWSTR cmd_line, LPWSTR env,
1380 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1381 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1382 LPPROCESS_INFORMATION info, LPCSTR unixdir )
1384 static const WCHAR argsW[] = {'%','s',' ','-','-','a','p','p','-','n','a','m','e',' ','"','%','s','"',' ','%','s',0};
1387 LPWSTR new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
1388 (strlenW(filename) + strlenW(cmd_line) + 30) * sizeof(WCHAR) );
1392 SetLastError( ERROR_OUTOFMEMORY );
1395 sprintfW( new_cmd_line, argsW, winevdmW, filename, cmd_line );
1396 ret = create_process( 0, winevdmW, new_cmd_line, env, psa, tsa, inherit,
1397 flags, startup, info, unixdir );
1398 HeapFree( GetProcessHeap(), 0, new_cmd_line );
1403 /***********************************************************************
1404 * create_cmd_process
1406 * Create a new cmd shell process for a .BAT file.
1408 static BOOL create_cmd_process( LPCWSTR filename, LPWSTR cmd_line, LPVOID env,
1409 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1410 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1411 LPPROCESS_INFORMATION info, LPCWSTR cur_dir )
1414 static const WCHAR comspecW[] = {'C','O','M','S','P','E','C',0};
1415 static const WCHAR slashcW[] = {' ','/','c',' ',0};
1416 WCHAR comspec[MAX_PATH];
1420 if (!GetEnvironmentVariableW( comspecW, comspec, sizeof(comspec)/sizeof(WCHAR) ))
1422 if (!(newcmdline = HeapAlloc( GetProcessHeap(), 0,
1423 (strlenW(comspec) + 4 + strlenW(cmd_line) + 1) * sizeof(WCHAR))))
1426 strcpyW( newcmdline, comspec );
1427 strcatW( newcmdline, slashcW );
1428 strcatW( newcmdline, cmd_line );
1429 ret = CreateProcessW( comspec, newcmdline, psa, tsa, inherit,
1430 flags, env, cur_dir, startup, info );
1431 HeapFree( GetProcessHeap(), 0, newcmdline );
1436 /*************************************************************************
1439 * Helper for CreateProcess: retrieve the file name to load from the
1440 * app name and command line. Store the file name in buffer, and
1441 * return a possibly modified command line.
1442 * Also returns a handle to the opened file if it's a Windows binary.
1444 static LPWSTR get_file_name( LPCWSTR appname, LPWSTR cmdline, LPWSTR buffer,
1445 int buflen, HANDLE *handle )
1447 static const WCHAR quotesW[] = {'"','%','s','"',0};
1449 WCHAR *name, *pos, *ret = NULL;
1452 /* if we have an app name, everything is easy */
1456 /* use the unmodified app name as file name */
1457 lstrcpynW( buffer, appname, buflen );
1458 *handle = open_exe_file( buffer );
1459 if (!(ret = cmdline) || !cmdline[0])
1461 /* no command-line, create one */
1462 if ((ret = HeapAlloc( GetProcessHeap(), 0, (strlenW(appname) + 3) + sizeof(WCHAR) )))
1463 sprintfW( ret, quotesW, appname );
1470 SetLastError( ERROR_INVALID_PARAMETER );
1474 /* first check for a quoted file name */
1476 if ((cmdline[0] == '"') && ((p = strchrW( cmdline + 1, '"' ))))
1478 int len = p - cmdline - 1;
1479 /* extract the quoted portion as file name */
1480 if (!(name = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return NULL;
1481 memcpy( name, cmdline + 1, len * sizeof(WCHAR) );
1484 if (find_exe_file( name, buffer, buflen, handle ))
1485 ret = cmdline; /* no change necessary */
1489 /* now try the command-line word by word */
1491 if (!(name = HeapAlloc( GetProcessHeap(), 0, (strlenW(cmdline) + 1) * sizeof(WCHAR) )))
1498 do *pos++ = *p++; while (*p && *p != ' ');
1500 if (find_exe_file( name, buffer, buflen, handle ))
1507 if (!ret || !strchrW( name, ' ' )) goto done; /* no change necessary */
1509 /* now build a new command-line with quotes */
1511 if (!(ret = HeapAlloc( GetProcessHeap(), 0, (strlenW(cmdline) + 3) * sizeof(WCHAR) )))
1513 sprintfW( ret, quotesW, name );
1517 HeapFree( GetProcessHeap(), 0, name );
1522 /**********************************************************************
1523 * CreateProcessA (KERNEL32.@)
1525 BOOL WINAPI CreateProcessA( LPCSTR app_name, LPSTR cmd_line, LPSECURITY_ATTRIBUTES process_attr,
1526 LPSECURITY_ATTRIBUTES thread_attr, BOOL inherit,
1527 DWORD flags, LPVOID env, LPCSTR cur_dir,
1528 LPSTARTUPINFOA startup_info, LPPROCESS_INFORMATION info )
1531 UNICODE_STRING app_nameW, cmd_lineW, cur_dirW, desktopW, titleW;
1534 if (app_name) RtlCreateUnicodeStringFromAsciiz( &app_nameW, app_name );
1535 else app_nameW.Buffer = NULL;
1536 if (cmd_line) RtlCreateUnicodeStringFromAsciiz( &cmd_lineW, cmd_line );
1537 else cmd_lineW.Buffer = NULL;
1538 if (cur_dir) RtlCreateUnicodeStringFromAsciiz( &cur_dirW, cur_dir );
1539 else cur_dirW.Buffer = NULL;
1540 if (startup_info->lpDesktop) RtlCreateUnicodeStringFromAsciiz( &desktopW, startup_info->lpDesktop );
1541 else desktopW.Buffer = NULL;
1542 if (startup_info->lpTitle) RtlCreateUnicodeStringFromAsciiz( &titleW, startup_info->lpTitle );
1543 else titleW.Buffer = NULL;
1545 memcpy( &infoW, startup_info, sizeof(infoW) );
1546 infoW.lpDesktop = desktopW.Buffer;
1547 infoW.lpTitle = titleW.Buffer;
1549 if (startup_info->lpReserved)
1550 FIXME("StartupInfo.lpReserved is used, please report (%s)\n",
1551 debugstr_a(startup_info->lpReserved));
1553 ret = CreateProcessW( app_nameW.Buffer, cmd_lineW.Buffer, process_attr, thread_attr,
1554 inherit, flags, env, cur_dirW.Buffer, &infoW, info );
1556 RtlFreeUnicodeString( &app_nameW );
1557 RtlFreeUnicodeString( &cmd_lineW );
1558 RtlFreeUnicodeString( &cur_dirW );
1559 RtlFreeUnicodeString( &desktopW );
1560 RtlFreeUnicodeString( &titleW );
1565 /**********************************************************************
1566 * CreateProcessW (KERNEL32.@)
1568 BOOL WINAPI CreateProcessW( LPCWSTR app_name, LPWSTR cmd_line, LPSECURITY_ATTRIBUTES process_attr,
1569 LPSECURITY_ATTRIBUTES thread_attr, BOOL inherit, DWORD flags,
1570 LPVOID env, LPCWSTR cur_dir, LPSTARTUPINFOW startup_info,
1571 LPPROCESS_INFORMATION info )
1575 const char *unixdir = NULL;
1576 DOS_FULL_NAME full_dir;
1577 WCHAR name[MAX_PATH];
1578 WCHAR *tidy_cmdline, *p, *envW = env;
1580 /* Process the AppName and/or CmdLine to get module name and path */
1582 TRACE("app %s cmdline %s\n", debugstr_w(app_name), debugstr_w(cmd_line) );
1584 if (!(tidy_cmdline = get_file_name( app_name, cmd_line, name, sizeof(name), &hFile )))
1586 if (hFile == INVALID_HANDLE_VALUE) goto done;
1588 /* Warn if unsupported features are used */
1590 if (flags & (IDLE_PRIORITY_CLASS | HIGH_PRIORITY_CLASS | REALTIME_PRIORITY_CLASS |
1591 CREATE_NEW_PROCESS_GROUP | CREATE_SEPARATE_WOW_VDM | CREATE_SHARED_WOW_VDM |
1592 CREATE_DEFAULT_ERROR_MODE | CREATE_NO_WINDOW |
1593 PROFILE_USER | PROFILE_KERNEL | PROFILE_SERVER))
1594 WARN("(%s,...): ignoring some flags in %lx\n", debugstr_w(name), flags);
1598 if (DOSFS_GetFullName( cur_dir, TRUE, &full_dir )) unixdir = full_dir.long_name;
1602 WCHAR buf[MAX_PATH];
1603 if (GetCurrentDirectoryW(MAX_PATH, buf))
1605 if (DOSFS_GetFullName( buf, TRUE, &full_dir )) unixdir = full_dir.long_name;
1609 if (env && !(flags & CREATE_UNICODE_ENVIRONMENT)) /* convert environment to unicode */
1614 while (*p) p += strlen(p) + 1;
1615 p++; /* final null */
1616 lenW = MultiByteToWideChar( CP_ACP, 0, env, p - (char*)env, NULL, 0 );
1617 envW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) );
1618 MultiByteToWideChar( CP_ACP, 0, env, p - (char*)env, envW, lenW );
1619 flags |= CREATE_UNICODE_ENVIRONMENT;
1622 info->hThread = info->hProcess = 0;
1623 info->dwProcessId = info->dwThreadId = 0;
1625 /* Determine executable type */
1627 if (!hFile) /* builtin exe */
1629 TRACE( "starting %s as Winelib app\n", debugstr_w(name) );
1630 retv = create_process( 0, name, tidy_cmdline, envW, process_attr, thread_attr,
1631 inherit, flags, startup_info, info, unixdir );
1635 switch( MODULE_GetBinaryType( hFile ))
1638 TRACE( "starting %s as Win32 binary\n", debugstr_w(name) );
1639 retv = create_process( hFile, name, tidy_cmdline, envW, process_attr, thread_attr,
1640 inherit, flags, startup_info, info, unixdir );
1644 TRACE( "starting %s as Win16/DOS binary\n", debugstr_w(name) );
1645 retv = create_vdm_process( name, tidy_cmdline, envW, process_attr, thread_attr,
1646 inherit, flags, startup_info, info, unixdir );
1649 FIXME( "%s is OS/2 binary, not supported\n", debugstr_w(name) );
1650 SetLastError( ERROR_BAD_EXE_FORMAT );
1653 TRACE( "not starting %s since it is a dll\n", debugstr_w(name) );
1654 SetLastError( ERROR_BAD_EXE_FORMAT );
1656 case BINARY_UNIX_LIB:
1657 TRACE( "%s is a Unix library, starting as Winelib app\n", debugstr_w(name) );
1658 retv = create_process( hFile, name, tidy_cmdline, envW, process_attr, thread_attr,
1659 inherit, flags, startup_info, info, unixdir );
1661 case BINARY_UNKNOWN:
1662 /* check for .com or .bat extension */
1663 if ((p = strrchrW( name, '.' )))
1665 if (!strcmpiW( p, comW ))
1667 TRACE( "starting %s as DOS binary\n", debugstr_w(name) );
1668 retv = create_vdm_process( name, tidy_cmdline, envW, process_attr, thread_attr,
1669 inherit, flags, startup_info, info, unixdir );
1672 if (!strcmpiW( p, batW ))
1674 TRACE( "starting %s as batch binary\n", debugstr_w(name) );
1675 retv = create_cmd_process( name, tidy_cmdline, envW, process_attr, thread_attr,
1676 inherit, flags, startup_info, info, cur_dir );
1681 case BINARY_UNIX_EXE:
1683 /* unknown file, try as unix executable */
1684 DOS_FULL_NAME full_name;
1686 TRACE( "starting %s as Unix binary\n", debugstr_w(name) );
1688 if (DOSFS_GetFullName( name, TRUE, &full_name ))
1689 retv = (fork_and_exec( full_name.long_name, tidy_cmdline, envW, unixdir ) != -1);
1693 CloseHandle( hFile );
1696 if (tidy_cmdline != cmd_line) HeapFree( GetProcessHeap(), 0, tidy_cmdline );
1697 if (envW != env) HeapFree( GetProcessHeap(), 0, envW );
1702 /***********************************************************************
1705 * Wrapper to call WaitForInputIdle USER function
1707 typedef DWORD (WINAPI *WaitForInputIdle_ptr)( HANDLE hProcess, DWORD dwTimeOut );
1709 static DWORD wait_input_idle( HANDLE process, DWORD timeout )
1711 HMODULE mod = GetModuleHandleA( "user32.dll" );
1714 WaitForInputIdle_ptr ptr = (WaitForInputIdle_ptr)GetProcAddress( mod, "WaitForInputIdle" );
1715 if (ptr) return ptr( process, timeout );
1721 /***********************************************************************
1722 * WinExec (KERNEL32.@)
1724 UINT WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow )
1726 PROCESS_INFORMATION info;
1727 STARTUPINFOA startup;
1731 memset( &startup, 0, sizeof(startup) );
1732 startup.cb = sizeof(startup);
1733 startup.dwFlags = STARTF_USESHOWWINDOW;
1734 startup.wShowWindow = nCmdShow;
1736 /* cmdline needs to be writeable for CreateProcess */
1737 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine)+1 ))) return 0;
1738 strcpy( cmdline, lpCmdLine );
1740 if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE,
1741 0, NULL, NULL, &startup, &info ))
1743 /* Give 30 seconds to the app to come up */
1744 if (wait_input_idle( info.hProcess, 30000 ) == 0xFFFFFFFF)
1745 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1747 /* Close off the handles */
1748 CloseHandle( info.hThread );
1749 CloseHandle( info.hProcess );
1751 else if ((ret = GetLastError()) >= 32)
1753 FIXME("Strange error set by CreateProcess: %d\n", ret );
1756 HeapFree( GetProcessHeap(), 0, cmdline );
1761 /**********************************************************************
1762 * LoadModule (KERNEL32.@)
1764 HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
1766 LOADPARAMS *params = (LOADPARAMS *)paramBlock;
1767 PROCESS_INFORMATION info;
1768 STARTUPINFOA startup;
1769 HINSTANCE hInstance;
1771 char filename[MAX_PATH];
1774 if (!name) return (HINSTANCE)ERROR_FILE_NOT_FOUND;
1776 if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) &&
1777 !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL ))
1778 return (HINSTANCE)GetLastError();
1780 len = (BYTE)params->lpCmdLine[0];
1781 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 )))
1782 return (HINSTANCE)ERROR_NOT_ENOUGH_MEMORY;
1784 strcpy( cmdline, filename );
1785 p = cmdline + strlen(cmdline);
1787 memcpy( p, params->lpCmdLine + 1, len );
1790 memset( &startup, 0, sizeof(startup) );
1791 startup.cb = sizeof(startup);
1792 if (params->lpCmdShow)
1794 startup.dwFlags = STARTF_USESHOWWINDOW;
1795 startup.wShowWindow = params->lpCmdShow[1];
1798 if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0,
1799 params->lpEnvAddress, NULL, &startup, &info ))
1801 /* Give 30 seconds to the app to come up */
1802 if (wait_input_idle( info.hProcess, 30000 ) == 0xFFFFFFFF )
1803 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1804 hInstance = (HINSTANCE)33;
1805 /* Close off the handles */
1806 CloseHandle( info.hThread );
1807 CloseHandle( info.hProcess );
1809 else if ((hInstance = (HINSTANCE)GetLastError()) >= (HINSTANCE)32)
1811 FIXME("Strange error set by CreateProcess: %p\n", hInstance );
1812 hInstance = (HINSTANCE)11;
1815 HeapFree( GetProcessHeap(), 0, cmdline );
1820 /******************************************************************************
1821 * TerminateProcess (KERNEL32.@)
1823 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
1825 NTSTATUS status = NtTerminateProcess( handle, exit_code );
1826 if (status) SetLastError( RtlNtStatusToDosError(status) );
1831 /***********************************************************************
1832 * ExitProcess (KERNEL32.@)
1834 void WINAPI ExitProcess( DWORD status )
1836 LdrShutdownProcess();
1837 SERVER_START_REQ( terminate_process )
1839 /* send the exit code to the server */
1840 req->handle = GetCurrentProcess();
1841 req->exit_code = status;
1842 wine_server_call( req );
1849 /***********************************************************************
1850 * GetExitCodeProcess [KERNEL32.@]
1852 * Gets termination status of specified process
1858 BOOL WINAPI GetExitCodeProcess(
1859 HANDLE hProcess, /* [in] handle to the process */
1860 LPDWORD lpExitCode) /* [out] address to receive termination status */
1863 SERVER_START_REQ( get_process_info )
1865 req->handle = hProcess;
1866 ret = !wine_server_call_err( req );
1867 if (ret && lpExitCode) *lpExitCode = reply->exit_code;
1874 /***********************************************************************
1875 * SetErrorMode (KERNEL32.@)
1877 UINT WINAPI SetErrorMode( UINT mode )
1879 UINT old = process_error_mode;
1880 process_error_mode = mode;
1885 /**********************************************************************
1886 * TlsAlloc [KERNEL32.@] Allocates a TLS index.
1888 * Allocates a thread local storage index
1891 * Success: TLS Index
1892 * Failure: 0xFFFFFFFF
1894 DWORD WINAPI TlsAlloc( void )
1898 RtlAcquirePebLock();
1899 index = RtlFindClearBitsAndSet( NtCurrentTeb()->Peb->TlsBitmap, 1, 0 );
1900 if (index != ~0UL) NtCurrentTeb()->TlsSlots[index] = 0; /* clear the value */
1901 else SetLastError( ERROR_NO_MORE_ITEMS );
1902 RtlReleasePebLock();
1907 /**********************************************************************
1908 * TlsFree [KERNEL32.@] Releases a TLS index.
1910 * Releases a thread local storage index, making it available for reuse
1916 BOOL WINAPI TlsFree(
1917 DWORD index) /* [in] TLS Index to free */
1921 RtlAcquirePebLock();
1922 ret = RtlAreBitsSet( NtCurrentTeb()->Peb->TlsBitmap, index, 1 );
1925 RtlClearBits( NtCurrentTeb()->Peb->TlsBitmap, index, 1 );
1926 NtSetInformationThread( GetCurrentThread(), ThreadZeroTlsCell, &index, sizeof(index) );
1928 else SetLastError( ERROR_INVALID_PARAMETER );
1929 RtlReleasePebLock();
1934 /**********************************************************************
1935 * TlsGetValue [KERNEL32.@] Gets value in a thread's TLS slot
1938 * Success: Value stored in calling thread's TLS slot for index
1939 * Failure: 0 and GetLastError returns NO_ERROR
1941 LPVOID WINAPI TlsGetValue(
1942 DWORD index) /* [in] TLS index to retrieve value for */
1944 if (index >= NtCurrentTeb()->Peb->TlsBitmap->SizeOfBitMap)
1946 SetLastError( ERROR_INVALID_PARAMETER );
1949 SetLastError( ERROR_SUCCESS );
1950 return NtCurrentTeb()->TlsSlots[index];
1954 /**********************************************************************
1955 * TlsSetValue [KERNEL32.@] Stores a value in the thread's TLS slot.
1961 BOOL WINAPI TlsSetValue(
1962 DWORD index, /* [in] TLS index to set value for */
1963 LPVOID value) /* [in] Value to be stored */
1965 if (index >= NtCurrentTeb()->Peb->TlsBitmap->SizeOfBitMap)
1967 SetLastError( ERROR_INVALID_PARAMETER );
1970 NtCurrentTeb()->TlsSlots[index] = value;
1975 /***********************************************************************
1976 * GetProcessFlags (KERNEL32.@)
1978 DWORD WINAPI GetProcessFlags( DWORD processid )
1980 IMAGE_NT_HEADERS *nt;
1983 if (processid && processid != GetCurrentProcessId()) return 0;
1985 if ((nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress )))
1987 if (nt->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
1988 flags |= PDB32_CONSOLE_PROC;
1990 if (!AreFileApisANSI()) flags |= PDB32_FILE_APIS_OEM;
1991 if (IsDebuggerPresent()) flags |= PDB32_DEBUGGED;
1996 /***********************************************************************
1997 * GetProcessDword (KERNEL.485)
1998 * GetProcessDword (KERNEL32.18)
1999 * 'Of course you cannot directly access Windows internal structures'
2001 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
2006 TRACE("(%ld, %d)\n", dwProcessID, offset );
2008 if (dwProcessID && dwProcessID != GetCurrentProcessId())
2010 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
2016 case GPD_APP_COMPAT_FLAGS:
2017 return GetAppCompatFlags16(0);
2018 case GPD_LOAD_DONE_EVENT:
2020 case GPD_HINSTANCE16:
2021 return GetTaskDS16();
2022 case GPD_WINDOWS_VERSION:
2023 return GetExeVersion16();
2025 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
2027 return (DWORD)NtCurrentTeb()->Peb;
2028 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
2029 GetStartupInfoW(&siw);
2030 return (DWORD)siw.hStdOutput;
2031 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
2032 GetStartupInfoW(&siw);
2033 return (DWORD)siw.hStdInput;
2034 case GPD_STARTF_SHOWWINDOW:
2035 GetStartupInfoW(&siw);
2036 return siw.wShowWindow;
2037 case GPD_STARTF_SIZE:
2038 GetStartupInfoW(&siw);
2040 if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
2042 if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
2043 return MAKELONG( x, y );
2044 case GPD_STARTF_POSITION:
2045 GetStartupInfoW(&siw);
2047 if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
2049 if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
2050 return MAKELONG( x, y );
2051 case GPD_STARTF_FLAGS:
2052 GetStartupInfoW(&siw);
2057 return GetProcessFlags(0);
2059 return process_dword;
2061 ERR("Unknown offset %d\n", offset );
2066 /***********************************************************************
2067 * SetProcessDword (KERNEL.484)
2068 * 'Of course you cannot directly access Windows internal structures'
2070 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
2072 TRACE("(%ld, %d)\n", dwProcessID, offset );
2074 if (dwProcessID && dwProcessID != GetCurrentProcessId())
2076 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
2082 case GPD_APP_COMPAT_FLAGS:
2083 case GPD_LOAD_DONE_EVENT:
2084 case GPD_HINSTANCE16:
2085 case GPD_WINDOWS_VERSION:
2088 case GPD_STARTF_SHELLDATA:
2089 case GPD_STARTF_HOTKEY:
2090 case GPD_STARTF_SHOWWINDOW:
2091 case GPD_STARTF_SIZE:
2092 case GPD_STARTF_POSITION:
2093 case GPD_STARTF_FLAGS:
2096 ERR("Not allowed to modify offset %d\n", offset );
2099 process_dword = value;
2102 ERR("Unknown offset %d\n", offset );
2108 /***********************************************************************
2109 * ExitProcess (KERNEL.466)
2111 void WINAPI ExitProcess16( WORD status )
2114 ReleaseThunkLock( &count );
2115 ExitProcess( status );
2119 /*********************************************************************
2120 * OpenProcess (KERNEL32.@)
2122 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
2125 SERVER_START_REQ( open_process )
2128 req->access = access;
2129 req->inherit = inherit;
2130 if (!wine_server_call_err( req )) ret = reply->handle;
2137 /*********************************************************************
2138 * MapProcessHandle (KERNEL.483)
2140 DWORD WINAPI MapProcessHandle( HANDLE handle )
2143 SERVER_START_REQ( get_process_info )
2145 req->handle = handle;
2146 if (!wine_server_call_err( req )) ret = reply->pid;
2153 /*********************************************************************
2154 * CloseW32Handle (KERNEL.474)
2155 * CloseHandle (KERNEL32.@)
2157 BOOL WINAPI CloseHandle( HANDLE handle )
2161 /* stdio handles need special treatment */
2162 if ((handle == (HANDLE)STD_INPUT_HANDLE) ||
2163 (handle == (HANDLE)STD_OUTPUT_HANDLE) ||
2164 (handle == (HANDLE)STD_ERROR_HANDLE))
2165 handle = GetStdHandle( (DWORD)handle );
2167 if (is_console_handle(handle))
2168 return CloseConsoleHandle(handle);
2170 status = NtClose( handle );
2171 if (status) SetLastError( RtlNtStatusToDosError(status) );
2176 /*********************************************************************
2177 * GetHandleInformation (KERNEL32.@)
2179 BOOL WINAPI GetHandleInformation( HANDLE handle, LPDWORD flags )
2182 SERVER_START_REQ( set_handle_info )
2184 req->handle = handle;
2188 ret = !wine_server_call_err( req );
2189 if (ret && flags) *flags = reply->old_flags;
2196 /*********************************************************************
2197 * SetHandleInformation (KERNEL32.@)
2199 BOOL WINAPI SetHandleInformation( HANDLE handle, DWORD mask, DWORD flags )
2202 SERVER_START_REQ( set_handle_info )
2204 req->handle = handle;
2208 ret = !wine_server_call_err( req );
2215 /*********************************************************************
2216 * DuplicateHandle (KERNEL32.@)
2218 BOOL WINAPI DuplicateHandle( HANDLE source_process, HANDLE source,
2219 HANDLE dest_process, HANDLE *dest,
2220 DWORD access, BOOL inherit, DWORD options )
2224 if (is_console_handle(source))
2226 /* FIXME: this test is not sufficient, we need to test process ids, not handles */
2227 if (source_process != dest_process ||
2228 source_process != GetCurrentProcess())
2230 SetLastError(ERROR_INVALID_PARAMETER);
2233 *dest = DuplicateConsoleHandle( source, access, inherit, options );
2234 return (*dest != INVALID_HANDLE_VALUE);
2236 status = NtDuplicateObject( source_process, source, dest_process, dest,
2237 access, inherit ? OBJ_INHERIT : 0, options );
2238 if (status) SetLastError( RtlNtStatusToDosError(status) );
2243 /***********************************************************************
2244 * ConvertToGlobalHandle (KERNEL.476)
2245 * ConvertToGlobalHandle (KERNEL32.@)
2247 HANDLE WINAPI ConvertToGlobalHandle(HANDLE hSrc)
2249 HANDLE ret = INVALID_HANDLE_VALUE;
2250 DuplicateHandle( GetCurrentProcess(), hSrc, GetCurrentProcess(), &ret, 0, FALSE,
2251 DUP_HANDLE_MAKE_GLOBAL | DUP_HANDLE_SAME_ACCESS | DUP_HANDLE_CLOSE_SOURCE );
2256 /***********************************************************************
2257 * SetHandleContext (KERNEL32.@)
2259 BOOL WINAPI SetHandleContext(HANDLE hnd,DWORD context)
2261 FIXME("(%p,%ld), stub. In case this got called by WSOCK32/WS2_32: "
2262 "the external WINSOCK DLLs won't work with WINE, don't use them.\n",hnd,context);
2263 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2268 /***********************************************************************
2269 * GetHandleContext (KERNEL32.@)
2271 DWORD WINAPI GetHandleContext(HANDLE hnd)
2273 FIXME("(%p), stub. In case this got called by WSOCK32/WS2_32: "
2274 "the external WINSOCK DLLs won't work with WINE, don't use them.\n",hnd);
2275 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2280 /***********************************************************************
2281 * CreateSocketHandle (KERNEL32.@)
2283 HANDLE WINAPI CreateSocketHandle(void)
2285 FIXME("(), stub. In case this got called by WSOCK32/WS2_32: "
2286 "the external WINSOCK DLLs won't work with WINE, don't use them.\n");
2287 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2288 return INVALID_HANDLE_VALUE;
2292 /***********************************************************************
2293 * SetPriorityClass (KERNEL32.@)
2295 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
2298 SERVER_START_REQ( set_process_info )
2300 req->handle = hprocess;
2301 req->priority = priorityclass;
2302 req->mask = SET_PROCESS_INFO_PRIORITY;
2303 ret = !wine_server_call_err( req );
2310 /***********************************************************************
2311 * GetPriorityClass (KERNEL32.@)
2313 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
2316 SERVER_START_REQ( get_process_info )
2318 req->handle = hprocess;
2319 if (!wine_server_call_err( req )) ret = reply->priority;
2326 /***********************************************************************
2327 * SetProcessAffinityMask (KERNEL32.@)
2329 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
2332 SERVER_START_REQ( set_process_info )
2334 req->handle = hProcess;
2335 req->affinity = affmask;
2336 req->mask = SET_PROCESS_INFO_AFFINITY;
2337 ret = !wine_server_call_err( req );
2344 /**********************************************************************
2345 * GetProcessAffinityMask (KERNEL32.@)
2347 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
2348 LPDWORD lpProcessAffinityMask,
2349 LPDWORD lpSystemAffinityMask )
2352 SERVER_START_REQ( get_process_info )
2354 req->handle = hProcess;
2355 if (!wine_server_call_err( req ))
2357 if (lpProcessAffinityMask) *lpProcessAffinityMask = reply->process_affinity;
2358 if (lpSystemAffinityMask) *lpSystemAffinityMask = reply->system_affinity;
2367 /***********************************************************************
2368 * GetProcessVersion (KERNEL32.@)
2370 DWORD WINAPI GetProcessVersion( DWORD processid )
2372 IMAGE_NT_HEADERS *nt;
2374 if (processid && processid != GetCurrentProcessId())
2376 FIXME("should use ReadProcessMemory\n");
2379 if ((nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress )))
2380 return ((nt->OptionalHeader.MajorSubsystemVersion << 16) |
2381 nt->OptionalHeader.MinorSubsystemVersion);
2386 /***********************************************************************
2387 * SetProcessWorkingSetSize [KERNEL32.@]
2388 * Sets the min/max working set sizes for a specified process.
2391 * hProcess [I] Handle to the process of interest
2392 * minset [I] Specifies minimum working set size
2393 * maxset [I] Specifies maximum working set size
2397 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess, SIZE_T minset,
2400 FIXME("(%p,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
2401 if(( minset == (SIZE_T)-1) && (maxset == (SIZE_T)-1)) {
2402 /* Trim the working set to zero */
2403 /* Swap the process out of physical RAM */
2408 /***********************************************************************
2409 * GetProcessWorkingSetSize (KERNEL32.@)
2411 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess, PSIZE_T minset,
2414 FIXME("(%p,%p,%p): stub\n",hProcess,minset,maxset);
2415 /* 32 MB working set size */
2416 if (minset) *minset = 32*1024*1024;
2417 if (maxset) *maxset = 32*1024*1024;
2422 /***********************************************************************
2423 * SetProcessShutdownParameters (KERNEL32.@)
2425 BOOL WINAPI SetProcessShutdownParameters(DWORD level, DWORD flags)
2427 FIXME("(%08lx, %08lx): partial stub.\n", level, flags);
2428 shutdown_flags = flags;
2429 shutdown_priority = level;
2434 /***********************************************************************
2435 * GetProcessShutdownParameters (KERNEL32.@)
2438 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel, LPDWORD lpdwFlags )
2440 *lpdwLevel = shutdown_priority;
2441 *lpdwFlags = shutdown_flags;
2446 /***********************************************************************
2447 * GetProcessPriorityBoost (KERNEL32.@)
2449 BOOL WINAPI GetProcessPriorityBoost(HANDLE hprocess,PBOOL pDisablePriorityBoost)
2451 FIXME("(%p,%p): semi-stub\n", hprocess, pDisablePriorityBoost);
2453 /* Report that no boost is present.. */
2454 *pDisablePriorityBoost = FALSE;
2459 /***********************************************************************
2460 * SetProcessPriorityBoost (KERNEL32.@)
2462 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
2464 FIXME("(%p,%d): stub\n",hprocess,disableboost);
2465 /* Say we can do it. I doubt the program will notice that we don't. */
2470 /***********************************************************************
2471 * ReadProcessMemory (KERNEL32.@)
2473 BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, SIZE_T size,
2474 SIZE_T *bytes_read )
2476 NTSTATUS status = NtReadVirtualMemory( process, addr, buffer, size, bytes_read );
2477 if (status) SetLastError( RtlNtStatusToDosError(status) );
2482 /***********************************************************************
2483 * WriteProcessMemory (KERNEL32.@)
2485 BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPCVOID buffer, SIZE_T size,
2486 SIZE_T *bytes_written )
2488 NTSTATUS status = NtWriteVirtualMemory( process, addr, buffer, size, bytes_written );
2489 if (status) SetLastError( RtlNtStatusToDosError(status) );
2494 /****************************************************************************
2495 * FlushInstructionCache (KERNEL32.@)
2497 BOOL WINAPI FlushInstructionCache(HANDLE hProcess, LPCVOID lpBaseAddress, SIZE_T dwSize)
2499 if (GetVersion() & 0x80000000) return TRUE; /* not NT, always TRUE */
2500 FIXME("(%p,%p,0x%08lx): stub\n",hProcess, lpBaseAddress, dwSize);
2505 /******************************************************************
2506 * GetProcessIoCounters (KERNEL32.@)
2508 BOOL WINAPI GetProcessIoCounters(HANDLE hProcess, PIO_COUNTERS ioc)
2512 status = NtQueryInformationProcess(hProcess, ProcessIoCounters,
2513 ioc, sizeof(*ioc), NULL);
2514 if (status) SetLastError( RtlNtStatusToDosError(status) );
2518 /***********************************************************************
2519 * ProcessIdToSessionId (KERNEL32.@)
2520 * This function is available on Terminal Server 4SP4 and Windows 2000
2522 BOOL WINAPI ProcessIdToSessionId( DWORD procid, DWORD *sessionid_ptr )
2524 /* According to MSDN, if the calling process is not in a terminal
2525 * services environment, then the sessionid returned is zero.
2532 /***********************************************************************
2533 * RegisterServiceProcess (KERNEL.491)
2534 * RegisterServiceProcess (KERNEL32.@)
2536 * A service process calls this function to ensure that it continues to run
2537 * even after a user logged off.
2539 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
2541 /* I don't think that Wine needs to do anything in that function */
2542 return 1; /* success */
2546 /**************************************************************************
2547 * SetFileApisToOEM (KERNEL32.@)
2549 VOID WINAPI SetFileApisToOEM(void)
2551 oem_file_apis = TRUE;
2555 /**************************************************************************
2556 * SetFileApisToANSI (KERNEL32.@)
2558 VOID WINAPI SetFileApisToANSI(void)
2560 oem_file_apis = FALSE;
2564 /******************************************************************************
2565 * AreFileApisANSI [KERNEL32.@] Determines if file functions are using ANSI
2568 * TRUE: Set of file functions is using ANSI code page
2569 * FALSE: Set of file functions is using OEM code page
2571 BOOL WINAPI AreFileApisANSI(void)
2573 return !oem_file_apis;
2577 /***********************************************************************
2578 * GetTickCount (KERNEL32.@)
2580 * Returns the number of milliseconds, modulo 2^32, since the start
2581 * of the wineserver.
2583 DWORD WINAPI GetTickCount(void)
2586 gettimeofday( &t, NULL );
2587 return ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - server_startticks;
2591 /***********************************************************************
2592 * GetCurrentProcess (KERNEL32.@)
2594 #undef GetCurrentProcess
2595 HANDLE WINAPI GetCurrentProcess(void)
2597 return (HANDLE)0xffffffff;