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"
39 #include "kernel_private.h"
40 #include "wine/server.h"
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(process);
45 WINE_DECLARE_DEBUG_CHANNEL(server);
46 WINE_DECLARE_DEBUG_CHANNEL(relay);
56 static UINT process_error_mode;
58 static HANDLE main_exe_file;
59 static DWORD shutdown_flags = 0;
60 static DWORD shutdown_priority = 0x280;
61 static DWORD process_dword;
62 static BOOL oem_file_apis;
64 static unsigned int server_startticks;
65 int main_create_flags = 0;
68 #define PDB32_DEBUGGED 0x0001 /* Process is being debugged */
69 #define PDB32_WIN16_PROC 0x0008 /* Win16 process */
70 #define PDB32_DOS_PROC 0x0010 /* Dos process */
71 #define PDB32_CONSOLE_PROC 0x0020 /* Console process */
72 #define PDB32_FILE_APIS_OEM 0x0040 /* File APIs are OEM */
73 #define PDB32_WIN32S_PROC 0x8000 /* Win32s process */
75 static const WCHAR comW[] = {'.','c','o','m',0};
76 static const WCHAR batW[] = {'.','b','a','t',0};
77 static const WCHAR winevdmW[] = {'w','i','n','e','v','d','m','.','e','x','e',0};
79 extern void SHELL_LoadRegistry(void);
80 extern void VERSION_Init( const WCHAR *appname );
81 extern void MODULE_InitLoadPath(void);
82 extern void LOCALE_Init(void);
84 /***********************************************************************
87 inline static int contains_path( LPCWSTR name )
89 return ((*name && (name[1] == ':')) || strchrW(name, '/') || strchrW(name, '\\'));
93 /***************************************************************************
96 * Get the path of a builtin module when the native file does not exist.
98 static BOOL get_builtin_path( const WCHAR *libname, const WCHAR *ext, WCHAR *filename, UINT size )
101 WCHAR sysdir[MAX_PATH];
102 UINT len = GetSystemDirectoryW( sysdir, MAX_PATH );
104 if (contains_path( libname ))
106 if (RtlGetFullPathName_U( libname, size * sizeof(WCHAR),
107 filename, &file_part ) > size * sizeof(WCHAR))
108 return FALSE; /* too long */
110 if (strncmpiW( filename, sysdir, len ) || filename[len] != '\\')
112 while (filename[len] == '\\') len++;
113 if (filename + len != file_part) return FALSE;
117 if (strlenW(libname) + len + 2 >= size) return FALSE; /* too long */
118 memcpy( filename, sysdir, len * sizeof(WCHAR) );
119 file_part = filename + len;
120 if (file_part > filename && file_part[-1] != '\\') *file_part++ = '\\';
121 strcpyW( file_part, libname );
123 if (ext && !strchrW( file_part, '.' ))
125 if (file_part + strlenW(file_part) + strlenW(ext) + 1 > filename + size)
126 return FALSE; /* too long */
127 strcatW( file_part, ext );
133 /***********************************************************************
134 * open_builtin_exe_file
136 * Open an exe file for a builtin exe.
138 static void *open_builtin_exe_file( const WCHAR *name, char *error, int error_size,
139 int test_only, int *file_exists )
141 char exename[MAX_PATH];
145 if ((p = strrchrW( name, '/' ))) name = p + 1;
146 if ((p = strrchrW( name, '\\' ))) name = p + 1;
148 /* we don't want to depend on the current codepage here */
149 len = strlenW( name ) + 1;
150 if (len >= sizeof(exename)) return NULL;
151 for (i = 0; i < len; i++)
153 if (name[i] > 127) return NULL;
154 exename[i] = (char)name[i];
155 if (exename[i] >= 'A' && exename[i] <= 'Z') exename[i] += 'a' - 'A';
157 return wine_dll_load_main_exe( exename, error, error_size, test_only, file_exists );
161 /***********************************************************************
164 * Open a specific exe file, taking load order into account.
165 * Returns the file handle or 0 for a builtin exe.
167 static HANDLE open_exe_file( const WCHAR *name )
169 enum loadorder_type loadorder[LOADORDER_NTYPES];
170 WCHAR buffer[MAX_PATH];
174 TRACE("looking for %s\n", debugstr_w(name) );
176 if ((handle = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ,
177 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
179 /* file doesn't exist, check for builtin */
180 if (!contains_path( name )) goto error;
181 if (!get_builtin_path( name, NULL, buffer, sizeof(buffer) )) goto error;
185 MODULE_GetLoadOrderW( loadorder, NULL, name );
187 for(i = 0; i < LOADORDER_NTYPES; i++)
189 if (loadorder[i] == LOADORDER_INVALID) break;
193 TRACE( "Trying native exe %s\n", debugstr_w(name) );
194 if (handle != INVALID_HANDLE_VALUE) return handle;
197 TRACE( "Trying built-in exe %s\n", debugstr_w(name) );
198 open_builtin_exe_file( name, NULL, 0, 1, &file_exists );
201 if (handle != INVALID_HANDLE_VALUE) CloseHandle(handle);
208 if (handle != INVALID_HANDLE_VALUE) CloseHandle(handle);
211 SetLastError( ERROR_FILE_NOT_FOUND );
212 return INVALID_HANDLE_VALUE;
216 /***********************************************************************
219 * Open an exe file, and return the full name and file handle.
220 * Returns FALSE if file could not be found.
221 * If file exists but cannot be opened, returns TRUE and set handle to INVALID_HANDLE_VALUE.
222 * If file is a builtin exe, returns TRUE and sets handle to 0.
224 static BOOL find_exe_file( const WCHAR *name, WCHAR *buffer, int buflen, HANDLE *handle )
226 static const WCHAR exeW[] = {'.','e','x','e',0};
228 enum loadorder_type loadorder[LOADORDER_NTYPES];
231 TRACE("looking for %s\n", debugstr_w(name) );
233 if (!SearchPathW( NULL, name, exeW, buflen, buffer, NULL ) &&
234 !get_builtin_path( name, exeW, buffer, buflen ))
236 /* no builtin found, try native without extension in case it is a Unix app */
238 if (SearchPathW( NULL, name, NULL, buflen, buffer, NULL ))
240 TRACE( "Trying native/Unix binary %s\n", debugstr_w(buffer) );
241 if ((*handle = CreateFileW( buffer, GENERIC_READ, FILE_SHARE_READ,
242 NULL, OPEN_EXISTING, 0, 0 )) != INVALID_HANDLE_VALUE)
248 MODULE_GetLoadOrderW( loadorder, NULL, buffer );
250 for(i = 0; i < LOADORDER_NTYPES; i++)
252 if (loadorder[i] == LOADORDER_INVALID) break;
256 TRACE( "Trying native exe %s\n", debugstr_w(buffer) );
257 if ((*handle = CreateFileW( buffer, GENERIC_READ, FILE_SHARE_READ,
258 NULL, OPEN_EXISTING, 0, 0 )) != INVALID_HANDLE_VALUE)
260 if (GetLastError() != ERROR_FILE_NOT_FOUND) return TRUE;
263 TRACE( "Trying built-in exe %s\n", debugstr_w(buffer) );
264 open_builtin_exe_file( buffer, NULL, 0, 1, &file_exists );
275 SetLastError( ERROR_FILE_NOT_FOUND );
280 /**********************************************************************
283 * Load a PE format EXE file.
285 static HMODULE load_pe_exe( const WCHAR *name, HANDLE file )
287 IMAGE_NT_HEADERS *nt;
290 OBJECT_ATTRIBUTES attr;
295 attr.Length = sizeof(attr);
296 attr.RootDirectory = 0;
297 attr.ObjectName = NULL;
299 attr.SecurityDescriptor = NULL;
300 attr.SecurityQualityOfService = NULL;
303 if (NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
304 &attr, &size, 0, SEC_IMAGE, file ) != STATUS_SUCCESS)
308 if (NtMapViewOfSection( mapping, GetCurrentProcess(), &module, 0, 0, &size, &len,
309 ViewShare, 0, PAGE_READONLY ) != STATUS_SUCCESS)
315 nt = RtlImageNtHeader( module );
316 if (nt->OptionalHeader.AddressOfEntryPoint)
318 if (!RtlImageRvaToSection( nt, module, nt->OptionalHeader.AddressOfEntryPoint ))
319 MESSAGE("VIRUS WARNING: PE module %s has an invalid entrypoint (0x%08lx) "
320 "outside all sections (possibly infected by Tchernobyl/SpaceFiller virus)!\n",
321 debugstr_w(name), nt->OptionalHeader.AddressOfEntryPoint );
324 drive_type = GetDriveTypeW( name );
325 /* don't keep the file handle open on removable media */
326 if (drive_type == DRIVE_REMOVABLE || drive_type == DRIVE_CDROM)
328 CloseHandle( main_exe_file );
335 /***********************************************************************
336 * build_initial_environment
338 * Build the Win32 environment from the Unix environment
340 static BOOL build_initial_environment( char **environ )
347 /* Compute the total size of the Unix environment */
348 for (e = environ; *e; e++)
350 if (!memcmp(*e, "PATH=", 5)) continue;
351 size += MultiByteToWideChar( CP_UNIXCP, 0, *e, -1, NULL, 0 );
353 size *= sizeof(WCHAR);
355 /* Now allocate the environment */
356 if (NtAllocateVirtualMemory(NtCurrentProcess(), &ptr, 0, &size,
357 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) != STATUS_SUCCESS)
360 NtCurrentTeb()->Peb->ProcessParameters->Environment = p = ptr;
361 endptr = p + size / sizeof(WCHAR);
363 /* And fill it with the Unix environment */
364 for (e = environ; *e; e++)
367 /* skip Unix PATH and store WINEPATH as PATH */
368 if (!memcmp(str, "PATH=", 5)) continue;
369 if (!memcmp(str, "WINEPATH=", 9 )) str += 4;
370 MultiByteToWideChar( CP_UNIXCP, 0, str, -1, p, endptr - p );
378 /***********************************************************************
381 * Set the Wine library Unicode argv global variables.
383 static void set_library_wargv( char **argv )
390 for (argc = 0; argv[argc]; argc++)
391 total += MultiByteToWideChar( CP_UNIXCP, 0, argv[argc], -1, NULL, 0 );
393 wargv = RtlAllocateHeap( GetProcessHeap(), 0,
394 total * sizeof(WCHAR) + (argc + 1) * sizeof(*wargv) );
395 p = (WCHAR *)(wargv + argc + 1);
396 for (argc = 0; argv[argc]; argc++)
398 DWORD reslen = MultiByteToWideChar( CP_UNIXCP, 0, argv[argc], -1, p, total );
404 __wine_main_wargv = wargv;
408 /***********************************************************************
411 * Build the command line of a process from the argv array.
413 * Note that it does NOT necessarily include the file name.
414 * Sometimes we don't even have any command line options at all.
416 * We must quote and escape characters so that the argv array can be rebuilt
417 * from the command line:
418 * - spaces and tabs must be quoted
420 * - quotes must be escaped
422 * - if '\'s are followed by a '"', they must be doubled and followed by '\"',
423 * resulting in an odd number of '\' followed by a '"'
426 * - '\'s that are not followed by a '"' can be left as is
430 static BOOL build_command_line( WCHAR **argv )
435 RTL_USER_PROCESS_PARAMETERS* rupp = NtCurrentTeb()->Peb->ProcessParameters;
437 if (rupp->CommandLine.Buffer) return TRUE; /* already got it from the server */
440 for (arg = argv; *arg; arg++)
442 int has_space,bcount;
448 if( !*a ) has_space=1;
453 if (*a==' ' || *a=='\t') {
455 } else if (*a=='"') {
456 /* doubling of '\' preceeding a '"',
457 * plus escaping of said '"'
465 len+=(a-*arg)+1 /* for the separating space */;
467 len+=2; /* for the quotes */
470 if (!(rupp->CommandLine.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR))))
473 p = rupp->CommandLine.Buffer;
474 rupp->CommandLine.Length = (len - 1) * sizeof(WCHAR);
475 rupp->CommandLine.MaximumLength = len * sizeof(WCHAR);
476 for (arg = argv; *arg; arg++)
478 int has_space,has_quote;
481 /* Check for quotes and spaces in this argument */
482 has_space=has_quote=0;
484 if( !*a ) has_space=1;
486 if (*a==' ' || *a=='\t') {
490 } else if (*a=='"') {
498 /* Now transfer it to the command line */
515 /* Double all the '\\' preceeding this '"', plus one */
516 for (i=0;i<=bcount;i++)
528 while ((*p=*x++)) p++;
534 if (p > rupp->CommandLine.Buffer)
535 p--; /* remove last space */
542 /* make sure the unicode string doesn't point beyond the end pointer */
543 static inline void fix_unicode_string( UNICODE_STRING *str, char *end_ptr )
545 if ((char *)str->Buffer >= end_ptr)
547 str->Length = str->MaximumLength = 0;
551 if ((char *)str->Buffer + str->MaximumLength > end_ptr)
553 str->MaximumLength = (end_ptr - (char *)str->Buffer) & ~(sizeof(WCHAR) - 1);
555 if (str->Length >= str->MaximumLength)
557 if (str->MaximumLength >= sizeof(WCHAR))
558 str->Length = str->MaximumLength - sizeof(WCHAR);
560 str->Length = str->MaximumLength = 0;
565 /***********************************************************************
566 * init_user_process_params
568 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
570 static RTL_USER_PROCESS_PARAMETERS *init_user_process_params( size_t info_size )
575 RTL_USER_PROCESS_PARAMETERS *params;
578 if ((status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, NULL, &size,
579 MEM_COMMIT, PAGE_READWRITE )) != STATUS_SUCCESS)
582 SERVER_START_REQ( get_startup_info )
584 wine_server_set_reply( req, ptr, info_size );
585 wine_server_call( req );
586 info_size = wine_server_reply_size( reply );
591 params->Size = info_size;
592 params->AllocationSize = size;
594 /* make sure the strings are valid */
595 fix_unicode_string( ¶ms->CurrentDirectoryName, (char *)info_size );
596 fix_unicode_string( ¶ms->DllPath, (char *)info_size );
597 fix_unicode_string( ¶ms->ImagePathName, (char *)info_size );
598 fix_unicode_string( ¶ms->CommandLine, (char *)info_size );
599 fix_unicode_string( ¶ms->WindowTitle, (char *)info_size );
600 fix_unicode_string( ¶ms->Desktop, (char *)info_size );
601 fix_unicode_string( ¶ms->ShellInfo, (char *)info_size );
602 fix_unicode_string( ¶ms->RuntimeInfo, (char *)info_size );
604 return RtlNormalizeProcessParams( params );
608 /***********************************************************************
611 * Main process initialisation code
613 static BOOL process_init( char *argv[], char **environ )
616 size_t info_size = 0;
617 RTL_USER_PROCESS_PARAMETERS *params;
618 PEB *peb = NtCurrentTeb()->Peb;
619 HANDLE hstdin, hstdout, hstderr;
623 setlocale(LC_CTYPE,"");
625 /* Retrieve startup info from the server */
626 SERVER_START_REQ( init_process )
629 req->ldt_copy = &wine_ldt_copy;
630 if ((ret = !wine_server_call_err( req )))
632 main_exe_file = reply->exe_file;
633 main_create_flags = reply->create_flags;
634 info_size = reply->info_size;
635 server_startticks = reply->server_start;
636 hstdin = reply->hstdin;
637 hstdout = reply->hstdout;
638 hstderr = reply->hstderr;
642 if (!ret) return FALSE;
646 params = peb->ProcessParameters;
648 /* This is wine specific: we have no parent (we're started from unix)
649 * so, create a simple console with bare handles to unix stdio
650 * input & output streams (aka simple console)
652 wine_server_fd_to_handle( 0, GENERIC_READ|SYNCHRONIZE, TRUE, ¶ms->hStdInput );
653 wine_server_fd_to_handle( 1, GENERIC_WRITE|SYNCHRONIZE, TRUE, ¶ms->hStdOutput );
654 wine_server_fd_to_handle( 2, GENERIC_WRITE|SYNCHRONIZE, TRUE, ¶ms->hStdError );
656 /* <hack: to be changed later on> */
657 params->CurrentDirectoryName.Length = 3 * sizeof(WCHAR);
658 params->CurrentDirectoryName.MaximumLength = RtlGetLongestNtPathLength() * sizeof(WCHAR);
659 params->CurrentDirectoryName.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, params->CurrentDirectoryName.MaximumLength);
660 params->CurrentDirectoryName.Buffer[0] = 'C';
661 params->CurrentDirectoryName.Buffer[1] = ':';
662 params->CurrentDirectoryName.Buffer[2] = '\\';
663 params->CurrentDirectoryName.Buffer[3] = '\0';
664 /* </hack: to be changed later on> */
668 if (!(params = init_user_process_params( info_size ))) return FALSE;
669 peb->ProcessParameters = params;
671 /* convert value from server:
672 * + 0 => INVALID_HANDLE_VALUE
673 * + console handle need to be mapped
676 hstdin = INVALID_HANDLE_VALUE;
677 else if (VerifyConsoleIoHandle(console_handle_map(hstdin)))
678 hstdin = console_handle_map(hstdin);
681 hstdout = INVALID_HANDLE_VALUE;
682 else if (VerifyConsoleIoHandle(console_handle_map(hstdout)))
683 hstdout = console_handle_map(hstdout);
686 hstderr = INVALID_HANDLE_VALUE;
687 else if (VerifyConsoleIoHandle(console_handle_map(hstderr)))
688 hstderr = console_handle_map(hstderr);
690 params->hStdInput = hstdin;
691 params->hStdOutput = hstdout;
692 params->hStdError = hstderr;
697 /* Copy the parent environment */
698 if (!build_initial_environment( environ )) return FALSE;
700 /* Parse command line arguments */
701 OPTIONS_ParseOptions( !info_size ? argv : NULL );
703 /* initialise DOS drives */
704 if (!DRIVE_Init()) return FALSE;
706 /* initialise DOS directories */
707 if (!DIR_Init()) return FALSE;
709 /* registry initialisation */
710 SHELL_LoadRegistry();
712 /* global boot finished, the rest is process-local */
713 SERVER_START_REQ( boot_done )
715 req->debug_level = TRACE_ON(server);
716 wine_server_call( req );
724 /***********************************************************************
727 * Startup routine of a new process. Runs on the new process stack.
729 static void start_process( void *arg )
733 PEB *peb = NtCurrentTeb()->Peb;
734 IMAGE_NT_HEADERS *nt;
735 LPTHREAD_START_ROUTINE entry;
737 LdrInitializeThunk( main_exe_file, CreateFileW, 0, 0 );
739 nt = RtlImageNtHeader( peb->ImageBaseAddress );
740 entry = (LPTHREAD_START_ROUTINE)((char *)peb->ImageBaseAddress +
741 nt->OptionalHeader.AddressOfEntryPoint);
744 DPRINTF( "%04lx:Starting process %s (entryproc=%p)\n", GetCurrentThreadId(),
745 debugstr_w(peb->ProcessParameters->ImagePathName.Buffer), entry );
747 SetLastError( 0 ); /* clear error code */
748 if (peb->BeingDebugged) DbgBreakPoint();
749 ExitProcess( entry( peb ) );
751 __EXCEPT(UnhandledExceptionFilter)
753 TerminateThread( GetCurrentThread(), GetExceptionCode() );
759 /***********************************************************************
762 * Wine initialisation: load and start the main exe file.
764 void __wine_kernel_init(void)
766 WCHAR *main_exe_name, *p;
768 DWORD stack_size = 0;
770 PEB *peb = NtCurrentTeb()->Peb;
772 /* Initialize everything */
773 if (!process_init( __wine_main_argv, __wine_main_environ )) exit(1);
774 /* update argc in case options have been removed */
775 for (__wine_main_argc = 0; __wine_main_argv[__wine_main_argc]; __wine_main_argc++) /*nothing*/;
777 __wine_main_argv++; /* remove argv[0] (wine itself) */
780 if (!(main_exe_name = peb->ProcessParameters->ImagePathName.Buffer))
782 WCHAR buffer[MAX_PATH];
783 WCHAR exe_nameW[MAX_PATH];
785 if (!__wine_main_argv[0]) OPTIONS_Usage();
787 MultiByteToWideChar( CP_UNIXCP, 0, __wine_main_argv[0], -1, exe_nameW, MAX_PATH );
788 if (!find_exe_file( exe_nameW, buffer, MAX_PATH, &main_exe_file ))
790 MESSAGE( "wine: cannot find '%s'\n", __wine_main_argv[0] );
793 if (main_exe_file == INVALID_HANDLE_VALUE)
795 MESSAGE( "wine: cannot open %s\n", debugstr_w(main_exe_name) );
798 RtlCreateUnicodeString( &peb->ProcessParameters->ImagePathName, buffer );
799 main_exe_name = peb->ProcessParameters->ImagePathName.Buffer;
802 TRACE( "starting process name=%s file=%p argv[0]=%s\n",
803 debugstr_w(main_exe_name), main_exe_file, debugstr_a(__wine_main_argv[0]) );
805 MODULE_InitLoadPath();
806 VERSION_Init( main_exe_name );
808 if (!main_exe_file) /* no file handle -> Winelib app */
810 TRACE( "starting Winelib app %s\n", debugstr_w(main_exe_name) );
811 if (open_builtin_exe_file( main_exe_name, error, sizeof(error), 0, &file_exists ))
813 MESSAGE( "wine: cannot open builtin library for %s: %s\n",
814 debugstr_w(main_exe_name), error );
818 switch( MODULE_GetBinaryType( main_exe_file ))
821 TRACE( "starting Win32 binary %s\n", debugstr_w(main_exe_name) );
822 if ((peb->ImageBaseAddress = load_pe_exe( main_exe_name, main_exe_file )))
824 MESSAGE( "wine: could not load %s as Win32 binary\n", debugstr_w(main_exe_name) );
827 MESSAGE( "wine: %s is a DLL, not an executable\n", debugstr_w(main_exe_name) );
830 /* check for .com extension */
831 if (!(p = strrchrW( main_exe_name, '.' )) || strcmpiW( p, comW ))
833 MESSAGE( "wine: cannot determine executable type for %s\n",
834 debugstr_w(main_exe_name) );
840 TRACE( "starting Win16/DOS binary %s\n", debugstr_w(main_exe_name) );
841 CloseHandle( main_exe_file );
845 __wine_main_argv[0] = "winevdm.exe";
846 if (open_builtin_exe_file( winevdmW, error, sizeof(error), 0, &file_exists ))
848 MESSAGE( "wine: trying to run %s, cannot open builtin library for 'winevdm.exe': %s\n",
849 debugstr_w(main_exe_name), error );
852 MESSAGE( "wine: %s is an OS/2 binary, not supported\n", debugstr_w(main_exe_name) );
854 case BINARY_UNIX_EXE:
855 MESSAGE( "wine: %s is a Unix binary, not supported\n", debugstr_w(main_exe_name) );
857 case BINARY_UNIX_LIB:
859 DOS_FULL_NAME full_name;
861 TRACE( "starting Winelib app %s\n", debugstr_w(main_exe_name) );
862 CloseHandle( main_exe_file );
864 if (DOSFS_GetFullName( main_exe_name, TRUE, &full_name ) &&
865 wine_dlopen( full_name.long_name, RTLD_NOW, error, sizeof(error) ))
867 static const WCHAR soW[] = {'.','s','o',0};
868 if ((p = strrchrW( main_exe_name, '.' )) && !strcmpW( p, soW ))
871 /* update the unicode string */
872 RtlInitUnicodeString( &peb->ProcessParameters->ImagePathName, main_exe_name );
876 MESSAGE( "wine: could not load %s: %s\n", debugstr_w(main_exe_name), error );
882 wine_free_pe_load_area(); /* the main binary is loaded, we don't need this anymore */
884 /* build command line */
885 set_library_wargv( __wine_main_argv );
886 if (!build_command_line( __wine_main_wargv )) goto error;
888 stack_size = RtlImageNtHeader(peb->ImageBaseAddress)->OptionalHeader.SizeOfStackReserve;
890 /* allocate main thread stack */
891 if (!THREAD_InitStack( NtCurrentTeb(), stack_size )) goto error;
893 /* switch to the new stack */
894 wine_switch_to_stack( start_process, NULL, NtCurrentTeb()->Tib.StackBase );
897 ExitProcess( GetLastError() );
901 /***********************************************************************
904 * Build an argv array from a command-line.
905 * 'reserved' is the number of args to reserve before the first one.
907 static char **build_argv( const WCHAR *cmdlineW, int reserved )
911 char *arg,*s,*d,*cmdline;
912 int in_quotes,bcount,len;
914 len = WideCharToMultiByte( CP_UNIXCP, 0, cmdlineW, -1, NULL, 0, NULL, NULL );
915 if (!(cmdline = malloc(len))) return NULL;
916 WideCharToMultiByte( CP_UNIXCP, 0, cmdlineW, -1, cmdline, len, NULL, NULL );
923 if (*s=='\0' || ((*s==' ' || *s=='\t') && !in_quotes)) {
926 /* skip the remaining spaces */
927 while (*s==' ' || *s=='\t') {
934 } else if (*s=='\\') {
935 /* '\', count them */
937 } else if ((*s=='"') && ((bcount & 1)==0)) {
939 in_quotes=!in_quotes;
942 /* a regular character */
947 argv=malloc(argc*sizeof(*argv));
956 if ((*s==' ' || *s=='\t') && !in_quotes) {
957 /* Close the argument and copy it */
961 /* skip the remaining spaces */
964 } while (*s==' ' || *s=='\t');
966 /* Start with a new argument */
969 } else if (*s=='\\') {
973 } else if (*s=='"') {
975 if ((bcount & 1)==0) {
976 /* Preceeded by an even number of '\', this is half that
977 * number of '\', plus a '"' which we discard.
981 in_quotes=!in_quotes;
983 /* Preceeded by an odd number of '\', this is half that
984 * number of '\' followed by a '"'
992 /* a regular character */
1007 /***********************************************************************
1010 * Allocate an environment string; helper for build_envp
1012 static char *alloc_env_string( const char *name, const char *value )
1014 char *ret = malloc( strlen(name) + strlen(value) + 1 );
1015 strcpy( ret, name );
1016 strcat( ret, value );
1020 /***********************************************************************
1023 * Build the environment of a new child process.
1025 static char **build_envp( const WCHAR *envW, const WCHAR *extra_envW )
1029 char *env, *extra_env = NULL;
1030 int count = 0, length;
1034 for (p = extra_envW; *p; count++) p += strlenW(p) + 1;
1036 length = WideCharToMultiByte( CP_UNIXCP, 0, extra_envW, p - extra_envW,
1037 NULL, 0, NULL, NULL );
1038 if ((extra_env = malloc( length )))
1039 WideCharToMultiByte( CP_UNIXCP, 0, extra_envW, p - extra_envW,
1040 extra_env, length, NULL, NULL );
1042 for (p = envW; *p; count++) p += strlenW(p) + 1;
1044 length = WideCharToMultiByte( CP_UNIXCP, 0, envW, p - envW, NULL, 0, NULL, NULL );
1045 if (!(env = malloc( length ))) return NULL;
1046 WideCharToMultiByte( CP_UNIXCP, 0, envW, p - envW, env, length, NULL, NULL );
1050 if ((envp = malloc( count * sizeof(*envp) )))
1052 char **envptr = envp;
1055 /* first the extra strings */
1056 if (extra_env) for (p = extra_env; *p; p += strlen(p) + 1) *envptr++ = p;
1057 /* then put PATH, HOME and WINEPREFIX from the unix env */
1058 if ((p = getenv("PATH"))) *envptr++ = alloc_env_string( "PATH=", p );
1059 if ((p = getenv("HOME"))) *envptr++ = alloc_env_string( "HOME=", p );
1060 if ((p = getenv("WINEPREFIX"))) *envptr++ = alloc_env_string( "WINEPREFIX=", p );
1061 /* now put the Windows environment strings */
1062 for (p = env; *p; p += strlen(p) + 1)
1064 if (!memcmp( p, "PATH=", 5 )) /* store PATH as WINEPATH */
1065 *envptr++ = alloc_env_string( "WINEPATH=", p + 5 );
1066 else if (memcmp( p, "HOME=", 5 ) &&
1067 memcmp( p, "WINEPATH=", 9 ) &&
1068 memcmp( p, "WINEPREFIX=", 11 )) *envptr++ = p;
1076 /***********************************************************************
1079 * Fork and exec a new Unix binary, checking for errors.
1081 static int fork_and_exec( const char *filename, const WCHAR *cmdline,
1082 const WCHAR *env, const char *newdir )
1087 if (!env) env = GetEnvironmentStringsW();
1094 fcntl( fd[1], F_SETFD, 1 ); /* set close on exec */
1095 if (!(pid = fork())) /* child */
1097 char **argv = build_argv( cmdline, 0 );
1098 char **envp = build_envp( env, NULL );
1101 /* Reset signals that we previously set to SIG_IGN */
1102 signal( SIGPIPE, SIG_DFL );
1103 signal( SIGCHLD, SIG_DFL );
1105 if (newdir) chdir(newdir);
1107 if (argv && envp) execve( filename, argv, envp );
1109 write( fd[1], &err, sizeof(err) );
1113 if ((pid != -1) && (read( fd[0], &err, sizeof(err) ) > 0)) /* exec failed */
1118 if (pid == -1) FILE_SetDosError();
1124 /***********************************************************************
1125 * create_user_params
1127 static RTL_USER_PROCESS_PARAMETERS *create_user_params( LPCWSTR filename, LPCWSTR cmdline,
1128 const STARTUPINFOW *startup )
1130 RTL_USER_PROCESS_PARAMETERS *params;
1131 UNICODE_STRING image_str, cmdline_str, desktop, title;
1133 WCHAR buffer[MAX_PATH];
1135 if (GetLongPathNameW( filename, buffer, MAX_PATH ))
1136 RtlInitUnicodeString( &image_str, buffer );
1138 RtlInitUnicodeString( &image_str, filename );
1140 RtlInitUnicodeString( &cmdline_str, cmdline );
1141 if (startup->lpDesktop) RtlInitUnicodeString( &desktop, startup->lpDesktop );
1142 if (startup->lpTitle) RtlInitUnicodeString( &title, startup->lpTitle );
1144 status = RtlCreateProcessParameters( ¶ms, &image_str, NULL, NULL, &cmdline_str, NULL,
1145 startup->lpTitle ? &title : NULL,
1146 startup->lpDesktop ? &desktop : NULL,
1148 if (status != STATUS_SUCCESS)
1150 SetLastError( RtlNtStatusToDosError(status) );
1154 params->Environment = NULL; /* we pass it through the Unix environment */
1155 params->hStdInput = startup->hStdInput;
1156 params->hStdOutput = startup->hStdOutput;
1157 params->hStdError = startup->hStdError;
1158 params->dwX = startup->dwX;
1159 params->dwY = startup->dwY;
1160 params->dwXSize = startup->dwXSize;
1161 params->dwYSize = startup->dwYSize;
1162 params->dwXCountChars = startup->dwXCountChars;
1163 params->dwYCountChars = startup->dwYCountChars;
1164 params->dwFillAttribute = startup->dwFillAttribute;
1165 params->dwFlags = startup->dwFlags;
1166 params->wShowWindow = startup->wShowWindow;
1171 /***********************************************************************
1174 * Create a new process. If hFile is a valid handle we have an exe
1175 * file, otherwise it is a Winelib app.
1177 static BOOL create_process( HANDLE hFile, LPCWSTR filename, LPWSTR cmd_line, LPWSTR env,
1178 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1179 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1180 LPPROCESS_INFORMATION info, LPCSTR unixdir )
1182 BOOL ret, success = FALSE;
1183 HANDLE process_info;
1184 RTL_USER_PROCESS_PARAMETERS *params;
1185 WCHAR *extra_env = NULL;
1194 env = GetEnvironmentStringsW();
1195 extra_env = DRIVE_BuildEnv();
1198 if (!(params = create_user_params( filename, cmd_line, startup )))
1200 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1204 /* create the synchronization pipes */
1206 if (pipe( startfd ) == -1)
1209 RtlDestroyProcessParameters( params );
1210 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1213 if (pipe( execfd ) == -1)
1216 close( startfd[0] );
1217 close( startfd[1] );
1218 RtlDestroyProcessParameters( params );
1219 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1222 fcntl( execfd[1], F_SETFD, 1 ); /* set close on exec */
1224 /* create the child process */
1226 if (!(pid = fork())) /* child */
1228 char **argv = build_argv( cmd_line, 1 );
1229 char **envp = build_envp( env, extra_env );
1231 close( startfd[1] );
1234 /* wait for parent to tell us to start */
1235 if (read( startfd[0], &dummy, 1 ) != 1) _exit(1);
1237 close( startfd[0] );
1238 /* Reset signals that we previously set to SIG_IGN */
1239 signal( SIGPIPE, SIG_DFL );
1240 signal( SIGCHLD, SIG_DFL );
1242 if (unixdir) chdir(unixdir);
1246 /* first, try for a WINELOADER environment variable */
1247 argv[0] = getenv("WINELOADER");
1248 if (argv[0]) execve( argv[0], argv, envp );
1249 /* now use the standard search strategy */
1250 wine_exec_wine_binary( NULL, argv, envp );
1253 write( execfd[1], &err, sizeof(err) );
1257 /* this is the parent */
1259 close( startfd[0] );
1261 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1264 close( startfd[1] );
1267 RtlDestroyProcessParameters( params );
1271 /* create the process on the server side */
1273 SERVER_START_REQ( new_process )
1275 req->inherit_all = inherit;
1276 req->create_flags = flags;
1277 req->unix_pid = pid;
1278 req->exe_file = hFile;
1279 if (startup->dwFlags & STARTF_USESTDHANDLES)
1281 req->hstdin = startup->hStdInput;
1282 req->hstdout = startup->hStdOutput;
1283 req->hstderr = startup->hStdError;
1287 req->hstdin = GetStdHandle( STD_INPUT_HANDLE );
1288 req->hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
1289 req->hstderr = GetStdHandle( STD_ERROR_HANDLE );
1292 if ((flags & (CREATE_NEW_CONSOLE | DETACHED_PROCESS)) != 0)
1294 /* this is temporary (for console handles). We have no way to control that the handle is invalid in child process otherwise */
1295 if (is_console_handle(req->hstdin)) req->hstdin = INVALID_HANDLE_VALUE;
1296 if (is_console_handle(req->hstdout)) req->hstdout = INVALID_HANDLE_VALUE;
1297 if (is_console_handle(req->hstderr)) req->hstderr = INVALID_HANDLE_VALUE;
1301 if (is_console_handle(req->hstdin)) req->hstdin = console_handle_unmap(req->hstdin);
1302 if (is_console_handle(req->hstdout)) req->hstdout = console_handle_unmap(req->hstdout);
1303 if (is_console_handle(req->hstderr)) req->hstderr = console_handle_unmap(req->hstderr);
1306 wine_server_add_data( req, params, params->Size );
1307 ret = !wine_server_call_err( req );
1308 process_info = reply->info;
1312 RtlDestroyProcessParameters( params );
1315 close( startfd[1] );
1320 /* tell child to start and wait for it to exec */
1322 write( startfd[1], &dummy, 1 );
1323 close( startfd[1] );
1325 if (read( execfd[0], &err, sizeof(err) ) > 0) /* exec failed */
1330 CloseHandle( process_info );
1334 /* wait for the new process info to be ready */
1336 WaitForSingleObject( process_info, INFINITE );
1337 SERVER_START_REQ( get_new_process_info )
1339 req->info = process_info;
1340 req->pinherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
1341 req->tinherit = (tsa && (tsa->nLength >= sizeof(*tsa)) && tsa->bInheritHandle);
1342 if ((ret = !wine_server_call_err( req )))
1344 info->dwProcessId = (DWORD)reply->pid;
1345 info->dwThreadId = (DWORD)reply->tid;
1346 info->hProcess = reply->phandle;
1347 info->hThread = reply->thandle;
1348 success = reply->success;
1353 if (ret && !success) /* new process failed to start */
1356 if (GetExitCodeProcess( info->hProcess, &exitcode )) SetLastError( exitcode );
1357 CloseHandle( info->hThread );
1358 CloseHandle( info->hProcess );
1361 CloseHandle( process_info );
1366 /***********************************************************************
1367 * create_vdm_process
1369 * Create a new VDM process for a 16-bit or DOS application.
1371 static BOOL create_vdm_process( LPCWSTR filename, LPWSTR cmd_line, LPWSTR env,
1372 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1373 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1374 LPPROCESS_INFORMATION info, LPCSTR unixdir )
1376 static const WCHAR argsW[] = {'%','s',' ','-','-','a','p','p','-','n','a','m','e',' ','"','%','s','"',' ','%','s',0};
1379 LPWSTR new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
1380 (strlenW(filename) + strlenW(cmd_line) + 30) * sizeof(WCHAR) );
1384 SetLastError( ERROR_OUTOFMEMORY );
1387 sprintfW( new_cmd_line, argsW, winevdmW, filename, cmd_line );
1388 ret = create_process( 0, winevdmW, new_cmd_line, env, psa, tsa, inherit,
1389 flags, startup, info, unixdir );
1390 HeapFree( GetProcessHeap(), 0, new_cmd_line );
1395 /***********************************************************************
1396 * create_cmd_process
1398 * Create a new cmd shell process for a .BAT file.
1400 static BOOL create_cmd_process( LPCWSTR filename, LPWSTR cmd_line, LPVOID env,
1401 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1402 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1403 LPPROCESS_INFORMATION info, LPCWSTR cur_dir )
1406 static const WCHAR comspecW[] = {'C','O','M','S','P','E','C',0};
1407 static const WCHAR slashcW[] = {' ','/','c',' ',0};
1408 WCHAR comspec[MAX_PATH];
1412 if (!GetEnvironmentVariableW( comspecW, comspec, sizeof(comspec)/sizeof(WCHAR) ))
1414 if (!(newcmdline = HeapAlloc( GetProcessHeap(), 0,
1415 (strlenW(comspec) + 4 + strlenW(cmd_line) + 1) * sizeof(WCHAR))))
1418 strcpyW( newcmdline, comspec );
1419 strcatW( newcmdline, slashcW );
1420 strcatW( newcmdline, cmd_line );
1421 ret = CreateProcessW( comspec, newcmdline, psa, tsa, inherit,
1422 flags, env, cur_dir, startup, info );
1423 HeapFree( GetProcessHeap(), 0, newcmdline );
1428 /*************************************************************************
1431 * Helper for CreateProcess: retrieve the file name to load from the
1432 * app name and command line. Store the file name in buffer, and
1433 * return a possibly modified command line.
1434 * Also returns a handle to the opened file if it's a Windows binary.
1436 static LPWSTR get_file_name( LPCWSTR appname, LPWSTR cmdline, LPWSTR buffer,
1437 int buflen, HANDLE *handle )
1439 static const WCHAR quotesW[] = {'"','%','s','"',0};
1441 WCHAR *name, *pos, *ret = NULL;
1444 /* if we have an app name, everything is easy */
1448 /* use the unmodified app name as file name */
1449 lstrcpynW( buffer, appname, buflen );
1450 *handle = open_exe_file( buffer );
1451 if (!(ret = cmdline) || !cmdline[0])
1453 /* no command-line, create one */
1454 if ((ret = HeapAlloc( GetProcessHeap(), 0, (strlenW(appname) + 3) * sizeof(WCHAR) )))
1455 sprintfW( ret, quotesW, appname );
1462 SetLastError( ERROR_INVALID_PARAMETER );
1466 /* first check for a quoted file name */
1468 if ((cmdline[0] == '"') && ((p = strchrW( cmdline + 1, '"' ))))
1470 int len = p - cmdline - 1;
1471 /* extract the quoted portion as file name */
1472 if (!(name = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return NULL;
1473 memcpy( name, cmdline + 1, len * sizeof(WCHAR) );
1476 if (find_exe_file( name, buffer, buflen, handle ))
1477 ret = cmdline; /* no change necessary */
1481 /* now try the command-line word by word */
1483 if (!(name = HeapAlloc( GetProcessHeap(), 0, (strlenW(cmdline) + 1) * sizeof(WCHAR) )))
1490 do *pos++ = *p++; while (*p && *p != ' ');
1492 if (find_exe_file( name, buffer, buflen, handle ))
1499 if (!ret || !strchrW( name, ' ' )) goto done; /* no change necessary */
1501 /* now build a new command-line with quotes */
1503 if (!(ret = HeapAlloc( GetProcessHeap(), 0, (strlenW(cmdline) + 3) * sizeof(WCHAR) )))
1505 sprintfW( ret, quotesW, name );
1509 HeapFree( GetProcessHeap(), 0, name );
1514 /**********************************************************************
1515 * CreateProcessA (KERNEL32.@)
1517 BOOL WINAPI CreateProcessA( LPCSTR app_name, LPSTR cmd_line, LPSECURITY_ATTRIBUTES process_attr,
1518 LPSECURITY_ATTRIBUTES thread_attr, BOOL inherit,
1519 DWORD flags, LPVOID env, LPCSTR cur_dir,
1520 LPSTARTUPINFOA startup_info, LPPROCESS_INFORMATION info )
1523 UNICODE_STRING app_nameW, cmd_lineW, cur_dirW, desktopW, titleW;
1526 if (app_name) RtlCreateUnicodeStringFromAsciiz( &app_nameW, app_name );
1527 else app_nameW.Buffer = NULL;
1528 if (cmd_line) RtlCreateUnicodeStringFromAsciiz( &cmd_lineW, cmd_line );
1529 else cmd_lineW.Buffer = NULL;
1530 if (cur_dir) RtlCreateUnicodeStringFromAsciiz( &cur_dirW, cur_dir );
1531 else cur_dirW.Buffer = NULL;
1532 if (startup_info->lpDesktop) RtlCreateUnicodeStringFromAsciiz( &desktopW, startup_info->lpDesktop );
1533 else desktopW.Buffer = NULL;
1534 if (startup_info->lpTitle) RtlCreateUnicodeStringFromAsciiz( &titleW, startup_info->lpTitle );
1535 else titleW.Buffer = NULL;
1537 memcpy( &infoW, startup_info, sizeof(infoW) );
1538 infoW.lpDesktop = desktopW.Buffer;
1539 infoW.lpTitle = titleW.Buffer;
1541 if (startup_info->lpReserved)
1542 FIXME("StartupInfo.lpReserved is used, please report (%s)\n",
1543 debugstr_a(startup_info->lpReserved));
1545 ret = CreateProcessW( app_nameW.Buffer, cmd_lineW.Buffer, process_attr, thread_attr,
1546 inherit, flags, env, cur_dirW.Buffer, &infoW, info );
1548 RtlFreeUnicodeString( &app_nameW );
1549 RtlFreeUnicodeString( &cmd_lineW );
1550 RtlFreeUnicodeString( &cur_dirW );
1551 RtlFreeUnicodeString( &desktopW );
1552 RtlFreeUnicodeString( &titleW );
1557 /**********************************************************************
1558 * CreateProcessW (KERNEL32.@)
1560 BOOL WINAPI CreateProcessW( LPCWSTR app_name, LPWSTR cmd_line, LPSECURITY_ATTRIBUTES process_attr,
1561 LPSECURITY_ATTRIBUTES thread_attr, BOOL inherit, DWORD flags,
1562 LPVOID env, LPCWSTR cur_dir, LPSTARTUPINFOW startup_info,
1563 LPPROCESS_INFORMATION info )
1567 const char *unixdir = NULL;
1568 DOS_FULL_NAME full_dir;
1569 WCHAR name[MAX_PATH];
1570 WCHAR *tidy_cmdline, *p, *envW = env;
1572 /* Process the AppName and/or CmdLine to get module name and path */
1574 TRACE("app %s cmdline %s\n", debugstr_w(app_name), debugstr_w(cmd_line) );
1576 if (!(tidy_cmdline = get_file_name( app_name, cmd_line, name, sizeof(name), &hFile )))
1578 if (hFile == INVALID_HANDLE_VALUE) goto done;
1580 /* Warn if unsupported features are used */
1582 if (flags & (IDLE_PRIORITY_CLASS | HIGH_PRIORITY_CLASS | REALTIME_PRIORITY_CLASS |
1583 CREATE_NEW_PROCESS_GROUP | CREATE_SEPARATE_WOW_VDM | CREATE_SHARED_WOW_VDM |
1584 CREATE_DEFAULT_ERROR_MODE | CREATE_NO_WINDOW |
1585 PROFILE_USER | PROFILE_KERNEL | PROFILE_SERVER))
1586 WARN("(%s,...): ignoring some flags in %lx\n", debugstr_w(name), flags);
1590 if (DOSFS_GetFullName( cur_dir, TRUE, &full_dir )) unixdir = full_dir.long_name;
1594 WCHAR buf[MAX_PATH];
1595 if (GetCurrentDirectoryW(MAX_PATH, buf))
1597 if (DOSFS_GetFullName( buf, TRUE, &full_dir )) unixdir = full_dir.long_name;
1601 if (env && !(flags & CREATE_UNICODE_ENVIRONMENT)) /* convert environment to unicode */
1606 while (*p) p += strlen(p) + 1;
1607 p++; /* final null */
1608 lenW = MultiByteToWideChar( CP_ACP, 0, env, p - (char*)env, NULL, 0 );
1609 envW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) );
1610 MultiByteToWideChar( CP_ACP, 0, env, p - (char*)env, envW, lenW );
1611 flags |= CREATE_UNICODE_ENVIRONMENT;
1614 info->hThread = info->hProcess = 0;
1615 info->dwProcessId = info->dwThreadId = 0;
1617 /* Determine executable type */
1619 if (!hFile) /* builtin exe */
1621 TRACE( "starting %s as Winelib app\n", debugstr_w(name) );
1622 retv = create_process( 0, name, tidy_cmdline, envW, process_attr, thread_attr,
1623 inherit, flags, startup_info, info, unixdir );
1627 switch( MODULE_GetBinaryType( hFile ))
1630 TRACE( "starting %s as Win32 binary\n", debugstr_w(name) );
1631 retv = create_process( hFile, name, tidy_cmdline, envW, process_attr, thread_attr,
1632 inherit, flags, startup_info, info, unixdir );
1636 TRACE( "starting %s as Win16/DOS binary\n", debugstr_w(name) );
1637 retv = create_vdm_process( name, tidy_cmdline, envW, process_attr, thread_attr,
1638 inherit, flags, startup_info, info, unixdir );
1641 FIXME( "%s is OS/2 binary, not supported\n", debugstr_w(name) );
1642 SetLastError( ERROR_BAD_EXE_FORMAT );
1645 TRACE( "not starting %s since it is a dll\n", debugstr_w(name) );
1646 SetLastError( ERROR_BAD_EXE_FORMAT );
1648 case BINARY_UNIX_LIB:
1649 TRACE( "%s is a Unix library, starting as Winelib app\n", debugstr_w(name) );
1650 retv = create_process( hFile, name, tidy_cmdline, envW, process_attr, thread_attr,
1651 inherit, flags, startup_info, info, unixdir );
1653 case BINARY_UNKNOWN:
1654 /* check for .com or .bat extension */
1655 if ((p = strrchrW( name, '.' )))
1657 if (!strcmpiW( p, comW ))
1659 TRACE( "starting %s as DOS binary\n", debugstr_w(name) );
1660 retv = create_vdm_process( name, tidy_cmdline, envW, process_attr, thread_attr,
1661 inherit, flags, startup_info, info, unixdir );
1664 if (!strcmpiW( p, batW ))
1666 TRACE( "starting %s as batch binary\n", debugstr_w(name) );
1667 retv = create_cmd_process( name, tidy_cmdline, envW, process_attr, thread_attr,
1668 inherit, flags, startup_info, info, cur_dir );
1673 case BINARY_UNIX_EXE:
1675 /* unknown file, try as unix executable */
1676 DOS_FULL_NAME full_name;
1678 TRACE( "starting %s as Unix binary\n", debugstr_w(name) );
1680 if (DOSFS_GetFullName( name, TRUE, &full_name ))
1681 retv = (fork_and_exec( full_name.long_name, tidy_cmdline, envW, unixdir ) != -1);
1685 CloseHandle( hFile );
1688 if (tidy_cmdline != cmd_line) HeapFree( GetProcessHeap(), 0, tidy_cmdline );
1689 if (envW != env) HeapFree( GetProcessHeap(), 0, envW );
1694 /***********************************************************************
1697 * Wrapper to call WaitForInputIdle USER function
1699 typedef DWORD (WINAPI *WaitForInputIdle_ptr)( HANDLE hProcess, DWORD dwTimeOut );
1701 static DWORD wait_input_idle( HANDLE process, DWORD timeout )
1703 HMODULE mod = GetModuleHandleA( "user32.dll" );
1706 WaitForInputIdle_ptr ptr = (WaitForInputIdle_ptr)GetProcAddress( mod, "WaitForInputIdle" );
1707 if (ptr) return ptr( process, timeout );
1713 /***********************************************************************
1714 * WinExec (KERNEL32.@)
1716 UINT WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow )
1718 PROCESS_INFORMATION info;
1719 STARTUPINFOA startup;
1723 memset( &startup, 0, sizeof(startup) );
1724 startup.cb = sizeof(startup);
1725 startup.dwFlags = STARTF_USESHOWWINDOW;
1726 startup.wShowWindow = nCmdShow;
1728 /* cmdline needs to be writeable for CreateProcess */
1729 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine)+1 ))) return 0;
1730 strcpy( cmdline, lpCmdLine );
1732 if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE,
1733 0, NULL, NULL, &startup, &info ))
1735 /* Give 30 seconds to the app to come up */
1736 if (wait_input_idle( info.hProcess, 30000 ) == WAIT_FAILED)
1737 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1739 /* Close off the handles */
1740 CloseHandle( info.hThread );
1741 CloseHandle( info.hProcess );
1743 else if ((ret = GetLastError()) >= 32)
1745 FIXME("Strange error set by CreateProcess: %d\n", ret );
1748 HeapFree( GetProcessHeap(), 0, cmdline );
1753 /**********************************************************************
1754 * LoadModule (KERNEL32.@)
1756 HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
1758 LOADPARMS32 *params = paramBlock;
1759 PROCESS_INFORMATION info;
1760 STARTUPINFOA startup;
1761 HINSTANCE hInstance;
1763 char filename[MAX_PATH];
1766 if (!name) return (HINSTANCE)ERROR_FILE_NOT_FOUND;
1768 if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) &&
1769 !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL ))
1770 return (HINSTANCE)GetLastError();
1772 len = (BYTE)params->lpCmdLine[0];
1773 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 )))
1774 return (HINSTANCE)ERROR_NOT_ENOUGH_MEMORY;
1776 strcpy( cmdline, filename );
1777 p = cmdline + strlen(cmdline);
1779 memcpy( p, params->lpCmdLine + 1, len );
1782 memset( &startup, 0, sizeof(startup) );
1783 startup.cb = sizeof(startup);
1784 if (params->lpCmdShow)
1786 startup.dwFlags = STARTF_USESHOWWINDOW;
1787 startup.wShowWindow = ((WORD *)params->lpCmdShow)[1];
1790 if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0,
1791 params->lpEnvAddress, NULL, &startup, &info ))
1793 /* Give 30 seconds to the app to come up */
1794 if (wait_input_idle( info.hProcess, 30000 ) == WAIT_FAILED)
1795 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1796 hInstance = (HINSTANCE)33;
1797 /* Close off the handles */
1798 CloseHandle( info.hThread );
1799 CloseHandle( info.hProcess );
1801 else if ((hInstance = (HINSTANCE)GetLastError()) >= (HINSTANCE)32)
1803 FIXME("Strange error set by CreateProcess: %p\n", hInstance );
1804 hInstance = (HINSTANCE)11;
1807 HeapFree( GetProcessHeap(), 0, cmdline );
1812 /******************************************************************************
1813 * TerminateProcess (KERNEL32.@)
1815 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
1817 NTSTATUS status = NtTerminateProcess( handle, exit_code );
1818 if (status) SetLastError( RtlNtStatusToDosError(status) );
1823 /***********************************************************************
1824 * ExitProcess (KERNEL32.@)
1826 void WINAPI ExitProcess( DWORD status )
1828 LdrShutdownProcess();
1829 SERVER_START_REQ( terminate_process )
1831 /* send the exit code to the server */
1832 req->handle = GetCurrentProcess();
1833 req->exit_code = status;
1834 wine_server_call( req );
1841 /***********************************************************************
1842 * GetExitCodeProcess [KERNEL32.@]
1844 * Gets termination status of specified process
1850 BOOL WINAPI GetExitCodeProcess(
1851 HANDLE hProcess, /* [in] handle to the process */
1852 LPDWORD lpExitCode) /* [out] address to receive termination status */
1855 SERVER_START_REQ( get_process_info )
1857 req->handle = hProcess;
1858 ret = !wine_server_call_err( req );
1859 if (ret && lpExitCode) *lpExitCode = reply->exit_code;
1866 /***********************************************************************
1867 * SetErrorMode (KERNEL32.@)
1869 UINT WINAPI SetErrorMode( UINT mode )
1871 UINT old = process_error_mode;
1872 process_error_mode = mode;
1877 /**********************************************************************
1878 * TlsAlloc [KERNEL32.@] Allocates a TLS index.
1880 * Allocates a thread local storage index
1883 * Success: TLS Index
1884 * Failure: 0xFFFFFFFF
1886 DWORD WINAPI TlsAlloc( void )
1890 RtlAcquirePebLock();
1891 index = RtlFindClearBitsAndSet( NtCurrentTeb()->Peb->TlsBitmap, 1, 0 );
1892 if (index != ~0UL) NtCurrentTeb()->TlsSlots[index] = 0; /* clear the value */
1893 else SetLastError( ERROR_NO_MORE_ITEMS );
1894 RtlReleasePebLock();
1899 /**********************************************************************
1900 * TlsFree [KERNEL32.@] Releases a TLS index.
1902 * Releases a thread local storage index, making it available for reuse
1908 BOOL WINAPI TlsFree(
1909 DWORD index) /* [in] TLS Index to free */
1913 RtlAcquirePebLock();
1914 ret = RtlAreBitsSet( NtCurrentTeb()->Peb->TlsBitmap, index, 1 );
1917 RtlClearBits( NtCurrentTeb()->Peb->TlsBitmap, index, 1 );
1918 NtSetInformationThread( GetCurrentThread(), ThreadZeroTlsCell, &index, sizeof(index) );
1920 else SetLastError( ERROR_INVALID_PARAMETER );
1921 RtlReleasePebLock();
1926 /**********************************************************************
1927 * TlsGetValue [KERNEL32.@] Gets value in a thread's TLS slot
1930 * Success: Value stored in calling thread's TLS slot for index
1931 * Failure: 0 and GetLastError returns NO_ERROR
1933 LPVOID WINAPI TlsGetValue(
1934 DWORD index) /* [in] TLS index to retrieve value for */
1936 if (index >= NtCurrentTeb()->Peb->TlsBitmap->SizeOfBitMap)
1938 SetLastError( ERROR_INVALID_PARAMETER );
1941 SetLastError( ERROR_SUCCESS );
1942 return NtCurrentTeb()->TlsSlots[index];
1946 /**********************************************************************
1947 * TlsSetValue [KERNEL32.@] Stores a value in the thread's TLS slot.
1953 BOOL WINAPI TlsSetValue(
1954 DWORD index, /* [in] TLS index to set value for */
1955 LPVOID value) /* [in] Value to be stored */
1957 if (index >= NtCurrentTeb()->Peb->TlsBitmap->SizeOfBitMap)
1959 SetLastError( ERROR_INVALID_PARAMETER );
1962 NtCurrentTeb()->TlsSlots[index] = value;
1967 /***********************************************************************
1968 * GetProcessFlags (KERNEL32.@)
1970 DWORD WINAPI GetProcessFlags( DWORD processid )
1972 IMAGE_NT_HEADERS *nt;
1975 if (processid && processid != GetCurrentProcessId()) return 0;
1977 if ((nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress )))
1979 if (nt->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
1980 flags |= PDB32_CONSOLE_PROC;
1982 if (!AreFileApisANSI()) flags |= PDB32_FILE_APIS_OEM;
1983 if (IsDebuggerPresent()) flags |= PDB32_DEBUGGED;
1988 /***********************************************************************
1989 * GetProcessDword (KERNEL.485)
1990 * GetProcessDword (KERNEL32.18)
1991 * 'Of course you cannot directly access Windows internal structures'
1993 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
1998 TRACE("(%ld, %d)\n", dwProcessID, offset );
2000 if (dwProcessID && dwProcessID != GetCurrentProcessId())
2002 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
2008 case GPD_APP_COMPAT_FLAGS:
2009 return GetAppCompatFlags16(0);
2010 case GPD_LOAD_DONE_EVENT:
2012 case GPD_HINSTANCE16:
2013 return GetTaskDS16();
2014 case GPD_WINDOWS_VERSION:
2015 return GetExeVersion16();
2017 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
2019 return (DWORD)NtCurrentTeb()->Peb;
2020 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
2021 GetStartupInfoW(&siw);
2022 return (DWORD)siw.hStdOutput;
2023 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
2024 GetStartupInfoW(&siw);
2025 return (DWORD)siw.hStdInput;
2026 case GPD_STARTF_SHOWWINDOW:
2027 GetStartupInfoW(&siw);
2028 return siw.wShowWindow;
2029 case GPD_STARTF_SIZE:
2030 GetStartupInfoW(&siw);
2032 if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
2034 if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
2035 return MAKELONG( x, y );
2036 case GPD_STARTF_POSITION:
2037 GetStartupInfoW(&siw);
2039 if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
2041 if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
2042 return MAKELONG( x, y );
2043 case GPD_STARTF_FLAGS:
2044 GetStartupInfoW(&siw);
2049 return GetProcessFlags(0);
2051 return process_dword;
2053 ERR("Unknown offset %d\n", offset );
2058 /***********************************************************************
2059 * SetProcessDword (KERNEL.484)
2060 * 'Of course you cannot directly access Windows internal structures'
2062 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
2064 TRACE("(%ld, %d)\n", dwProcessID, offset );
2066 if (dwProcessID && dwProcessID != GetCurrentProcessId())
2068 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
2074 case GPD_APP_COMPAT_FLAGS:
2075 case GPD_LOAD_DONE_EVENT:
2076 case GPD_HINSTANCE16:
2077 case GPD_WINDOWS_VERSION:
2080 case GPD_STARTF_SHELLDATA:
2081 case GPD_STARTF_HOTKEY:
2082 case GPD_STARTF_SHOWWINDOW:
2083 case GPD_STARTF_SIZE:
2084 case GPD_STARTF_POSITION:
2085 case GPD_STARTF_FLAGS:
2088 ERR("Not allowed to modify offset %d\n", offset );
2091 process_dword = value;
2094 ERR("Unknown offset %d\n", offset );
2100 /***********************************************************************
2101 * ExitProcess (KERNEL.466)
2103 void WINAPI ExitProcess16( WORD status )
2106 ReleaseThunkLock( &count );
2107 ExitProcess( status );
2111 /*********************************************************************
2112 * OpenProcess (KERNEL32.@)
2114 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
2117 SERVER_START_REQ( open_process )
2120 req->access = access;
2121 req->inherit = inherit;
2122 if (!wine_server_call_err( req )) ret = reply->handle;
2129 /*********************************************************************
2130 * MapProcessHandle (KERNEL.483)
2132 DWORD WINAPI MapProcessHandle( HANDLE handle )
2135 SERVER_START_REQ( get_process_info )
2137 req->handle = handle;
2138 if (!wine_server_call_err( req )) ret = reply->pid;
2145 /*********************************************************************
2146 * CloseW32Handle (KERNEL.474)
2147 * CloseHandle (KERNEL32.@)
2149 BOOL WINAPI CloseHandle( HANDLE handle )
2153 /* stdio handles need special treatment */
2154 if ((handle == (HANDLE)STD_INPUT_HANDLE) ||
2155 (handle == (HANDLE)STD_OUTPUT_HANDLE) ||
2156 (handle == (HANDLE)STD_ERROR_HANDLE))
2157 handle = GetStdHandle( (DWORD)handle );
2159 if (is_console_handle(handle))
2160 return CloseConsoleHandle(handle);
2162 status = NtClose( handle );
2163 if (status) SetLastError( RtlNtStatusToDosError(status) );
2168 /*********************************************************************
2169 * GetHandleInformation (KERNEL32.@)
2171 BOOL WINAPI GetHandleInformation( HANDLE handle, LPDWORD flags )
2174 SERVER_START_REQ( set_handle_info )
2176 req->handle = handle;
2180 ret = !wine_server_call_err( req );
2181 if (ret && flags) *flags = reply->old_flags;
2188 /*********************************************************************
2189 * SetHandleInformation (KERNEL32.@)
2191 BOOL WINAPI SetHandleInformation( HANDLE handle, DWORD mask, DWORD flags )
2194 SERVER_START_REQ( set_handle_info )
2196 req->handle = handle;
2200 ret = !wine_server_call_err( req );
2207 /*********************************************************************
2208 * DuplicateHandle (KERNEL32.@)
2210 BOOL WINAPI DuplicateHandle( HANDLE source_process, HANDLE source,
2211 HANDLE dest_process, HANDLE *dest,
2212 DWORD access, BOOL inherit, DWORD options )
2216 if (is_console_handle(source))
2218 /* FIXME: this test is not sufficient, we need to test process ids, not handles */
2219 if (source_process != dest_process ||
2220 source_process != GetCurrentProcess())
2222 SetLastError(ERROR_INVALID_PARAMETER);
2225 *dest = DuplicateConsoleHandle( source, access, inherit, options );
2226 return (*dest != INVALID_HANDLE_VALUE);
2228 status = NtDuplicateObject( source_process, source, dest_process, dest,
2229 access, inherit ? OBJ_INHERIT : 0, options );
2230 if (status) SetLastError( RtlNtStatusToDosError(status) );
2235 /***********************************************************************
2236 * ConvertToGlobalHandle (KERNEL.476)
2237 * ConvertToGlobalHandle (KERNEL32.@)
2239 HANDLE WINAPI ConvertToGlobalHandle(HANDLE hSrc)
2241 HANDLE ret = INVALID_HANDLE_VALUE;
2242 DuplicateHandle( GetCurrentProcess(), hSrc, GetCurrentProcess(), &ret, 0, FALSE,
2243 DUP_HANDLE_MAKE_GLOBAL | DUP_HANDLE_SAME_ACCESS | DUP_HANDLE_CLOSE_SOURCE );
2248 /***********************************************************************
2249 * SetHandleContext (KERNEL32.@)
2251 BOOL WINAPI SetHandleContext(HANDLE hnd,DWORD context)
2253 FIXME("(%p,%ld), stub. In case this got called by WSOCK32/WS2_32: "
2254 "the external WINSOCK DLLs won't work with WINE, don't use them.\n",hnd,context);
2255 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2260 /***********************************************************************
2261 * GetHandleContext (KERNEL32.@)
2263 DWORD WINAPI GetHandleContext(HANDLE hnd)
2265 FIXME("(%p), stub. In case this got called by WSOCK32/WS2_32: "
2266 "the external WINSOCK DLLs won't work with WINE, don't use them.\n",hnd);
2267 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2272 /***********************************************************************
2273 * CreateSocketHandle (KERNEL32.@)
2275 HANDLE WINAPI CreateSocketHandle(void)
2277 FIXME("(), stub. In case this got called by WSOCK32/WS2_32: "
2278 "the external WINSOCK DLLs won't work with WINE, don't use them.\n");
2279 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2280 return INVALID_HANDLE_VALUE;
2284 /***********************************************************************
2285 * SetPriorityClass (KERNEL32.@)
2287 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
2290 SERVER_START_REQ( set_process_info )
2292 req->handle = hprocess;
2293 req->priority = priorityclass;
2294 req->mask = SET_PROCESS_INFO_PRIORITY;
2295 ret = !wine_server_call_err( req );
2302 /***********************************************************************
2303 * GetPriorityClass (KERNEL32.@)
2305 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
2308 SERVER_START_REQ( get_process_info )
2310 req->handle = hprocess;
2311 if (!wine_server_call_err( req )) ret = reply->priority;
2318 /***********************************************************************
2319 * SetProcessAffinityMask (KERNEL32.@)
2321 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
2324 SERVER_START_REQ( set_process_info )
2326 req->handle = hProcess;
2327 req->affinity = affmask;
2328 req->mask = SET_PROCESS_INFO_AFFINITY;
2329 ret = !wine_server_call_err( req );
2336 /**********************************************************************
2337 * GetProcessAffinityMask (KERNEL32.@)
2339 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
2340 LPDWORD lpProcessAffinityMask,
2341 LPDWORD lpSystemAffinityMask )
2344 SERVER_START_REQ( get_process_info )
2346 req->handle = hProcess;
2347 if (!wine_server_call_err( req ))
2349 if (lpProcessAffinityMask) *lpProcessAffinityMask = reply->process_affinity;
2350 if (lpSystemAffinityMask) *lpSystemAffinityMask = reply->system_affinity;
2359 /***********************************************************************
2360 * GetProcessVersion (KERNEL32.@)
2362 DWORD WINAPI GetProcessVersion( DWORD processid )
2364 IMAGE_NT_HEADERS *nt;
2366 if (processid && processid != GetCurrentProcessId())
2368 FIXME("should use ReadProcessMemory\n");
2371 if ((nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress )))
2372 return ((nt->OptionalHeader.MajorSubsystemVersion << 16) |
2373 nt->OptionalHeader.MinorSubsystemVersion);
2378 /***********************************************************************
2379 * SetProcessWorkingSetSize [KERNEL32.@]
2380 * Sets the min/max working set sizes for a specified process.
2383 * hProcess [I] Handle to the process of interest
2384 * minset [I] Specifies minimum working set size
2385 * maxset [I] Specifies maximum working set size
2389 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess, SIZE_T minset,
2392 FIXME("(%p,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
2393 if(( minset == (SIZE_T)-1) && (maxset == (SIZE_T)-1)) {
2394 /* Trim the working set to zero */
2395 /* Swap the process out of physical RAM */
2400 /***********************************************************************
2401 * GetProcessWorkingSetSize (KERNEL32.@)
2403 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess, PSIZE_T minset,
2406 FIXME("(%p,%p,%p): stub\n",hProcess,minset,maxset);
2407 /* 32 MB working set size */
2408 if (minset) *minset = 32*1024*1024;
2409 if (maxset) *maxset = 32*1024*1024;
2414 /***********************************************************************
2415 * SetProcessShutdownParameters (KERNEL32.@)
2417 BOOL WINAPI SetProcessShutdownParameters(DWORD level, DWORD flags)
2419 FIXME("(%08lx, %08lx): partial stub.\n", level, flags);
2420 shutdown_flags = flags;
2421 shutdown_priority = level;
2426 /***********************************************************************
2427 * GetProcessShutdownParameters (KERNEL32.@)
2430 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel, LPDWORD lpdwFlags )
2432 *lpdwLevel = shutdown_priority;
2433 *lpdwFlags = shutdown_flags;
2438 /***********************************************************************
2439 * GetProcessPriorityBoost (KERNEL32.@)
2441 BOOL WINAPI GetProcessPriorityBoost(HANDLE hprocess,PBOOL pDisablePriorityBoost)
2443 FIXME("(%p,%p): semi-stub\n", hprocess, pDisablePriorityBoost);
2445 /* Report that no boost is present.. */
2446 *pDisablePriorityBoost = FALSE;
2451 /***********************************************************************
2452 * SetProcessPriorityBoost (KERNEL32.@)
2454 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
2456 FIXME("(%p,%d): stub\n",hprocess,disableboost);
2457 /* Say we can do it. I doubt the program will notice that we don't. */
2462 /***********************************************************************
2463 * ReadProcessMemory (KERNEL32.@)
2465 BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, SIZE_T size,
2466 SIZE_T *bytes_read )
2468 NTSTATUS status = NtReadVirtualMemory( process, addr, buffer, size, bytes_read );
2469 if (status) SetLastError( RtlNtStatusToDosError(status) );
2474 /***********************************************************************
2475 * WriteProcessMemory (KERNEL32.@)
2477 BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPCVOID buffer, SIZE_T size,
2478 SIZE_T *bytes_written )
2480 NTSTATUS status = NtWriteVirtualMemory( process, addr, buffer, size, bytes_written );
2481 if (status) SetLastError( RtlNtStatusToDosError(status) );
2486 /****************************************************************************
2487 * FlushInstructionCache (KERNEL32.@)
2489 BOOL WINAPI FlushInstructionCache(HANDLE hProcess, LPCVOID lpBaseAddress, SIZE_T dwSize)
2491 if (GetVersion() & 0x80000000) return TRUE; /* not NT, always TRUE */
2492 FIXME("(%p,%p,0x%08lx): stub\n",hProcess, lpBaseAddress, dwSize);
2497 /******************************************************************
2498 * GetProcessIoCounters (KERNEL32.@)
2500 BOOL WINAPI GetProcessIoCounters(HANDLE hProcess, PIO_COUNTERS ioc)
2504 status = NtQueryInformationProcess(hProcess, ProcessIoCounters,
2505 ioc, sizeof(*ioc), NULL);
2506 if (status) SetLastError( RtlNtStatusToDosError(status) );
2510 /***********************************************************************
2511 * ProcessIdToSessionId (KERNEL32.@)
2512 * This function is available on Terminal Server 4SP4 and Windows 2000
2514 BOOL WINAPI ProcessIdToSessionId( DWORD procid, DWORD *sessionid_ptr )
2516 /* According to MSDN, if the calling process is not in a terminal
2517 * services environment, then the sessionid returned is zero.
2524 /***********************************************************************
2525 * RegisterServiceProcess (KERNEL.491)
2526 * RegisterServiceProcess (KERNEL32.@)
2528 * A service process calls this function to ensure that it continues to run
2529 * even after a user logged off.
2531 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
2533 /* I don't think that Wine needs to do anything in that function */
2534 return 1; /* success */
2538 /**************************************************************************
2539 * SetFileApisToOEM (KERNEL32.@)
2541 VOID WINAPI SetFileApisToOEM(void)
2543 oem_file_apis = TRUE;
2547 /**************************************************************************
2548 * SetFileApisToANSI (KERNEL32.@)
2550 VOID WINAPI SetFileApisToANSI(void)
2552 oem_file_apis = FALSE;
2556 /******************************************************************************
2557 * AreFileApisANSI [KERNEL32.@] Determines if file functions are using ANSI
2560 * TRUE: Set of file functions is using ANSI code page
2561 * FALSE: Set of file functions is using OEM code page
2563 BOOL WINAPI AreFileApisANSI(void)
2565 return !oem_file_apis;
2569 /***********************************************************************
2570 * GetTickCount (KERNEL32.@)
2572 * Returns the number of milliseconds, modulo 2^32, since the start
2573 * of the wineserver.
2575 DWORD WINAPI GetTickCount(void)
2578 gettimeofday( &t, NULL );
2579 return ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - server_startticks;
2583 /***********************************************************************
2584 * GetCurrentProcess (KERNEL32.@)
2586 #undef GetCurrentProcess
2587 HANDLE WINAPI GetCurrentProcess(void)
2589 return (HANDLE)0xffffffff;