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 != 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, TRUE );
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, TRUE );
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 /* build command line */
883 set_library_wargv( __wine_main_argv );
884 if (!build_command_line( __wine_main_wargv )) goto error;
886 stack_size = RtlImageNtHeader(peb->ImageBaseAddress)->OptionalHeader.SizeOfStackReserve;
888 /* allocate main thread stack */
889 if (!THREAD_InitStack( NtCurrentTeb(), stack_size )) goto error;
891 /* switch to the new stack */
892 wine_switch_to_stack( start_process, NULL, NtCurrentTeb()->Tib.StackBase );
895 ExitProcess( GetLastError() );
899 /***********************************************************************
902 * Build an argv array from a command-line.
903 * 'reserved' is the number of args to reserve before the first one.
905 static char **build_argv( const WCHAR *cmdlineW, int reserved )
909 char *arg,*s,*d,*cmdline;
910 int in_quotes,bcount,len;
912 len = WideCharToMultiByte( CP_UNIXCP, 0, cmdlineW, -1, NULL, 0, NULL, NULL );
913 if (!(cmdline = malloc(len))) return NULL;
914 WideCharToMultiByte( CP_UNIXCP, 0, cmdlineW, -1, cmdline, len, NULL, NULL );
921 if (*s=='\0' || ((*s==' ' || *s=='\t') && !in_quotes)) {
924 /* skip the remaining spaces */
925 while (*s==' ' || *s=='\t') {
932 } else if (*s=='\\') {
933 /* '\', count them */
935 } else if ((*s=='"') && ((bcount & 1)==0)) {
937 in_quotes=!in_quotes;
940 /* a regular character */
945 argv=malloc(argc*sizeof(*argv));
954 if ((*s==' ' || *s=='\t') && !in_quotes) {
955 /* Close the argument and copy it */
959 /* skip the remaining spaces */
962 } while (*s==' ' || *s=='\t');
964 /* Start with a new argument */
967 } else if (*s=='\\') {
971 } else if (*s=='"') {
973 if ((bcount & 1)==0) {
974 /* Preceeded by an even number of '\', this is half that
975 * number of '\', plus a '"' which we discard.
979 in_quotes=!in_quotes;
981 /* Preceeded by an odd number of '\', this is half that
982 * number of '\' followed by a '"'
990 /* a regular character */
1005 /***********************************************************************
1008 * Allocate an environment string; helper for build_envp
1010 static char *alloc_env_string( const char *name, const char *value )
1012 char *ret = malloc( strlen(name) + strlen(value) + 1 );
1013 strcpy( ret, name );
1014 strcat( ret, value );
1018 /***********************************************************************
1021 * Build the environment of a new child process.
1023 static char **build_envp( const WCHAR *envW, const WCHAR *extra_envW )
1027 char *env, *extra_env = NULL;
1028 int count = 0, length;
1032 for (p = extra_envW; *p; count++) p += strlenW(p) + 1;
1034 length = WideCharToMultiByte( CP_UNIXCP, 0, extra_envW, p - extra_envW,
1035 NULL, 0, NULL, NULL );
1036 if ((extra_env = malloc( length )))
1037 WideCharToMultiByte( CP_UNIXCP, 0, extra_envW, p - extra_envW,
1038 extra_env, length, NULL, NULL );
1040 for (p = envW; *p; count++) p += strlenW(p) + 1;
1042 length = WideCharToMultiByte( CP_UNIXCP, 0, envW, p - envW, NULL, 0, NULL, NULL );
1043 if (!(env = malloc( length ))) return NULL;
1044 WideCharToMultiByte( CP_UNIXCP, 0, envW, p - envW, env, length, NULL, NULL );
1048 if ((envp = malloc( count * sizeof(*envp) )))
1050 char **envptr = envp;
1053 /* first the extra strings */
1054 if (extra_env) for (p = extra_env; *p; p += strlen(p) + 1) *envptr++ = p;
1055 /* then put PATH, HOME and WINEPREFIX from the unix env */
1056 if ((p = getenv("PATH"))) *envptr++ = alloc_env_string( "PATH=", p );
1057 if ((p = getenv("HOME"))) *envptr++ = alloc_env_string( "HOME=", p );
1058 if ((p = getenv("WINEPREFIX"))) *envptr++ = alloc_env_string( "WINEPREFIX=", p );
1059 /* now put the Windows environment strings */
1060 for (p = env; *p; p += strlen(p) + 1)
1062 if (!memcmp( p, "PATH=", 5 )) /* store PATH as WINEPATH */
1063 *envptr++ = alloc_env_string( "WINEPATH=", p + 5 );
1064 else if (memcmp( p, "HOME=", 5 ) &&
1065 memcmp( p, "WINEPATH=", 9 ) &&
1066 memcmp( p, "WINEPREFIX=", 11 )) *envptr++ = p;
1074 /***********************************************************************
1077 * Fork and exec a new Unix binary, checking for errors.
1079 static int fork_and_exec( const char *filename, const WCHAR *cmdline,
1080 const WCHAR *env, const char *newdir )
1085 if (!env) env = GetEnvironmentStringsW();
1092 fcntl( fd[1], F_SETFD, 1 ); /* set close on exec */
1093 if (!(pid = fork())) /* child */
1095 char **argv = build_argv( cmdline, 0 );
1096 char **envp = build_envp( env, NULL );
1099 /* Reset signals that we previously set to SIG_IGN */
1100 signal( SIGPIPE, SIG_DFL );
1101 signal( SIGCHLD, SIG_DFL );
1103 if (newdir) chdir(newdir);
1105 if (argv && envp) execve( filename, argv, envp );
1107 write( fd[1], &err, sizeof(err) );
1111 if ((pid != -1) && (read( fd[0], &err, sizeof(err) ) > 0)) /* exec failed */
1116 if (pid == -1) FILE_SetDosError();
1122 /***********************************************************************
1123 * create_user_params
1125 static RTL_USER_PROCESS_PARAMETERS *create_user_params( LPCWSTR filename, LPCWSTR cmdline,
1126 const STARTUPINFOW *startup )
1128 RTL_USER_PROCESS_PARAMETERS *params;
1129 UNICODE_STRING image_str, cmdline_str, desktop, title;
1131 WCHAR buffer[MAX_PATH];
1133 if (GetLongPathNameW( filename, buffer, MAX_PATH ))
1134 RtlInitUnicodeString( &image_str, buffer );
1136 RtlInitUnicodeString( &image_str, filename );
1138 RtlInitUnicodeString( &cmdline_str, cmdline );
1139 if (startup->lpDesktop) RtlInitUnicodeString( &desktop, startup->lpDesktop );
1140 if (startup->lpTitle) RtlInitUnicodeString( &title, startup->lpTitle );
1142 status = RtlCreateProcessParameters( ¶ms, &image_str, NULL, NULL, &cmdline_str, NULL,
1143 startup->lpTitle ? &title : NULL,
1144 startup->lpDesktop ? &desktop : NULL,
1146 if (status != STATUS_SUCCESS)
1148 SetLastError( RtlNtStatusToDosError(status) );
1152 params->Environment = NULL; /* we pass it through the Unix environment */
1153 params->hStdInput = startup->hStdInput;
1154 params->hStdOutput = startup->hStdOutput;
1155 params->hStdError = startup->hStdError;
1156 params->dwX = startup->dwX;
1157 params->dwY = startup->dwY;
1158 params->dwXSize = startup->dwXSize;
1159 params->dwYSize = startup->dwYSize;
1160 params->dwXCountChars = startup->dwXCountChars;
1161 params->dwYCountChars = startup->dwYCountChars;
1162 params->dwFillAttribute = startup->dwFillAttribute;
1163 params->dwFlags = startup->dwFlags;
1164 params->wShowWindow = startup->wShowWindow;
1169 /***********************************************************************
1172 * Create a new process. If hFile is a valid handle we have an exe
1173 * file, otherwise it is a Winelib app.
1175 static BOOL create_process( HANDLE hFile, LPCWSTR filename, LPWSTR cmd_line, LPWSTR env,
1176 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1177 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1178 LPPROCESS_INFORMATION info, LPCSTR unixdir )
1180 BOOL ret, success = FALSE;
1181 HANDLE process_info;
1182 RTL_USER_PROCESS_PARAMETERS *params;
1183 WCHAR *extra_env = NULL;
1192 env = GetEnvironmentStringsW();
1193 extra_env = DRIVE_BuildEnv();
1196 if (!(params = create_user_params( filename, cmd_line, startup )))
1198 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1202 /* create the synchronization pipes */
1204 if (pipe( startfd ) == -1)
1207 RtlDestroyProcessParameters( params );
1208 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1211 if (pipe( execfd ) == -1)
1214 close( startfd[0] );
1215 close( startfd[1] );
1216 RtlDestroyProcessParameters( params );
1217 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1220 fcntl( execfd[1], F_SETFD, 1 ); /* set close on exec */
1222 /* create the child process */
1224 if (!(pid = fork())) /* child */
1226 char **argv = build_argv( cmd_line, 1 );
1227 char **envp = build_envp( env, extra_env );
1229 close( startfd[1] );
1232 /* wait for parent to tell us to start */
1233 if (read( startfd[0], &dummy, 1 ) != 1) _exit(1);
1235 close( startfd[0] );
1236 /* Reset signals that we previously set to SIG_IGN */
1237 signal( SIGPIPE, SIG_DFL );
1238 signal( SIGCHLD, SIG_DFL );
1240 if (unixdir) chdir(unixdir);
1244 /* first, try for a WINELOADER environment variable */
1245 argv[0] = getenv("WINELOADER");
1246 if (argv[0]) execve( argv[0], argv, envp );
1247 /* now use the standard search strategy */
1248 wine_exec_wine_binary( NULL, argv, envp );
1251 write( execfd[1], &err, sizeof(err) );
1255 /* this is the parent */
1257 close( startfd[0] );
1259 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1262 close( startfd[1] );
1265 RtlDestroyProcessParameters( params );
1269 /* create the process on the server side */
1271 SERVER_START_REQ( new_process )
1273 req->inherit_all = inherit;
1274 req->create_flags = flags;
1275 req->unix_pid = pid;
1276 req->exe_file = hFile;
1277 if (startup->dwFlags & STARTF_USESTDHANDLES)
1279 req->hstdin = startup->hStdInput;
1280 req->hstdout = startup->hStdOutput;
1281 req->hstderr = startup->hStdError;
1285 req->hstdin = GetStdHandle( STD_INPUT_HANDLE );
1286 req->hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
1287 req->hstderr = GetStdHandle( STD_ERROR_HANDLE );
1290 if ((flags & (CREATE_NEW_CONSOLE | DETACHED_PROCESS)) != 0)
1292 /* this is temporary (for console handles). We have no way to control that the handle is invalid in child process otherwise */
1293 if (is_console_handle(req->hstdin)) req->hstdin = INVALID_HANDLE_VALUE;
1294 if (is_console_handle(req->hstdout)) req->hstdout = INVALID_HANDLE_VALUE;
1295 if (is_console_handle(req->hstderr)) req->hstderr = INVALID_HANDLE_VALUE;
1299 if (is_console_handle(req->hstdin)) req->hstdin = console_handle_unmap(req->hstdin);
1300 if (is_console_handle(req->hstdout)) req->hstdout = console_handle_unmap(req->hstdout);
1301 if (is_console_handle(req->hstderr)) req->hstderr = console_handle_unmap(req->hstderr);
1304 wine_server_add_data( req, params, params->Size );
1305 ret = !wine_server_call_err( req );
1306 process_info = reply->info;
1310 RtlDestroyProcessParameters( params );
1313 close( startfd[1] );
1318 /* tell child to start and wait for it to exec */
1320 write( startfd[1], &dummy, 1 );
1321 close( startfd[1] );
1323 if (read( execfd[0], &err, sizeof(err) ) > 0) /* exec failed */
1328 CloseHandle( process_info );
1332 /* wait for the new process info to be ready */
1334 WaitForSingleObject( process_info, INFINITE );
1335 SERVER_START_REQ( get_new_process_info )
1337 req->info = process_info;
1338 req->pinherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
1339 req->tinherit = (tsa && (tsa->nLength >= sizeof(*tsa)) && tsa->bInheritHandle);
1340 if ((ret = !wine_server_call_err( req )))
1342 info->dwProcessId = (DWORD)reply->pid;
1343 info->dwThreadId = (DWORD)reply->tid;
1344 info->hProcess = reply->phandle;
1345 info->hThread = reply->thandle;
1346 success = reply->success;
1351 if (ret && !success) /* new process failed to start */
1354 if (GetExitCodeProcess( info->hProcess, &exitcode )) SetLastError( exitcode );
1355 CloseHandle( info->hThread );
1356 CloseHandle( info->hProcess );
1359 CloseHandle( process_info );
1364 /***********************************************************************
1365 * create_vdm_process
1367 * Create a new VDM process for a 16-bit or DOS application.
1369 static BOOL create_vdm_process( LPCWSTR filename, LPWSTR cmd_line, LPWSTR env,
1370 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1371 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1372 LPPROCESS_INFORMATION info, LPCSTR unixdir )
1374 static const WCHAR argsW[] = {'%','s',' ','-','-','a','p','p','-','n','a','m','e',' ','"','%','s','"',' ','%','s',0};
1377 LPWSTR new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
1378 (strlenW(filename) + strlenW(cmd_line) + 30) * sizeof(WCHAR) );
1382 SetLastError( ERROR_OUTOFMEMORY );
1385 sprintfW( new_cmd_line, argsW, winevdmW, filename, cmd_line );
1386 ret = create_process( 0, winevdmW, new_cmd_line, env, psa, tsa, inherit,
1387 flags, startup, info, unixdir );
1388 HeapFree( GetProcessHeap(), 0, new_cmd_line );
1393 /***********************************************************************
1394 * create_cmd_process
1396 * Create a new cmd shell process for a .BAT file.
1398 static BOOL create_cmd_process( LPCWSTR filename, LPWSTR cmd_line, LPVOID env,
1399 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1400 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1401 LPPROCESS_INFORMATION info, LPCWSTR cur_dir )
1404 static const WCHAR comspecW[] = {'C','O','M','S','P','E','C',0};
1405 static const WCHAR slashcW[] = {' ','/','c',' ',0};
1406 WCHAR comspec[MAX_PATH];
1410 if (!GetEnvironmentVariableW( comspecW, comspec, sizeof(comspec)/sizeof(WCHAR) ))
1412 if (!(newcmdline = HeapAlloc( GetProcessHeap(), 0,
1413 (strlenW(comspec) + 4 + strlenW(cmd_line) + 1) * sizeof(WCHAR))))
1416 strcpyW( newcmdline, comspec );
1417 strcatW( newcmdline, slashcW );
1418 strcatW( newcmdline, cmd_line );
1419 ret = CreateProcessW( comspec, newcmdline, psa, tsa, inherit,
1420 flags, env, cur_dir, startup, info );
1421 HeapFree( GetProcessHeap(), 0, newcmdline );
1426 /*************************************************************************
1429 * Helper for CreateProcess: retrieve the file name to load from the
1430 * app name and command line. Store the file name in buffer, and
1431 * return a possibly modified command line.
1432 * Also returns a handle to the opened file if it's a Windows binary.
1434 static LPWSTR get_file_name( LPCWSTR appname, LPWSTR cmdline, LPWSTR buffer,
1435 int buflen, HANDLE *handle )
1437 static const WCHAR quotesW[] = {'"','%','s','"',0};
1439 WCHAR *name, *pos, *ret = NULL;
1442 /* if we have an app name, everything is easy */
1446 /* use the unmodified app name as file name */
1447 lstrcpynW( buffer, appname, buflen );
1448 *handle = open_exe_file( buffer );
1449 if (!(ret = cmdline) || !cmdline[0])
1451 /* no command-line, create one */
1452 if ((ret = HeapAlloc( GetProcessHeap(), 0, (strlenW(appname) + 3) * sizeof(WCHAR) )))
1453 sprintfW( ret, quotesW, appname );
1460 SetLastError( ERROR_INVALID_PARAMETER );
1464 /* first check for a quoted file name */
1466 if ((cmdline[0] == '"') && ((p = strchrW( cmdline + 1, '"' ))))
1468 int len = p - cmdline - 1;
1469 /* extract the quoted portion as file name */
1470 if (!(name = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return NULL;
1471 memcpy( name, cmdline + 1, len * sizeof(WCHAR) );
1474 if (find_exe_file( name, buffer, buflen, handle ))
1475 ret = cmdline; /* no change necessary */
1479 /* now try the command-line word by word */
1481 if (!(name = HeapAlloc( GetProcessHeap(), 0, (strlenW(cmdline) + 1) * sizeof(WCHAR) )))
1488 do *pos++ = *p++; while (*p && *p != ' ');
1490 if (find_exe_file( name, buffer, buflen, handle ))
1497 if (!ret || !strchrW( name, ' ' )) goto done; /* no change necessary */
1499 /* now build a new command-line with quotes */
1501 if (!(ret = HeapAlloc( GetProcessHeap(), 0, (strlenW(cmdline) + 3) * sizeof(WCHAR) )))
1503 sprintfW( ret, quotesW, name );
1507 HeapFree( GetProcessHeap(), 0, name );
1512 /**********************************************************************
1513 * CreateProcessA (KERNEL32.@)
1515 BOOL WINAPI CreateProcessA( LPCSTR app_name, LPSTR cmd_line, LPSECURITY_ATTRIBUTES process_attr,
1516 LPSECURITY_ATTRIBUTES thread_attr, BOOL inherit,
1517 DWORD flags, LPVOID env, LPCSTR cur_dir,
1518 LPSTARTUPINFOA startup_info, LPPROCESS_INFORMATION info )
1521 UNICODE_STRING app_nameW, cmd_lineW, cur_dirW, desktopW, titleW;
1524 if (app_name) RtlCreateUnicodeStringFromAsciiz( &app_nameW, app_name );
1525 else app_nameW.Buffer = NULL;
1526 if (cmd_line) RtlCreateUnicodeStringFromAsciiz( &cmd_lineW, cmd_line );
1527 else cmd_lineW.Buffer = NULL;
1528 if (cur_dir) RtlCreateUnicodeStringFromAsciiz( &cur_dirW, cur_dir );
1529 else cur_dirW.Buffer = NULL;
1530 if (startup_info->lpDesktop) RtlCreateUnicodeStringFromAsciiz( &desktopW, startup_info->lpDesktop );
1531 else desktopW.Buffer = NULL;
1532 if (startup_info->lpTitle) RtlCreateUnicodeStringFromAsciiz( &titleW, startup_info->lpTitle );
1533 else titleW.Buffer = NULL;
1535 memcpy( &infoW, startup_info, sizeof(infoW) );
1536 infoW.lpDesktop = desktopW.Buffer;
1537 infoW.lpTitle = titleW.Buffer;
1539 if (startup_info->lpReserved)
1540 FIXME("StartupInfo.lpReserved is used, please report (%s)\n",
1541 debugstr_a(startup_info->lpReserved));
1543 ret = CreateProcessW( app_nameW.Buffer, cmd_lineW.Buffer, process_attr, thread_attr,
1544 inherit, flags, env, cur_dirW.Buffer, &infoW, info );
1546 RtlFreeUnicodeString( &app_nameW );
1547 RtlFreeUnicodeString( &cmd_lineW );
1548 RtlFreeUnicodeString( &cur_dirW );
1549 RtlFreeUnicodeString( &desktopW );
1550 RtlFreeUnicodeString( &titleW );
1555 /**********************************************************************
1556 * CreateProcessW (KERNEL32.@)
1558 BOOL WINAPI CreateProcessW( LPCWSTR app_name, LPWSTR cmd_line, LPSECURITY_ATTRIBUTES process_attr,
1559 LPSECURITY_ATTRIBUTES thread_attr, BOOL inherit, DWORD flags,
1560 LPVOID env, LPCWSTR cur_dir, LPSTARTUPINFOW startup_info,
1561 LPPROCESS_INFORMATION info )
1565 const char *unixdir = NULL;
1566 DOS_FULL_NAME full_dir;
1567 WCHAR name[MAX_PATH];
1568 WCHAR *tidy_cmdline, *p, *envW = env;
1570 /* Process the AppName and/or CmdLine to get module name and path */
1572 TRACE("app %s cmdline %s\n", debugstr_w(app_name), debugstr_w(cmd_line) );
1574 if (!(tidy_cmdline = get_file_name( app_name, cmd_line, name, sizeof(name), &hFile )))
1576 if (hFile == INVALID_HANDLE_VALUE) goto done;
1578 /* Warn if unsupported features are used */
1580 if (flags & (IDLE_PRIORITY_CLASS | HIGH_PRIORITY_CLASS | REALTIME_PRIORITY_CLASS |
1581 CREATE_NEW_PROCESS_GROUP | CREATE_SEPARATE_WOW_VDM | CREATE_SHARED_WOW_VDM |
1582 CREATE_DEFAULT_ERROR_MODE | CREATE_NO_WINDOW |
1583 PROFILE_USER | PROFILE_KERNEL | PROFILE_SERVER))
1584 WARN("(%s,...): ignoring some flags in %lx\n", debugstr_w(name), flags);
1588 if (DOSFS_GetFullName( cur_dir, TRUE, &full_dir )) unixdir = full_dir.long_name;
1592 WCHAR buf[MAX_PATH];
1593 if (GetCurrentDirectoryW(MAX_PATH, buf))
1595 if (DOSFS_GetFullName( buf, TRUE, &full_dir )) unixdir = full_dir.long_name;
1599 if (env && !(flags & CREATE_UNICODE_ENVIRONMENT)) /* convert environment to unicode */
1604 while (*p) p += strlen(p) + 1;
1605 p++; /* final null */
1606 lenW = MultiByteToWideChar( CP_ACP, 0, env, p - (char*)env, NULL, 0 );
1607 envW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) );
1608 MultiByteToWideChar( CP_ACP, 0, env, p - (char*)env, envW, lenW );
1609 flags |= CREATE_UNICODE_ENVIRONMENT;
1612 info->hThread = info->hProcess = 0;
1613 info->dwProcessId = info->dwThreadId = 0;
1615 /* Determine executable type */
1617 if (!hFile) /* builtin exe */
1619 TRACE( "starting %s as Winelib app\n", debugstr_w(name) );
1620 retv = create_process( 0, name, tidy_cmdline, envW, process_attr, thread_attr,
1621 inherit, flags, startup_info, info, unixdir );
1625 switch( MODULE_GetBinaryType( hFile ))
1628 TRACE( "starting %s as Win32 binary\n", debugstr_w(name) );
1629 retv = create_process( hFile, name, tidy_cmdline, envW, process_attr, thread_attr,
1630 inherit, flags, startup_info, info, unixdir );
1634 TRACE( "starting %s as Win16/DOS binary\n", debugstr_w(name) );
1635 retv = create_vdm_process( name, tidy_cmdline, envW, process_attr, thread_attr,
1636 inherit, flags, startup_info, info, unixdir );
1639 FIXME( "%s is OS/2 binary, not supported\n", debugstr_w(name) );
1640 SetLastError( ERROR_BAD_EXE_FORMAT );
1643 TRACE( "not starting %s since it is a dll\n", debugstr_w(name) );
1644 SetLastError( ERROR_BAD_EXE_FORMAT );
1646 case BINARY_UNIX_LIB:
1647 TRACE( "%s is a Unix library, starting as Winelib app\n", debugstr_w(name) );
1648 retv = create_process( hFile, name, tidy_cmdline, envW, process_attr, thread_attr,
1649 inherit, flags, startup_info, info, unixdir );
1651 case BINARY_UNKNOWN:
1652 /* check for .com or .bat extension */
1653 if ((p = strrchrW( name, '.' )))
1655 if (!strcmpiW( p, comW ))
1657 TRACE( "starting %s as DOS binary\n", debugstr_w(name) );
1658 retv = create_vdm_process( name, tidy_cmdline, envW, process_attr, thread_attr,
1659 inherit, flags, startup_info, info, unixdir );
1662 if (!strcmpiW( p, batW ))
1664 TRACE( "starting %s as batch binary\n", debugstr_w(name) );
1665 retv = create_cmd_process( name, tidy_cmdline, envW, process_attr, thread_attr,
1666 inherit, flags, startup_info, info, cur_dir );
1671 case BINARY_UNIX_EXE:
1673 /* unknown file, try as unix executable */
1674 DOS_FULL_NAME full_name;
1676 TRACE( "starting %s as Unix binary\n", debugstr_w(name) );
1678 if (DOSFS_GetFullName( name, TRUE, &full_name ))
1679 retv = (fork_and_exec( full_name.long_name, tidy_cmdline, envW, unixdir ) != -1);
1683 CloseHandle( hFile );
1686 if (tidy_cmdline != cmd_line) HeapFree( GetProcessHeap(), 0, tidy_cmdline );
1687 if (envW != env) HeapFree( GetProcessHeap(), 0, envW );
1692 /***********************************************************************
1695 * Wrapper to call WaitForInputIdle USER function
1697 typedef DWORD (WINAPI *WaitForInputIdle_ptr)( HANDLE hProcess, DWORD dwTimeOut );
1699 static DWORD wait_input_idle( HANDLE process, DWORD timeout )
1701 HMODULE mod = GetModuleHandleA( "user32.dll" );
1704 WaitForInputIdle_ptr ptr = (WaitForInputIdle_ptr)GetProcAddress( mod, "WaitForInputIdle" );
1705 if (ptr) return ptr( process, timeout );
1711 /***********************************************************************
1712 * WinExec (KERNEL32.@)
1714 UINT WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow )
1716 PROCESS_INFORMATION info;
1717 STARTUPINFOA startup;
1721 memset( &startup, 0, sizeof(startup) );
1722 startup.cb = sizeof(startup);
1723 startup.dwFlags = STARTF_USESHOWWINDOW;
1724 startup.wShowWindow = nCmdShow;
1726 /* cmdline needs to be writeable for CreateProcess */
1727 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine)+1 ))) return 0;
1728 strcpy( cmdline, lpCmdLine );
1730 if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE,
1731 0, NULL, NULL, &startup, &info ))
1733 /* Give 30 seconds to the app to come up */
1734 if (wait_input_idle( info.hProcess, 30000 ) == WAIT_FAILED)
1735 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1737 /* Close off the handles */
1738 CloseHandle( info.hThread );
1739 CloseHandle( info.hProcess );
1741 else if ((ret = GetLastError()) >= 32)
1743 FIXME("Strange error set by CreateProcess: %d\n", ret );
1746 HeapFree( GetProcessHeap(), 0, cmdline );
1751 /**********************************************************************
1752 * LoadModule (KERNEL32.@)
1754 HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
1756 LOADPARMS32 *params = paramBlock;
1757 PROCESS_INFORMATION info;
1758 STARTUPINFOA startup;
1759 HINSTANCE hInstance;
1761 char filename[MAX_PATH];
1764 if (!name) return (HINSTANCE)ERROR_FILE_NOT_FOUND;
1766 if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) &&
1767 !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL ))
1768 return (HINSTANCE)GetLastError();
1770 len = (BYTE)params->lpCmdLine[0];
1771 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 )))
1772 return (HINSTANCE)ERROR_NOT_ENOUGH_MEMORY;
1774 strcpy( cmdline, filename );
1775 p = cmdline + strlen(cmdline);
1777 memcpy( p, params->lpCmdLine + 1, len );
1780 memset( &startup, 0, sizeof(startup) );
1781 startup.cb = sizeof(startup);
1782 if (params->lpCmdShow)
1784 startup.dwFlags = STARTF_USESHOWWINDOW;
1785 startup.wShowWindow = ((WORD *)params->lpCmdShow)[1];
1788 if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0,
1789 params->lpEnvAddress, NULL, &startup, &info ))
1791 /* Give 30 seconds to the app to come up */
1792 if (wait_input_idle( info.hProcess, 30000 ) == WAIT_FAILED)
1793 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1794 hInstance = (HINSTANCE)33;
1795 /* Close off the handles */
1796 CloseHandle( info.hThread );
1797 CloseHandle( info.hProcess );
1799 else if ((hInstance = (HINSTANCE)GetLastError()) >= (HINSTANCE)32)
1801 FIXME("Strange error set by CreateProcess: %p\n", hInstance );
1802 hInstance = (HINSTANCE)11;
1805 HeapFree( GetProcessHeap(), 0, cmdline );
1810 /******************************************************************************
1811 * TerminateProcess (KERNEL32.@)
1813 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
1815 NTSTATUS status = NtTerminateProcess( handle, exit_code );
1816 if (status) SetLastError( RtlNtStatusToDosError(status) );
1821 /***********************************************************************
1822 * ExitProcess (KERNEL32.@)
1824 void WINAPI ExitProcess( DWORD status )
1826 LdrShutdownProcess();
1827 SERVER_START_REQ( terminate_process )
1829 /* send the exit code to the server */
1830 req->handle = GetCurrentProcess();
1831 req->exit_code = status;
1832 wine_server_call( req );
1839 /***********************************************************************
1840 * GetExitCodeProcess [KERNEL32.@]
1842 * Gets termination status of specified process
1848 BOOL WINAPI GetExitCodeProcess(
1849 HANDLE hProcess, /* [in] handle to the process */
1850 LPDWORD lpExitCode) /* [out] address to receive termination status */
1853 SERVER_START_REQ( get_process_info )
1855 req->handle = hProcess;
1856 ret = !wine_server_call_err( req );
1857 if (ret && lpExitCode) *lpExitCode = reply->exit_code;
1864 /***********************************************************************
1865 * SetErrorMode (KERNEL32.@)
1867 UINT WINAPI SetErrorMode( UINT mode )
1869 UINT old = process_error_mode;
1870 process_error_mode = mode;
1875 /**********************************************************************
1876 * TlsAlloc [KERNEL32.@] Allocates a TLS index.
1878 * Allocates a thread local storage index
1881 * Success: TLS Index
1882 * Failure: 0xFFFFFFFF
1884 DWORD WINAPI TlsAlloc( void )
1888 RtlAcquirePebLock();
1889 index = RtlFindClearBitsAndSet( NtCurrentTeb()->Peb->TlsBitmap, 1, 0 );
1890 if (index != ~0UL) NtCurrentTeb()->TlsSlots[index] = 0; /* clear the value */
1891 else SetLastError( ERROR_NO_MORE_ITEMS );
1892 RtlReleasePebLock();
1897 /**********************************************************************
1898 * TlsFree [KERNEL32.@] Releases a TLS index.
1900 * Releases a thread local storage index, making it available for reuse
1906 BOOL WINAPI TlsFree(
1907 DWORD index) /* [in] TLS Index to free */
1911 RtlAcquirePebLock();
1912 ret = RtlAreBitsSet( NtCurrentTeb()->Peb->TlsBitmap, index, 1 );
1915 RtlClearBits( NtCurrentTeb()->Peb->TlsBitmap, index, 1 );
1916 NtSetInformationThread( GetCurrentThread(), ThreadZeroTlsCell, &index, sizeof(index) );
1918 else SetLastError( ERROR_INVALID_PARAMETER );
1919 RtlReleasePebLock();
1924 /**********************************************************************
1925 * TlsGetValue [KERNEL32.@] Gets value in a thread's TLS slot
1928 * Success: Value stored in calling thread's TLS slot for index
1929 * Failure: 0 and GetLastError returns NO_ERROR
1931 LPVOID WINAPI TlsGetValue(
1932 DWORD index) /* [in] TLS index to retrieve value for */
1934 if (index >= NtCurrentTeb()->Peb->TlsBitmap->SizeOfBitMap)
1936 SetLastError( ERROR_INVALID_PARAMETER );
1939 SetLastError( ERROR_SUCCESS );
1940 return NtCurrentTeb()->TlsSlots[index];
1944 /**********************************************************************
1945 * TlsSetValue [KERNEL32.@] Stores a value in the thread's TLS slot.
1951 BOOL WINAPI TlsSetValue(
1952 DWORD index, /* [in] TLS index to set value for */
1953 LPVOID value) /* [in] Value to be stored */
1955 if (index >= NtCurrentTeb()->Peb->TlsBitmap->SizeOfBitMap)
1957 SetLastError( ERROR_INVALID_PARAMETER );
1960 NtCurrentTeb()->TlsSlots[index] = value;
1965 /***********************************************************************
1966 * GetProcessFlags (KERNEL32.@)
1968 DWORD WINAPI GetProcessFlags( DWORD processid )
1970 IMAGE_NT_HEADERS *nt;
1973 if (processid && processid != GetCurrentProcessId()) return 0;
1975 if ((nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress )))
1977 if (nt->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
1978 flags |= PDB32_CONSOLE_PROC;
1980 if (!AreFileApisANSI()) flags |= PDB32_FILE_APIS_OEM;
1981 if (IsDebuggerPresent()) flags |= PDB32_DEBUGGED;
1986 /***********************************************************************
1987 * GetProcessDword (KERNEL.485)
1988 * GetProcessDword (KERNEL32.18)
1989 * 'Of course you cannot directly access Windows internal structures'
1991 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
1996 TRACE("(%ld, %d)\n", dwProcessID, offset );
1998 if (dwProcessID && dwProcessID != GetCurrentProcessId())
2000 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
2006 case GPD_APP_COMPAT_FLAGS:
2007 return GetAppCompatFlags16(0);
2008 case GPD_LOAD_DONE_EVENT:
2010 case GPD_HINSTANCE16:
2011 return GetTaskDS16();
2012 case GPD_WINDOWS_VERSION:
2013 return GetExeVersion16();
2015 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
2017 return (DWORD)NtCurrentTeb()->Peb;
2018 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
2019 GetStartupInfoW(&siw);
2020 return (DWORD)siw.hStdOutput;
2021 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
2022 GetStartupInfoW(&siw);
2023 return (DWORD)siw.hStdInput;
2024 case GPD_STARTF_SHOWWINDOW:
2025 GetStartupInfoW(&siw);
2026 return siw.wShowWindow;
2027 case GPD_STARTF_SIZE:
2028 GetStartupInfoW(&siw);
2030 if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
2032 if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
2033 return MAKELONG( x, y );
2034 case GPD_STARTF_POSITION:
2035 GetStartupInfoW(&siw);
2037 if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
2039 if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
2040 return MAKELONG( x, y );
2041 case GPD_STARTF_FLAGS:
2042 GetStartupInfoW(&siw);
2047 return GetProcessFlags(0);
2049 return process_dword;
2051 ERR("Unknown offset %d\n", offset );
2056 /***********************************************************************
2057 * SetProcessDword (KERNEL.484)
2058 * 'Of course you cannot directly access Windows internal structures'
2060 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
2062 TRACE("(%ld, %d)\n", dwProcessID, offset );
2064 if (dwProcessID && dwProcessID != GetCurrentProcessId())
2066 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
2072 case GPD_APP_COMPAT_FLAGS:
2073 case GPD_LOAD_DONE_EVENT:
2074 case GPD_HINSTANCE16:
2075 case GPD_WINDOWS_VERSION:
2078 case GPD_STARTF_SHELLDATA:
2079 case GPD_STARTF_HOTKEY:
2080 case GPD_STARTF_SHOWWINDOW:
2081 case GPD_STARTF_SIZE:
2082 case GPD_STARTF_POSITION:
2083 case GPD_STARTF_FLAGS:
2086 ERR("Not allowed to modify offset %d\n", offset );
2089 process_dword = value;
2092 ERR("Unknown offset %d\n", offset );
2098 /***********************************************************************
2099 * ExitProcess (KERNEL.466)
2101 void WINAPI ExitProcess16( WORD status )
2104 ReleaseThunkLock( &count );
2105 ExitProcess( status );
2109 /*********************************************************************
2110 * OpenProcess (KERNEL32.@)
2112 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
2115 SERVER_START_REQ( open_process )
2118 req->access = access;
2119 req->inherit = inherit;
2120 if (!wine_server_call_err( req )) ret = reply->handle;
2127 /*********************************************************************
2128 * MapProcessHandle (KERNEL.483)
2130 DWORD WINAPI MapProcessHandle( HANDLE handle )
2133 SERVER_START_REQ( get_process_info )
2135 req->handle = handle;
2136 if (!wine_server_call_err( req )) ret = reply->pid;
2143 /*********************************************************************
2144 * CloseW32Handle (KERNEL.474)
2145 * CloseHandle (KERNEL32.@)
2147 BOOL WINAPI CloseHandle( HANDLE handle )
2151 /* stdio handles need special treatment */
2152 if ((handle == (HANDLE)STD_INPUT_HANDLE) ||
2153 (handle == (HANDLE)STD_OUTPUT_HANDLE) ||
2154 (handle == (HANDLE)STD_ERROR_HANDLE))
2155 handle = GetStdHandle( (DWORD)handle );
2157 if (is_console_handle(handle))
2158 return CloseConsoleHandle(handle);
2160 status = NtClose( handle );
2161 if (status) SetLastError( RtlNtStatusToDosError(status) );
2166 /*********************************************************************
2167 * GetHandleInformation (KERNEL32.@)
2169 BOOL WINAPI GetHandleInformation( HANDLE handle, LPDWORD flags )
2172 SERVER_START_REQ( set_handle_info )
2174 req->handle = handle;
2178 ret = !wine_server_call_err( req );
2179 if (ret && flags) *flags = reply->old_flags;
2186 /*********************************************************************
2187 * SetHandleInformation (KERNEL32.@)
2189 BOOL WINAPI SetHandleInformation( HANDLE handle, DWORD mask, DWORD flags )
2192 SERVER_START_REQ( set_handle_info )
2194 req->handle = handle;
2198 ret = !wine_server_call_err( req );
2205 /*********************************************************************
2206 * DuplicateHandle (KERNEL32.@)
2208 BOOL WINAPI DuplicateHandle( HANDLE source_process, HANDLE source,
2209 HANDLE dest_process, HANDLE *dest,
2210 DWORD access, BOOL inherit, DWORD options )
2214 if (is_console_handle(source))
2216 /* FIXME: this test is not sufficient, we need to test process ids, not handles */
2217 if (source_process != dest_process ||
2218 source_process != GetCurrentProcess())
2220 SetLastError(ERROR_INVALID_PARAMETER);
2223 *dest = DuplicateConsoleHandle( source, access, inherit, options );
2224 return (*dest != INVALID_HANDLE_VALUE);
2226 status = NtDuplicateObject( source_process, source, dest_process, dest,
2227 access, inherit ? OBJ_INHERIT : 0, options );
2228 if (status) SetLastError( RtlNtStatusToDosError(status) );
2233 /***********************************************************************
2234 * ConvertToGlobalHandle (KERNEL.476)
2235 * ConvertToGlobalHandle (KERNEL32.@)
2237 HANDLE WINAPI ConvertToGlobalHandle(HANDLE hSrc)
2239 HANDLE ret = INVALID_HANDLE_VALUE;
2240 DuplicateHandle( GetCurrentProcess(), hSrc, GetCurrentProcess(), &ret, 0, FALSE,
2241 DUP_HANDLE_MAKE_GLOBAL | DUP_HANDLE_SAME_ACCESS | DUP_HANDLE_CLOSE_SOURCE );
2246 /***********************************************************************
2247 * SetHandleContext (KERNEL32.@)
2249 BOOL WINAPI SetHandleContext(HANDLE hnd,DWORD context)
2251 FIXME("(%p,%ld), stub. In case this got called by WSOCK32/WS2_32: "
2252 "the external WINSOCK DLLs won't work with WINE, don't use them.\n",hnd,context);
2253 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2258 /***********************************************************************
2259 * GetHandleContext (KERNEL32.@)
2261 DWORD WINAPI GetHandleContext(HANDLE hnd)
2263 FIXME("(%p), stub. In case this got called by WSOCK32/WS2_32: "
2264 "the external WINSOCK DLLs won't work with WINE, don't use them.\n",hnd);
2265 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2270 /***********************************************************************
2271 * CreateSocketHandle (KERNEL32.@)
2273 HANDLE WINAPI CreateSocketHandle(void)
2275 FIXME("(), stub. In case this got called by WSOCK32/WS2_32: "
2276 "the external WINSOCK DLLs won't work with WINE, don't use them.\n");
2277 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2278 return INVALID_HANDLE_VALUE;
2282 /***********************************************************************
2283 * SetPriorityClass (KERNEL32.@)
2285 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
2288 SERVER_START_REQ( set_process_info )
2290 req->handle = hprocess;
2291 req->priority = priorityclass;
2292 req->mask = SET_PROCESS_INFO_PRIORITY;
2293 ret = !wine_server_call_err( req );
2300 /***********************************************************************
2301 * GetPriorityClass (KERNEL32.@)
2303 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
2306 SERVER_START_REQ( get_process_info )
2308 req->handle = hprocess;
2309 if (!wine_server_call_err( req )) ret = reply->priority;
2316 /***********************************************************************
2317 * SetProcessAffinityMask (KERNEL32.@)
2319 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
2322 SERVER_START_REQ( set_process_info )
2324 req->handle = hProcess;
2325 req->affinity = affmask;
2326 req->mask = SET_PROCESS_INFO_AFFINITY;
2327 ret = !wine_server_call_err( req );
2334 /**********************************************************************
2335 * GetProcessAffinityMask (KERNEL32.@)
2337 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
2338 LPDWORD lpProcessAffinityMask,
2339 LPDWORD lpSystemAffinityMask )
2342 SERVER_START_REQ( get_process_info )
2344 req->handle = hProcess;
2345 if (!wine_server_call_err( req ))
2347 if (lpProcessAffinityMask) *lpProcessAffinityMask = reply->process_affinity;
2348 if (lpSystemAffinityMask) *lpSystemAffinityMask = reply->system_affinity;
2357 /***********************************************************************
2358 * GetProcessVersion (KERNEL32.@)
2360 DWORD WINAPI GetProcessVersion( DWORD processid )
2362 IMAGE_NT_HEADERS *nt;
2364 if (processid && processid != GetCurrentProcessId())
2366 FIXME("should use ReadProcessMemory\n");
2369 if ((nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress )))
2370 return ((nt->OptionalHeader.MajorSubsystemVersion << 16) |
2371 nt->OptionalHeader.MinorSubsystemVersion);
2376 /***********************************************************************
2377 * SetProcessWorkingSetSize [KERNEL32.@]
2378 * Sets the min/max working set sizes for a specified process.
2381 * hProcess [I] Handle to the process of interest
2382 * minset [I] Specifies minimum working set size
2383 * maxset [I] Specifies maximum working set size
2387 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess, SIZE_T minset,
2390 FIXME("(%p,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
2391 if(( minset == (SIZE_T)-1) && (maxset == (SIZE_T)-1)) {
2392 /* Trim the working set to zero */
2393 /* Swap the process out of physical RAM */
2398 /***********************************************************************
2399 * GetProcessWorkingSetSize (KERNEL32.@)
2401 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess, PSIZE_T minset,
2404 FIXME("(%p,%p,%p): stub\n",hProcess,minset,maxset);
2405 /* 32 MB working set size */
2406 if (minset) *minset = 32*1024*1024;
2407 if (maxset) *maxset = 32*1024*1024;
2412 /***********************************************************************
2413 * SetProcessShutdownParameters (KERNEL32.@)
2415 BOOL WINAPI SetProcessShutdownParameters(DWORD level, DWORD flags)
2417 FIXME("(%08lx, %08lx): partial stub.\n", level, flags);
2418 shutdown_flags = flags;
2419 shutdown_priority = level;
2424 /***********************************************************************
2425 * GetProcessShutdownParameters (KERNEL32.@)
2428 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel, LPDWORD lpdwFlags )
2430 *lpdwLevel = shutdown_priority;
2431 *lpdwFlags = shutdown_flags;
2436 /***********************************************************************
2437 * GetProcessPriorityBoost (KERNEL32.@)
2439 BOOL WINAPI GetProcessPriorityBoost(HANDLE hprocess,PBOOL pDisablePriorityBoost)
2441 FIXME("(%p,%p): semi-stub\n", hprocess, pDisablePriorityBoost);
2443 /* Report that no boost is present.. */
2444 *pDisablePriorityBoost = FALSE;
2449 /***********************************************************************
2450 * SetProcessPriorityBoost (KERNEL32.@)
2452 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
2454 FIXME("(%p,%d): stub\n",hprocess,disableboost);
2455 /* Say we can do it. I doubt the program will notice that we don't. */
2460 /***********************************************************************
2461 * ReadProcessMemory (KERNEL32.@)
2463 BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, SIZE_T size,
2464 SIZE_T *bytes_read )
2466 NTSTATUS status = NtReadVirtualMemory( process, addr, buffer, size, bytes_read );
2467 if (status) SetLastError( RtlNtStatusToDosError(status) );
2472 /***********************************************************************
2473 * WriteProcessMemory (KERNEL32.@)
2475 BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPCVOID buffer, SIZE_T size,
2476 SIZE_T *bytes_written )
2478 NTSTATUS status = NtWriteVirtualMemory( process, addr, buffer, size, bytes_written );
2479 if (status) SetLastError( RtlNtStatusToDosError(status) );
2484 /****************************************************************************
2485 * FlushInstructionCache (KERNEL32.@)
2487 BOOL WINAPI FlushInstructionCache(HANDLE hProcess, LPCVOID lpBaseAddress, SIZE_T dwSize)
2489 if (GetVersion() & 0x80000000) return TRUE; /* not NT, always TRUE */
2490 FIXME("(%p,%p,0x%08lx): stub\n",hProcess, lpBaseAddress, dwSize);
2495 /******************************************************************
2496 * GetProcessIoCounters (KERNEL32.@)
2498 BOOL WINAPI GetProcessIoCounters(HANDLE hProcess, PIO_COUNTERS ioc)
2502 status = NtQueryInformationProcess(hProcess, ProcessIoCounters,
2503 ioc, sizeof(*ioc), NULL);
2504 if (status) SetLastError( RtlNtStatusToDosError(status) );
2508 /***********************************************************************
2509 * ProcessIdToSessionId (KERNEL32.@)
2510 * This function is available on Terminal Server 4SP4 and Windows 2000
2512 BOOL WINAPI ProcessIdToSessionId( DWORD procid, DWORD *sessionid_ptr )
2514 /* According to MSDN, if the calling process is not in a terminal
2515 * services environment, then the sessionid returned is zero.
2522 /***********************************************************************
2523 * RegisterServiceProcess (KERNEL.491)
2524 * RegisterServiceProcess (KERNEL32.@)
2526 * A service process calls this function to ensure that it continues to run
2527 * even after a user logged off.
2529 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
2531 /* I don't think that Wine needs to do anything in that function */
2532 return 1; /* success */
2536 /**************************************************************************
2537 * SetFileApisToOEM (KERNEL32.@)
2539 VOID WINAPI SetFileApisToOEM(void)
2541 oem_file_apis = TRUE;
2545 /**************************************************************************
2546 * SetFileApisToANSI (KERNEL32.@)
2548 VOID WINAPI SetFileApisToANSI(void)
2550 oem_file_apis = FALSE;
2554 /******************************************************************************
2555 * AreFileApisANSI [KERNEL32.@] Determines if file functions are using ANSI
2558 * TRUE: Set of file functions is using ANSI code page
2559 * FALSE: Set of file functions is using OEM code page
2561 BOOL WINAPI AreFileApisANSI(void)
2563 return !oem_file_apis;
2567 /***********************************************************************
2568 * GetTickCount (KERNEL32.@)
2570 * Returns the number of milliseconds, modulo 2^32, since the start
2571 * of the wineserver.
2573 DWORD WINAPI GetTickCount(void)
2576 gettimeofday( &t, NULL );
2577 return ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - server_startticks;
2581 /***********************************************************************
2582 * GetCurrentProcess (KERNEL32.@)
2584 #undef GetCurrentProcess
2585 HANDLE WINAPI GetCurrentProcess(void)
2587 return (HANDLE)0xffffffff;