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 #ifdef HAVE_SYS_TIME_H
32 # include <sys/time.h>
34 #include <sys/types.h>
36 #include "wine/winbase16.h"
37 #include "wine/winuser16.h"
43 #include "kernel_private.h"
44 #include "wine/exception.h"
45 #include "wine/server.h"
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(process);
50 WINE_DECLARE_DEBUG_CHANNEL(server);
51 WINE_DECLARE_DEBUG_CHANNEL(relay);
61 static UINT process_error_mode;
63 static HANDLE main_exe_file;
64 static DWORD shutdown_flags = 0;
65 static DWORD shutdown_priority = 0x280;
66 static DWORD process_dword;
67 static BOOL oem_file_apis;
69 static unsigned int server_startticks;
70 int main_create_flags = 0;
71 HMODULE kernel32_handle = 0;
74 #define PDB32_DEBUGGED 0x0001 /* Process is being debugged */
75 #define PDB32_WIN16_PROC 0x0008 /* Win16 process */
76 #define PDB32_DOS_PROC 0x0010 /* Dos process */
77 #define PDB32_CONSOLE_PROC 0x0020 /* Console process */
78 #define PDB32_FILE_APIS_OEM 0x0040 /* File APIs are OEM */
79 #define PDB32_WIN32S_PROC 0x8000 /* Win32s process */
81 static const WCHAR comW[] = {'.','c','o','m',0};
82 static const WCHAR batW[] = {'.','b','a','t',0};
83 static const WCHAR winevdmW[] = {'w','i','n','e','v','d','m','.','e','x','e',0};
85 extern void SHELL_LoadRegistry(void);
86 extern void VOLUME_CreateDevices(void);
87 extern void VERSION_Init( const WCHAR *appname );
88 extern void LOCALE_Init(void);
90 /***********************************************************************
93 inline static int contains_path( LPCWSTR name )
95 return ((*name && (name[1] == ':')) || strchrW(name, '/') || strchrW(name, '\\'));
99 /***********************************************************************
102 * Check if an environment variable needs to be handled specially when
103 * passed through the Unix environment (i.e. prefixed with "WINE").
105 inline static int is_special_env_var( const char *var )
107 return (!strncmp( var, "PATH=", sizeof("PATH=")-1 ) ||
108 !strncmp( var, "TEMP=", sizeof("TEMP=")-1 ) ||
109 !strncmp( var, "TMP=", sizeof("TMP=")-1 ));
113 /***************************************************************************
116 * Get the path of a builtin module when the native file does not exist.
118 static BOOL get_builtin_path( const WCHAR *libname, const WCHAR *ext, WCHAR *filename, UINT size )
121 WCHAR sysdir[MAX_PATH];
122 UINT len = GetSystemDirectoryW( sysdir, MAX_PATH );
124 if (contains_path( libname ))
126 if (RtlGetFullPathName_U( libname, size * sizeof(WCHAR),
127 filename, &file_part ) > size * sizeof(WCHAR))
128 return FALSE; /* too long */
130 if (strncmpiW( filename, sysdir, len ) || filename[len] != '\\')
132 while (filename[len] == '\\') len++;
133 if (filename + len != file_part) return FALSE;
137 if (strlenW(libname) + len + 2 >= size) return FALSE; /* too long */
138 memcpy( filename, sysdir, len * sizeof(WCHAR) );
139 file_part = filename + len;
140 if (file_part > filename && file_part[-1] != '\\') *file_part++ = '\\';
141 strcpyW( file_part, libname );
143 if (ext && !strchrW( file_part, '.' ))
145 if (file_part + strlenW(file_part) + strlenW(ext) + 1 > filename + size)
146 return FALSE; /* too long */
147 strcatW( file_part, ext );
153 /***********************************************************************
154 * open_builtin_exe_file
156 * Open an exe file for a builtin exe.
158 static void *open_builtin_exe_file( const WCHAR *name, char *error, int error_size,
159 int test_only, int *file_exists )
161 char exename[MAX_PATH];
165 if ((p = strrchrW( name, '/' ))) name = p + 1;
166 if ((p = strrchrW( name, '\\' ))) name = p + 1;
168 /* we don't want to depend on the current codepage here */
169 len = strlenW( name ) + 1;
170 if (len >= sizeof(exename)) return NULL;
171 for (i = 0; i < len; i++)
173 if (name[i] > 127) return NULL;
174 exename[i] = (char)name[i];
175 if (exename[i] >= 'A' && exename[i] <= 'Z') exename[i] += 'a' - 'A';
177 return wine_dll_load_main_exe( exename, error, error_size, test_only, file_exists );
181 /***********************************************************************
184 * Open a specific exe file, taking load order into account.
185 * Returns the file handle or 0 for a builtin exe.
187 static HANDLE open_exe_file( const WCHAR *name )
189 enum loadorder_type loadorder[LOADORDER_NTYPES];
190 WCHAR buffer[MAX_PATH];
194 TRACE("looking for %s\n", debugstr_w(name) );
196 if ((handle = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ,
197 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
199 /* file doesn't exist, check for builtin */
200 if (!contains_path( name )) goto error;
201 if (!get_builtin_path( name, NULL, buffer, sizeof(buffer) )) goto error;
205 MODULE_GetLoadOrderW( loadorder, NULL, name );
207 for(i = 0; i < LOADORDER_NTYPES; i++)
209 if (loadorder[i] == LOADORDER_INVALID) break;
213 TRACE( "Trying native exe %s\n", debugstr_w(name) );
214 if (handle != INVALID_HANDLE_VALUE) return handle;
217 TRACE( "Trying built-in exe %s\n", debugstr_w(name) );
218 open_builtin_exe_file( name, NULL, 0, 1, &file_exists );
221 if (handle != INVALID_HANDLE_VALUE) CloseHandle(handle);
228 if (handle != INVALID_HANDLE_VALUE) CloseHandle(handle);
231 SetLastError( ERROR_FILE_NOT_FOUND );
232 return INVALID_HANDLE_VALUE;
236 /***********************************************************************
239 * Open an exe file, and return the full name and file handle.
240 * Returns FALSE if file could not be found.
241 * If file exists but cannot be opened, returns TRUE and set handle to INVALID_HANDLE_VALUE.
242 * If file is a builtin exe, returns TRUE and sets handle to 0.
244 static BOOL find_exe_file( const WCHAR *name, WCHAR *buffer, int buflen, HANDLE *handle )
246 static const WCHAR exeW[] = {'.','e','x','e',0};
248 enum loadorder_type loadorder[LOADORDER_NTYPES];
251 TRACE("looking for %s\n", debugstr_w(name) );
253 if (!SearchPathW( NULL, name, exeW, buflen, buffer, NULL ) &&
254 !get_builtin_path( name, exeW, buffer, buflen ))
256 /* no builtin found, try native without extension in case it is a Unix app */
258 if (SearchPathW( NULL, name, NULL, buflen, buffer, NULL ))
260 TRACE( "Trying native/Unix binary %s\n", debugstr_w(buffer) );
261 if ((*handle = CreateFileW( buffer, GENERIC_READ, FILE_SHARE_READ,
262 NULL, OPEN_EXISTING, 0, 0 )) != INVALID_HANDLE_VALUE)
268 MODULE_GetLoadOrderW( loadorder, NULL, buffer );
270 for(i = 0; i < LOADORDER_NTYPES; i++)
272 if (loadorder[i] == LOADORDER_INVALID) break;
276 TRACE( "Trying native exe %s\n", debugstr_w(buffer) );
277 if ((*handle = CreateFileW( buffer, GENERIC_READ, FILE_SHARE_READ,
278 NULL, OPEN_EXISTING, 0, 0 )) != INVALID_HANDLE_VALUE)
280 if (GetLastError() != ERROR_FILE_NOT_FOUND) return TRUE;
283 TRACE( "Trying built-in exe %s\n", debugstr_w(buffer) );
284 open_builtin_exe_file( buffer, NULL, 0, 1, &file_exists );
295 SetLastError( ERROR_FILE_NOT_FOUND );
300 /**********************************************************************
303 * Load a PE format EXE file.
305 static HMODULE load_pe_exe( const WCHAR *name, HANDLE file )
307 IMAGE_NT_HEADERS *nt;
310 OBJECT_ATTRIBUTES attr;
315 attr.Length = sizeof(attr);
316 attr.RootDirectory = 0;
317 attr.ObjectName = NULL;
319 attr.SecurityDescriptor = NULL;
320 attr.SecurityQualityOfService = NULL;
323 if (NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
324 &attr, &size, 0, SEC_IMAGE, file ) != STATUS_SUCCESS)
328 if (NtMapViewOfSection( mapping, GetCurrentProcess(), &module, 0, 0, &size, &len,
329 ViewShare, 0, PAGE_READONLY ) != STATUS_SUCCESS)
335 nt = RtlImageNtHeader( module );
336 if (nt->OptionalHeader.AddressOfEntryPoint)
338 if (!RtlImageRvaToSection( nt, module, nt->OptionalHeader.AddressOfEntryPoint ))
339 MESSAGE("VIRUS WARNING: PE module %s has an invalid entrypoint (0x%08lx) "
340 "outside all sections (possibly infected by Tchernobyl/SpaceFiller virus)!\n",
341 debugstr_w(name), nt->OptionalHeader.AddressOfEntryPoint );
344 drive_type = GetDriveTypeW( name );
345 /* don't keep the file handle open on removable media */
346 if (drive_type == DRIVE_REMOVABLE || drive_type == DRIVE_CDROM)
348 CloseHandle( main_exe_file );
355 /***********************************************************************
356 * build_initial_environment
358 * Build the Win32 environment from the Unix environment
360 static BOOL build_initial_environment( char **environ )
367 /* Compute the total size of the Unix environment */
368 for (e = environ; *e; e++)
370 if (is_special_env_var( *e )) continue;
371 size += MultiByteToWideChar( CP_UNIXCP, 0, *e, -1, NULL, 0 );
373 size *= sizeof(WCHAR);
375 /* Now allocate the environment */
376 if (NtAllocateVirtualMemory(NtCurrentProcess(), &ptr, 0, &size,
377 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) != STATUS_SUCCESS)
380 NtCurrentTeb()->Peb->ProcessParameters->Environment = p = ptr;
381 endptr = p + size / sizeof(WCHAR);
383 /* And fill it with the Unix environment */
384 for (e = environ; *e; e++)
388 /* skip Unix special variables and use the Wine variants instead */
389 if (!strncmp( str, "WINE", 4 ))
391 if (is_special_env_var( str + 4 )) str += 4;
393 else if (is_special_env_var( str )) continue; /* skip it */
395 MultiByteToWideChar( CP_UNIXCP, 0, str, -1, p, endptr - p );
403 /***********************************************************************
406 * Set the Wine library Unicode argv global variables.
408 static void set_library_wargv( char **argv )
415 for (argc = 0; argv[argc]; argc++)
416 total += MultiByteToWideChar( CP_UNIXCP, 0, argv[argc], -1, NULL, 0 );
418 wargv = RtlAllocateHeap( GetProcessHeap(), 0,
419 total * sizeof(WCHAR) + (argc + 1) * sizeof(*wargv) );
420 p = (WCHAR *)(wargv + argc + 1);
421 for (argc = 0; argv[argc]; argc++)
423 DWORD reslen = MultiByteToWideChar( CP_UNIXCP, 0, argv[argc], -1, p, total );
429 __wine_main_wargv = wargv;
433 /***********************************************************************
436 * Build the command line of a process from the argv array.
438 * Note that it does NOT necessarily include the file name.
439 * Sometimes we don't even have any command line options at all.
441 * We must quote and escape characters so that the argv array can be rebuilt
442 * from the command line:
443 * - spaces and tabs must be quoted
445 * - quotes must be escaped
447 * - if '\'s are followed by a '"', they must be doubled and followed by '\"',
448 * resulting in an odd number of '\' followed by a '"'
451 * - '\'s that are not followed by a '"' can be left as is
455 static BOOL build_command_line( WCHAR **argv )
460 RTL_USER_PROCESS_PARAMETERS* rupp = NtCurrentTeb()->Peb->ProcessParameters;
462 if (rupp->CommandLine.Buffer) return TRUE; /* already got it from the server */
465 for (arg = argv; *arg; arg++)
467 int has_space,bcount;
473 if( !*a ) has_space=1;
478 if (*a==' ' || *a=='\t') {
480 } else if (*a=='"') {
481 /* doubling of '\' preceeding a '"',
482 * plus escaping of said '"'
490 len+=(a-*arg)+1 /* for the separating space */;
492 len+=2; /* for the quotes */
495 if (!(rupp->CommandLine.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR))))
498 p = rupp->CommandLine.Buffer;
499 rupp->CommandLine.Length = (len - 1) * sizeof(WCHAR);
500 rupp->CommandLine.MaximumLength = len * sizeof(WCHAR);
501 for (arg = argv; *arg; arg++)
503 int has_space,has_quote;
506 /* Check for quotes and spaces in this argument */
507 has_space=has_quote=0;
509 if( !*a ) has_space=1;
511 if (*a==' ' || *a=='\t') {
515 } else if (*a=='"') {
523 /* Now transfer it to the command line */
540 /* Double all the '\\' preceeding this '"', plus one */
541 for (i=0;i<=bcount;i++)
553 while ((*p=*x++)) p++;
559 if (p > rupp->CommandLine.Buffer)
560 p--; /* remove last space */
567 /* make sure the unicode string doesn't point beyond the end pointer */
568 static inline void fix_unicode_string( UNICODE_STRING *str, char *end_ptr )
570 if ((char *)str->Buffer >= end_ptr)
572 str->Length = str->MaximumLength = 0;
576 if ((char *)str->Buffer + str->MaximumLength > end_ptr)
578 str->MaximumLength = (end_ptr - (char *)str->Buffer) & ~(sizeof(WCHAR) - 1);
580 if (str->Length >= str->MaximumLength)
582 if (str->MaximumLength >= sizeof(WCHAR))
583 str->Length = str->MaximumLength - sizeof(WCHAR);
585 str->Length = str->MaximumLength = 0;
590 /***********************************************************************
591 * init_user_process_params
593 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
595 static RTL_USER_PROCESS_PARAMETERS *init_user_process_params( size_t info_size )
600 RTL_USER_PROCESS_PARAMETERS *params;
603 if ((status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, NULL, &size,
604 MEM_COMMIT, PAGE_READWRITE )) != STATUS_SUCCESS)
607 SERVER_START_REQ( get_startup_info )
609 wine_server_set_reply( req, ptr, info_size );
610 wine_server_call( req );
611 info_size = wine_server_reply_size( reply );
616 params->Size = info_size;
617 params->AllocationSize = size;
619 /* make sure the strings are valid */
620 fix_unicode_string( ¶ms->CurrentDirectory.DosPath, (char *)info_size );
621 fix_unicode_string( ¶ms->DllPath, (char *)info_size );
622 fix_unicode_string( ¶ms->ImagePathName, (char *)info_size );
623 fix_unicode_string( ¶ms->CommandLine, (char *)info_size );
624 fix_unicode_string( ¶ms->WindowTitle, (char *)info_size );
625 fix_unicode_string( ¶ms->Desktop, (char *)info_size );
626 fix_unicode_string( ¶ms->ShellInfo, (char *)info_size );
627 fix_unicode_string( ¶ms->RuntimeInfo, (char *)info_size );
629 return RtlNormalizeProcessParams( params );
633 /***********************************************************************
636 * Main process initialisation code
638 static BOOL process_init( char *argv[], char **environ )
640 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2',0};
642 size_t info_size = 0;
643 RTL_USER_PROCESS_PARAMETERS *params;
644 PEB *peb = NtCurrentTeb()->Peb;
645 HANDLE hstdin, hstdout, hstderr;
646 extern void __wine_dbg_kernel32_init(void);
650 __wine_dbg_kernel32_init(); /* hack: register debug channels early */
654 setlocale(LC_CTYPE,"");
656 /* Retrieve startup info from the server */
657 SERVER_START_REQ( init_process )
660 req->ldt_copy = &wine_ldt_copy;
661 if ((ret = !wine_server_call_err( req )))
663 main_exe_file = reply->exe_file;
664 main_create_flags = reply->create_flags;
665 info_size = reply->info_size;
666 server_startticks = reply->server_start;
667 hstdin = reply->hstdin;
668 hstdout = reply->hstdout;
669 hstderr = reply->hstderr;
673 if (!ret) return FALSE;
677 params = peb->ProcessParameters;
679 /* This is wine specific: we have no parent (we're started from unix)
680 * so, create a simple console with bare handles to unix stdio
681 * input & output streams (aka simple console)
683 wine_server_fd_to_handle( 0, GENERIC_READ|SYNCHRONIZE, TRUE, ¶ms->hStdInput );
684 wine_server_fd_to_handle( 1, GENERIC_WRITE|SYNCHRONIZE, TRUE, ¶ms->hStdOutput );
685 wine_server_fd_to_handle( 2, GENERIC_WRITE|SYNCHRONIZE, TRUE, ¶ms->hStdError );
687 /* <hack: to be changed later on> */
688 params->CurrentDirectory.DosPath.Length = 3 * sizeof(WCHAR);
689 params->CurrentDirectory.DosPath.MaximumLength = RtlGetLongestNtPathLength() * sizeof(WCHAR);
690 params->CurrentDirectory.DosPath.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, params->CurrentDirectory.DosPath.MaximumLength);
691 params->CurrentDirectory.DosPath.Buffer[0] = 'C';
692 params->CurrentDirectory.DosPath.Buffer[1] = ':';
693 params->CurrentDirectory.DosPath.Buffer[2] = '\\';
694 params->CurrentDirectory.DosPath.Buffer[3] = '\0';
695 /* </hack: to be changed later on> */
699 if (!(params = init_user_process_params( info_size ))) return FALSE;
700 peb->ProcessParameters = params;
702 /* convert value from server:
703 * + 0 => INVALID_HANDLE_VALUE
704 * + console handle need to be mapped
707 hstdin = INVALID_HANDLE_VALUE;
708 else if (VerifyConsoleIoHandle(console_handle_map(hstdin)))
709 hstdin = console_handle_map(hstdin);
712 hstdout = INVALID_HANDLE_VALUE;
713 else if (VerifyConsoleIoHandle(console_handle_map(hstdout)))
714 hstdout = console_handle_map(hstdout);
717 hstderr = INVALID_HANDLE_VALUE;
718 else if (VerifyConsoleIoHandle(console_handle_map(hstderr)))
719 hstderr = console_handle_map(hstderr);
721 params->hStdInput = hstdin;
722 params->hStdOutput = hstdout;
723 params->hStdError = hstderr;
726 kernel32_handle = GetModuleHandleW(kernel32W);
730 /* Copy the parent environment */
731 if (!build_initial_environment( environ )) return FALSE;
733 /* Parse command line arguments */
734 if (!info_size) OPTIONS_ParseOptions( argv );
736 /* initialise DOS drives */
737 if (!DRIVE_Init()) return FALSE;
739 /* initialise DOS directories */
740 if (!DIR_Init()) return FALSE;
742 /* registry initialisation */
743 SHELL_LoadRegistry();
745 /* Create device symlinks */
746 VOLUME_CreateDevices();
748 /* global boot finished, the rest is process-local */
749 SERVER_START_REQ( boot_done )
751 req->debug_level = TRACE_ON(server);
752 wine_server_call( req );
760 /***********************************************************************
763 * Startup routine of a new process. Runs on the new process stack.
765 static void start_process( void *arg )
769 PEB *peb = NtCurrentTeb()->Peb;
770 IMAGE_NT_HEADERS *nt;
771 LPTHREAD_START_ROUTINE entry;
773 LdrInitializeThunk( main_exe_file, 0, 0, 0 );
775 nt = RtlImageNtHeader( peb->ImageBaseAddress );
776 entry = (LPTHREAD_START_ROUTINE)((char *)peb->ImageBaseAddress +
777 nt->OptionalHeader.AddressOfEntryPoint);
780 DPRINTF( "%04lx:Starting process %s (entryproc=%p)\n", GetCurrentThreadId(),
781 debugstr_w(peb->ProcessParameters->ImagePathName.Buffer), entry );
783 SetLastError( 0 ); /* clear error code */
784 if (peb->BeingDebugged) DbgBreakPoint();
785 ExitProcess( entry( peb ) );
787 __EXCEPT(UnhandledExceptionFilter)
789 TerminateThread( GetCurrentThread(), GetExceptionCode() );
795 /***********************************************************************
798 * Wine initialisation: load and start the main exe file.
800 void __wine_kernel_init(void)
802 WCHAR *main_exe_name, *p;
804 DWORD stack_size = 0;
806 PEB *peb = NtCurrentTeb()->Peb;
808 /* Initialize everything */
809 if (!process_init( __wine_main_argv, __wine_main_environ )) exit(1);
810 /* update argc in case options have been removed */
811 for (__wine_main_argc = 0; __wine_main_argv[__wine_main_argc]; __wine_main_argc++) /*nothing*/;
813 __wine_main_argv++; /* remove argv[0] (wine itself) */
816 if (!(main_exe_name = peb->ProcessParameters->ImagePathName.Buffer))
818 WCHAR buffer[MAX_PATH];
819 WCHAR exe_nameW[MAX_PATH];
821 if (!__wine_main_argv[0]) OPTIONS_Usage();
823 MultiByteToWideChar( CP_UNIXCP, 0, __wine_main_argv[0], -1, exe_nameW, MAX_PATH );
824 if (!find_exe_file( exe_nameW, buffer, MAX_PATH, &main_exe_file ))
826 MESSAGE( "wine: cannot find '%s'\n", __wine_main_argv[0] );
829 if (main_exe_file == INVALID_HANDLE_VALUE)
831 MESSAGE( "wine: cannot open %s\n", debugstr_w(main_exe_name) );
834 RtlCreateUnicodeString( &peb->ProcessParameters->ImagePathName, buffer );
835 main_exe_name = peb->ProcessParameters->ImagePathName.Buffer;
838 TRACE( "starting process name=%s file=%p argv[0]=%s\n",
839 debugstr_w(main_exe_name), main_exe_file, debugstr_a(__wine_main_argv[0]) );
841 RtlInitUnicodeString( &NtCurrentTeb()->Peb->ProcessParameters->DllPath,
842 MODULE_get_dll_load_path(NULL) );
843 VERSION_Init( main_exe_name );
845 if (!main_exe_file) /* no file handle -> Winelib app */
847 TRACE( "starting Winelib app %s\n", debugstr_w(main_exe_name) );
848 if (open_builtin_exe_file( main_exe_name, error, sizeof(error), 0, &file_exists ))
850 MESSAGE( "wine: cannot open builtin library for %s: %s\n",
851 debugstr_w(main_exe_name), error );
855 switch( MODULE_GetBinaryType( main_exe_file ))
858 TRACE( "starting Win32 binary %s\n", debugstr_w(main_exe_name) );
859 if ((peb->ImageBaseAddress = load_pe_exe( main_exe_name, main_exe_file )))
861 MESSAGE( "wine: could not load %s as Win32 binary\n", debugstr_w(main_exe_name) );
864 MESSAGE( "wine: %s is a DLL, not an executable\n", debugstr_w(main_exe_name) );
867 /* check for .com extension */
868 if (!(p = strrchrW( main_exe_name, '.' )) || strcmpiW( p, comW ))
870 MESSAGE( "wine: cannot determine executable type for %s\n",
871 debugstr_w(main_exe_name) );
877 TRACE( "starting Win16/DOS binary %s\n", debugstr_w(main_exe_name) );
878 CloseHandle( main_exe_file );
882 __wine_main_argv[0] = "winevdm.exe";
883 if (open_builtin_exe_file( winevdmW, error, sizeof(error), 0, &file_exists ))
885 MESSAGE( "wine: trying to run %s, cannot open builtin library for 'winevdm.exe': %s\n",
886 debugstr_w(main_exe_name), error );
889 MESSAGE( "wine: %s is an OS/2 binary, not supported\n", debugstr_w(main_exe_name) );
891 case BINARY_UNIX_EXE:
892 MESSAGE( "wine: %s is a Unix binary, not supported\n", debugstr_w(main_exe_name) );
894 case BINARY_UNIX_LIB:
898 TRACE( "starting Winelib app %s\n", debugstr_w(main_exe_name) );
899 CloseHandle( main_exe_file );
901 if ((unix_name = wine_get_unix_file_name( main_exe_name )) &&
902 wine_dlopen( unix_name, RTLD_NOW, error, sizeof(error) ))
904 static const WCHAR soW[] = {'.','s','o',0};
905 if ((p = strrchrW( main_exe_name, '.' )) && !strcmpW( p, soW ))
908 /* update the unicode string */
909 RtlInitUnicodeString( &peb->ProcessParameters->ImagePathName, main_exe_name );
911 HeapFree( GetProcessHeap(), 0, unix_name );
914 MESSAGE( "wine: could not load %s: %s\n", debugstr_w(main_exe_name), error );
920 wine_free_pe_load_area(); /* the main binary is loaded, we don't need this anymore */
922 /* build command line */
923 set_library_wargv( __wine_main_argv );
924 if (!build_command_line( __wine_main_wargv )) goto error;
926 stack_size = RtlImageNtHeader(peb->ImageBaseAddress)->OptionalHeader.SizeOfStackReserve;
928 /* allocate main thread stack */
929 if (!THREAD_InitStack( NtCurrentTeb(), stack_size )) goto error;
931 /* switch to the new stack */
932 wine_switch_to_stack( start_process, NULL, NtCurrentTeb()->Tib.StackBase );
935 ExitProcess( GetLastError() );
939 /***********************************************************************
942 * Build an argv array from a command-line.
943 * 'reserved' is the number of args to reserve before the first one.
945 static char **build_argv( const WCHAR *cmdlineW, int reserved )
949 char *arg,*s,*d,*cmdline;
950 int in_quotes,bcount,len;
952 len = WideCharToMultiByte( CP_UNIXCP, 0, cmdlineW, -1, NULL, 0, NULL, NULL );
953 if (!(cmdline = malloc(len))) return NULL;
954 WideCharToMultiByte( CP_UNIXCP, 0, cmdlineW, -1, cmdline, len, NULL, NULL );
961 if (*s=='\0' || ((*s==' ' || *s=='\t') && !in_quotes)) {
964 /* skip the remaining spaces */
965 while (*s==' ' || *s=='\t') {
972 } else if (*s=='\\') {
973 /* '\', count them */
975 } else if ((*s=='"') && ((bcount & 1)==0)) {
977 in_quotes=!in_quotes;
980 /* a regular character */
985 argv=malloc(argc*sizeof(*argv));
994 if ((*s==' ' || *s=='\t') && !in_quotes) {
995 /* Close the argument and copy it */
999 /* skip the remaining spaces */
1002 } while (*s==' ' || *s=='\t');
1004 /* Start with a new argument */
1007 } else if (*s=='\\') {
1011 } else if (*s=='"') {
1013 if ((bcount & 1)==0) {
1014 /* Preceeded by an even number of '\', this is half that
1015 * number of '\', plus a '"' which we discard.
1019 in_quotes=!in_quotes;
1021 /* Preceeded by an odd number of '\', this is half that
1022 * number of '\' followed by a '"'
1030 /* a regular character */
1045 /***********************************************************************
1048 * Allocate an environment string; helper for build_envp
1050 static char *alloc_env_string( const char *name, const char *value )
1052 char *ret = malloc( strlen(name) + strlen(value) + 1 );
1053 strcpy( ret, name );
1054 strcat( ret, value );
1058 /***********************************************************************
1061 * Build the environment of a new child process.
1063 static char **build_envp( const WCHAR *envW, const WCHAR *extra_envW )
1067 char *env, *extra_env = NULL;
1068 int count = 0, length;
1072 for (p = extra_envW; *p; count++) p += strlenW(p) + 1;
1074 length = WideCharToMultiByte( CP_UNIXCP, 0, extra_envW, p - extra_envW,
1075 NULL, 0, NULL, NULL );
1076 if ((extra_env = malloc( length )))
1077 WideCharToMultiByte( CP_UNIXCP, 0, extra_envW, p - extra_envW,
1078 extra_env, length, NULL, NULL );
1080 for (p = envW; *p; count++) p += strlenW(p) + 1;
1082 length = WideCharToMultiByte( CP_UNIXCP, 0, envW, p - envW, NULL, 0, NULL, NULL );
1083 if (!(env = malloc( length ))) return NULL;
1084 WideCharToMultiByte( CP_UNIXCP, 0, envW, p - envW, env, length, NULL, NULL );
1088 if ((envp = malloc( count * sizeof(*envp) )))
1090 char **envptr = envp;
1093 /* first the extra strings */
1094 if (extra_env) for (p = extra_env; *p; p += strlen(p) + 1) *envptr++ = p;
1095 /* then put PATH, TEMP, TMP, HOME and WINEPREFIX from the unix env */
1096 if ((p = getenv("PATH"))) *envptr++ = alloc_env_string( "PATH=", p );
1097 if ((p = getenv("TEMP"))) *envptr++ = alloc_env_string( "TEMP=", p );
1098 if ((p = getenv("TMP"))) *envptr++ = alloc_env_string( "TMP=", p );
1099 if ((p = getenv("HOME"))) *envptr++ = alloc_env_string( "HOME=", p );
1100 if ((p = getenv("WINEPREFIX"))) *envptr++ = alloc_env_string( "WINEPREFIX=", p );
1101 /* now put the Windows environment strings */
1102 for (p = env; *p; p += strlen(p) + 1)
1104 if (extra_env && p[0]=='=' && 'A'<=p[1] && p[1]<='Z' && p[2]==':' && p[3]=='=')
1105 continue; /* skipped */
1106 if (is_special_env_var( p )) /* prefix it with "WINE" */
1107 *envptr++ = alloc_env_string( "WINE", p );
1108 else if (strncmp( p, "HOME=", 5 ) &&
1109 strncmp( p, "WINEPREFIX=", 11 )) *envptr++ = p;
1117 /***********************************************************************
1120 * Fork and exec a new Unix binary, checking for errors.
1122 static int fork_and_exec( const char *filename, const WCHAR *cmdline,
1123 const WCHAR *env, const char *newdir )
1128 if (!env) env = GetEnvironmentStringsW();
1132 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1135 fcntl( fd[1], F_SETFD, 1 ); /* set close on exec */
1136 if (!(pid = fork())) /* child */
1138 char **argv = build_argv( cmdline, 0 );
1139 char **envp = build_envp( env, NULL );
1142 /* Reset signals that we previously set to SIG_IGN */
1143 signal( SIGPIPE, SIG_DFL );
1144 signal( SIGCHLD, SIG_DFL );
1146 if (newdir) chdir(newdir);
1148 if (argv && envp) execve( filename, argv, envp );
1150 write( fd[1], &err, sizeof(err) );
1154 if ((pid != -1) && (read( fd[0], &err, sizeof(err) ) > 0)) /* exec failed */
1159 if (pid == -1) FILE_SetDosError();
1165 /***********************************************************************
1166 * create_user_params
1168 static RTL_USER_PROCESS_PARAMETERS *create_user_params( LPCWSTR filename, LPCWSTR cmdline,
1169 const STARTUPINFOW *startup )
1171 RTL_USER_PROCESS_PARAMETERS *params;
1172 UNICODE_STRING image_str, cmdline_str, desktop, title;
1174 WCHAR buffer[MAX_PATH];
1176 if(!GetLongPathNameW( filename, buffer, MAX_PATH ))
1177 lstrcpynW( buffer, filename, MAX_PATH );
1178 if(!GetFullPathNameW( buffer, MAX_PATH, buffer, NULL ))
1179 lstrcpynW( buffer, filename, MAX_PATH );
1180 RtlInitUnicodeString( &image_str, buffer );
1182 RtlInitUnicodeString( &cmdline_str, cmdline );
1183 if (startup->lpDesktop) RtlInitUnicodeString( &desktop, startup->lpDesktop );
1184 if (startup->lpTitle) RtlInitUnicodeString( &title, startup->lpTitle );
1186 status = RtlCreateProcessParameters( ¶ms, &image_str, NULL, NULL, &cmdline_str, NULL,
1187 startup->lpTitle ? &title : NULL,
1188 startup->lpDesktop ? &desktop : NULL,
1190 if (status != STATUS_SUCCESS)
1192 SetLastError( RtlNtStatusToDosError(status) );
1196 params->Environment = NULL; /* we pass it through the Unix environment */
1197 params->hStdInput = startup->hStdInput;
1198 params->hStdOutput = startup->hStdOutput;
1199 params->hStdError = startup->hStdError;
1200 params->dwX = startup->dwX;
1201 params->dwY = startup->dwY;
1202 params->dwXSize = startup->dwXSize;
1203 params->dwYSize = startup->dwYSize;
1204 params->dwXCountChars = startup->dwXCountChars;
1205 params->dwYCountChars = startup->dwYCountChars;
1206 params->dwFillAttribute = startup->dwFillAttribute;
1207 params->dwFlags = startup->dwFlags;
1208 params->wShowWindow = startup->wShowWindow;
1213 /***********************************************************************
1216 * Create a new process. If hFile is a valid handle we have an exe
1217 * file, otherwise it is a Winelib app.
1219 static BOOL create_process( HANDLE hFile, LPCWSTR filename, LPWSTR cmd_line, LPWSTR env,
1220 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1221 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1222 LPPROCESS_INFORMATION info, LPCSTR unixdir )
1224 BOOL ret, success = FALSE;
1225 HANDLE process_info;
1226 RTL_USER_PROCESS_PARAMETERS *params;
1227 WCHAR *extra_env = NULL;
1236 env = GetEnvironmentStringsW();
1237 extra_env = DRIVE_BuildEnv();
1240 if (!(params = create_user_params( filename, cmd_line, startup )))
1242 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1246 /* create the synchronization pipes */
1248 if (pipe( startfd ) == -1)
1250 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1251 RtlDestroyProcessParameters( params );
1252 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1255 if (pipe( execfd ) == -1)
1257 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1258 close( startfd[0] );
1259 close( startfd[1] );
1260 RtlDestroyProcessParameters( params );
1261 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1264 fcntl( execfd[1], F_SETFD, 1 ); /* set close on exec */
1266 /* create the child process */
1268 if (!(pid = fork())) /* child */
1270 char **argv = build_argv( cmd_line, 1 );
1271 char **envp = build_envp( env, extra_env );
1273 close( startfd[1] );
1276 /* wait for parent to tell us to start */
1277 if (read( startfd[0], &dummy, 1 ) != 1) _exit(1);
1279 close( startfd[0] );
1280 /* Reset signals that we previously set to SIG_IGN */
1281 signal( SIGPIPE, SIG_DFL );
1282 signal( SIGCHLD, SIG_DFL );
1284 if (unixdir) chdir(unixdir);
1288 /* first, try for a WINELOADER environment variable */
1289 argv[0] = getenv("WINELOADER");
1290 if (argv[0]) execve( argv[0], argv, envp );
1291 /* now use the standard search strategy */
1292 wine_exec_wine_binary( NULL, argv, envp );
1295 write( execfd[1], &err, sizeof(err) );
1299 /* this is the parent */
1301 close( startfd[0] );
1303 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1306 close( startfd[1] );
1309 RtlDestroyProcessParameters( params );
1313 /* create the process on the server side */
1315 SERVER_START_REQ( new_process )
1317 req->inherit_all = inherit;
1318 req->create_flags = flags;
1319 req->unix_pid = pid;
1320 req->exe_file = hFile;
1321 if (startup->dwFlags & STARTF_USESTDHANDLES)
1323 req->hstdin = startup->hStdInput;
1324 req->hstdout = startup->hStdOutput;
1325 req->hstderr = startup->hStdError;
1329 req->hstdin = GetStdHandle( STD_INPUT_HANDLE );
1330 req->hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
1331 req->hstderr = GetStdHandle( STD_ERROR_HANDLE );
1334 if ((flags & (CREATE_NEW_CONSOLE | DETACHED_PROCESS)) != 0)
1336 /* this is temporary (for console handles). We have no way to control that the handle is invalid in child process otherwise */
1337 if (is_console_handle(req->hstdin)) req->hstdin = INVALID_HANDLE_VALUE;
1338 if (is_console_handle(req->hstdout)) req->hstdout = INVALID_HANDLE_VALUE;
1339 if (is_console_handle(req->hstderr)) req->hstderr = INVALID_HANDLE_VALUE;
1343 if (is_console_handle(req->hstdin)) req->hstdin = console_handle_unmap(req->hstdin);
1344 if (is_console_handle(req->hstdout)) req->hstdout = console_handle_unmap(req->hstdout);
1345 if (is_console_handle(req->hstderr)) req->hstderr = console_handle_unmap(req->hstderr);
1348 wine_server_add_data( req, params, params->Size );
1349 ret = !wine_server_call_err( req );
1350 process_info = reply->info;
1354 RtlDestroyProcessParameters( params );
1357 close( startfd[1] );
1362 /* tell child to start and wait for it to exec */
1364 write( startfd[1], &dummy, 1 );
1365 close( startfd[1] );
1367 if (read( execfd[0], &err, sizeof(err) ) > 0) /* exec failed */
1372 CloseHandle( process_info );
1377 /* wait for the new process info to be ready */
1379 WaitForSingleObject( process_info, INFINITE );
1380 SERVER_START_REQ( get_new_process_info )
1382 req->info = process_info;
1383 req->pinherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
1384 req->tinherit = (tsa && (tsa->nLength >= sizeof(*tsa)) && tsa->bInheritHandle);
1385 if ((ret = !wine_server_call_err( req )))
1387 info->dwProcessId = (DWORD)reply->pid;
1388 info->dwThreadId = (DWORD)reply->tid;
1389 info->hProcess = reply->phandle;
1390 info->hThread = reply->thandle;
1391 success = reply->success;
1396 if (ret && !success) /* new process failed to start */
1399 if (GetExitCodeProcess( info->hProcess, &exitcode )) SetLastError( exitcode );
1400 CloseHandle( info->hThread );
1401 CloseHandle( info->hProcess );
1404 CloseHandle( process_info );
1409 /***********************************************************************
1410 * create_vdm_process
1412 * Create a new VDM process for a 16-bit or DOS application.
1414 static BOOL create_vdm_process( LPCWSTR filename, LPWSTR cmd_line, LPWSTR env,
1415 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1416 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1417 LPPROCESS_INFORMATION info, LPCSTR unixdir )
1419 static const WCHAR argsW[] = {'%','s',' ','-','-','a','p','p','-','n','a','m','e',' ','"','%','s','"',' ','%','s',0};
1422 LPWSTR new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
1423 (strlenW(filename) + strlenW(cmd_line) + 30) * sizeof(WCHAR) );
1427 SetLastError( ERROR_OUTOFMEMORY );
1430 sprintfW( new_cmd_line, argsW, winevdmW, filename, cmd_line );
1431 ret = create_process( 0, winevdmW, new_cmd_line, env, psa, tsa, inherit,
1432 flags, startup, info, unixdir );
1433 HeapFree( GetProcessHeap(), 0, new_cmd_line );
1438 /***********************************************************************
1439 * create_cmd_process
1441 * Create a new cmd shell process for a .BAT file.
1443 static BOOL create_cmd_process( LPCWSTR filename, LPWSTR cmd_line, LPVOID env,
1444 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1445 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1446 LPPROCESS_INFORMATION info, LPCWSTR cur_dir )
1449 static const WCHAR comspecW[] = {'C','O','M','S','P','E','C',0};
1450 static const WCHAR slashcW[] = {' ','/','c',' ',0};
1451 WCHAR comspec[MAX_PATH];
1455 if (!GetEnvironmentVariableW( comspecW, comspec, sizeof(comspec)/sizeof(WCHAR) ))
1457 if (!(newcmdline = HeapAlloc( GetProcessHeap(), 0,
1458 (strlenW(comspec) + 4 + strlenW(cmd_line) + 1) * sizeof(WCHAR))))
1461 strcpyW( newcmdline, comspec );
1462 strcatW( newcmdline, slashcW );
1463 strcatW( newcmdline, cmd_line );
1464 ret = CreateProcessW( comspec, newcmdline, psa, tsa, inherit,
1465 flags, env, cur_dir, startup, info );
1466 HeapFree( GetProcessHeap(), 0, newcmdline );
1471 /*************************************************************************
1474 * Helper for CreateProcess: retrieve the file name to load from the
1475 * app name and command line. Store the file name in buffer, and
1476 * return a possibly modified command line.
1477 * Also returns a handle to the opened file if it's a Windows binary.
1479 static LPWSTR get_file_name( LPCWSTR appname, LPWSTR cmdline, LPWSTR buffer,
1480 int buflen, HANDLE *handle )
1482 static const WCHAR quotesW[] = {'"','%','s','"',0};
1484 WCHAR *name, *pos, *ret = NULL;
1487 /* if we have an app name, everything is easy */
1491 /* use the unmodified app name as file name */
1492 lstrcpynW( buffer, appname, buflen );
1493 *handle = open_exe_file( buffer );
1494 if (!(ret = cmdline) || !cmdline[0])
1496 /* no command-line, create one */
1497 if ((ret = HeapAlloc( GetProcessHeap(), 0, (strlenW(appname) + 3) * sizeof(WCHAR) )))
1498 sprintfW( ret, quotesW, appname );
1505 SetLastError( ERROR_INVALID_PARAMETER );
1509 /* first check for a quoted file name */
1511 if ((cmdline[0] == '"') && ((p = strchrW( cmdline + 1, '"' ))))
1513 int len = p - cmdline - 1;
1514 /* extract the quoted portion as file name */
1515 if (!(name = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return NULL;
1516 memcpy( name, cmdline + 1, len * sizeof(WCHAR) );
1519 if (find_exe_file( name, buffer, buflen, handle ))
1520 ret = cmdline; /* no change necessary */
1524 /* now try the command-line word by word */
1526 if (!(name = HeapAlloc( GetProcessHeap(), 0, (strlenW(cmdline) + 1) * sizeof(WCHAR) )))
1533 do *pos++ = *p++; while (*p && *p != ' ');
1535 if (find_exe_file( name, buffer, buflen, handle ))
1542 if (!ret || !strchrW( name, ' ' )) goto done; /* no change necessary */
1544 /* now build a new command-line with quotes */
1546 if (!(ret = HeapAlloc( GetProcessHeap(), 0, (strlenW(cmdline) + 3) * sizeof(WCHAR) )))
1548 sprintfW( ret, quotesW, name );
1552 HeapFree( GetProcessHeap(), 0, name );
1557 /**********************************************************************
1558 * CreateProcessA (KERNEL32.@)
1560 BOOL WINAPI CreateProcessA( LPCSTR app_name, LPSTR cmd_line, LPSECURITY_ATTRIBUTES process_attr,
1561 LPSECURITY_ATTRIBUTES thread_attr, BOOL inherit,
1562 DWORD flags, LPVOID env, LPCSTR cur_dir,
1563 LPSTARTUPINFOA startup_info, LPPROCESS_INFORMATION info )
1566 UNICODE_STRING app_nameW, cmd_lineW, cur_dirW, desktopW, titleW;
1569 if (app_name) RtlCreateUnicodeStringFromAsciiz( &app_nameW, app_name );
1570 else app_nameW.Buffer = NULL;
1571 if (cmd_line) RtlCreateUnicodeStringFromAsciiz( &cmd_lineW, cmd_line );
1572 else cmd_lineW.Buffer = NULL;
1573 if (cur_dir) RtlCreateUnicodeStringFromAsciiz( &cur_dirW, cur_dir );
1574 else cur_dirW.Buffer = NULL;
1575 if (startup_info->lpDesktop) RtlCreateUnicodeStringFromAsciiz( &desktopW, startup_info->lpDesktop );
1576 else desktopW.Buffer = NULL;
1577 if (startup_info->lpTitle) RtlCreateUnicodeStringFromAsciiz( &titleW, startup_info->lpTitle );
1578 else titleW.Buffer = NULL;
1580 memcpy( &infoW, startup_info, sizeof(infoW) );
1581 infoW.lpDesktop = desktopW.Buffer;
1582 infoW.lpTitle = titleW.Buffer;
1584 if (startup_info->lpReserved)
1585 FIXME("StartupInfo.lpReserved is used, please report (%s)\n",
1586 debugstr_a(startup_info->lpReserved));
1588 ret = CreateProcessW( app_nameW.Buffer, cmd_lineW.Buffer, process_attr, thread_attr,
1589 inherit, flags, env, cur_dirW.Buffer, &infoW, info );
1591 RtlFreeUnicodeString( &app_nameW );
1592 RtlFreeUnicodeString( &cmd_lineW );
1593 RtlFreeUnicodeString( &cur_dirW );
1594 RtlFreeUnicodeString( &desktopW );
1595 RtlFreeUnicodeString( &titleW );
1600 /**********************************************************************
1601 * CreateProcessW (KERNEL32.@)
1603 BOOL WINAPI CreateProcessW( LPCWSTR app_name, LPWSTR cmd_line, LPSECURITY_ATTRIBUTES process_attr,
1604 LPSECURITY_ATTRIBUTES thread_attr, BOOL inherit, DWORD flags,
1605 LPVOID env, LPCWSTR cur_dir, LPSTARTUPINFOW startup_info,
1606 LPPROCESS_INFORMATION info )
1610 char *unixdir = NULL;
1611 WCHAR name[MAX_PATH];
1612 WCHAR *tidy_cmdline, *p, *envW = env;
1614 /* Process the AppName and/or CmdLine to get module name and path */
1616 TRACE("app %s cmdline %s\n", debugstr_w(app_name), debugstr_w(cmd_line) );
1618 if (!(tidy_cmdline = get_file_name( app_name, cmd_line, name, sizeof(name), &hFile )))
1620 if (hFile == INVALID_HANDLE_VALUE) goto done;
1622 /* Warn if unsupported features are used */
1624 if (flags & (IDLE_PRIORITY_CLASS | HIGH_PRIORITY_CLASS | REALTIME_PRIORITY_CLASS |
1625 CREATE_NEW_PROCESS_GROUP | CREATE_SEPARATE_WOW_VDM | CREATE_SHARED_WOW_VDM |
1626 CREATE_DEFAULT_ERROR_MODE | CREATE_NO_WINDOW |
1627 PROFILE_USER | PROFILE_KERNEL | PROFILE_SERVER))
1628 WARN("(%s,...): ignoring some flags in %lx\n", debugstr_w(name), flags);
1632 unixdir = wine_get_unix_file_name( cur_dir );
1636 WCHAR buf[MAX_PATH];
1637 if (GetCurrentDirectoryW(MAX_PATH, buf)) unixdir = wine_get_unix_file_name( buf );
1640 if (env && !(flags & CREATE_UNICODE_ENVIRONMENT)) /* convert environment to unicode */
1645 while (*p) p += strlen(p) + 1;
1646 p++; /* final null */
1647 lenW = MultiByteToWideChar( CP_ACP, 0, env, p - (char*)env, NULL, 0 );
1648 envW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) );
1649 MultiByteToWideChar( CP_ACP, 0, env, p - (char*)env, envW, lenW );
1650 flags |= CREATE_UNICODE_ENVIRONMENT;
1653 info->hThread = info->hProcess = 0;
1654 info->dwProcessId = info->dwThreadId = 0;
1656 /* Determine executable type */
1658 if (!hFile) /* builtin exe */
1660 TRACE( "starting %s as Winelib app\n", debugstr_w(name) );
1661 retv = create_process( 0, name, tidy_cmdline, envW, process_attr, thread_attr,
1662 inherit, flags, startup_info, info, unixdir );
1666 switch( MODULE_GetBinaryType( hFile ))
1669 TRACE( "starting %s as Win32 binary\n", debugstr_w(name) );
1670 retv = create_process( hFile, name, tidy_cmdline, envW, process_attr, thread_attr,
1671 inherit, flags, startup_info, info, unixdir );
1675 TRACE( "starting %s as Win16/DOS binary\n", debugstr_w(name) );
1676 retv = create_vdm_process( name, tidy_cmdline, envW, process_attr, thread_attr,
1677 inherit, flags, startup_info, info, unixdir );
1680 FIXME( "%s is OS/2 binary, not supported\n", debugstr_w(name) );
1681 SetLastError( ERROR_BAD_EXE_FORMAT );
1684 TRACE( "not starting %s since it is a dll\n", debugstr_w(name) );
1685 SetLastError( ERROR_BAD_EXE_FORMAT );
1687 case BINARY_UNIX_LIB:
1688 TRACE( "%s is a Unix library, starting as Winelib app\n", debugstr_w(name) );
1689 retv = create_process( hFile, name, tidy_cmdline, envW, process_attr, thread_attr,
1690 inherit, flags, startup_info, info, unixdir );
1692 case BINARY_UNKNOWN:
1693 /* check for .com or .bat extension */
1694 if ((p = strrchrW( name, '.' )))
1696 if (!strcmpiW( p, comW ))
1698 TRACE( "starting %s as DOS binary\n", debugstr_w(name) );
1699 retv = create_vdm_process( name, tidy_cmdline, envW, process_attr, thread_attr,
1700 inherit, flags, startup_info, info, unixdir );
1703 if (!strcmpiW( p, batW ))
1705 TRACE( "starting %s as batch binary\n", debugstr_w(name) );
1706 retv = create_cmd_process( name, tidy_cmdline, envW, process_attr, thread_attr,
1707 inherit, flags, startup_info, info, cur_dir );
1712 case BINARY_UNIX_EXE:
1714 /* unknown file, try as unix executable */
1717 TRACE( "starting %s as Unix binary\n", debugstr_w(name) );
1719 if ((unix_name = wine_get_unix_file_name( name )))
1721 retv = (fork_and_exec( unix_name, tidy_cmdline, envW, unixdir ) != -1);
1722 HeapFree( GetProcessHeap(), 0, unix_name );
1727 CloseHandle( hFile );
1730 if (tidy_cmdline != cmd_line) HeapFree( GetProcessHeap(), 0, tidy_cmdline );
1731 if (envW != env) HeapFree( GetProcessHeap(), 0, envW );
1732 if (unixdir) HeapFree( GetProcessHeap(), 0, unixdir );
1737 /***********************************************************************
1740 * Wrapper to call WaitForInputIdle USER function
1742 typedef DWORD (WINAPI *WaitForInputIdle_ptr)( HANDLE hProcess, DWORD dwTimeOut );
1744 static DWORD wait_input_idle( HANDLE process, DWORD timeout )
1746 HMODULE mod = GetModuleHandleA( "user32.dll" );
1749 WaitForInputIdle_ptr ptr = (WaitForInputIdle_ptr)GetProcAddress( mod, "WaitForInputIdle" );
1750 if (ptr) return ptr( process, timeout );
1756 /***********************************************************************
1757 * WinExec (KERNEL32.@)
1759 UINT WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow )
1761 PROCESS_INFORMATION info;
1762 STARTUPINFOA startup;
1766 memset( &startup, 0, sizeof(startup) );
1767 startup.cb = sizeof(startup);
1768 startup.dwFlags = STARTF_USESHOWWINDOW;
1769 startup.wShowWindow = nCmdShow;
1771 /* cmdline needs to be writeable for CreateProcess */
1772 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine)+1 ))) return 0;
1773 strcpy( cmdline, lpCmdLine );
1775 if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE,
1776 0, NULL, NULL, &startup, &info ))
1778 /* Give 30 seconds to the app to come up */
1779 if (wait_input_idle( info.hProcess, 30000 ) == WAIT_FAILED)
1780 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1782 /* Close off the handles */
1783 CloseHandle( info.hThread );
1784 CloseHandle( info.hProcess );
1786 else if ((ret = GetLastError()) >= 32)
1788 FIXME("Strange error set by CreateProcess: %d\n", ret );
1791 HeapFree( GetProcessHeap(), 0, cmdline );
1796 /**********************************************************************
1797 * LoadModule (KERNEL32.@)
1799 HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
1801 LOADPARMS32 *params = paramBlock;
1802 PROCESS_INFORMATION info;
1803 STARTUPINFOA startup;
1804 HINSTANCE hInstance;
1806 char filename[MAX_PATH];
1809 if (!name) return (HINSTANCE)ERROR_FILE_NOT_FOUND;
1811 if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) &&
1812 !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL ))
1813 return (HINSTANCE)GetLastError();
1815 len = (BYTE)params->lpCmdLine[0];
1816 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 )))
1817 return (HINSTANCE)ERROR_NOT_ENOUGH_MEMORY;
1819 strcpy( cmdline, filename );
1820 p = cmdline + strlen(cmdline);
1822 memcpy( p, params->lpCmdLine + 1, len );
1825 memset( &startup, 0, sizeof(startup) );
1826 startup.cb = sizeof(startup);
1827 if (params->lpCmdShow)
1829 startup.dwFlags = STARTF_USESHOWWINDOW;
1830 startup.wShowWindow = ((WORD *)params->lpCmdShow)[1];
1833 if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0,
1834 params->lpEnvAddress, NULL, &startup, &info ))
1836 /* Give 30 seconds to the app to come up */
1837 if (wait_input_idle( info.hProcess, 30000 ) == WAIT_FAILED)
1838 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1839 hInstance = (HINSTANCE)33;
1840 /* Close off the handles */
1841 CloseHandle( info.hThread );
1842 CloseHandle( info.hProcess );
1844 else if ((hInstance = (HINSTANCE)GetLastError()) >= (HINSTANCE)32)
1846 FIXME("Strange error set by CreateProcess: %p\n", hInstance );
1847 hInstance = (HINSTANCE)11;
1850 HeapFree( GetProcessHeap(), 0, cmdline );
1855 /******************************************************************************
1856 * TerminateProcess (KERNEL32.@)
1858 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
1860 NTSTATUS status = NtTerminateProcess( handle, exit_code );
1861 if (status) SetLastError( RtlNtStatusToDosError(status) );
1866 /***********************************************************************
1867 * ExitProcess (KERNEL32.@)
1869 void WINAPI ExitProcess( DWORD status )
1871 LdrShutdownProcess();
1872 SERVER_START_REQ( terminate_process )
1874 /* send the exit code to the server */
1875 req->handle = GetCurrentProcess();
1876 req->exit_code = status;
1877 wine_server_call( req );
1884 /***********************************************************************
1885 * GetExitCodeProcess [KERNEL32.@]
1887 * Gets termination status of specified process
1893 BOOL WINAPI GetExitCodeProcess(
1894 HANDLE hProcess, /* [in] handle to the process */
1895 LPDWORD lpExitCode) /* [out] address to receive termination status */
1898 SERVER_START_REQ( get_process_info )
1900 req->handle = hProcess;
1901 ret = !wine_server_call_err( req );
1902 if (ret && lpExitCode) *lpExitCode = reply->exit_code;
1909 /***********************************************************************
1910 * SetErrorMode (KERNEL32.@)
1912 UINT WINAPI SetErrorMode( UINT mode )
1914 UINT old = process_error_mode;
1915 process_error_mode = mode;
1920 /**********************************************************************
1921 * TlsAlloc [KERNEL32.@] Allocates a TLS index.
1923 * Allocates a thread local storage index
1926 * Success: TLS Index
1927 * Failure: 0xFFFFFFFF
1929 DWORD WINAPI TlsAlloc( void )
1933 RtlAcquirePebLock();
1934 index = RtlFindClearBitsAndSet( NtCurrentTeb()->Peb->TlsBitmap, 1, 0 );
1935 if (index != ~0UL) NtCurrentTeb()->TlsSlots[index] = 0; /* clear the value */
1936 else SetLastError( ERROR_NO_MORE_ITEMS );
1937 RtlReleasePebLock();
1942 /**********************************************************************
1943 * TlsFree [KERNEL32.@] Releases a TLS index.
1945 * Releases a thread local storage index, making it available for reuse
1951 BOOL WINAPI TlsFree(
1952 DWORD index) /* [in] TLS Index to free */
1956 RtlAcquirePebLock();
1957 ret = RtlAreBitsSet( NtCurrentTeb()->Peb->TlsBitmap, index, 1 );
1960 RtlClearBits( NtCurrentTeb()->Peb->TlsBitmap, index, 1 );
1961 NtSetInformationThread( GetCurrentThread(), ThreadZeroTlsCell, &index, sizeof(index) );
1963 else SetLastError( ERROR_INVALID_PARAMETER );
1964 RtlReleasePebLock();
1969 /**********************************************************************
1970 * TlsGetValue [KERNEL32.@] Gets value in a thread's TLS slot
1973 * Success: Value stored in calling thread's TLS slot for index
1974 * Failure: 0 and GetLastError returns NO_ERROR
1976 LPVOID WINAPI TlsGetValue(
1977 DWORD index) /* [in] TLS index to retrieve value for */
1979 if (index >= NtCurrentTeb()->Peb->TlsBitmap->SizeOfBitMap)
1981 SetLastError( ERROR_INVALID_PARAMETER );
1984 SetLastError( ERROR_SUCCESS );
1985 return NtCurrentTeb()->TlsSlots[index];
1989 /**********************************************************************
1990 * TlsSetValue [KERNEL32.@] Stores a value in the thread's TLS slot.
1996 BOOL WINAPI TlsSetValue(
1997 DWORD index, /* [in] TLS index to set value for */
1998 LPVOID value) /* [in] Value to be stored */
2000 if (index >= NtCurrentTeb()->Peb->TlsBitmap->SizeOfBitMap)
2002 SetLastError( ERROR_INVALID_PARAMETER );
2005 NtCurrentTeb()->TlsSlots[index] = value;
2010 /***********************************************************************
2011 * GetProcessFlags (KERNEL32.@)
2013 DWORD WINAPI GetProcessFlags( DWORD processid )
2015 IMAGE_NT_HEADERS *nt;
2018 if (processid && processid != GetCurrentProcessId()) return 0;
2020 if ((nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress )))
2022 if (nt->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
2023 flags |= PDB32_CONSOLE_PROC;
2025 if (!AreFileApisANSI()) flags |= PDB32_FILE_APIS_OEM;
2026 if (IsDebuggerPresent()) flags |= PDB32_DEBUGGED;
2031 /***********************************************************************
2032 * GetProcessDword (KERNEL.485)
2033 * GetProcessDword (KERNEL32.18)
2034 * 'Of course you cannot directly access Windows internal structures'
2036 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
2041 TRACE("(%ld, %d)\n", dwProcessID, offset );
2043 if (dwProcessID && dwProcessID != GetCurrentProcessId())
2045 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
2051 case GPD_APP_COMPAT_FLAGS:
2052 return GetAppCompatFlags16(0);
2053 case GPD_LOAD_DONE_EVENT:
2055 case GPD_HINSTANCE16:
2056 return GetTaskDS16();
2057 case GPD_WINDOWS_VERSION:
2058 return GetExeVersion16();
2060 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
2062 return (DWORD)NtCurrentTeb()->Peb;
2063 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
2064 GetStartupInfoW(&siw);
2065 return (DWORD)siw.hStdOutput;
2066 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
2067 GetStartupInfoW(&siw);
2068 return (DWORD)siw.hStdInput;
2069 case GPD_STARTF_SHOWWINDOW:
2070 GetStartupInfoW(&siw);
2071 return siw.wShowWindow;
2072 case GPD_STARTF_SIZE:
2073 GetStartupInfoW(&siw);
2075 if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
2077 if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
2078 return MAKELONG( x, y );
2079 case GPD_STARTF_POSITION:
2080 GetStartupInfoW(&siw);
2082 if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
2084 if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
2085 return MAKELONG( x, y );
2086 case GPD_STARTF_FLAGS:
2087 GetStartupInfoW(&siw);
2092 return GetProcessFlags(0);
2094 return process_dword;
2096 ERR("Unknown offset %d\n", offset );
2101 /***********************************************************************
2102 * SetProcessDword (KERNEL.484)
2103 * 'Of course you cannot directly access Windows internal structures'
2105 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
2107 TRACE("(%ld, %d)\n", dwProcessID, offset );
2109 if (dwProcessID && dwProcessID != GetCurrentProcessId())
2111 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
2117 case GPD_APP_COMPAT_FLAGS:
2118 case GPD_LOAD_DONE_EVENT:
2119 case GPD_HINSTANCE16:
2120 case GPD_WINDOWS_VERSION:
2123 case GPD_STARTF_SHELLDATA:
2124 case GPD_STARTF_HOTKEY:
2125 case GPD_STARTF_SHOWWINDOW:
2126 case GPD_STARTF_SIZE:
2127 case GPD_STARTF_POSITION:
2128 case GPD_STARTF_FLAGS:
2131 ERR("Not allowed to modify offset %d\n", offset );
2134 process_dword = value;
2137 ERR("Unknown offset %d\n", offset );
2143 /***********************************************************************
2144 * ExitProcess (KERNEL.466)
2146 void WINAPI ExitProcess16( WORD status )
2149 ReleaseThunkLock( &count );
2150 ExitProcess( status );
2154 /*********************************************************************
2155 * OpenProcess (KERNEL32.@)
2157 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
2160 SERVER_START_REQ( open_process )
2163 req->access = access;
2164 req->inherit = inherit;
2165 if (!wine_server_call_err( req )) ret = reply->handle;
2172 /*********************************************************************
2173 * MapProcessHandle (KERNEL.483)
2175 DWORD WINAPI MapProcessHandle( HANDLE handle )
2178 SERVER_START_REQ( get_process_info )
2180 req->handle = handle;
2181 if (!wine_server_call_err( req )) ret = reply->pid;
2188 /*********************************************************************
2189 * CloseW32Handle (KERNEL.474)
2190 * CloseHandle (KERNEL32.@)
2192 BOOL WINAPI CloseHandle( HANDLE handle )
2196 /* stdio handles need special treatment */
2197 if ((handle == (HANDLE)STD_INPUT_HANDLE) ||
2198 (handle == (HANDLE)STD_OUTPUT_HANDLE) ||
2199 (handle == (HANDLE)STD_ERROR_HANDLE))
2200 handle = GetStdHandle( (DWORD)handle );
2202 if (is_console_handle(handle))
2203 return CloseConsoleHandle(handle);
2205 status = NtClose( handle );
2206 if (status) SetLastError( RtlNtStatusToDosError(status) );
2211 /*********************************************************************
2212 * GetHandleInformation (KERNEL32.@)
2214 BOOL WINAPI GetHandleInformation( HANDLE handle, LPDWORD flags )
2217 SERVER_START_REQ( set_handle_info )
2219 req->handle = handle;
2223 ret = !wine_server_call_err( req );
2224 if (ret && flags) *flags = reply->old_flags;
2231 /*********************************************************************
2232 * SetHandleInformation (KERNEL32.@)
2234 BOOL WINAPI SetHandleInformation( HANDLE handle, DWORD mask, DWORD flags )
2237 SERVER_START_REQ( set_handle_info )
2239 req->handle = handle;
2243 ret = !wine_server_call_err( req );
2250 /*********************************************************************
2251 * DuplicateHandle (KERNEL32.@)
2253 BOOL WINAPI DuplicateHandle( HANDLE source_process, HANDLE source,
2254 HANDLE dest_process, HANDLE *dest,
2255 DWORD access, BOOL inherit, DWORD options )
2259 if (is_console_handle(source))
2261 /* FIXME: this test is not sufficient, we need to test process ids, not handles */
2262 if (source_process != dest_process ||
2263 source_process != GetCurrentProcess())
2265 SetLastError(ERROR_INVALID_PARAMETER);
2268 *dest = DuplicateConsoleHandle( source, access, inherit, options );
2269 return (*dest != INVALID_HANDLE_VALUE);
2271 status = NtDuplicateObject( source_process, source, dest_process, dest,
2272 access, inherit ? OBJ_INHERIT : 0, options );
2273 if (status) SetLastError( RtlNtStatusToDosError(status) );
2278 /***********************************************************************
2279 * ConvertToGlobalHandle (KERNEL.476)
2280 * ConvertToGlobalHandle (KERNEL32.@)
2282 HANDLE WINAPI ConvertToGlobalHandle(HANDLE hSrc)
2284 HANDLE ret = INVALID_HANDLE_VALUE;
2285 DuplicateHandle( GetCurrentProcess(), hSrc, GetCurrentProcess(), &ret, 0, FALSE,
2286 DUP_HANDLE_MAKE_GLOBAL | DUP_HANDLE_SAME_ACCESS | DUP_HANDLE_CLOSE_SOURCE );
2291 /***********************************************************************
2292 * SetHandleContext (KERNEL32.@)
2294 BOOL WINAPI SetHandleContext(HANDLE hnd,DWORD context)
2296 FIXME("(%p,%ld), stub. In case this got called by WSOCK32/WS2_32: "
2297 "the external WINSOCK DLLs won't work with WINE, don't use them.\n",hnd,context);
2298 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2303 /***********************************************************************
2304 * GetHandleContext (KERNEL32.@)
2306 DWORD WINAPI GetHandleContext(HANDLE hnd)
2308 FIXME("(%p), stub. In case this got called by WSOCK32/WS2_32: "
2309 "the external WINSOCK DLLs won't work with WINE, don't use them.\n",hnd);
2310 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2315 /***********************************************************************
2316 * CreateSocketHandle (KERNEL32.@)
2318 HANDLE WINAPI CreateSocketHandle(void)
2320 FIXME("(), stub. In case this got called by WSOCK32/WS2_32: "
2321 "the external WINSOCK DLLs won't work with WINE, don't use them.\n");
2322 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2323 return INVALID_HANDLE_VALUE;
2327 /***********************************************************************
2328 * SetPriorityClass (KERNEL32.@)
2330 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
2333 SERVER_START_REQ( set_process_info )
2335 req->handle = hprocess;
2336 req->priority = priorityclass;
2337 req->mask = SET_PROCESS_INFO_PRIORITY;
2338 ret = !wine_server_call_err( req );
2345 /***********************************************************************
2346 * GetPriorityClass (KERNEL32.@)
2348 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
2351 SERVER_START_REQ( get_process_info )
2353 req->handle = hprocess;
2354 if (!wine_server_call_err( req )) ret = reply->priority;
2361 /***********************************************************************
2362 * SetProcessAffinityMask (KERNEL32.@)
2364 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
2367 SERVER_START_REQ( set_process_info )
2369 req->handle = hProcess;
2370 req->affinity = affmask;
2371 req->mask = SET_PROCESS_INFO_AFFINITY;
2372 ret = !wine_server_call_err( req );
2379 /**********************************************************************
2380 * GetProcessAffinityMask (KERNEL32.@)
2382 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
2383 LPDWORD lpProcessAffinityMask,
2384 LPDWORD lpSystemAffinityMask )
2387 SERVER_START_REQ( get_process_info )
2389 req->handle = hProcess;
2390 if (!wine_server_call_err( req ))
2392 if (lpProcessAffinityMask) *lpProcessAffinityMask = reply->process_affinity;
2393 if (lpSystemAffinityMask) *lpSystemAffinityMask = reply->system_affinity;
2402 /***********************************************************************
2403 * GetProcessVersion (KERNEL32.@)
2405 DWORD WINAPI GetProcessVersion( DWORD processid )
2407 IMAGE_NT_HEADERS *nt;
2409 if (processid && processid != GetCurrentProcessId())
2411 FIXME("should use ReadProcessMemory\n");
2414 if ((nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress )))
2415 return ((nt->OptionalHeader.MajorSubsystemVersion << 16) |
2416 nt->OptionalHeader.MinorSubsystemVersion);
2421 /***********************************************************************
2422 * SetProcessWorkingSetSize [KERNEL32.@]
2423 * Sets the min/max working set sizes for a specified process.
2426 * hProcess [I] Handle to the process of interest
2427 * minset [I] Specifies minimum working set size
2428 * maxset [I] Specifies maximum working set size
2432 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess, SIZE_T minset,
2435 FIXME("(%p,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
2436 if(( minset == (SIZE_T)-1) && (maxset == (SIZE_T)-1)) {
2437 /* Trim the working set to zero */
2438 /* Swap the process out of physical RAM */
2443 /***********************************************************************
2444 * GetProcessWorkingSetSize (KERNEL32.@)
2446 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess, PSIZE_T minset,
2449 FIXME("(%p,%p,%p): stub\n",hProcess,minset,maxset);
2450 /* 32 MB working set size */
2451 if (minset) *minset = 32*1024*1024;
2452 if (maxset) *maxset = 32*1024*1024;
2457 /***********************************************************************
2458 * SetProcessShutdownParameters (KERNEL32.@)
2460 BOOL WINAPI SetProcessShutdownParameters(DWORD level, DWORD flags)
2462 FIXME("(%08lx, %08lx): partial stub.\n", level, flags);
2463 shutdown_flags = flags;
2464 shutdown_priority = level;
2469 /***********************************************************************
2470 * GetProcessShutdownParameters (KERNEL32.@)
2473 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel, LPDWORD lpdwFlags )
2475 *lpdwLevel = shutdown_priority;
2476 *lpdwFlags = shutdown_flags;
2481 /***********************************************************************
2482 * GetProcessPriorityBoost (KERNEL32.@)
2484 BOOL WINAPI GetProcessPriorityBoost(HANDLE hprocess,PBOOL pDisablePriorityBoost)
2486 FIXME("(%p,%p): semi-stub\n", hprocess, pDisablePriorityBoost);
2488 /* Report that no boost is present.. */
2489 *pDisablePriorityBoost = FALSE;
2494 /***********************************************************************
2495 * SetProcessPriorityBoost (KERNEL32.@)
2497 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
2499 FIXME("(%p,%d): stub\n",hprocess,disableboost);
2500 /* Say we can do it. I doubt the program will notice that we don't. */
2505 /***********************************************************************
2506 * ReadProcessMemory (KERNEL32.@)
2508 BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, SIZE_T size,
2509 SIZE_T *bytes_read )
2511 NTSTATUS status = NtReadVirtualMemory( process, addr, buffer, size, bytes_read );
2512 if (status) SetLastError( RtlNtStatusToDosError(status) );
2517 /***********************************************************************
2518 * WriteProcessMemory (KERNEL32.@)
2520 BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPCVOID buffer, SIZE_T size,
2521 SIZE_T *bytes_written )
2523 NTSTATUS status = NtWriteVirtualMemory( process, addr, buffer, size, bytes_written );
2524 if (status) SetLastError( RtlNtStatusToDosError(status) );
2529 /****************************************************************************
2530 * FlushInstructionCache (KERNEL32.@)
2532 BOOL WINAPI FlushInstructionCache(HANDLE hProcess, LPCVOID lpBaseAddress, SIZE_T dwSize)
2534 if (GetVersion() & 0x80000000) return TRUE; /* not NT, always TRUE */
2535 FIXME("(%p,%p,0x%08lx): stub\n",hProcess, lpBaseAddress, dwSize);
2540 /******************************************************************
2541 * GetProcessIoCounters (KERNEL32.@)
2543 BOOL WINAPI GetProcessIoCounters(HANDLE hProcess, PIO_COUNTERS ioc)
2547 status = NtQueryInformationProcess(hProcess, ProcessIoCounters,
2548 ioc, sizeof(*ioc), NULL);
2549 if (status) SetLastError( RtlNtStatusToDosError(status) );
2553 /***********************************************************************
2554 * ProcessIdToSessionId (KERNEL32.@)
2555 * This function is available on Terminal Server 4SP4 and Windows 2000
2557 BOOL WINAPI ProcessIdToSessionId( DWORD procid, DWORD *sessionid_ptr )
2559 /* According to MSDN, if the calling process is not in a terminal
2560 * services environment, then the sessionid returned is zero.
2567 /***********************************************************************
2568 * RegisterServiceProcess (KERNEL.491)
2569 * RegisterServiceProcess (KERNEL32.@)
2571 * A service process calls this function to ensure that it continues to run
2572 * even after a user logged off.
2574 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
2576 /* I don't think that Wine needs to do anything in that function */
2577 return 1; /* success */
2581 /**************************************************************************
2582 * SetFileApisToOEM (KERNEL32.@)
2584 VOID WINAPI SetFileApisToOEM(void)
2586 oem_file_apis = TRUE;
2590 /**************************************************************************
2591 * SetFileApisToANSI (KERNEL32.@)
2593 VOID WINAPI SetFileApisToANSI(void)
2595 oem_file_apis = FALSE;
2599 /******************************************************************************
2600 * AreFileApisANSI [KERNEL32.@] Determines if file functions are using ANSI
2603 * TRUE: Set of file functions is using ANSI code page
2604 * FALSE: Set of file functions is using OEM code page
2606 BOOL WINAPI AreFileApisANSI(void)
2608 return !oem_file_apis;
2612 /***********************************************************************
2613 * GetSystemMSecCount (SYSTEM.6)
2614 * GetTickCount (KERNEL32.@)
2616 * Returns the number of milliseconds, modulo 2^32, since the start
2617 * of the wineserver.
2619 DWORD WINAPI GetTickCount(void)
2622 gettimeofday( &t, NULL );
2623 return ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - server_startticks;
2627 /***********************************************************************
2628 * GetCurrentProcess (KERNEL32.@)
2630 #undef GetCurrentProcess
2631 HANDLE WINAPI GetCurrentProcess(void)
2633 return (HANDLE)0xffffffff;