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/exception.h"
41 #include "wine/server.h"
42 #include "wine/unicode.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(process);
46 WINE_DECLARE_DEBUG_CHANNEL(server);
47 WINE_DECLARE_DEBUG_CHANNEL(relay);
57 static UINT process_error_mode;
59 static HANDLE main_exe_file;
60 static DWORD shutdown_flags = 0;
61 static DWORD shutdown_priority = 0x280;
62 static DWORD process_dword;
63 static BOOL oem_file_apis;
65 static unsigned int server_startticks;
66 int main_create_flags = 0;
69 #define PDB32_DEBUGGED 0x0001 /* Process is being debugged */
70 #define PDB32_WIN16_PROC 0x0008 /* Win16 process */
71 #define PDB32_DOS_PROC 0x0010 /* Dos process */
72 #define PDB32_CONSOLE_PROC 0x0020 /* Console process */
73 #define PDB32_FILE_APIS_OEM 0x0040 /* File APIs are OEM */
74 #define PDB32_WIN32S_PROC 0x8000 /* Win32s process */
76 static const WCHAR comW[] = {'.','c','o','m',0};
77 static const WCHAR batW[] = {'.','b','a','t',0};
78 static const WCHAR winevdmW[] = {'w','i','n','e','v','d','m','.','e','x','e',0};
80 extern void SHELL_LoadRegistry(void);
81 extern void VERSION_Init( const WCHAR *appname );
82 extern void MODULE_InitLoadPath(void);
83 extern void LOCALE_Init(void);
85 /***********************************************************************
88 inline static int contains_path( LPCWSTR name )
90 return ((*name && (name[1] == ':')) || strchrW(name, '/') || strchrW(name, '\\'));
94 /***************************************************************************
97 * Get the path of a builtin module when the native file does not exist.
99 static BOOL get_builtin_path( const WCHAR *libname, const WCHAR *ext, WCHAR *filename, UINT size )
102 WCHAR sysdir[MAX_PATH];
103 UINT len = GetSystemDirectoryW( sysdir, MAX_PATH );
105 if (contains_path( libname ))
107 if (RtlGetFullPathName_U( libname, size * sizeof(WCHAR),
108 filename, &file_part ) > size * sizeof(WCHAR))
109 return FALSE; /* too long */
111 if (strncmpiW( filename, sysdir, len ) || filename[len] != '\\')
113 while (filename[len] == '\\') len++;
114 if (filename + len != file_part) return FALSE;
118 if (strlenW(libname) + len + 2 >= size) return FALSE; /* too long */
119 memcpy( filename, sysdir, len * sizeof(WCHAR) );
120 file_part = filename + len;
121 if (file_part > filename && file_part[-1] != '\\') *file_part++ = '\\';
122 strcpyW( file_part, libname );
124 if (ext && !strchrW( file_part, '.' ))
126 if (file_part + strlenW(file_part) + strlenW(ext) + 1 > filename + size)
127 return FALSE; /* too long */
128 strcatW( file_part, ext );
134 /***********************************************************************
135 * open_builtin_exe_file
137 * Open an exe file for a builtin exe.
139 static void *open_builtin_exe_file( const WCHAR *name, char *error, int error_size,
140 int test_only, int *file_exists )
142 char exename[MAX_PATH];
146 if ((p = strrchrW( name, '/' ))) name = p + 1;
147 if ((p = strrchrW( name, '\\' ))) name = p + 1;
149 /* we don't want to depend on the current codepage here */
150 len = strlenW( name ) + 1;
151 if (len >= sizeof(exename)) return NULL;
152 for (i = 0; i < len; i++)
154 if (name[i] > 127) return NULL;
155 exename[i] = (char)name[i];
156 if (exename[i] >= 'A' && exename[i] <= 'Z') exename[i] += 'a' - 'A';
158 return wine_dll_load_main_exe( exename, error, error_size, test_only, file_exists );
162 /***********************************************************************
165 * Open a specific exe file, taking load order into account.
166 * Returns the file handle or 0 for a builtin exe.
168 static HANDLE open_exe_file( const WCHAR *name )
170 enum loadorder_type loadorder[LOADORDER_NTYPES];
171 WCHAR buffer[MAX_PATH];
175 TRACE("looking for %s\n", debugstr_w(name) );
177 if ((handle = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ,
178 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
180 /* file doesn't exist, check for builtin */
181 if (!contains_path( name )) goto error;
182 if (!get_builtin_path( name, NULL, buffer, sizeof(buffer) )) goto error;
186 MODULE_GetLoadOrderW( loadorder, NULL, name );
188 for(i = 0; i < LOADORDER_NTYPES; i++)
190 if (loadorder[i] == LOADORDER_INVALID) break;
194 TRACE( "Trying native exe %s\n", debugstr_w(name) );
195 if (handle != INVALID_HANDLE_VALUE) return handle;
198 TRACE( "Trying built-in exe %s\n", debugstr_w(name) );
199 open_builtin_exe_file( name, NULL, 0, 1, &file_exists );
202 if (handle != INVALID_HANDLE_VALUE) CloseHandle(handle);
209 if (handle != INVALID_HANDLE_VALUE) CloseHandle(handle);
212 SetLastError( ERROR_FILE_NOT_FOUND );
213 return INVALID_HANDLE_VALUE;
217 /***********************************************************************
220 * Open an exe file, and return the full name and file handle.
221 * Returns FALSE if file could not be found.
222 * If file exists but cannot be opened, returns TRUE and set handle to INVALID_HANDLE_VALUE.
223 * If file is a builtin exe, returns TRUE and sets handle to 0.
225 static BOOL find_exe_file( const WCHAR *name, WCHAR *buffer, int buflen, HANDLE *handle )
227 static const WCHAR exeW[] = {'.','e','x','e',0};
229 enum loadorder_type loadorder[LOADORDER_NTYPES];
232 TRACE("looking for %s\n", debugstr_w(name) );
234 if (!SearchPathW( NULL, name, exeW, buflen, buffer, NULL ) &&
235 !get_builtin_path( name, exeW, buffer, buflen ))
237 /* no builtin found, try native without extension in case it is a Unix app */
239 if (SearchPathW( NULL, name, NULL, buflen, buffer, NULL ))
241 TRACE( "Trying native/Unix binary %s\n", debugstr_w(buffer) );
242 if ((*handle = CreateFileW( buffer, GENERIC_READ, FILE_SHARE_READ,
243 NULL, OPEN_EXISTING, 0, 0 )) != INVALID_HANDLE_VALUE)
249 MODULE_GetLoadOrderW( loadorder, NULL, buffer );
251 for(i = 0; i < LOADORDER_NTYPES; i++)
253 if (loadorder[i] == LOADORDER_INVALID) break;
257 TRACE( "Trying native exe %s\n", debugstr_w(buffer) );
258 if ((*handle = CreateFileW( buffer, GENERIC_READ, FILE_SHARE_READ,
259 NULL, OPEN_EXISTING, 0, 0 )) != INVALID_HANDLE_VALUE)
261 if (GetLastError() != ERROR_FILE_NOT_FOUND) return TRUE;
264 TRACE( "Trying built-in exe %s\n", debugstr_w(buffer) );
265 open_builtin_exe_file( buffer, NULL, 0, 1, &file_exists );
276 SetLastError( ERROR_FILE_NOT_FOUND );
281 /**********************************************************************
284 * Load a PE format EXE file.
286 static HMODULE load_pe_exe( const WCHAR *name, HANDLE file )
288 IMAGE_NT_HEADERS *nt;
291 OBJECT_ATTRIBUTES attr;
296 attr.Length = sizeof(attr);
297 attr.RootDirectory = 0;
298 attr.ObjectName = NULL;
300 attr.SecurityDescriptor = NULL;
301 attr.SecurityQualityOfService = NULL;
304 if (NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
305 &attr, &size, 0, SEC_IMAGE, file ) != STATUS_SUCCESS)
309 if (NtMapViewOfSection( mapping, GetCurrentProcess(), &module, 0, 0, &size, &len,
310 ViewShare, 0, PAGE_READONLY ) != STATUS_SUCCESS)
316 nt = RtlImageNtHeader( module );
317 if (nt->OptionalHeader.AddressOfEntryPoint)
319 if (!RtlImageRvaToSection( nt, module, nt->OptionalHeader.AddressOfEntryPoint ))
320 MESSAGE("VIRUS WARNING: PE module %s has an invalid entrypoint (0x%08lx) "
321 "outside all sections (possibly infected by Tchernobyl/SpaceFiller virus)!\n",
322 debugstr_w(name), nt->OptionalHeader.AddressOfEntryPoint );
325 drive_type = GetDriveTypeW( name );
326 /* don't keep the file handle open on removable media */
327 if (drive_type == DRIVE_REMOVABLE || drive_type == DRIVE_CDROM)
329 CloseHandle( main_exe_file );
336 /***********************************************************************
337 * build_initial_environment
339 * Build the Win32 environment from the Unix environment
341 static BOOL build_initial_environment( char **environ )
348 /* Compute the total size of the Unix environment */
349 for (e = environ; *e; e++)
351 if (!memcmp(*e, "PATH=", 5)) continue;
352 size += MultiByteToWideChar( CP_UNIXCP, 0, *e, -1, NULL, 0 );
354 size *= sizeof(WCHAR);
356 /* Now allocate the environment */
357 if (NtAllocateVirtualMemory(NtCurrentProcess(), &ptr, 0, &size,
358 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) != STATUS_SUCCESS)
361 NtCurrentTeb()->Peb->ProcessParameters->Environment = p = ptr;
362 endptr = p + size / sizeof(WCHAR);
364 /* And fill it with the Unix environment */
365 for (e = environ; *e; e++)
368 /* skip Unix PATH and store WINEPATH as PATH */
369 if (!memcmp(str, "PATH=", 5)) continue;
370 if (!memcmp(str, "WINEPATH=", 9 )) str += 4;
371 MultiByteToWideChar( CP_UNIXCP, 0, str, -1, p, endptr - p );
379 /***********************************************************************
382 * Set the Wine library Unicode argv global variables.
384 static void set_library_wargv( char **argv )
391 for (argc = 0; argv[argc]; argc++)
392 total += MultiByteToWideChar( CP_UNIXCP, 0, argv[argc], -1, NULL, 0 );
394 wargv = RtlAllocateHeap( GetProcessHeap(), 0,
395 total * sizeof(WCHAR) + (argc + 1) * sizeof(*wargv) );
396 p = (WCHAR *)(wargv + argc + 1);
397 for (argc = 0; argv[argc]; argc++)
399 DWORD reslen = MultiByteToWideChar( CP_UNIXCP, 0, argv[argc], -1, p, total );
405 __wine_main_wargv = wargv;
409 /***********************************************************************
412 * Build the command line of a process from the argv array.
414 * Note that it does NOT necessarily include the file name.
415 * Sometimes we don't even have any command line options at all.
417 * We must quote and escape characters so that the argv array can be rebuilt
418 * from the command line:
419 * - spaces and tabs must be quoted
421 * - quotes must be escaped
423 * - if '\'s are followed by a '"', they must be doubled and followed by '\"',
424 * resulting in an odd number of '\' followed by a '"'
427 * - '\'s that are not followed by a '"' can be left as is
431 static BOOL build_command_line( WCHAR **argv )
436 RTL_USER_PROCESS_PARAMETERS* rupp = NtCurrentTeb()->Peb->ProcessParameters;
438 if (rupp->CommandLine.Buffer) return TRUE; /* already got it from the server */
441 for (arg = argv; *arg; arg++)
443 int has_space,bcount;
449 if( !*a ) has_space=1;
454 if (*a==' ' || *a=='\t') {
456 } else if (*a=='"') {
457 /* doubling of '\' preceeding a '"',
458 * plus escaping of said '"'
466 len+=(a-*arg)+1 /* for the separating space */;
468 len+=2; /* for the quotes */
471 if (!(rupp->CommandLine.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR))))
474 p = rupp->CommandLine.Buffer;
475 rupp->CommandLine.Length = (len - 1) * sizeof(WCHAR);
476 rupp->CommandLine.MaximumLength = len * sizeof(WCHAR);
477 for (arg = argv; *arg; arg++)
479 int has_space,has_quote;
482 /* Check for quotes and spaces in this argument */
483 has_space=has_quote=0;
485 if( !*a ) has_space=1;
487 if (*a==' ' || *a=='\t') {
491 } else if (*a=='"') {
499 /* Now transfer it to the command line */
516 /* Double all the '\\' preceeding this '"', plus one */
517 for (i=0;i<=bcount;i++)
529 while ((*p=*x++)) p++;
535 if (p > rupp->CommandLine.Buffer)
536 p--; /* remove last space */
543 /* make sure the unicode string doesn't point beyond the end pointer */
544 static inline void fix_unicode_string( UNICODE_STRING *str, char *end_ptr )
546 if ((char *)str->Buffer >= end_ptr)
548 str->Length = str->MaximumLength = 0;
552 if ((char *)str->Buffer + str->MaximumLength > end_ptr)
554 str->MaximumLength = (end_ptr - (char *)str->Buffer) & ~(sizeof(WCHAR) - 1);
556 if (str->Length >= str->MaximumLength)
558 if (str->MaximumLength >= sizeof(WCHAR))
559 str->Length = str->MaximumLength - sizeof(WCHAR);
561 str->Length = str->MaximumLength = 0;
566 /***********************************************************************
567 * init_user_process_params
569 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
571 static RTL_USER_PROCESS_PARAMETERS *init_user_process_params( size_t info_size )
576 RTL_USER_PROCESS_PARAMETERS *params;
579 if ((status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, NULL, &size,
580 MEM_COMMIT, PAGE_READWRITE )) != STATUS_SUCCESS)
583 SERVER_START_REQ( get_startup_info )
585 wine_server_set_reply( req, ptr, info_size );
586 wine_server_call( req );
587 info_size = wine_server_reply_size( reply );
592 params->Size = info_size;
593 params->AllocationSize = size;
595 /* make sure the strings are valid */
596 fix_unicode_string( ¶ms->CurrentDirectoryName, (char *)info_size );
597 fix_unicode_string( ¶ms->DllPath, (char *)info_size );
598 fix_unicode_string( ¶ms->ImagePathName, (char *)info_size );
599 fix_unicode_string( ¶ms->CommandLine, (char *)info_size );
600 fix_unicode_string( ¶ms->WindowTitle, (char *)info_size );
601 fix_unicode_string( ¶ms->Desktop, (char *)info_size );
602 fix_unicode_string( ¶ms->ShellInfo, (char *)info_size );
603 fix_unicode_string( ¶ms->RuntimeInfo, (char *)info_size );
605 return RtlNormalizeProcessParams( params );
609 /***********************************************************************
612 * Main process initialisation code
614 static BOOL process_init( char *argv[], char **environ )
617 size_t info_size = 0;
618 RTL_USER_PROCESS_PARAMETERS *params;
619 PEB *peb = NtCurrentTeb()->Peb;
620 HANDLE hstdin, hstdout, hstderr;
621 extern void __wine_dbg_kernel32_init(void);
625 __wine_dbg_kernel32_init(); /* hack: register debug channels early */
629 setlocale(LC_CTYPE,"");
631 /* Retrieve startup info from the server */
632 SERVER_START_REQ( init_process )
635 req->ldt_copy = &wine_ldt_copy;
636 if ((ret = !wine_server_call_err( req )))
638 main_exe_file = reply->exe_file;
639 main_create_flags = reply->create_flags;
640 info_size = reply->info_size;
641 server_startticks = reply->server_start;
642 hstdin = reply->hstdin;
643 hstdout = reply->hstdout;
644 hstderr = reply->hstderr;
648 if (!ret) return FALSE;
652 params = peb->ProcessParameters;
654 /* This is wine specific: we have no parent (we're started from unix)
655 * so, create a simple console with bare handles to unix stdio
656 * input & output streams (aka simple console)
658 wine_server_fd_to_handle( 0, GENERIC_READ|SYNCHRONIZE, TRUE, ¶ms->hStdInput );
659 wine_server_fd_to_handle( 1, GENERIC_WRITE|SYNCHRONIZE, TRUE, ¶ms->hStdOutput );
660 wine_server_fd_to_handle( 2, GENERIC_WRITE|SYNCHRONIZE, TRUE, ¶ms->hStdError );
662 /* <hack: to be changed later on> */
663 params->CurrentDirectoryName.Length = 3 * sizeof(WCHAR);
664 params->CurrentDirectoryName.MaximumLength = RtlGetLongestNtPathLength() * sizeof(WCHAR);
665 params->CurrentDirectoryName.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, params->CurrentDirectoryName.MaximumLength);
666 params->CurrentDirectoryName.Buffer[0] = 'C';
667 params->CurrentDirectoryName.Buffer[1] = ':';
668 params->CurrentDirectoryName.Buffer[2] = '\\';
669 params->CurrentDirectoryName.Buffer[3] = '\0';
670 /* </hack: to be changed later on> */
674 if (!(params = init_user_process_params( info_size ))) return FALSE;
675 peb->ProcessParameters = params;
677 /* convert value from server:
678 * + 0 => INVALID_HANDLE_VALUE
679 * + console handle need to be mapped
682 hstdin = INVALID_HANDLE_VALUE;
683 else if (VerifyConsoleIoHandle(console_handle_map(hstdin)))
684 hstdin = console_handle_map(hstdin);
687 hstdout = INVALID_HANDLE_VALUE;
688 else if (VerifyConsoleIoHandle(console_handle_map(hstdout)))
689 hstdout = console_handle_map(hstdout);
692 hstderr = INVALID_HANDLE_VALUE;
693 else if (VerifyConsoleIoHandle(console_handle_map(hstderr)))
694 hstderr = console_handle_map(hstderr);
696 params->hStdInput = hstdin;
697 params->hStdOutput = hstdout;
698 params->hStdError = hstderr;
703 /* Copy the parent environment */
704 if (!build_initial_environment( environ )) return FALSE;
706 /* Parse command line arguments */
707 OPTIONS_ParseOptions( !info_size ? argv : NULL );
709 /* initialise DOS drives */
710 if (!DRIVE_Init()) return FALSE;
712 /* initialise DOS directories */
713 if (!DIR_Init()) return FALSE;
715 /* registry initialisation */
716 SHELL_LoadRegistry();
718 /* global boot finished, the rest is process-local */
719 SERVER_START_REQ( boot_done )
721 req->debug_level = TRACE_ON(server);
722 wine_server_call( req );
730 /***********************************************************************
733 * Startup routine of a new process. Runs on the new process stack.
735 static void start_process( void *arg )
739 PEB *peb = NtCurrentTeb()->Peb;
740 IMAGE_NT_HEADERS *nt;
741 LPTHREAD_START_ROUTINE entry;
743 LdrInitializeThunk( main_exe_file, 0, 0, 0 );
745 nt = RtlImageNtHeader( peb->ImageBaseAddress );
746 entry = (LPTHREAD_START_ROUTINE)((char *)peb->ImageBaseAddress +
747 nt->OptionalHeader.AddressOfEntryPoint);
750 DPRINTF( "%04lx:Starting process %s (entryproc=%p)\n", GetCurrentThreadId(),
751 debugstr_w(peb->ProcessParameters->ImagePathName.Buffer), entry );
753 SetLastError( 0 ); /* clear error code */
754 if (peb->BeingDebugged) DbgBreakPoint();
755 ExitProcess( entry( peb ) );
757 __EXCEPT(UnhandledExceptionFilter)
759 TerminateThread( GetCurrentThread(), GetExceptionCode() );
765 /***********************************************************************
768 * Wine initialisation: load and start the main exe file.
770 void __wine_kernel_init(void)
772 WCHAR *main_exe_name, *p;
774 DWORD stack_size = 0;
776 PEB *peb = NtCurrentTeb()->Peb;
778 /* Initialize everything */
779 if (!process_init( __wine_main_argv, __wine_main_environ )) exit(1);
780 /* update argc in case options have been removed */
781 for (__wine_main_argc = 0; __wine_main_argv[__wine_main_argc]; __wine_main_argc++) /*nothing*/;
783 __wine_main_argv++; /* remove argv[0] (wine itself) */
786 if (!(main_exe_name = peb->ProcessParameters->ImagePathName.Buffer))
788 WCHAR buffer[MAX_PATH];
789 WCHAR exe_nameW[MAX_PATH];
791 if (!__wine_main_argv[0]) OPTIONS_Usage();
793 MultiByteToWideChar( CP_UNIXCP, 0, __wine_main_argv[0], -1, exe_nameW, MAX_PATH );
794 if (!find_exe_file( exe_nameW, buffer, MAX_PATH, &main_exe_file ))
796 MESSAGE( "wine: cannot find '%s'\n", __wine_main_argv[0] );
799 if (main_exe_file == INVALID_HANDLE_VALUE)
801 MESSAGE( "wine: cannot open %s\n", debugstr_w(main_exe_name) );
804 RtlCreateUnicodeString( &peb->ProcessParameters->ImagePathName, buffer );
805 main_exe_name = peb->ProcessParameters->ImagePathName.Buffer;
808 TRACE( "starting process name=%s file=%p argv[0]=%s\n",
809 debugstr_w(main_exe_name), main_exe_file, debugstr_a(__wine_main_argv[0]) );
811 MODULE_InitLoadPath();
812 VERSION_Init( main_exe_name );
814 if (!main_exe_file) /* no file handle -> Winelib app */
816 TRACE( "starting Winelib app %s\n", debugstr_w(main_exe_name) );
817 if (open_builtin_exe_file( main_exe_name, error, sizeof(error), 0, &file_exists ))
819 MESSAGE( "wine: cannot open builtin library for %s: %s\n",
820 debugstr_w(main_exe_name), error );
824 switch( MODULE_GetBinaryType( main_exe_file ))
827 TRACE( "starting Win32 binary %s\n", debugstr_w(main_exe_name) );
828 if ((peb->ImageBaseAddress = load_pe_exe( main_exe_name, main_exe_file )))
830 MESSAGE( "wine: could not load %s as Win32 binary\n", debugstr_w(main_exe_name) );
833 MESSAGE( "wine: %s is a DLL, not an executable\n", debugstr_w(main_exe_name) );
836 /* check for .com extension */
837 if (!(p = strrchrW( main_exe_name, '.' )) || strcmpiW( p, comW ))
839 MESSAGE( "wine: cannot determine executable type for %s\n",
840 debugstr_w(main_exe_name) );
846 TRACE( "starting Win16/DOS binary %s\n", debugstr_w(main_exe_name) );
847 CloseHandle( main_exe_file );
851 __wine_main_argv[0] = "winevdm.exe";
852 if (open_builtin_exe_file( winevdmW, error, sizeof(error), 0, &file_exists ))
854 MESSAGE( "wine: trying to run %s, cannot open builtin library for 'winevdm.exe': %s\n",
855 debugstr_w(main_exe_name), error );
858 MESSAGE( "wine: %s is an OS/2 binary, not supported\n", debugstr_w(main_exe_name) );
860 case BINARY_UNIX_EXE:
861 MESSAGE( "wine: %s is a Unix binary, not supported\n", debugstr_w(main_exe_name) );
863 case BINARY_UNIX_LIB:
865 DOS_FULL_NAME full_name;
867 TRACE( "starting Winelib app %s\n", debugstr_w(main_exe_name) );
868 CloseHandle( main_exe_file );
870 if (DOSFS_GetFullName( main_exe_name, TRUE, &full_name ) &&
871 wine_dlopen( full_name.long_name, RTLD_NOW, error, sizeof(error) ))
873 static const WCHAR soW[] = {'.','s','o',0};
874 if ((p = strrchrW( main_exe_name, '.' )) && !strcmpW( p, soW ))
877 /* update the unicode string */
878 RtlInitUnicodeString( &peb->ProcessParameters->ImagePathName, main_exe_name );
882 MESSAGE( "wine: could not load %s: %s\n", debugstr_w(main_exe_name), error );
888 wine_free_pe_load_area(); /* the main binary is loaded, we don't need this anymore */
890 /* build command line */
891 set_library_wargv( __wine_main_argv );
892 if (!build_command_line( __wine_main_wargv )) goto error;
894 stack_size = RtlImageNtHeader(peb->ImageBaseAddress)->OptionalHeader.SizeOfStackReserve;
896 /* allocate main thread stack */
897 if (!THREAD_InitStack( NtCurrentTeb(), stack_size )) goto error;
899 /* switch to the new stack */
900 wine_switch_to_stack( start_process, NULL, NtCurrentTeb()->Tib.StackBase );
903 ExitProcess( GetLastError() );
907 /***********************************************************************
910 * Build an argv array from a command-line.
911 * 'reserved' is the number of args to reserve before the first one.
913 static char **build_argv( const WCHAR *cmdlineW, int reserved )
917 char *arg,*s,*d,*cmdline;
918 int in_quotes,bcount,len;
920 len = WideCharToMultiByte( CP_UNIXCP, 0, cmdlineW, -1, NULL, 0, NULL, NULL );
921 if (!(cmdline = malloc(len))) return NULL;
922 WideCharToMultiByte( CP_UNIXCP, 0, cmdlineW, -1, cmdline, len, NULL, NULL );
929 if (*s=='\0' || ((*s==' ' || *s=='\t') && !in_quotes)) {
932 /* skip the remaining spaces */
933 while (*s==' ' || *s=='\t') {
940 } else if (*s=='\\') {
941 /* '\', count them */
943 } else if ((*s=='"') && ((bcount & 1)==0)) {
945 in_quotes=!in_quotes;
948 /* a regular character */
953 argv=malloc(argc*sizeof(*argv));
962 if ((*s==' ' || *s=='\t') && !in_quotes) {
963 /* Close the argument and copy it */
967 /* skip the remaining spaces */
970 } while (*s==' ' || *s=='\t');
972 /* Start with a new argument */
975 } else if (*s=='\\') {
979 } else if (*s=='"') {
981 if ((bcount & 1)==0) {
982 /* Preceeded by an even number of '\', this is half that
983 * number of '\', plus a '"' which we discard.
987 in_quotes=!in_quotes;
989 /* Preceeded by an odd number of '\', this is half that
990 * number of '\' followed by a '"'
998 /* a regular character */
1013 /***********************************************************************
1016 * Allocate an environment string; helper for build_envp
1018 static char *alloc_env_string( const char *name, const char *value )
1020 char *ret = malloc( strlen(name) + strlen(value) + 1 );
1021 strcpy( ret, name );
1022 strcat( ret, value );
1026 /***********************************************************************
1029 * Build the environment of a new child process.
1031 static char **build_envp( const WCHAR *envW, const WCHAR *extra_envW )
1035 char *env, *extra_env = NULL;
1036 int count = 0, length;
1040 for (p = extra_envW; *p; count++) p += strlenW(p) + 1;
1042 length = WideCharToMultiByte( CP_UNIXCP, 0, extra_envW, p - extra_envW,
1043 NULL, 0, NULL, NULL );
1044 if ((extra_env = malloc( length )))
1045 WideCharToMultiByte( CP_UNIXCP, 0, extra_envW, p - extra_envW,
1046 extra_env, length, NULL, NULL );
1048 for (p = envW; *p; count++) p += strlenW(p) + 1;
1050 length = WideCharToMultiByte( CP_UNIXCP, 0, envW, p - envW, NULL, 0, NULL, NULL );
1051 if (!(env = malloc( length ))) return NULL;
1052 WideCharToMultiByte( CP_UNIXCP, 0, envW, p - envW, env, length, NULL, NULL );
1056 if ((envp = malloc( count * sizeof(*envp) )))
1058 char **envptr = envp;
1061 /* first the extra strings */
1062 if (extra_env) for (p = extra_env; *p; p += strlen(p) + 1) *envptr++ = p;
1063 /* then put PATH, HOME and WINEPREFIX from the unix env */
1064 if ((p = getenv("PATH"))) *envptr++ = alloc_env_string( "PATH=", p );
1065 if ((p = getenv("HOME"))) *envptr++ = alloc_env_string( "HOME=", p );
1066 if ((p = getenv("WINEPREFIX"))) *envptr++ = alloc_env_string( "WINEPREFIX=", p );
1067 /* now put the Windows environment strings */
1068 for (p = env; *p; p += strlen(p) + 1)
1070 if (extra_env && p[0]=='=' && 'A'<=p[1] && p[1]<='Z' && p[2]==':' && p[3]=='=')
1071 continue; /* skipped */
1072 if (!memcmp( p, "PATH=", 5 )) /* store PATH as WINEPATH */
1073 *envptr++ = alloc_env_string( "WINEPATH=", p + 5 );
1074 else if (memcmp( p, "HOME=", 5 ) &&
1075 memcmp( p, "WINEPATH=", 9 ) &&
1076 memcmp( p, "WINEPREFIX=", 11 )) *envptr++ = p;
1084 /***********************************************************************
1087 * Fork and exec a new Unix binary, checking for errors.
1089 static int fork_and_exec( const char *filename, const WCHAR *cmdline,
1090 const WCHAR *env, const char *newdir )
1095 if (!env) env = GetEnvironmentStringsW();
1102 fcntl( fd[1], F_SETFD, 1 ); /* set close on exec */
1103 if (!(pid = fork())) /* child */
1105 char **argv = build_argv( cmdline, 0 );
1106 char **envp = build_envp( env, NULL );
1109 /* Reset signals that we previously set to SIG_IGN */
1110 signal( SIGPIPE, SIG_DFL );
1111 signal( SIGCHLD, SIG_DFL );
1113 if (newdir) chdir(newdir);
1115 if (argv && envp) execve( filename, argv, envp );
1117 write( fd[1], &err, sizeof(err) );
1121 if ((pid != -1) && (read( fd[0], &err, sizeof(err) ) > 0)) /* exec failed */
1126 if (pid == -1) FILE_SetDosError();
1132 /***********************************************************************
1133 * create_user_params
1135 static RTL_USER_PROCESS_PARAMETERS *create_user_params( LPCWSTR filename, LPCWSTR cmdline,
1136 const STARTUPINFOW *startup )
1138 RTL_USER_PROCESS_PARAMETERS *params;
1139 UNICODE_STRING image_str, cmdline_str, desktop, title;
1141 WCHAR buffer[MAX_PATH];
1143 if(!GetLongPathNameW( filename, buffer, MAX_PATH ))
1144 lstrcpynW( buffer, filename, MAX_PATH );
1145 if(!GetFullPathNameW( buffer, MAX_PATH, buffer, NULL ))
1146 lstrcpynW( buffer, filename, MAX_PATH );
1147 RtlInitUnicodeString( &image_str, buffer );
1149 RtlInitUnicodeString( &cmdline_str, cmdline );
1150 if (startup->lpDesktop) RtlInitUnicodeString( &desktop, startup->lpDesktop );
1151 if (startup->lpTitle) RtlInitUnicodeString( &title, startup->lpTitle );
1153 status = RtlCreateProcessParameters( ¶ms, &image_str, NULL, NULL, &cmdline_str, NULL,
1154 startup->lpTitle ? &title : NULL,
1155 startup->lpDesktop ? &desktop : NULL,
1157 if (status != STATUS_SUCCESS)
1159 SetLastError( RtlNtStatusToDosError(status) );
1163 params->Environment = NULL; /* we pass it through the Unix environment */
1164 params->hStdInput = startup->hStdInput;
1165 params->hStdOutput = startup->hStdOutput;
1166 params->hStdError = startup->hStdError;
1167 params->dwX = startup->dwX;
1168 params->dwY = startup->dwY;
1169 params->dwXSize = startup->dwXSize;
1170 params->dwYSize = startup->dwYSize;
1171 params->dwXCountChars = startup->dwXCountChars;
1172 params->dwYCountChars = startup->dwYCountChars;
1173 params->dwFillAttribute = startup->dwFillAttribute;
1174 params->dwFlags = startup->dwFlags;
1175 params->wShowWindow = startup->wShowWindow;
1180 /***********************************************************************
1183 * Create a new process. If hFile is a valid handle we have an exe
1184 * file, otherwise it is a Winelib app.
1186 static BOOL create_process( HANDLE hFile, LPCWSTR filename, LPWSTR cmd_line, LPWSTR env,
1187 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1188 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1189 LPPROCESS_INFORMATION info, LPCSTR unixdir )
1191 BOOL ret, success = FALSE;
1192 HANDLE process_info;
1193 RTL_USER_PROCESS_PARAMETERS *params;
1194 WCHAR *extra_env = NULL;
1203 env = GetEnvironmentStringsW();
1204 extra_env = DRIVE_BuildEnv();
1207 if (!(params = create_user_params( filename, cmd_line, startup )))
1209 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1213 /* create the synchronization pipes */
1215 if (pipe( startfd ) == -1)
1218 RtlDestroyProcessParameters( params );
1219 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1222 if (pipe( execfd ) == -1)
1225 close( startfd[0] );
1226 close( startfd[1] );
1227 RtlDestroyProcessParameters( params );
1228 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1231 fcntl( execfd[1], F_SETFD, 1 ); /* set close on exec */
1233 /* create the child process */
1235 if (!(pid = fork())) /* child */
1237 char **argv = build_argv( cmd_line, 1 );
1238 char **envp = build_envp( env, extra_env );
1240 close( startfd[1] );
1243 /* wait for parent to tell us to start */
1244 if (read( startfd[0], &dummy, 1 ) != 1) _exit(1);
1246 close( startfd[0] );
1247 /* Reset signals that we previously set to SIG_IGN */
1248 signal( SIGPIPE, SIG_DFL );
1249 signal( SIGCHLD, SIG_DFL );
1251 if (unixdir) chdir(unixdir);
1255 /* first, try for a WINELOADER environment variable */
1256 argv[0] = getenv("WINELOADER");
1257 if (argv[0]) execve( argv[0], argv, envp );
1258 /* now use the standard search strategy */
1259 wine_exec_wine_binary( NULL, argv, envp );
1262 write( execfd[1], &err, sizeof(err) );
1266 /* this is the parent */
1268 close( startfd[0] );
1270 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
1273 close( startfd[1] );
1276 RtlDestroyProcessParameters( params );
1280 /* create the process on the server side */
1282 SERVER_START_REQ( new_process )
1284 req->inherit_all = inherit;
1285 req->create_flags = flags;
1286 req->unix_pid = pid;
1287 req->exe_file = hFile;
1288 if (startup->dwFlags & STARTF_USESTDHANDLES)
1290 req->hstdin = startup->hStdInput;
1291 req->hstdout = startup->hStdOutput;
1292 req->hstderr = startup->hStdError;
1296 req->hstdin = GetStdHandle( STD_INPUT_HANDLE );
1297 req->hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
1298 req->hstderr = GetStdHandle( STD_ERROR_HANDLE );
1301 if ((flags & (CREATE_NEW_CONSOLE | DETACHED_PROCESS)) != 0)
1303 /* this is temporary (for console handles). We have no way to control that the handle is invalid in child process otherwise */
1304 if (is_console_handle(req->hstdin)) req->hstdin = INVALID_HANDLE_VALUE;
1305 if (is_console_handle(req->hstdout)) req->hstdout = INVALID_HANDLE_VALUE;
1306 if (is_console_handle(req->hstderr)) req->hstderr = INVALID_HANDLE_VALUE;
1310 if (is_console_handle(req->hstdin)) req->hstdin = console_handle_unmap(req->hstdin);
1311 if (is_console_handle(req->hstdout)) req->hstdout = console_handle_unmap(req->hstdout);
1312 if (is_console_handle(req->hstderr)) req->hstderr = console_handle_unmap(req->hstderr);
1315 wine_server_add_data( req, params, params->Size );
1316 ret = !wine_server_call_err( req );
1317 process_info = reply->info;
1321 RtlDestroyProcessParameters( params );
1324 close( startfd[1] );
1329 /* tell child to start and wait for it to exec */
1331 write( startfd[1], &dummy, 1 );
1332 close( startfd[1] );
1334 if (read( execfd[0], &err, sizeof(err) ) > 0) /* exec failed */
1339 CloseHandle( process_info );
1344 /* wait for the new process info to be ready */
1346 WaitForSingleObject( process_info, INFINITE );
1347 SERVER_START_REQ( get_new_process_info )
1349 req->info = process_info;
1350 req->pinherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
1351 req->tinherit = (tsa && (tsa->nLength >= sizeof(*tsa)) && tsa->bInheritHandle);
1352 if ((ret = !wine_server_call_err( req )))
1354 info->dwProcessId = (DWORD)reply->pid;
1355 info->dwThreadId = (DWORD)reply->tid;
1356 info->hProcess = reply->phandle;
1357 info->hThread = reply->thandle;
1358 success = reply->success;
1363 if (ret && !success) /* new process failed to start */
1366 if (GetExitCodeProcess( info->hProcess, &exitcode )) SetLastError( exitcode );
1367 CloseHandle( info->hThread );
1368 CloseHandle( info->hProcess );
1371 CloseHandle( process_info );
1376 /***********************************************************************
1377 * create_vdm_process
1379 * Create a new VDM process for a 16-bit or DOS application.
1381 static BOOL create_vdm_process( LPCWSTR filename, LPWSTR cmd_line, LPWSTR env,
1382 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1383 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1384 LPPROCESS_INFORMATION info, LPCSTR unixdir )
1386 static const WCHAR argsW[] = {'%','s',' ','-','-','a','p','p','-','n','a','m','e',' ','"','%','s','"',' ','%','s',0};
1389 LPWSTR new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
1390 (strlenW(filename) + strlenW(cmd_line) + 30) * sizeof(WCHAR) );
1394 SetLastError( ERROR_OUTOFMEMORY );
1397 sprintfW( new_cmd_line, argsW, winevdmW, filename, cmd_line );
1398 ret = create_process( 0, winevdmW, new_cmd_line, env, psa, tsa, inherit,
1399 flags, startup, info, unixdir );
1400 HeapFree( GetProcessHeap(), 0, new_cmd_line );
1405 /***********************************************************************
1406 * create_cmd_process
1408 * Create a new cmd shell process for a .BAT file.
1410 static BOOL create_cmd_process( LPCWSTR filename, LPWSTR cmd_line, LPVOID env,
1411 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1412 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1413 LPPROCESS_INFORMATION info, LPCWSTR cur_dir )
1416 static const WCHAR comspecW[] = {'C','O','M','S','P','E','C',0};
1417 static const WCHAR slashcW[] = {' ','/','c',' ',0};
1418 WCHAR comspec[MAX_PATH];
1422 if (!GetEnvironmentVariableW( comspecW, comspec, sizeof(comspec)/sizeof(WCHAR) ))
1424 if (!(newcmdline = HeapAlloc( GetProcessHeap(), 0,
1425 (strlenW(comspec) + 4 + strlenW(cmd_line) + 1) * sizeof(WCHAR))))
1428 strcpyW( newcmdline, comspec );
1429 strcatW( newcmdline, slashcW );
1430 strcatW( newcmdline, cmd_line );
1431 ret = CreateProcessW( comspec, newcmdline, psa, tsa, inherit,
1432 flags, env, cur_dir, startup, info );
1433 HeapFree( GetProcessHeap(), 0, newcmdline );
1438 /*************************************************************************
1441 * Helper for CreateProcess: retrieve the file name to load from the
1442 * app name and command line. Store the file name in buffer, and
1443 * return a possibly modified command line.
1444 * Also returns a handle to the opened file if it's a Windows binary.
1446 static LPWSTR get_file_name( LPCWSTR appname, LPWSTR cmdline, LPWSTR buffer,
1447 int buflen, HANDLE *handle )
1449 static const WCHAR quotesW[] = {'"','%','s','"',0};
1451 WCHAR *name, *pos, *ret = NULL;
1454 /* if we have an app name, everything is easy */
1458 /* use the unmodified app name as file name */
1459 lstrcpynW( buffer, appname, buflen );
1460 *handle = open_exe_file( buffer );
1461 if (!(ret = cmdline) || !cmdline[0])
1463 /* no command-line, create one */
1464 if ((ret = HeapAlloc( GetProcessHeap(), 0, (strlenW(appname) + 3) * sizeof(WCHAR) )))
1465 sprintfW( ret, quotesW, appname );
1472 SetLastError( ERROR_INVALID_PARAMETER );
1476 /* first check for a quoted file name */
1478 if ((cmdline[0] == '"') && ((p = strchrW( cmdline + 1, '"' ))))
1480 int len = p - cmdline - 1;
1481 /* extract the quoted portion as file name */
1482 if (!(name = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return NULL;
1483 memcpy( name, cmdline + 1, len * sizeof(WCHAR) );
1486 if (find_exe_file( name, buffer, buflen, handle ))
1487 ret = cmdline; /* no change necessary */
1491 /* now try the command-line word by word */
1493 if (!(name = HeapAlloc( GetProcessHeap(), 0, (strlenW(cmdline) + 1) * sizeof(WCHAR) )))
1500 do *pos++ = *p++; while (*p && *p != ' ');
1502 if (find_exe_file( name, buffer, buflen, handle ))
1509 if (!ret || !strchrW( name, ' ' )) goto done; /* no change necessary */
1511 /* now build a new command-line with quotes */
1513 if (!(ret = HeapAlloc( GetProcessHeap(), 0, (strlenW(cmdline) + 3) * sizeof(WCHAR) )))
1515 sprintfW( ret, quotesW, name );
1519 HeapFree( GetProcessHeap(), 0, name );
1524 /**********************************************************************
1525 * CreateProcessA (KERNEL32.@)
1527 BOOL WINAPI CreateProcessA( LPCSTR app_name, LPSTR cmd_line, LPSECURITY_ATTRIBUTES process_attr,
1528 LPSECURITY_ATTRIBUTES thread_attr, BOOL inherit,
1529 DWORD flags, LPVOID env, LPCSTR cur_dir,
1530 LPSTARTUPINFOA startup_info, LPPROCESS_INFORMATION info )
1533 UNICODE_STRING app_nameW, cmd_lineW, cur_dirW, desktopW, titleW;
1536 if (app_name) RtlCreateUnicodeStringFromAsciiz( &app_nameW, app_name );
1537 else app_nameW.Buffer = NULL;
1538 if (cmd_line) RtlCreateUnicodeStringFromAsciiz( &cmd_lineW, cmd_line );
1539 else cmd_lineW.Buffer = NULL;
1540 if (cur_dir) RtlCreateUnicodeStringFromAsciiz( &cur_dirW, cur_dir );
1541 else cur_dirW.Buffer = NULL;
1542 if (startup_info->lpDesktop) RtlCreateUnicodeStringFromAsciiz( &desktopW, startup_info->lpDesktop );
1543 else desktopW.Buffer = NULL;
1544 if (startup_info->lpTitle) RtlCreateUnicodeStringFromAsciiz( &titleW, startup_info->lpTitle );
1545 else titleW.Buffer = NULL;
1547 memcpy( &infoW, startup_info, sizeof(infoW) );
1548 infoW.lpDesktop = desktopW.Buffer;
1549 infoW.lpTitle = titleW.Buffer;
1551 if (startup_info->lpReserved)
1552 FIXME("StartupInfo.lpReserved is used, please report (%s)\n",
1553 debugstr_a(startup_info->lpReserved));
1555 ret = CreateProcessW( app_nameW.Buffer, cmd_lineW.Buffer, process_attr, thread_attr,
1556 inherit, flags, env, cur_dirW.Buffer, &infoW, info );
1558 RtlFreeUnicodeString( &app_nameW );
1559 RtlFreeUnicodeString( &cmd_lineW );
1560 RtlFreeUnicodeString( &cur_dirW );
1561 RtlFreeUnicodeString( &desktopW );
1562 RtlFreeUnicodeString( &titleW );
1567 /**********************************************************************
1568 * CreateProcessW (KERNEL32.@)
1570 BOOL WINAPI CreateProcessW( LPCWSTR app_name, LPWSTR cmd_line, LPSECURITY_ATTRIBUTES process_attr,
1571 LPSECURITY_ATTRIBUTES thread_attr, BOOL inherit, DWORD flags,
1572 LPVOID env, LPCWSTR cur_dir, LPSTARTUPINFOW startup_info,
1573 LPPROCESS_INFORMATION info )
1577 const char *unixdir = NULL;
1578 DOS_FULL_NAME full_dir;
1579 WCHAR name[MAX_PATH];
1580 WCHAR *tidy_cmdline, *p, *envW = env;
1582 /* Process the AppName and/or CmdLine to get module name and path */
1584 TRACE("app %s cmdline %s\n", debugstr_w(app_name), debugstr_w(cmd_line) );
1586 if (!(tidy_cmdline = get_file_name( app_name, cmd_line, name, sizeof(name), &hFile )))
1588 if (hFile == INVALID_HANDLE_VALUE) goto done;
1590 /* Warn if unsupported features are used */
1592 if (flags & (IDLE_PRIORITY_CLASS | HIGH_PRIORITY_CLASS | REALTIME_PRIORITY_CLASS |
1593 CREATE_NEW_PROCESS_GROUP | CREATE_SEPARATE_WOW_VDM | CREATE_SHARED_WOW_VDM |
1594 CREATE_DEFAULT_ERROR_MODE | CREATE_NO_WINDOW |
1595 PROFILE_USER | PROFILE_KERNEL | PROFILE_SERVER))
1596 WARN("(%s,...): ignoring some flags in %lx\n", debugstr_w(name), flags);
1600 if (DOSFS_GetFullName( cur_dir, TRUE, &full_dir )) unixdir = full_dir.long_name;
1604 WCHAR buf[MAX_PATH];
1605 if (GetCurrentDirectoryW(MAX_PATH, buf))
1607 if (DOSFS_GetFullName( buf, TRUE, &full_dir )) unixdir = full_dir.long_name;
1611 if (env && !(flags & CREATE_UNICODE_ENVIRONMENT)) /* convert environment to unicode */
1616 while (*p) p += strlen(p) + 1;
1617 p++; /* final null */
1618 lenW = MultiByteToWideChar( CP_ACP, 0, env, p - (char*)env, NULL, 0 );
1619 envW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) );
1620 MultiByteToWideChar( CP_ACP, 0, env, p - (char*)env, envW, lenW );
1621 flags |= CREATE_UNICODE_ENVIRONMENT;
1624 info->hThread = info->hProcess = 0;
1625 info->dwProcessId = info->dwThreadId = 0;
1627 /* Determine executable type */
1629 if (!hFile) /* builtin exe */
1631 TRACE( "starting %s as Winelib app\n", debugstr_w(name) );
1632 retv = create_process( 0, name, tidy_cmdline, envW, process_attr, thread_attr,
1633 inherit, flags, startup_info, info, unixdir );
1637 switch( MODULE_GetBinaryType( hFile ))
1640 TRACE( "starting %s as Win32 binary\n", debugstr_w(name) );
1641 retv = create_process( hFile, name, tidy_cmdline, envW, process_attr, thread_attr,
1642 inherit, flags, startup_info, info, unixdir );
1646 TRACE( "starting %s as Win16/DOS binary\n", debugstr_w(name) );
1647 retv = create_vdm_process( name, tidy_cmdline, envW, process_attr, thread_attr,
1648 inherit, flags, startup_info, info, unixdir );
1651 FIXME( "%s is OS/2 binary, not supported\n", debugstr_w(name) );
1652 SetLastError( ERROR_BAD_EXE_FORMAT );
1655 TRACE( "not starting %s since it is a dll\n", debugstr_w(name) );
1656 SetLastError( ERROR_BAD_EXE_FORMAT );
1658 case BINARY_UNIX_LIB:
1659 TRACE( "%s is a Unix library, starting as Winelib app\n", debugstr_w(name) );
1660 retv = create_process( hFile, name, tidy_cmdline, envW, process_attr, thread_attr,
1661 inherit, flags, startup_info, info, unixdir );
1663 case BINARY_UNKNOWN:
1664 /* check for .com or .bat extension */
1665 if ((p = strrchrW( name, '.' )))
1667 if (!strcmpiW( p, comW ))
1669 TRACE( "starting %s as DOS binary\n", debugstr_w(name) );
1670 retv = create_vdm_process( name, tidy_cmdline, envW, process_attr, thread_attr,
1671 inherit, flags, startup_info, info, unixdir );
1674 if (!strcmpiW( p, batW ))
1676 TRACE( "starting %s as batch binary\n", debugstr_w(name) );
1677 retv = create_cmd_process( name, tidy_cmdline, envW, process_attr, thread_attr,
1678 inherit, flags, startup_info, info, cur_dir );
1683 case BINARY_UNIX_EXE:
1685 /* unknown file, try as unix executable */
1686 DOS_FULL_NAME full_name;
1688 TRACE( "starting %s as Unix binary\n", debugstr_w(name) );
1690 if (DOSFS_GetFullName( name, TRUE, &full_name ))
1691 retv = (fork_and_exec( full_name.long_name, tidy_cmdline, envW, unixdir ) != -1);
1695 CloseHandle( hFile );
1698 if (tidy_cmdline != cmd_line) HeapFree( GetProcessHeap(), 0, tidy_cmdline );
1699 if (envW != env) HeapFree( GetProcessHeap(), 0, envW );
1704 /***********************************************************************
1707 * Wrapper to call WaitForInputIdle USER function
1709 typedef DWORD (WINAPI *WaitForInputIdle_ptr)( HANDLE hProcess, DWORD dwTimeOut );
1711 static DWORD wait_input_idle( HANDLE process, DWORD timeout )
1713 HMODULE mod = GetModuleHandleA( "user32.dll" );
1716 WaitForInputIdle_ptr ptr = (WaitForInputIdle_ptr)GetProcAddress( mod, "WaitForInputIdle" );
1717 if (ptr) return ptr( process, timeout );
1723 /***********************************************************************
1724 * WinExec (KERNEL32.@)
1726 UINT WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow )
1728 PROCESS_INFORMATION info;
1729 STARTUPINFOA startup;
1733 memset( &startup, 0, sizeof(startup) );
1734 startup.cb = sizeof(startup);
1735 startup.dwFlags = STARTF_USESHOWWINDOW;
1736 startup.wShowWindow = nCmdShow;
1738 /* cmdline needs to be writeable for CreateProcess */
1739 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine)+1 ))) return 0;
1740 strcpy( cmdline, lpCmdLine );
1742 if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE,
1743 0, NULL, NULL, &startup, &info ))
1745 /* Give 30 seconds to the app to come up */
1746 if (wait_input_idle( info.hProcess, 30000 ) == WAIT_FAILED)
1747 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1749 /* Close off the handles */
1750 CloseHandle( info.hThread );
1751 CloseHandle( info.hProcess );
1753 else if ((ret = GetLastError()) >= 32)
1755 FIXME("Strange error set by CreateProcess: %d\n", ret );
1758 HeapFree( GetProcessHeap(), 0, cmdline );
1763 /**********************************************************************
1764 * LoadModule (KERNEL32.@)
1766 HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
1768 LOADPARMS32 *params = paramBlock;
1769 PROCESS_INFORMATION info;
1770 STARTUPINFOA startup;
1771 HINSTANCE hInstance;
1773 char filename[MAX_PATH];
1776 if (!name) return (HINSTANCE)ERROR_FILE_NOT_FOUND;
1778 if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) &&
1779 !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL ))
1780 return (HINSTANCE)GetLastError();
1782 len = (BYTE)params->lpCmdLine[0];
1783 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 )))
1784 return (HINSTANCE)ERROR_NOT_ENOUGH_MEMORY;
1786 strcpy( cmdline, filename );
1787 p = cmdline + strlen(cmdline);
1789 memcpy( p, params->lpCmdLine + 1, len );
1792 memset( &startup, 0, sizeof(startup) );
1793 startup.cb = sizeof(startup);
1794 if (params->lpCmdShow)
1796 startup.dwFlags = STARTF_USESHOWWINDOW;
1797 startup.wShowWindow = ((WORD *)params->lpCmdShow)[1];
1800 if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0,
1801 params->lpEnvAddress, NULL, &startup, &info ))
1803 /* Give 30 seconds to the app to come up */
1804 if (wait_input_idle( info.hProcess, 30000 ) == WAIT_FAILED)
1805 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1806 hInstance = (HINSTANCE)33;
1807 /* Close off the handles */
1808 CloseHandle( info.hThread );
1809 CloseHandle( info.hProcess );
1811 else if ((hInstance = (HINSTANCE)GetLastError()) >= (HINSTANCE)32)
1813 FIXME("Strange error set by CreateProcess: %p\n", hInstance );
1814 hInstance = (HINSTANCE)11;
1817 HeapFree( GetProcessHeap(), 0, cmdline );
1822 /******************************************************************************
1823 * TerminateProcess (KERNEL32.@)
1825 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
1827 NTSTATUS status = NtTerminateProcess( handle, exit_code );
1828 if (status) SetLastError( RtlNtStatusToDosError(status) );
1833 /***********************************************************************
1834 * ExitProcess (KERNEL32.@)
1836 void WINAPI ExitProcess( DWORD status )
1838 LdrShutdownProcess();
1839 SERVER_START_REQ( terminate_process )
1841 /* send the exit code to the server */
1842 req->handle = GetCurrentProcess();
1843 req->exit_code = status;
1844 wine_server_call( req );
1851 /***********************************************************************
1852 * GetExitCodeProcess [KERNEL32.@]
1854 * Gets termination status of specified process
1860 BOOL WINAPI GetExitCodeProcess(
1861 HANDLE hProcess, /* [in] handle to the process */
1862 LPDWORD lpExitCode) /* [out] address to receive termination status */
1865 SERVER_START_REQ( get_process_info )
1867 req->handle = hProcess;
1868 ret = !wine_server_call_err( req );
1869 if (ret && lpExitCode) *lpExitCode = reply->exit_code;
1876 /***********************************************************************
1877 * SetErrorMode (KERNEL32.@)
1879 UINT WINAPI SetErrorMode( UINT mode )
1881 UINT old = process_error_mode;
1882 process_error_mode = mode;
1887 /**********************************************************************
1888 * TlsAlloc [KERNEL32.@] Allocates a TLS index.
1890 * Allocates a thread local storage index
1893 * Success: TLS Index
1894 * Failure: 0xFFFFFFFF
1896 DWORD WINAPI TlsAlloc( void )
1900 RtlAcquirePebLock();
1901 index = RtlFindClearBitsAndSet( NtCurrentTeb()->Peb->TlsBitmap, 1, 0 );
1902 if (index != ~0UL) NtCurrentTeb()->TlsSlots[index] = 0; /* clear the value */
1903 else SetLastError( ERROR_NO_MORE_ITEMS );
1904 RtlReleasePebLock();
1909 /**********************************************************************
1910 * TlsFree [KERNEL32.@] Releases a TLS index.
1912 * Releases a thread local storage index, making it available for reuse
1918 BOOL WINAPI TlsFree(
1919 DWORD index) /* [in] TLS Index to free */
1923 RtlAcquirePebLock();
1924 ret = RtlAreBitsSet( NtCurrentTeb()->Peb->TlsBitmap, index, 1 );
1927 RtlClearBits( NtCurrentTeb()->Peb->TlsBitmap, index, 1 );
1928 NtSetInformationThread( GetCurrentThread(), ThreadZeroTlsCell, &index, sizeof(index) );
1930 else SetLastError( ERROR_INVALID_PARAMETER );
1931 RtlReleasePebLock();
1936 /**********************************************************************
1937 * TlsGetValue [KERNEL32.@] Gets value in a thread's TLS slot
1940 * Success: Value stored in calling thread's TLS slot for index
1941 * Failure: 0 and GetLastError returns NO_ERROR
1943 LPVOID WINAPI TlsGetValue(
1944 DWORD index) /* [in] TLS index to retrieve value for */
1946 if (index >= NtCurrentTeb()->Peb->TlsBitmap->SizeOfBitMap)
1948 SetLastError( ERROR_INVALID_PARAMETER );
1951 SetLastError( ERROR_SUCCESS );
1952 return NtCurrentTeb()->TlsSlots[index];
1956 /**********************************************************************
1957 * TlsSetValue [KERNEL32.@] Stores a value in the thread's TLS slot.
1963 BOOL WINAPI TlsSetValue(
1964 DWORD index, /* [in] TLS index to set value for */
1965 LPVOID value) /* [in] Value to be stored */
1967 if (index >= NtCurrentTeb()->Peb->TlsBitmap->SizeOfBitMap)
1969 SetLastError( ERROR_INVALID_PARAMETER );
1972 NtCurrentTeb()->TlsSlots[index] = value;
1977 /***********************************************************************
1978 * GetProcessFlags (KERNEL32.@)
1980 DWORD WINAPI GetProcessFlags( DWORD processid )
1982 IMAGE_NT_HEADERS *nt;
1985 if (processid && processid != GetCurrentProcessId()) return 0;
1987 if ((nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress )))
1989 if (nt->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
1990 flags |= PDB32_CONSOLE_PROC;
1992 if (!AreFileApisANSI()) flags |= PDB32_FILE_APIS_OEM;
1993 if (IsDebuggerPresent()) flags |= PDB32_DEBUGGED;
1998 /***********************************************************************
1999 * GetProcessDword (KERNEL.485)
2000 * GetProcessDword (KERNEL32.18)
2001 * 'Of course you cannot directly access Windows internal structures'
2003 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
2008 TRACE("(%ld, %d)\n", dwProcessID, offset );
2010 if (dwProcessID && dwProcessID != GetCurrentProcessId())
2012 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
2018 case GPD_APP_COMPAT_FLAGS:
2019 return GetAppCompatFlags16(0);
2020 case GPD_LOAD_DONE_EVENT:
2022 case GPD_HINSTANCE16:
2023 return GetTaskDS16();
2024 case GPD_WINDOWS_VERSION:
2025 return GetExeVersion16();
2027 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
2029 return (DWORD)NtCurrentTeb()->Peb;
2030 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
2031 GetStartupInfoW(&siw);
2032 return (DWORD)siw.hStdOutput;
2033 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
2034 GetStartupInfoW(&siw);
2035 return (DWORD)siw.hStdInput;
2036 case GPD_STARTF_SHOWWINDOW:
2037 GetStartupInfoW(&siw);
2038 return siw.wShowWindow;
2039 case GPD_STARTF_SIZE:
2040 GetStartupInfoW(&siw);
2042 if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
2044 if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
2045 return MAKELONG( x, y );
2046 case GPD_STARTF_POSITION:
2047 GetStartupInfoW(&siw);
2049 if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
2051 if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
2052 return MAKELONG( x, y );
2053 case GPD_STARTF_FLAGS:
2054 GetStartupInfoW(&siw);
2059 return GetProcessFlags(0);
2061 return process_dword;
2063 ERR("Unknown offset %d\n", offset );
2068 /***********************************************************************
2069 * SetProcessDword (KERNEL.484)
2070 * 'Of course you cannot directly access Windows internal structures'
2072 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
2074 TRACE("(%ld, %d)\n", dwProcessID, offset );
2076 if (dwProcessID && dwProcessID != GetCurrentProcessId())
2078 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
2084 case GPD_APP_COMPAT_FLAGS:
2085 case GPD_LOAD_DONE_EVENT:
2086 case GPD_HINSTANCE16:
2087 case GPD_WINDOWS_VERSION:
2090 case GPD_STARTF_SHELLDATA:
2091 case GPD_STARTF_HOTKEY:
2092 case GPD_STARTF_SHOWWINDOW:
2093 case GPD_STARTF_SIZE:
2094 case GPD_STARTF_POSITION:
2095 case GPD_STARTF_FLAGS:
2098 ERR("Not allowed to modify offset %d\n", offset );
2101 process_dword = value;
2104 ERR("Unknown offset %d\n", offset );
2110 /***********************************************************************
2111 * ExitProcess (KERNEL.466)
2113 void WINAPI ExitProcess16( WORD status )
2116 ReleaseThunkLock( &count );
2117 ExitProcess( status );
2121 /*********************************************************************
2122 * OpenProcess (KERNEL32.@)
2124 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
2127 SERVER_START_REQ( open_process )
2130 req->access = access;
2131 req->inherit = inherit;
2132 if (!wine_server_call_err( req )) ret = reply->handle;
2139 /*********************************************************************
2140 * MapProcessHandle (KERNEL.483)
2142 DWORD WINAPI MapProcessHandle( HANDLE handle )
2145 SERVER_START_REQ( get_process_info )
2147 req->handle = handle;
2148 if (!wine_server_call_err( req )) ret = reply->pid;
2155 /*********************************************************************
2156 * CloseW32Handle (KERNEL.474)
2157 * CloseHandle (KERNEL32.@)
2159 BOOL WINAPI CloseHandle( HANDLE handle )
2163 /* stdio handles need special treatment */
2164 if ((handle == (HANDLE)STD_INPUT_HANDLE) ||
2165 (handle == (HANDLE)STD_OUTPUT_HANDLE) ||
2166 (handle == (HANDLE)STD_ERROR_HANDLE))
2167 handle = GetStdHandle( (DWORD)handle );
2169 if (is_console_handle(handle))
2170 return CloseConsoleHandle(handle);
2172 status = NtClose( handle );
2173 if (status) SetLastError( RtlNtStatusToDosError(status) );
2178 /*********************************************************************
2179 * GetHandleInformation (KERNEL32.@)
2181 BOOL WINAPI GetHandleInformation( HANDLE handle, LPDWORD flags )
2184 SERVER_START_REQ( set_handle_info )
2186 req->handle = handle;
2190 ret = !wine_server_call_err( req );
2191 if (ret && flags) *flags = reply->old_flags;
2198 /*********************************************************************
2199 * SetHandleInformation (KERNEL32.@)
2201 BOOL WINAPI SetHandleInformation( HANDLE handle, DWORD mask, DWORD flags )
2204 SERVER_START_REQ( set_handle_info )
2206 req->handle = handle;
2210 ret = !wine_server_call_err( req );
2217 /*********************************************************************
2218 * DuplicateHandle (KERNEL32.@)
2220 BOOL WINAPI DuplicateHandle( HANDLE source_process, HANDLE source,
2221 HANDLE dest_process, HANDLE *dest,
2222 DWORD access, BOOL inherit, DWORD options )
2226 if (is_console_handle(source))
2228 /* FIXME: this test is not sufficient, we need to test process ids, not handles */
2229 if (source_process != dest_process ||
2230 source_process != GetCurrentProcess())
2232 SetLastError(ERROR_INVALID_PARAMETER);
2235 *dest = DuplicateConsoleHandle( source, access, inherit, options );
2236 return (*dest != INVALID_HANDLE_VALUE);
2238 status = NtDuplicateObject( source_process, source, dest_process, dest,
2239 access, inherit ? OBJ_INHERIT : 0, options );
2240 if (status) SetLastError( RtlNtStatusToDosError(status) );
2245 /***********************************************************************
2246 * ConvertToGlobalHandle (KERNEL.476)
2247 * ConvertToGlobalHandle (KERNEL32.@)
2249 HANDLE WINAPI ConvertToGlobalHandle(HANDLE hSrc)
2251 HANDLE ret = INVALID_HANDLE_VALUE;
2252 DuplicateHandle( GetCurrentProcess(), hSrc, GetCurrentProcess(), &ret, 0, FALSE,
2253 DUP_HANDLE_MAKE_GLOBAL | DUP_HANDLE_SAME_ACCESS | DUP_HANDLE_CLOSE_SOURCE );
2258 /***********************************************************************
2259 * SetHandleContext (KERNEL32.@)
2261 BOOL WINAPI SetHandleContext(HANDLE hnd,DWORD context)
2263 FIXME("(%p,%ld), 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,context);
2265 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2270 /***********************************************************************
2271 * GetHandleContext (KERNEL32.@)
2273 DWORD WINAPI GetHandleContext(HANDLE hnd)
2275 FIXME("(%p), 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",hnd);
2277 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2282 /***********************************************************************
2283 * CreateSocketHandle (KERNEL32.@)
2285 HANDLE WINAPI CreateSocketHandle(void)
2287 FIXME("(), stub. In case this got called by WSOCK32/WS2_32: "
2288 "the external WINSOCK DLLs won't work with WINE, don't use them.\n");
2289 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2290 return INVALID_HANDLE_VALUE;
2294 /***********************************************************************
2295 * SetPriorityClass (KERNEL32.@)
2297 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
2300 SERVER_START_REQ( set_process_info )
2302 req->handle = hprocess;
2303 req->priority = priorityclass;
2304 req->mask = SET_PROCESS_INFO_PRIORITY;
2305 ret = !wine_server_call_err( req );
2312 /***********************************************************************
2313 * GetPriorityClass (KERNEL32.@)
2315 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
2318 SERVER_START_REQ( get_process_info )
2320 req->handle = hprocess;
2321 if (!wine_server_call_err( req )) ret = reply->priority;
2328 /***********************************************************************
2329 * SetProcessAffinityMask (KERNEL32.@)
2331 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
2334 SERVER_START_REQ( set_process_info )
2336 req->handle = hProcess;
2337 req->affinity = affmask;
2338 req->mask = SET_PROCESS_INFO_AFFINITY;
2339 ret = !wine_server_call_err( req );
2346 /**********************************************************************
2347 * GetProcessAffinityMask (KERNEL32.@)
2349 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
2350 LPDWORD lpProcessAffinityMask,
2351 LPDWORD lpSystemAffinityMask )
2354 SERVER_START_REQ( get_process_info )
2356 req->handle = hProcess;
2357 if (!wine_server_call_err( req ))
2359 if (lpProcessAffinityMask) *lpProcessAffinityMask = reply->process_affinity;
2360 if (lpSystemAffinityMask) *lpSystemAffinityMask = reply->system_affinity;
2369 /***********************************************************************
2370 * GetProcessVersion (KERNEL32.@)
2372 DWORD WINAPI GetProcessVersion( DWORD processid )
2374 IMAGE_NT_HEADERS *nt;
2376 if (processid && processid != GetCurrentProcessId())
2378 FIXME("should use ReadProcessMemory\n");
2381 if ((nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress )))
2382 return ((nt->OptionalHeader.MajorSubsystemVersion << 16) |
2383 nt->OptionalHeader.MinorSubsystemVersion);
2388 /***********************************************************************
2389 * SetProcessWorkingSetSize [KERNEL32.@]
2390 * Sets the min/max working set sizes for a specified process.
2393 * hProcess [I] Handle to the process of interest
2394 * minset [I] Specifies minimum working set size
2395 * maxset [I] Specifies maximum working set size
2399 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess, SIZE_T minset,
2402 FIXME("(%p,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
2403 if(( minset == (SIZE_T)-1) && (maxset == (SIZE_T)-1)) {
2404 /* Trim the working set to zero */
2405 /* Swap the process out of physical RAM */
2410 /***********************************************************************
2411 * GetProcessWorkingSetSize (KERNEL32.@)
2413 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess, PSIZE_T minset,
2416 FIXME("(%p,%p,%p): stub\n",hProcess,minset,maxset);
2417 /* 32 MB working set size */
2418 if (minset) *minset = 32*1024*1024;
2419 if (maxset) *maxset = 32*1024*1024;
2424 /***********************************************************************
2425 * SetProcessShutdownParameters (KERNEL32.@)
2427 BOOL WINAPI SetProcessShutdownParameters(DWORD level, DWORD flags)
2429 FIXME("(%08lx, %08lx): partial stub.\n", level, flags);
2430 shutdown_flags = flags;
2431 shutdown_priority = level;
2436 /***********************************************************************
2437 * GetProcessShutdownParameters (KERNEL32.@)
2440 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel, LPDWORD lpdwFlags )
2442 *lpdwLevel = shutdown_priority;
2443 *lpdwFlags = shutdown_flags;
2448 /***********************************************************************
2449 * GetProcessPriorityBoost (KERNEL32.@)
2451 BOOL WINAPI GetProcessPriorityBoost(HANDLE hprocess,PBOOL pDisablePriorityBoost)
2453 FIXME("(%p,%p): semi-stub\n", hprocess, pDisablePriorityBoost);
2455 /* Report that no boost is present.. */
2456 *pDisablePriorityBoost = FALSE;
2461 /***********************************************************************
2462 * SetProcessPriorityBoost (KERNEL32.@)
2464 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
2466 FIXME("(%p,%d): stub\n",hprocess,disableboost);
2467 /* Say we can do it. I doubt the program will notice that we don't. */
2472 /***********************************************************************
2473 * ReadProcessMemory (KERNEL32.@)
2475 BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, SIZE_T size,
2476 SIZE_T *bytes_read )
2478 NTSTATUS status = NtReadVirtualMemory( process, addr, buffer, size, bytes_read );
2479 if (status) SetLastError( RtlNtStatusToDosError(status) );
2484 /***********************************************************************
2485 * WriteProcessMemory (KERNEL32.@)
2487 BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPCVOID buffer, SIZE_T size,
2488 SIZE_T *bytes_written )
2490 NTSTATUS status = NtWriteVirtualMemory( process, addr, buffer, size, bytes_written );
2491 if (status) SetLastError( RtlNtStatusToDosError(status) );
2496 /****************************************************************************
2497 * FlushInstructionCache (KERNEL32.@)
2499 BOOL WINAPI FlushInstructionCache(HANDLE hProcess, LPCVOID lpBaseAddress, SIZE_T dwSize)
2501 if (GetVersion() & 0x80000000) return TRUE; /* not NT, always TRUE */
2502 FIXME("(%p,%p,0x%08lx): stub\n",hProcess, lpBaseAddress, dwSize);
2507 /******************************************************************
2508 * GetProcessIoCounters (KERNEL32.@)
2510 BOOL WINAPI GetProcessIoCounters(HANDLE hProcess, PIO_COUNTERS ioc)
2514 status = NtQueryInformationProcess(hProcess, ProcessIoCounters,
2515 ioc, sizeof(*ioc), NULL);
2516 if (status) SetLastError( RtlNtStatusToDosError(status) );
2520 /***********************************************************************
2521 * ProcessIdToSessionId (KERNEL32.@)
2522 * This function is available on Terminal Server 4SP4 and Windows 2000
2524 BOOL WINAPI ProcessIdToSessionId( DWORD procid, DWORD *sessionid_ptr )
2526 /* According to MSDN, if the calling process is not in a terminal
2527 * services environment, then the sessionid returned is zero.
2534 /***********************************************************************
2535 * RegisterServiceProcess (KERNEL.491)
2536 * RegisterServiceProcess (KERNEL32.@)
2538 * A service process calls this function to ensure that it continues to run
2539 * even after a user logged off.
2541 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
2543 /* I don't think that Wine needs to do anything in that function */
2544 return 1; /* success */
2548 /**************************************************************************
2549 * SetFileApisToOEM (KERNEL32.@)
2551 VOID WINAPI SetFileApisToOEM(void)
2553 oem_file_apis = TRUE;
2557 /**************************************************************************
2558 * SetFileApisToANSI (KERNEL32.@)
2560 VOID WINAPI SetFileApisToANSI(void)
2562 oem_file_apis = FALSE;
2566 /******************************************************************************
2567 * AreFileApisANSI [KERNEL32.@] Determines if file functions are using ANSI
2570 * TRUE: Set of file functions is using ANSI code page
2571 * FALSE: Set of file functions is using OEM code page
2573 BOOL WINAPI AreFileApisANSI(void)
2575 return !oem_file_apis;
2579 /***********************************************************************
2580 * GetTickCount (KERNEL32.@)
2582 * Returns the number of milliseconds, modulo 2^32, since the start
2583 * of the wineserver.
2585 DWORD WINAPI GetTickCount(void)
2588 gettimeofday( &t, NULL );
2589 return ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - server_startticks;
2593 /***********************************************************************
2594 * GetCurrentProcess (KERNEL32.@)
2596 #undef GetCurrentProcess
2597 HANDLE WINAPI GetCurrentProcess(void)
2599 return (HANDLE)0xffffffff;