4 * Copyright 1996, 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
31 #include "wine/winbase16.h"
32 #include "wine/winuser16.h"
40 #include "kernel_private.h"
41 #include "wine/server.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(process
);
45 WINE_DECLARE_DEBUG_CHANNEL(server
);
46 WINE_DECLARE_DEBUG_CHANNEL(relay
);
47 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
49 /* Win32 process database */
52 LONG header
[2]; /* 00 Kernel object header */
53 HMODULE module
; /* 08 Main exe module (NT) */
54 PPEB_LDR_DATA LdrData
; /* 0c Pointer to loader information */
55 RTL_USER_PROCESS_PARAMETERS
*ProcessParameters
; /* 10 Process parameters */
56 DWORD unknown2
; /* 14 Unknown */
57 HANDLE heap
; /* 18 Default process heap */
58 HANDLE mem_context
; /* 1c Process memory context */
59 DWORD flags
; /* 20 Flags */
60 void *pdb16
; /* 24 DOS PSP */
61 WORD PSP_sel
; /* 28 Selector to DOS PSP */
62 WORD imte
; /* 2a IMTE for the process module */
63 WORD threads
; /* 2c Number of threads */
64 WORD running_threads
; /* 2e Number of running threads */
65 WORD free_lib_count
; /* 30 Recursion depth of FreeLibrary calls */
66 WORD ring0_threads
; /* 32 Number of ring 0 threads */
67 HANDLE system_heap
; /* 34 System heap to allocate handles */
68 HTASK task
; /* 38 Win16 task */
69 void *mem_map_files
; /* 3c Pointer to mem-mapped files */
70 struct _ENVDB
*env_db
; /* 40 Environment database */
71 void *handle_table
; /* 44 Handle table */
72 struct _PDB
*parent
; /* 48 Parent process */
73 void *modref_list
; /* 4c MODREF list */
74 void *thread_list
; /* 50 List of threads */
75 void *debuggee_CB
; /* 54 Debuggee context block */
76 void *local_heap_free
; /* 58 Head of local heap free list */
77 DWORD unknown4
; /* 5c Unknown */
78 CRITICAL_SECTION crit_section
; /* 60 Critical section */
79 DWORD unknown5
[3]; /* 78 Unknown */
80 void *console
; /* 84 Console */
81 DWORD tls_bits
[2]; /* 88 TLS in-use bits */
82 DWORD process_dword
; /* 90 Unknown */
83 struct _PDB
*group
; /* 94 Process group */
84 void *exe_modref
; /* 98 MODREF for the process EXE */
85 void *top_filter
; /* 9c Top exception filter */
86 DWORD priority
; /* a0 Priority level */
87 HANDLE heap_list
; /* a4 Head of process heap list */
88 void *heap_handles
; /* a8 Head of heap handles list */
89 DWORD unknown6
; /* ac Unknown */
90 void *console_provider
; /* b0 Console provider (??) */
91 WORD env_selector
; /* b4 Selector to process environment */
92 WORD error_mode
; /* b6 Error mode */
93 HANDLE load_done_evt
; /* b8 Event for process loading done */
94 void *UTState
; /* bc Head of Univeral Thunk list */
95 DWORD unknown8
; /* c0 Unknown (NT) */
96 LCID locale
; /* c4 Locale to be queried by GetThreadLocale (NT) */
101 static RTL_USER_PROCESS_PARAMETERS process_pmts
;
102 static PEB_LDR_DATA process_ldr
;
104 static HANDLE main_exe_file
;
105 static DWORD shutdown_flags
= 0;
106 static DWORD shutdown_priority
= 0x280;
107 static DWORD process_dword
;
108 static BOOL oem_file_apis
;
110 extern unsigned int server_startticks
;
111 int main_create_flags
= 0;
114 #define PDB32_DEBUGGED 0x0001 /* Process is being debugged */
115 #define PDB32_WIN16_PROC 0x0008 /* Win16 process */
116 #define PDB32_DOS_PROC 0x0010 /* Dos process */
117 #define PDB32_CONSOLE_PROC 0x0020 /* Console process */
118 #define PDB32_FILE_APIS_OEM 0x0040 /* File APIs are OEM */
119 #define PDB32_WIN32S_PROC 0x8000 /* Win32s process */
121 /* dlls/ntdll/env.c */
122 extern BOOL
init_user_process_pmts( size_t );
123 extern BOOL
build_command_line( char **argv
);
125 extern void RELAY_InitDebugLists(void);
126 extern void SHELL_LoadRegistry(void);
127 extern void VERSION_Init( const char *appname
);
128 extern void MODULE_InitLoadPath(void);
130 /***********************************************************************
133 inline static const char *get_basename( const char *name
)
137 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
138 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
143 /***********************************************************************
144 * open_builtin_exe_file
146 * Open an exe file for a builtin exe.
148 static void *open_builtin_exe_file( const char *name
, char *error
, int error_size
,
149 int test_only
, int *file_exists
)
151 char exename
[MAX_PATH
], *p
;
152 const char *basename
= get_basename(name
);
154 if (strlen(basename
) >= sizeof(exename
)) return NULL
;
155 strcpy( exename
, basename
);
156 for (p
= exename
; *p
; p
++) *p
= FILE_tolower(*p
);
157 return wine_dll_load_main_exe( exename
, error
, error_size
, test_only
, file_exists
);
161 /***********************************************************************
164 * Open a specific exe file, taking load order into account.
165 * Returns the file handle or 0 for a builtin exe.
167 static HANDLE
open_exe_file( const char *name
)
169 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
170 char buffer
[MAX_PATH
];
174 TRACE("looking for %s\n", debugstr_a(name
) );
176 if ((handle
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
,
177 NULL
, OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
179 /* file doesn't exist, check for builtin */
180 if (!FILE_contains_path( name
)) goto error
;
181 if (!MODULE_GetBuiltinPath( name
, "", buffer
, sizeof(buffer
) )) goto error
;
185 MODULE_GetLoadOrderA( loadorder
, NULL
, name
, TRUE
);
187 for(i
= 0; i
< LOADORDER_NTYPES
; i
++)
189 if (loadorder
[i
] == LOADORDER_INVALID
) break;
193 TRACE( "Trying native exe %s\n", debugstr_a(name
) );
194 if (handle
!= INVALID_HANDLE_VALUE
) return handle
;
197 TRACE( "Trying built-in exe %s\n", debugstr_a(name
) );
198 open_builtin_exe_file( name
, NULL
, 0, 1, &file_exists
);
201 if (handle
!= INVALID_HANDLE_VALUE
) CloseHandle(handle
);
208 if (handle
!= INVALID_HANDLE_VALUE
) CloseHandle(handle
);
211 SetLastError( ERROR_FILE_NOT_FOUND
);
212 return INVALID_HANDLE_VALUE
;
216 /***********************************************************************
219 * Open an exe file, and return the full name and file handle.
220 * Returns FALSE if file could not be found.
221 * If file exists but cannot be opened, returns TRUE and set handle to INVALID_HANDLE_VALUE.
222 * If file is a builtin exe, returns TRUE and sets handle to 0.
224 static BOOL
find_exe_file( const char *name
, char *buffer
, int buflen
, HANDLE
*handle
)
226 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
229 TRACE("looking for %s\n", debugstr_a(name
) );
231 if (!SearchPathA( NULL
, name
, ".exe", buflen
, buffer
, NULL
) &&
232 !MODULE_GetBuiltinPath( name
, ".exe", buffer
, buflen
))
234 /* no builtin found, try native without extension in case it is a Unix app */
236 if (SearchPathA( NULL
, name
, NULL
, buflen
, buffer
, NULL
))
238 TRACE( "Trying native/Unix binary %s\n", debugstr_a(buffer
) );
239 if ((*handle
= CreateFileA( buffer
, GENERIC_READ
, FILE_SHARE_READ
,
240 NULL
, OPEN_EXISTING
, 0, 0 )) != INVALID_HANDLE_VALUE
)
246 MODULE_GetLoadOrderA( loadorder
, NULL
, buffer
, TRUE
);
248 for(i
= 0; i
< LOADORDER_NTYPES
; i
++)
250 if (loadorder
[i
] == LOADORDER_INVALID
) break;
254 TRACE( "Trying native exe %s\n", debugstr_a(buffer
) );
255 if ((*handle
= CreateFileA( buffer
, GENERIC_READ
, FILE_SHARE_READ
,
256 NULL
, OPEN_EXISTING
, 0, 0 )) != INVALID_HANDLE_VALUE
)
258 if (GetLastError() != ERROR_FILE_NOT_FOUND
) return TRUE
;
261 TRACE( "Trying built-in exe %s\n", debugstr_a(buffer
) );
262 open_builtin_exe_file( buffer
, NULL
, 0, 1, &file_exists
);
273 SetLastError( ERROR_FILE_NOT_FOUND
);
278 /**********************************************************************
281 * Load a PE format EXE file.
283 static HMODULE
load_pe_exe( HANDLE file
)
285 IMAGE_NT_HEADERS
*nt
;
288 OBJECT_ATTRIBUTES attr
;
292 attr
.Length
= sizeof(attr
);
293 attr
.RootDirectory
= 0;
294 attr
.ObjectName
= NULL
;
296 attr
.SecurityDescriptor
= NULL
;
297 attr
.SecurityQualityOfService
= NULL
;
300 if (NtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
301 &attr
, &size
, 0, SEC_IMAGE
, file
) != STATUS_SUCCESS
)
305 if (NtMapViewOfSection( mapping
, GetCurrentProcess(), &module
, 0, 0, &size
, &len
,
306 ViewShare
, 0, PAGE_READONLY
) != STATUS_SUCCESS
)
312 nt
= RtlImageNtHeader( module
);
313 if (nt
->OptionalHeader
.AddressOfEntryPoint
)
315 if (!RtlImageRvaToSection( nt
, module
, nt
->OptionalHeader
.AddressOfEntryPoint
))
316 MESSAGE("VIRUS WARNING: PE module has an invalid entrypoint (0x%08lx) "
317 "outside all sections (possibly infected by Tchernobyl/SpaceFiller virus)!\n",
318 nt
->OptionalHeader
.AddressOfEntryPoint
);
323 /***********************************************************************
326 * Main process initialisation code
328 static BOOL
process_init( char *argv
[] )
331 size_t info_size
= 0;
335 setlocale(LC_CTYPE
,"");
337 /* store the program name */
340 /* Fill the initial process structure */
341 current_process
.threads
= 1;
342 current_process
.running_threads
= 1;
343 current_process
.ring0_threads
= 1;
344 current_process
.group
= ¤t_process
;
345 current_process
.priority
= 8; /* Normal */
346 current_process
.ProcessParameters
= &process_pmts
;
347 current_process
.LdrData
= &process_ldr
;
348 InitializeListHead(&process_ldr
.InLoadOrderModuleList
);
349 InitializeListHead(&process_ldr
.InMemoryOrderModuleList
);
350 InitializeListHead(&process_ldr
.InInitializationOrderModuleList
);
352 /* Setup the server connection */
353 wine_server_init_thread();
355 /* Retrieve startup info from the server */
356 SERVER_START_REQ( init_process
)
358 req
->ldt_copy
= &wine_ldt_copy
;
359 if ((ret
= !wine_server_call_err( req
)))
361 main_exe_file
= reply
->exe_file
;
362 main_create_flags
= reply
->create_flags
;
363 info_size
= reply
->info_size
;
364 server_startticks
= reply
->server_start
;
365 process_pmts
.hStdInput
= reply
->hstdin
;
366 process_pmts
.hStdOutput
= reply
->hstdout
;
367 process_pmts
.hStdError
= reply
->hstderr
;
371 if (!ret
) return FALSE
;
373 /* Create the process heap */
374 current_process
.heap
= RtlCreateHeap( HEAP_GROWABLE
, NULL
, 0, 0, NULL
, NULL
);
378 /* This is wine specific: we have no parent (we're started from unix)
379 * so, create a simple console with bare handles to unix stdio
380 * input & output streams (aka simple console)
382 wine_server_fd_to_handle( 0, GENERIC_READ
|SYNCHRONIZE
, TRUE
, &process_pmts
.hStdInput
);
383 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, TRUE
, &process_pmts
.hStdOutput
);
384 wine_server_fd_to_handle( 2, GENERIC_WRITE
|SYNCHRONIZE
, TRUE
, &process_pmts
.hStdError
);
388 /* convert value from server:
389 * + 0 => INVALID_HANDLE_VALUE
390 * + console handle need to be mapped
392 if (!process_pmts
.hStdInput
)
393 process_pmts
.hStdInput
= INVALID_HANDLE_VALUE
;
394 else if (VerifyConsoleIoHandle(console_handle_map(process_pmts
.hStdInput
)))
395 process_pmts
.hStdInput
= console_handle_map(process_pmts
.hStdInput
);
396 if (!process_pmts
.hStdOutput
)
397 process_pmts
.hStdOutput
= INVALID_HANDLE_VALUE
;
398 else if (VerifyConsoleIoHandle(console_handle_map(process_pmts
.hStdOutput
)))
399 process_pmts
.hStdOutput
= console_handle_map(process_pmts
.hStdOutput
);
400 if (!process_pmts
.hStdError
)
401 process_pmts
.hStdError
= INVALID_HANDLE_VALUE
;
402 else if (VerifyConsoleIoHandle(console_handle_map(process_pmts
.hStdError
)))
403 process_pmts
.hStdError
= console_handle_map(process_pmts
.hStdError
);
406 /* Copy the parent environment */
407 if (!init_user_process_pmts( info_size
)) return FALSE
;
409 /* Parse command line arguments */
410 OPTIONS_ParseOptions( !info_size
? argv
: NULL
);
412 /* <hack: to be changed later on> */
413 process_pmts
.CurrentDirectoryName
.Length
= 3 * sizeof(WCHAR
);
414 process_pmts
.CurrentDirectoryName
.MaximumLength
= RtlGetLongestNtPathLength() * sizeof(WCHAR
);
415 process_pmts
.CurrentDirectoryName
.Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, process_pmts
.CurrentDirectoryName
.MaximumLength
);
416 process_pmts
.CurrentDirectoryName
.Buffer
[0] = 'C';
417 process_pmts
.CurrentDirectoryName
.Buffer
[1] = ':';
418 process_pmts
.CurrentDirectoryName
.Buffer
[2] = '\\';
419 process_pmts
.CurrentDirectoryName
.Buffer
[3] = '\0';
420 /* </hack: to be changed later on> */
422 /* initialise DOS drives */
423 if (!DRIVE_Init()) return FALSE
;
425 /* initialise DOS directories */
426 if (!DIR_Init()) return FALSE
;
428 /* registry initialisation */
429 SHELL_LoadRegistry();
431 /* global boot finished, the rest is process-local */
432 SERVER_START_REQ( boot_done
)
434 req
->debug_level
= TRACE_ON(server
);
435 wine_server_call( req
);
439 if (TRACE_ON(relay
) || TRACE_ON(snoop
)) RELAY_InitDebugLists();
445 /***********************************************************************
448 * Startup routine of a new process. Runs on the new process stack.
450 static void start_process( void *arg
)
454 LPTHREAD_START_ROUTINE entry
;
455 HANDLE main_file
= main_exe_file
;
456 IMAGE_NT_HEADERS
*nt
;
457 PEB
*peb
= NtCurrentTeb()->Peb
;
458 UNICODE_STRING
*main_exe_name
= &peb
->ProcessParameters
->ImagePathName
;
462 UINT drive_type
= GetDriveTypeW( main_exe_name
->Buffer
);
463 /* don't keep the file handle open on removable media */
464 if (drive_type
== DRIVE_REMOVABLE
|| drive_type
== DRIVE_CDROM
) main_file
= 0;
467 /* Retrieve entry point address */
468 nt
= RtlImageNtHeader( peb
->ImageBaseAddress
);
469 entry
= (LPTHREAD_START_ROUTINE
)((char*)peb
->ImageBaseAddress
+
470 nt
->OptionalHeader
.AddressOfEntryPoint
);
472 /* Install signal handlers; this cannot be done before, since we cannot
473 * send exceptions to the debugger before the create process event that
474 * is sent by REQ_INIT_PROCESS_DONE.
475 * We do need the handlers in place by the time the request is over, so
476 * we set them up here. If we segfault between here and the server call
477 * something is very wrong... */
478 if (!SIGNAL_Init()) goto error
;
480 /* Signal the parent process to continue */
481 SERVER_START_REQ( init_process_done
)
483 req
->module
= peb
->ImageBaseAddress
;
484 req
->module_size
= nt
->OptionalHeader
.SizeOfImage
;
486 /* API requires a double indirection */
487 req
->name
= &main_exe_name
->Buffer
;
488 req
->exe_file
= main_file
;
489 req
->gui
= (nt
->OptionalHeader
.Subsystem
!= IMAGE_SUBSYSTEM_WINDOWS_CUI
);
490 wine_server_add_data( req
, main_exe_name
->Buffer
, main_exe_name
->Length
);
491 wine_server_call( req
);
492 peb
->BeingDebugged
= reply
->debugged
;
496 /* create the main modref and load dependencies */
497 if (main_exe_file
) CloseHandle( main_exe_file
); /* we no longer need it */
498 if (MODULE_DllProcessAttach( NULL
, (LPVOID
)1 ) != STATUS_SUCCESS
)
500 ERR( "Main exe initialization failed\n" );
505 DPRINTF( "%04lx:Starting process %s (entryproc=%p)\n",
506 GetCurrentThreadId(), debugstr_w(main_exe_name
->Buffer
), entry
);
507 if (peb
->BeingDebugged
) DbgBreakPoint();
508 SetLastError(0); /* clear error code */
509 ExitThread( entry( NtCurrentTeb()->Peb
) );
512 ExitProcess( GetLastError() );
514 __EXCEPT(UnhandledExceptionFilter
)
516 TerminateThread( GetCurrentThread(), GetExceptionCode() );
522 /***********************************************************************
523 * __wine_process_init
525 * Wine initialisation: load and start the main exe file.
527 void __wine_process_init( int argc
, char *argv
[] )
529 char main_exe_name
[MAX_PATH
];
530 char error
[1024], *p
;
531 DWORD stack_size
= 0;
534 /* Initialize everything */
535 if (!process_init( argv
)) exit(1);
537 argv
++; /* remove argv[0] (wine itself) */
539 if (process_pmts
.ImagePathName
.Buffer
)
541 WideCharToMultiByte( CP_ACP
, 0, process_pmts
.ImagePathName
.Buffer
, -1,
542 main_exe_name
, sizeof(main_exe_name
), NULL
, NULL
);
546 if (!argv
[0]) OPTIONS_Usage();
548 if (!find_exe_file( argv
[0], main_exe_name
, sizeof(main_exe_name
), &main_exe_file
))
550 MESSAGE( "%s: cannot find '%s'\n", argv0
, argv
[0] );
553 if (main_exe_file
== INVALID_HANDLE_VALUE
)
555 MESSAGE( "%s: cannot open '%s'\n", argv0
, main_exe_name
);
558 RtlCreateUnicodeStringFromAsciiz( &NtCurrentTeb()->Peb
->ProcessParameters
->ImagePathName
,
562 TRACE( "starting process name=%s file=%p argv[0]=%s\n",
563 debugstr_a(main_exe_name
), main_exe_file
, debugstr_a(argv
[0]) );
565 MODULE_InitLoadPath();
566 VERSION_Init( main_exe_name
);
568 if (!main_exe_file
) /* no file handle -> Winelib app */
570 TRACE( "starting Winelib app %s\n", debugstr_a(main_exe_name
) );
571 if (open_builtin_exe_file( main_exe_name
, error
, sizeof(error
), 0, &file_exists
))
573 MESSAGE( "%s: cannot open builtin library for '%s': %s\n", argv0
, main_exe_name
, error
);
577 switch( MODULE_GetBinaryType( main_exe_file
))
580 TRACE( "starting Win32 binary %s\n", debugstr_a(main_exe_name
) );
581 if ((current_process
.module
= load_pe_exe( main_exe_file
))) goto found
;
582 MESSAGE( "%s: could not load '%s' as Win32 binary\n", argv0
, main_exe_name
);
585 MESSAGE( "%s: '%s' is a DLL, not an executable\n", argv0
, main_exe_name
);
588 /* check for .com extension */
589 if (!(p
= strrchr( main_exe_name
, '.' )) || FILE_strcasecmp( p
, ".com" ))
591 MESSAGE( "%s: cannot determine executable type for '%s'\n", argv0
, main_exe_name
);
597 TRACE( "starting Win16/DOS binary %s\n", debugstr_a(main_exe_name
) );
598 CloseHandle( main_exe_file
);
601 argv
[0] = "winevdm.exe";
602 if (open_builtin_exe_file( "winevdm.exe", error
, sizeof(error
), 0, &file_exists
))
604 MESSAGE( "%s: trying to run '%s', cannot open builtin library for 'winevdm.exe': %s\n",
605 argv0
, main_exe_name
, error
);
608 MESSAGE( "%s: '%s' is an OS/2 binary, not supported\n", argv0
, main_exe_name
);
610 case BINARY_UNIX_EXE
:
611 MESSAGE( "%s: '%s' is a Unix binary, not supported\n", argv0
, main_exe_name
);
613 case BINARY_UNIX_LIB
:
615 DOS_FULL_NAME full_name
;
616 const char *name
= main_exe_name
;
617 UNICODE_STRING nameW
;
619 TRACE( "starting Winelib app %s\n", debugstr_a(main_exe_name
) );
620 RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
621 if (DOSFS_GetFullName( nameW
.Buffer
, TRUE
, &full_name
)) name
= full_name
.long_name
;
622 RtlFreeUnicodeString(&nameW
);
623 CloseHandle( main_exe_file
);
625 if (wine_dlopen( name
, RTLD_NOW
, error
, sizeof(error
) ))
627 if ((p
= strrchr( main_exe_name
, '.' )) && !strcmp( p
, ".so" )) *p
= 0;
630 MESSAGE( "%s: could not load '%s': %s\n", argv0
, main_exe_name
, error
);
636 /* build command line */
637 if (!build_command_line( argv
)) goto error
;
639 /* create 32-bit module for main exe */
640 if (!(current_process
.module
= BUILTIN32_LoadExeModule( current_process
.module
))) goto error
;
641 stack_size
= RtlImageNtHeader(current_process
.module
)->OptionalHeader
.SizeOfStackReserve
;
643 /* allocate main thread stack */
644 if (!THREAD_InitStack( NtCurrentTeb(), stack_size
)) goto error
;
646 /* switch to the new stack */
647 wine_switch_to_stack( start_process
, NULL
, NtCurrentTeb()->Tib
.StackBase
);
650 ExitProcess( GetLastError() );
654 /***********************************************************************
657 * Build an argv array from a command-line.
658 * The command-line is modified to insert nulls.
659 * 'reserved' is the number of args to reserve before the first one.
661 static char **build_argv( char *cmdline
, int reserved
)
666 int in_quotes
,bcount
;
673 if (*s
=='\0' || ((*s
==' ' || *s
=='\t') && !in_quotes
)) {
676 /* skip the remaining spaces */
677 while (*s
==' ' || *s
=='\t') {
684 } else if (*s
=='\\') {
685 /* '\', count them */
687 } else if ((*s
=='"') && ((bcount
& 1)==0)) {
689 in_quotes
=!in_quotes
;
692 /* a regular character */
697 argv
=malloc(argc
*sizeof(*argv
));
706 if ((*s
==' ' || *s
=='\t') && !in_quotes
) {
707 /* Close the argument and copy it */
711 /* skip the remaining spaces */
714 } while (*s
==' ' || *s
=='\t');
716 /* Start with a new argument */
719 } else if (*s
=='\\') {
723 } else if (*s
=='"') {
725 if ((bcount
& 1)==0) {
726 /* Preceeded by an even number of '\', this is half that
727 * number of '\', plus a '"' which we discard.
731 in_quotes
=!in_quotes
;
733 /* Preceeded by an odd number of '\', this is half that
734 * number of '\' followed by a '"'
742 /* a regular character */
757 /***********************************************************************
760 * Build the environment of a new child process.
762 static char **build_envp( const char *env
, const char *extra_env
)
768 if (extra_env
) for (p
= extra_env
; *p
; count
++) p
+= strlen(p
) + 1;
769 for (p
= env
; *p
; count
++) p
+= strlen(p
) + 1;
772 if ((envp
= malloc( count
* sizeof(*envp
) )))
774 extern char **environ
;
775 char **envptr
= envp
;
776 char **unixptr
= environ
;
777 /* first the extra strings */
778 if (extra_env
) for (p
= extra_env
; *p
; p
+= strlen(p
) + 1) *envptr
++ = (char *)p
;
779 /* then put PATH, HOME and WINEPREFIX from the unix env */
780 for (unixptr
= environ
; unixptr
&& *unixptr
; unixptr
++)
781 if (!memcmp( *unixptr
, "PATH=", 5 ) ||
782 !memcmp( *unixptr
, "HOME=", 5 ) ||
783 !memcmp( *unixptr
, "WINEPREFIX=", 11 )) *envptr
++ = *unixptr
;
784 /* now put the Windows environment strings */
785 for (p
= env
; *p
; p
+= strlen(p
) + 1)
787 if (!memcmp( p
, "PATH=", 5 )) /* store PATH as WINEPATH */
789 char *winepath
= malloc( strlen(p
) + 5 );
790 strcpy( winepath
, "WINE" );
791 strcpy( winepath
+ 4, p
);
792 *envptr
++ = winepath
;
794 else if (memcmp( p
, "HOME=", 5 ) &&
795 memcmp( p
, "WINEPATH=", 9 ) &&
796 memcmp( p
, "WINEPREFIX=", 11 )) *envptr
++ = (char *)p
;
804 /***********************************************************************
807 * Locate the Wine binary to exec for a new Win32 process.
809 static void exec_wine_binary( char **argv
, char **envp
)
811 const char *path
, *pos
, *ptr
;
813 /* first, try for a WINELOADER environment variable */
814 argv
[0] = getenv("WINELOADER");
816 execve( argv
[0], argv
, envp
);
818 /* next, try bin directory */
819 argv
[0] = BINDIR
"/wine";
820 execve( argv
[0], argv
, envp
);
822 /* now try the path of argv0 of the current binary */
823 if (!(argv
[0] = malloc( strlen(full_argv0
) + 6 ))) return;
824 if ((ptr
= strrchr( full_argv0
, '/' )))
826 memcpy( argv
[0], full_argv0
, ptr
- full_argv0
);
827 strcpy( argv
[0] + (ptr
- full_argv0
), "/wine" );
828 execve( argv
[0], argv
, envp
);
832 /* now search in the Unix path */
833 if ((path
= getenv( "PATH" )))
835 if (!(argv
[0] = malloc( strlen(path
) + 6 ))) return;
839 while (*pos
== ':') pos
++;
841 if (!(ptr
= strchr( pos
, ':' ))) ptr
= pos
+ strlen(pos
);
842 memcpy( argv
[0], pos
, ptr
- pos
);
843 strcpy( argv
[0] + (ptr
- pos
), "/wine" );
844 execve( argv
[0], argv
, envp
);
852 /***********************************************************************
855 * Fork and exec a new Unix binary, checking for errors.
857 static int fork_and_exec( const char *filename
, char *cmdline
,
858 const char *env
, const char *newdir
)
863 if (!env
) env
= GetEnvironmentStringsA();
870 fcntl( fd
[1], F_SETFD
, 1 ); /* set close on exec */
871 if (!(pid
= fork())) /* child */
873 char **argv
= build_argv( cmdline
, 0 );
874 char **envp
= build_envp( env
, NULL
);
877 /* Reset signals that we previously set to SIG_IGN */
878 signal( SIGPIPE
, SIG_DFL
);
879 signal( SIGCHLD
, SIG_DFL
);
881 if (newdir
) chdir(newdir
);
883 if (argv
&& envp
) execve( filename
, argv
, envp
);
885 write( fd
[1], &err
, sizeof(err
) );
889 if ((pid
!= -1) && (read( fd
[0], &err
, sizeof(err
) ) > 0)) /* exec failed */
894 if (pid
== -1) FILE_SetDosError();
900 /***********************************************************************
903 * Create a new process. If hFile is a valid handle we have an exe
904 * file, otherwise it is a Winelib app.
906 static BOOL
create_process( HANDLE hFile
, LPCSTR filename
, LPSTR cmd_line
, LPCSTR env
,
907 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
908 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
909 LPPROCESS_INFORMATION info
, LPCSTR unixdir
)
911 BOOL ret
, success
= FALSE
;
913 startup_info_t startup_info
;
914 char *extra_env
= NULL
;
923 env
= GetEnvironmentStringsA();
924 extra_env
= DRIVE_BuildEnv();
927 /* create the synchronization pipes */
929 if (pipe( startfd
) == -1)
934 if (pipe( execfd
) == -1)
941 fcntl( execfd
[1], F_SETFD
, 1 ); /* set close on exec */
943 /* create the child process */
945 if (!(pid
= fork())) /* child */
947 char **argv
= build_argv( cmd_line
, 1 );
948 char **envp
= build_envp( env
, extra_env
);
953 /* wait for parent to tell us to start */
954 if (read( startfd
[0], &dummy
, 1 ) != 1) _exit(1);
957 /* Reset signals that we previously set to SIG_IGN */
958 signal( SIGPIPE
, SIG_DFL
);
959 signal( SIGCHLD
, SIG_DFL
);
961 if (unixdir
) chdir(unixdir
);
963 if (argv
&& envp
) exec_wine_binary( argv
, envp
);
966 write( execfd
[1], &err
, sizeof(err
) );
970 /* this is the parent */
974 if (extra_env
) HeapFree( GetProcessHeap(), 0, extra_env
);
983 /* fill the startup info structure */
985 startup_info
.size
= sizeof(startup_info
);
986 /* startup_info.filename_len is set below */
987 startup_info
.cmdline_len
= cmd_line
? strlen(cmd_line
) : 0;
988 startup_info
.desktop_len
= startup
->lpDesktop
? strlen(startup
->lpDesktop
) : 0;
989 startup_info
.title_len
= startup
->lpTitle
? strlen(startup
->lpTitle
) : 0;
990 startup_info
.x
= startup
->dwX
;
991 startup_info
.y
= startup
->dwY
;
992 startup_info
.cx
= startup
->dwXSize
;
993 startup_info
.cy
= startup
->dwYSize
;
994 startup_info
.x_chars
= startup
->dwXCountChars
;
995 startup_info
.y_chars
= startup
->dwYCountChars
;
996 startup_info
.attribute
= startup
->dwFillAttribute
;
997 startup_info
.cmd_show
= startup
->wShowWindow
;
998 startup_info
.flags
= startup
->dwFlags
;
1000 /* create the process on the server side */
1002 SERVER_START_REQ( new_process
)
1007 req
->inherit_all
= inherit
;
1008 req
->create_flags
= flags
;
1009 req
->unix_pid
= pid
;
1010 req
->exe_file
= hFile
;
1011 if (startup
->dwFlags
& STARTF_USESTDHANDLES
)
1013 req
->hstdin
= startup
->hStdInput
;
1014 req
->hstdout
= startup
->hStdOutput
;
1015 req
->hstderr
= startup
->hStdError
;
1019 req
->hstdin
= GetStdHandle( STD_INPUT_HANDLE
);
1020 req
->hstdout
= GetStdHandle( STD_OUTPUT_HANDLE
);
1021 req
->hstderr
= GetStdHandle( STD_ERROR_HANDLE
);
1024 if ((flags
& (CREATE_NEW_CONSOLE
| DETACHED_PROCESS
)) != 0)
1026 /* this is temporary (for console handles). We have no way to control that the handle is invalid in child process otherwise */
1027 if (is_console_handle(req
->hstdin
)) req
->hstdin
= INVALID_HANDLE_VALUE
;
1028 if (is_console_handle(req
->hstdout
)) req
->hstdout
= INVALID_HANDLE_VALUE
;
1029 if (is_console_handle(req
->hstderr
)) req
->hstderr
= INVALID_HANDLE_VALUE
;
1033 if (is_console_handle(req
->hstdin
)) req
->hstdin
= console_handle_unmap(req
->hstdin
);
1034 if (is_console_handle(req
->hstdout
)) req
->hstdout
= console_handle_unmap(req
->hstdout
);
1035 if (is_console_handle(req
->hstderr
)) req
->hstderr
= console_handle_unmap(req
->hstderr
);
1038 if (GetLongPathNameA( filename
, buf
, MAX_PATH
))
1043 startup_info
.filename_len
= strlen(nameptr
);
1044 wine_server_add_data( req
, &startup_info
, sizeof(startup_info
) );
1045 wine_server_add_data( req
, nameptr
, startup_info
.filename_len
);
1046 wine_server_add_data( req
, cmd_line
, startup_info
.cmdline_len
);
1047 wine_server_add_data( req
, startup
->lpDesktop
, startup_info
.desktop_len
);
1048 wine_server_add_data( req
, startup
->lpTitle
, startup_info
.title_len
);
1050 ret
= !wine_server_call_err( req
);
1051 process_info
= reply
->info
;
1057 close( startfd
[1] );
1062 /* tell child to start and wait for it to exec */
1064 write( startfd
[1], &dummy
, 1 );
1065 close( startfd
[1] );
1067 if (read( execfd
[0], &err
, sizeof(err
) ) > 0) /* exec failed */
1072 CloseHandle( process_info
);
1076 /* wait for the new process info to be ready */
1078 WaitForSingleObject( process_info
, INFINITE
);
1079 SERVER_START_REQ( get_new_process_info
)
1081 req
->info
= process_info
;
1082 req
->pinherit
= (psa
&& (psa
->nLength
>= sizeof(*psa
)) && psa
->bInheritHandle
);
1083 req
->tinherit
= (tsa
&& (tsa
->nLength
>= sizeof(*tsa
)) && tsa
->bInheritHandle
);
1084 if ((ret
= !wine_server_call_err( req
)))
1086 info
->dwProcessId
= (DWORD
)reply
->pid
;
1087 info
->dwThreadId
= (DWORD
)reply
->tid
;
1088 info
->hProcess
= reply
->phandle
;
1089 info
->hThread
= reply
->thandle
;
1090 success
= reply
->success
;
1095 if (ret
&& !success
) /* new process failed to start */
1098 if (GetExitCodeProcess( info
->hProcess
, &exitcode
)) SetLastError( exitcode
);
1099 CloseHandle( info
->hThread
);
1100 CloseHandle( info
->hProcess
);
1103 CloseHandle( process_info
);
1108 /***********************************************************************
1109 * create_vdm_process
1111 * Create a new VDM process for a 16-bit or DOS application.
1113 static BOOL
create_vdm_process( LPCSTR filename
, LPSTR cmd_line
, LPCSTR env
,
1114 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
1115 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
1116 LPPROCESS_INFORMATION info
, LPCSTR unixdir
)
1119 LPSTR new_cmd_line
= HeapAlloc( GetProcessHeap(), 0, strlen(filename
) + strlen(cmd_line
) + 30 );
1123 SetLastError( ERROR_OUTOFMEMORY
);
1126 sprintf( new_cmd_line
, "winevdm.exe --app-name \"%s\" %s", filename
, cmd_line
);
1127 ret
= create_process( 0, "winevdm.exe", new_cmd_line
, env
, psa
, tsa
, inherit
,
1128 flags
, startup
, info
, unixdir
);
1129 HeapFree( GetProcessHeap(), 0, new_cmd_line
);
1134 /*************************************************************************
1137 * Helper for CreateProcess: retrieve the file name to load from the
1138 * app name and command line. Store the file name in buffer, and
1139 * return a possibly modified command line.
1140 * Also returns a handle to the opened file if it's a Windows binary.
1142 static LPSTR
get_file_name( LPCSTR appname
, LPSTR cmdline
, LPSTR buffer
,
1143 int buflen
, HANDLE
*handle
)
1145 char *name
, *pos
, *ret
= NULL
;
1148 /* if we have an app name, everything is easy */
1152 /* use the unmodified app name as file name */
1153 lstrcpynA( buffer
, appname
, buflen
);
1154 *handle
= open_exe_file( buffer
);
1155 if (!(ret
= cmdline
) || !cmdline
[0])
1157 /* no command-line, create one */
1158 if ((ret
= HeapAlloc( GetProcessHeap(), 0, strlen(appname
) + 3 )))
1159 sprintf( ret
, "\"%s\"", appname
);
1166 SetLastError( ERROR_INVALID_PARAMETER
);
1170 /* first check for a quoted file name */
1172 if ((cmdline
[0] == '"') && ((p
= strchr( cmdline
+ 1, '"' ))))
1174 int len
= p
- cmdline
- 1;
1175 /* extract the quoted portion as file name */
1176 if (!(name
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 ))) return NULL
;
1177 memcpy( name
, cmdline
+ 1, len
);
1180 if (find_exe_file( name
, buffer
, buflen
, handle
))
1181 ret
= cmdline
; /* no change necessary */
1185 /* now try the command-line word by word */
1187 if (!(name
= HeapAlloc( GetProcessHeap(), 0, strlen(cmdline
) + 1 ))) return NULL
;
1193 do *pos
++ = *p
++; while (*p
&& *p
!= ' ');
1195 if (find_exe_file( name
, buffer
, buflen
, handle
))
1202 if (!ret
|| !strchr( name
, ' ' )) goto done
; /* no change necessary */
1204 /* now build a new command-line with quotes */
1206 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, strlen(cmdline
) + 3 ))) goto done
;
1207 sprintf( ret
, "\"%s\"%s", name
, p
);
1210 HeapFree( GetProcessHeap(), 0, name
);
1215 /**********************************************************************
1216 * CreateProcessA (KERNEL32.@)
1218 BOOL WINAPI
CreateProcessA( LPCSTR app_name
, LPSTR cmd_line
, LPSECURITY_ATTRIBUTES process_attr
,
1219 LPSECURITY_ATTRIBUTES thread_attr
, BOOL inherit
,
1220 DWORD flags
, LPVOID env
, LPCSTR cur_dir
,
1221 LPSTARTUPINFOA startup_info
, LPPROCESS_INFORMATION info
)
1225 const char *unixdir
= NULL
;
1226 DOS_FULL_NAME full_dir
;
1227 char name
[MAX_PATH
];
1231 /* Process the AppName and/or CmdLine to get module name and path */
1233 TRACE("app %s cmdline %s\n", debugstr_a(app_name
), debugstr_a(cmd_line
) );
1235 if (!(tidy_cmdline
= get_file_name( app_name
, cmd_line
, name
, sizeof(name
), &hFile
)))
1237 if (hFile
== INVALID_HANDLE_VALUE
) goto done
;
1239 /* Warn if unsupported features are used */
1241 if (flags
& NORMAL_PRIORITY_CLASS
)
1242 FIXME("(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name
);
1243 if (flags
& IDLE_PRIORITY_CLASS
)
1244 FIXME("(%s,...): IDLE_PRIORITY_CLASS ignored\n", name
);
1245 if (flags
& HIGH_PRIORITY_CLASS
)
1246 FIXME("(%s,...): HIGH_PRIORITY_CLASS ignored\n", name
);
1247 if (flags
& REALTIME_PRIORITY_CLASS
)
1248 FIXME("(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name
);
1249 if (flags
& CREATE_NEW_PROCESS_GROUP
)
1250 FIXME("(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name
);
1251 if (flags
& CREATE_UNICODE_ENVIRONMENT
)
1252 FIXME("(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name
);
1253 if (flags
& CREATE_SEPARATE_WOW_VDM
)
1254 FIXME("(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name
);
1255 if (flags
& CREATE_SHARED_WOW_VDM
)
1256 FIXME("(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name
);
1257 if (flags
& CREATE_DEFAULT_ERROR_MODE
)
1258 FIXME("(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name
);
1259 if (flags
& CREATE_NO_WINDOW
)
1260 FIXME("(%s,...): CREATE_NO_WINDOW ignored\n", name
);
1261 if (flags
& PROFILE_USER
)
1262 FIXME("(%s,...): PROFILE_USER ignored\n", name
);
1263 if (flags
& PROFILE_KERNEL
)
1264 FIXME("(%s,...): PROFILE_KERNEL ignored\n", name
);
1265 if (flags
& PROFILE_SERVER
)
1266 FIXME("(%s,...): PROFILE_SERVER ignored\n", name
);
1267 if (startup_info
->lpDesktop
)
1268 FIXME("(%s,...): startup_info->lpDesktop %s ignored\n",
1269 name
, debugstr_a(startup_info
->lpDesktop
));
1270 if (startup_info
->dwFlags
& STARTF_RUNFULLSCREEN
)
1271 FIXME("(%s,...): STARTF_RUNFULLSCREEN ignored\n", name
);
1272 if (startup_info
->dwFlags
& STARTF_FORCEONFEEDBACK
)
1273 FIXME("(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name
);
1274 if (startup_info
->dwFlags
& STARTF_FORCEOFFFEEDBACK
)
1275 FIXME("(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name
);
1276 if (startup_info
->dwFlags
& STARTF_USEHOTKEY
)
1277 FIXME("(%s,...): STARTF_USEHOTKEY ignored\n", name
);
1281 UNICODE_STRING cur_dirW
;
1282 RtlCreateUnicodeStringFromAsciiz(&cur_dirW
, cur_dir
);
1283 if (DOSFS_GetFullName( cur_dirW
.Buffer
, TRUE
, &full_dir
))
1284 unixdir
= full_dir
.long_name
;
1285 RtlFreeUnicodeString(&cur_dirW
);
1289 WCHAR buf
[MAX_PATH
];
1290 if (GetCurrentDirectoryW(MAX_PATH
, buf
))
1292 if (DOSFS_GetFullName( buf
, TRUE
, &full_dir
)) unixdir
= full_dir
.long_name
;
1296 info
->hThread
= info
->hProcess
= 0;
1297 info
->dwProcessId
= info
->dwThreadId
= 0;
1299 /* Determine executable type */
1301 if (!hFile
) /* builtin exe */
1303 TRACE( "starting %s as Winelib app\n", debugstr_a(name
) );
1304 retv
= create_process( 0, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1305 inherit
, flags
, startup_info
, info
, unixdir
);
1309 switch( MODULE_GetBinaryType( hFile
))
1312 TRACE( "starting %s as Win32 binary\n", debugstr_a(name
) );
1313 retv
= create_process( hFile
, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1314 inherit
, flags
, startup_info
, info
, unixdir
);
1318 TRACE( "starting %s as Win16/DOS binary\n", debugstr_a(name
) );
1319 retv
= create_vdm_process( name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1320 inherit
, flags
, startup_info
, info
, unixdir
);
1323 FIXME( "%s is OS/2 binary, not supported\n", debugstr_a(name
) );
1324 SetLastError( ERROR_BAD_EXE_FORMAT
);
1327 TRACE( "not starting %s since it is a dll\n", debugstr_a(name
) );
1328 SetLastError( ERROR_BAD_EXE_FORMAT
);
1330 case BINARY_UNIX_LIB
:
1331 TRACE( "%s is a Unix library, starting as Winelib app\n", debugstr_a(name
) );
1332 retv
= create_process( hFile
, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1333 inherit
, flags
, startup_info
, info
, unixdir
);
1335 case BINARY_UNKNOWN
:
1336 /* check for .com or .bat extension */
1337 if ((p
= strrchr( name
, '.' )))
1339 if (!FILE_strcasecmp( p
, ".com" ))
1341 TRACE( "starting %s as DOS binary\n", debugstr_a(name
) );
1342 retv
= create_vdm_process( name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1343 inherit
, flags
, startup_info
, info
, unixdir
);
1346 if (!FILE_strcasecmp( p
, ".bat" ))
1348 char comspec
[MAX_PATH
];
1349 if (GetEnvironmentVariableA("COMSPEC", comspec
, sizeof(comspec
)))
1352 if ((newcmdline
= HeapAlloc( GetProcessHeap(), 0,
1353 strlen(comspec
) + 4 + strlen(tidy_cmdline
) + 1)))
1355 sprintf( newcmdline
, "%s /c %s", comspec
, tidy_cmdline
);
1356 TRACE( "starting %s as batch binary: %s\n",
1357 debugstr_a(name
), debugstr_a(newcmdline
) );
1358 retv
= CreateProcessA( comspec
, newcmdline
, process_attr
, thread_attr
,
1359 inherit
, flags
, env
, cur_dir
, startup_info
, info
);
1360 HeapFree( GetProcessHeap(), 0, newcmdline
);
1367 case BINARY_UNIX_EXE
:
1369 /* unknown file, try as unix executable */
1370 UNICODE_STRING nameW
;
1371 DOS_FULL_NAME full_name
;
1372 const char *unixfilename
= name
;
1374 TRACE( "starting %s as Unix binary\n", debugstr_a(name
) );
1376 RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
1377 if (DOSFS_GetFullName( nameW
.Buffer
, TRUE
, &full_name
)) unixfilename
= full_name
.long_name
;
1378 RtlFreeUnicodeString(&nameW
);
1379 retv
= (fork_and_exec( unixfilename
, tidy_cmdline
, env
, unixdir
) != -1);
1383 CloseHandle( hFile
);
1386 if (tidy_cmdline
!= cmd_line
) HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1391 /**********************************************************************
1392 * CreateProcessW (KERNEL32.@)
1394 * lpReserved is not converted
1396 BOOL WINAPI
CreateProcessW( LPCWSTR app_name
, LPWSTR cmd_line
, LPSECURITY_ATTRIBUTES process_attr
,
1397 LPSECURITY_ATTRIBUTES thread_attr
, BOOL inherit
, DWORD flags
,
1398 LPVOID env
, LPCWSTR cur_dir
, LPSTARTUPINFOW startup_info
,
1399 LPPROCESS_INFORMATION info
)
1402 STARTUPINFOA StartupInfoA
;
1404 LPSTR app_nameA
= HEAP_strdupWtoA (GetProcessHeap(),0,app_name
);
1405 LPSTR cmd_lineA
= HEAP_strdupWtoA (GetProcessHeap(),0,cmd_line
);
1406 LPSTR cur_dirA
= HEAP_strdupWtoA (GetProcessHeap(),0,cur_dir
);
1408 memcpy (&StartupInfoA
, startup_info
, sizeof(STARTUPINFOA
));
1409 StartupInfoA
.lpDesktop
= HEAP_strdupWtoA (GetProcessHeap(),0,startup_info
->lpDesktop
);
1410 StartupInfoA
.lpTitle
= HEAP_strdupWtoA (GetProcessHeap(),0,startup_info
->lpTitle
);
1412 TRACE("(%s,%s,...)\n", debugstr_w(app_name
), debugstr_w(cmd_line
));
1414 if (startup_info
->lpReserved
)
1415 FIXME("StartupInfo.lpReserved is used, please report (%s)\n",
1416 debugstr_w(startup_info
->lpReserved
));
1418 ret
= CreateProcessA( app_nameA
, cmd_lineA
, process_attr
, thread_attr
,
1419 inherit
, flags
, env
, cur_dirA
, &StartupInfoA
, info
);
1421 HeapFree( GetProcessHeap(), 0, cur_dirA
);
1422 HeapFree( GetProcessHeap(), 0, cmd_lineA
);
1423 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpDesktop
);
1424 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpTitle
);
1430 /***********************************************************************
1433 * Wrapper to call WaitForInputIdle USER function
1435 typedef DWORD (WINAPI
*WaitForInputIdle_ptr
)( HANDLE hProcess
, DWORD dwTimeOut
);
1437 static DWORD
wait_input_idle( HANDLE process
, DWORD timeout
)
1439 HMODULE mod
= GetModuleHandleA( "user32.dll" );
1442 WaitForInputIdle_ptr ptr
= (WaitForInputIdle_ptr
)GetProcAddress( mod
, "WaitForInputIdle" );
1443 if (ptr
) return ptr( process
, timeout
);
1449 /***********************************************************************
1450 * WinExec (KERNEL32.@)
1452 UINT WINAPI
WinExec( LPCSTR lpCmdLine
, UINT nCmdShow
)
1454 PROCESS_INFORMATION info
;
1455 STARTUPINFOA startup
;
1459 memset( &startup
, 0, sizeof(startup
) );
1460 startup
.cb
= sizeof(startup
);
1461 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
1462 startup
.wShowWindow
= nCmdShow
;
1464 /* cmdline needs to be writeable for CreateProcess */
1465 if (!(cmdline
= HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine
)+1 ))) return 0;
1466 strcpy( cmdline
, lpCmdLine
);
1468 if (CreateProcessA( NULL
, cmdline
, NULL
, NULL
, FALSE
,
1469 0, NULL
, NULL
, &startup
, &info
))
1471 /* Give 30 seconds to the app to come up */
1472 if (wait_input_idle( info
.hProcess
, 30000 ) == 0xFFFFFFFF)
1473 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1475 /* Close off the handles */
1476 CloseHandle( info
.hThread
);
1477 CloseHandle( info
.hProcess
);
1479 else if ((ret
= GetLastError()) >= 32)
1481 FIXME("Strange error set by CreateProcess: %d\n", ret
);
1484 HeapFree( GetProcessHeap(), 0, cmdline
);
1489 /**********************************************************************
1490 * LoadModule (KERNEL32.@)
1492 HINSTANCE WINAPI
LoadModule( LPCSTR name
, LPVOID paramBlock
)
1494 LOADPARAMS
*params
= (LOADPARAMS
*)paramBlock
;
1495 PROCESS_INFORMATION info
;
1496 STARTUPINFOA startup
;
1497 HINSTANCE hInstance
;
1499 char filename
[MAX_PATH
];
1502 if (!name
) return (HINSTANCE
)ERROR_FILE_NOT_FOUND
;
1504 if (!SearchPathA( NULL
, name
, ".exe", sizeof(filename
), filename
, NULL
) &&
1505 !SearchPathA( NULL
, name
, NULL
, sizeof(filename
), filename
, NULL
))
1506 return (HINSTANCE
)GetLastError();
1508 len
= (BYTE
)params
->lpCmdLine
[0];
1509 if (!(cmdline
= HeapAlloc( GetProcessHeap(), 0, strlen(filename
) + len
+ 2 )))
1510 return (HINSTANCE
)ERROR_NOT_ENOUGH_MEMORY
;
1512 strcpy( cmdline
, filename
);
1513 p
= cmdline
+ strlen(cmdline
);
1515 memcpy( p
, params
->lpCmdLine
+ 1, len
);
1518 memset( &startup
, 0, sizeof(startup
) );
1519 startup
.cb
= sizeof(startup
);
1520 if (params
->lpCmdShow
)
1522 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
1523 startup
.wShowWindow
= params
->lpCmdShow
[1];
1526 if (CreateProcessA( filename
, cmdline
, NULL
, NULL
, FALSE
, 0,
1527 params
->lpEnvAddress
, NULL
, &startup
, &info
))
1529 /* Give 30 seconds to the app to come up */
1530 if (wait_input_idle( info
.hProcess
, 30000 ) == 0xFFFFFFFF )
1531 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1532 hInstance
= (HINSTANCE
)33;
1533 /* Close off the handles */
1534 CloseHandle( info
.hThread
);
1535 CloseHandle( info
.hProcess
);
1537 else if ((hInstance
= (HINSTANCE
)GetLastError()) >= (HINSTANCE
)32)
1539 FIXME("Strange error set by CreateProcess: %p\n", hInstance
);
1540 hInstance
= (HINSTANCE
)11;
1543 HeapFree( GetProcessHeap(), 0, cmdline
);
1548 /******************************************************************************
1549 * TerminateProcess (KERNEL32.@)
1551 BOOL WINAPI
TerminateProcess( HANDLE handle
, DWORD exit_code
)
1553 NTSTATUS status
= NtTerminateProcess( handle
, exit_code
);
1554 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1559 /***********************************************************************
1560 * GetExitCodeProcess [KERNEL32.@]
1562 * Gets termination status of specified process
1568 BOOL WINAPI
GetExitCodeProcess(
1569 HANDLE hProcess
, /* [in] handle to the process */
1570 LPDWORD lpExitCode
) /* [out] address to receive termination status */
1573 SERVER_START_REQ( get_process_info
)
1575 req
->handle
= hProcess
;
1576 ret
= !wine_server_call_err( req
);
1577 if (ret
&& lpExitCode
) *lpExitCode
= reply
->exit_code
;
1584 /***********************************************************************
1585 * SetErrorMode (KERNEL32.@)
1587 UINT WINAPI
SetErrorMode( UINT mode
)
1589 UINT old
= current_process
.error_mode
;
1590 current_process
.error_mode
= mode
;
1595 /**********************************************************************
1596 * TlsAlloc [KERNEL32.@] Allocates a TLS index.
1598 * Allocates a thread local storage index
1601 * Success: TLS Index
1602 * Failure: 0xFFFFFFFF
1604 DWORD WINAPI
TlsAlloc( void )
1606 DWORD i
, mask
, ret
= 0;
1607 DWORD
*bits
= current_process
.tls_bits
;
1608 RtlAcquirePebLock();
1609 if (*bits
== 0xffffffff)
1613 if (*bits
== 0xffffffff)
1615 RtlReleasePebLock();
1616 SetLastError( ERROR_NO_MORE_ITEMS
);
1620 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) if (!(*bits
& mask
)) break;
1622 RtlReleasePebLock();
1623 NtCurrentTeb()->TlsSlots
[ret
+i
] = 0; /* clear the value */
1628 /**********************************************************************
1629 * TlsFree [KERNEL32.@] Releases a TLS index.
1631 * Releases a thread local storage index, making it available for reuse
1637 BOOL WINAPI
TlsFree(
1638 DWORD index
) /* [in] TLS Index to free */
1640 DWORD mask
= (1 << (index
& 31));
1641 DWORD
*bits
= current_process
.tls_bits
;
1644 SetLastError( ERROR_INVALID_PARAMETER
);
1647 if (index
>= 32) bits
++;
1648 RtlAcquirePebLock();
1649 if (!(*bits
& mask
)) /* already free? */
1651 RtlReleasePebLock();
1652 SetLastError( ERROR_INVALID_PARAMETER
);
1656 NtSetInformationThread( GetCurrentThread(), ThreadZeroTlsCell
, &index
, sizeof(index
) );
1657 RtlReleasePebLock();
1662 /**********************************************************************
1663 * TlsGetValue [KERNEL32.@] Gets value in a thread's TLS slot
1666 * Success: Value stored in calling thread's TLS slot for index
1667 * Failure: 0 and GetLastError returns NO_ERROR
1669 LPVOID WINAPI
TlsGetValue(
1670 DWORD index
) /* [in] TLS index to retrieve value for */
1674 SetLastError( ERROR_INVALID_PARAMETER
);
1677 SetLastError( ERROR_SUCCESS
);
1678 return NtCurrentTeb()->TlsSlots
[index
];
1682 /**********************************************************************
1683 * TlsSetValue [KERNEL32.@] Stores a value in the thread's TLS slot.
1689 BOOL WINAPI
TlsSetValue(
1690 DWORD index
, /* [in] TLS index to set value for */
1691 LPVOID value
) /* [in] Value to be stored */
1695 SetLastError( ERROR_INVALID_PARAMETER
);
1698 NtCurrentTeb()->TlsSlots
[index
] = value
;
1703 /***********************************************************************
1704 * GetProcessFlags (KERNEL32.@)
1706 DWORD WINAPI
GetProcessFlags( DWORD processid
)
1708 IMAGE_NT_HEADERS
*nt
;
1711 if (processid
&& processid
!= GetCurrentProcessId()) return 0;
1713 if ((nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
)))
1715 if (nt
->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
)
1716 flags
|= PDB32_CONSOLE_PROC
;
1718 if (!AreFileApisANSI()) flags
|= PDB32_FILE_APIS_OEM
;
1719 if (IsDebuggerPresent()) flags
|= PDB32_DEBUGGED
;
1724 /***********************************************************************
1725 * GetProcessDword (KERNEL.485)
1726 * GetProcessDword (KERNEL32.18)
1727 * 'Of course you cannot directly access Windows internal structures'
1729 DWORD WINAPI
GetProcessDword( DWORD dwProcessID
, INT offset
)
1734 TRACE("(%ld, %d)\n", dwProcessID
, offset
);
1736 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
1738 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
1744 case GPD_APP_COMPAT_FLAGS
:
1745 return GetAppCompatFlags16(0);
1746 case GPD_LOAD_DONE_EVENT
:
1748 case GPD_HINSTANCE16
:
1749 return GetTaskDS16();
1750 case GPD_WINDOWS_VERSION
:
1751 return GetExeVersion16();
1753 return (DWORD
)NtCurrentTeb() - 0x10 /* FIXME */;
1755 return (DWORD
)NtCurrentTeb()->Peb
;
1756 case GPD_STARTF_SHELLDATA
: /* return stdoutput handle from startupinfo ??? */
1757 GetStartupInfoW(&siw
);
1758 return (DWORD
)siw
.hStdOutput
;
1759 case GPD_STARTF_HOTKEY
: /* return stdinput handle from startupinfo ??? */
1760 GetStartupInfoW(&siw
);
1761 return (DWORD
)siw
.hStdInput
;
1762 case GPD_STARTF_SHOWWINDOW
:
1763 GetStartupInfoW(&siw
);
1764 return siw
.wShowWindow
;
1765 case GPD_STARTF_SIZE
:
1766 GetStartupInfoW(&siw
);
1768 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
1770 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
1771 return MAKELONG( x
, y
);
1772 case GPD_STARTF_POSITION
:
1773 GetStartupInfoW(&siw
);
1775 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
1777 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
1778 return MAKELONG( x
, y
);
1779 case GPD_STARTF_FLAGS
:
1780 GetStartupInfoW(&siw
);
1785 return GetProcessFlags(0);
1787 return process_dword
;
1789 ERR("Unknown offset %d\n", offset
);
1794 /***********************************************************************
1795 * SetProcessDword (KERNEL.484)
1796 * 'Of course you cannot directly access Windows internal structures'
1798 void WINAPI
SetProcessDword( DWORD dwProcessID
, INT offset
, DWORD value
)
1800 TRACE("(%ld, %d)\n", dwProcessID
, offset
);
1802 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
1804 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
1810 case GPD_APP_COMPAT_FLAGS
:
1811 case GPD_LOAD_DONE_EVENT
:
1812 case GPD_HINSTANCE16
:
1813 case GPD_WINDOWS_VERSION
:
1816 case GPD_STARTF_SHELLDATA
:
1817 case GPD_STARTF_HOTKEY
:
1818 case GPD_STARTF_SHOWWINDOW
:
1819 case GPD_STARTF_SIZE
:
1820 case GPD_STARTF_POSITION
:
1821 case GPD_STARTF_FLAGS
:
1824 ERR("Not allowed to modify offset %d\n", offset
);
1827 process_dword
= value
;
1830 ERR("Unknown offset %d\n", offset
);
1836 /***********************************************************************
1837 * ExitProcess (KERNEL.466)
1839 void WINAPI
ExitProcess16( WORD status
)
1842 ReleaseThunkLock( &count
);
1843 ExitProcess( status
);
1847 /*********************************************************************
1848 * OpenProcess (KERNEL32.@)
1850 HANDLE WINAPI
OpenProcess( DWORD access
, BOOL inherit
, DWORD id
)
1853 SERVER_START_REQ( open_process
)
1856 req
->access
= access
;
1857 req
->inherit
= inherit
;
1858 if (!wine_server_call_err( req
)) ret
= reply
->handle
;
1865 /*********************************************************************
1866 * MapProcessHandle (KERNEL.483)
1868 DWORD WINAPI
MapProcessHandle( HANDLE handle
)
1871 SERVER_START_REQ( get_process_info
)
1873 req
->handle
= handle
;
1874 if (!wine_server_call_err( req
)) ret
= reply
->pid
;
1881 /***********************************************************************
1882 * SetPriorityClass (KERNEL32.@)
1884 BOOL WINAPI
SetPriorityClass( HANDLE hprocess
, DWORD priorityclass
)
1887 SERVER_START_REQ( set_process_info
)
1889 req
->handle
= hprocess
;
1890 req
->priority
= priorityclass
;
1891 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
1892 ret
= !wine_server_call_err( req
);
1899 /***********************************************************************
1900 * GetPriorityClass (KERNEL32.@)
1902 DWORD WINAPI
GetPriorityClass(HANDLE hprocess
)
1905 SERVER_START_REQ( get_process_info
)
1907 req
->handle
= hprocess
;
1908 if (!wine_server_call_err( req
)) ret
= reply
->priority
;
1915 /***********************************************************************
1916 * SetProcessAffinityMask (KERNEL32.@)
1918 BOOL WINAPI
SetProcessAffinityMask( HANDLE hProcess
, DWORD affmask
)
1921 SERVER_START_REQ( set_process_info
)
1923 req
->handle
= hProcess
;
1924 req
->affinity
= affmask
;
1925 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
1926 ret
= !wine_server_call_err( req
);
1933 /**********************************************************************
1934 * GetProcessAffinityMask (KERNEL32.@)
1936 BOOL WINAPI
GetProcessAffinityMask( HANDLE hProcess
,
1937 LPDWORD lpProcessAffinityMask
,
1938 LPDWORD lpSystemAffinityMask
)
1941 SERVER_START_REQ( get_process_info
)
1943 req
->handle
= hProcess
;
1944 if (!wine_server_call_err( req
))
1946 if (lpProcessAffinityMask
) *lpProcessAffinityMask
= reply
->process_affinity
;
1947 if (lpSystemAffinityMask
) *lpSystemAffinityMask
= reply
->system_affinity
;
1956 /***********************************************************************
1957 * GetProcessVersion (KERNEL32.@)
1959 DWORD WINAPI
GetProcessVersion( DWORD processid
)
1961 IMAGE_NT_HEADERS
*nt
;
1963 if (processid
&& processid
!= GetCurrentProcessId())
1965 FIXME("should use ReadProcessMemory\n");
1968 if ((nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
)))
1969 return ((nt
->OptionalHeader
.MajorSubsystemVersion
<< 16) |
1970 nt
->OptionalHeader
.MinorSubsystemVersion
);
1975 /***********************************************************************
1976 * SetProcessWorkingSetSize [KERNEL32.@]
1977 * Sets the min/max working set sizes for a specified process.
1980 * hProcess [I] Handle to the process of interest
1981 * minset [I] Specifies minimum working set size
1982 * maxset [I] Specifies maximum working set size
1986 BOOL WINAPI
SetProcessWorkingSetSize(HANDLE hProcess
, SIZE_T minset
,
1989 FIXME("(%p,%ld,%ld): stub - harmless\n",hProcess
,minset
,maxset
);
1990 if(( minset
== (SIZE_T
)-1) && (maxset
== (SIZE_T
)-1)) {
1991 /* Trim the working set to zero */
1992 /* Swap the process out of physical RAM */
1997 /***********************************************************************
1998 * GetProcessWorkingSetSize (KERNEL32.@)
2000 BOOL WINAPI
GetProcessWorkingSetSize(HANDLE hProcess
, PSIZE_T minset
,
2003 FIXME("(%p,%p,%p): stub\n",hProcess
,minset
,maxset
);
2004 /* 32 MB working set size */
2005 if (minset
) *minset
= 32*1024*1024;
2006 if (maxset
) *maxset
= 32*1024*1024;
2011 /***********************************************************************
2012 * SetProcessShutdownParameters (KERNEL32.@)
2014 BOOL WINAPI
SetProcessShutdownParameters(DWORD level
, DWORD flags
)
2016 FIXME("(%08lx, %08lx): partial stub.\n", level
, flags
);
2017 shutdown_flags
= flags
;
2018 shutdown_priority
= level
;
2023 /***********************************************************************
2024 * GetProcessShutdownParameters (KERNEL32.@)
2027 BOOL WINAPI
GetProcessShutdownParameters( LPDWORD lpdwLevel
, LPDWORD lpdwFlags
)
2029 *lpdwLevel
= shutdown_priority
;
2030 *lpdwFlags
= shutdown_flags
;
2035 /***********************************************************************
2036 * GetProcessPriorityBoost (KERNEL32.@)
2038 BOOL WINAPI
GetProcessPriorityBoost(HANDLE hprocess
,PBOOL pDisablePriorityBoost
)
2040 FIXME("(%p,%p): semi-stub\n", hprocess
, pDisablePriorityBoost
);
2042 /* Report that no boost is present.. */
2043 *pDisablePriorityBoost
= FALSE
;
2048 /***********************************************************************
2049 * SetProcessPriorityBoost (KERNEL32.@)
2051 BOOL WINAPI
SetProcessPriorityBoost(HANDLE hprocess
,BOOL disableboost
)
2053 FIXME("(%p,%d): stub\n",hprocess
,disableboost
);
2054 /* Say we can do it. I doubt the program will notice that we don't. */
2059 /***********************************************************************
2060 * ReadProcessMemory (KERNEL32.@)
2062 BOOL WINAPI
ReadProcessMemory( HANDLE process
, LPCVOID addr
, LPVOID buffer
, SIZE_T size
,
2063 SIZE_T
*bytes_read
)
2065 NTSTATUS status
= NtReadVirtualMemory( process
, addr
, buffer
, size
, bytes_read
);
2066 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
2071 /***********************************************************************
2072 * WriteProcessMemory (KERNEL32.@)
2074 BOOL WINAPI
WriteProcessMemory( HANDLE process
, LPVOID addr
, LPCVOID buffer
, SIZE_T size
,
2075 SIZE_T
*bytes_written
)
2077 NTSTATUS status
= NtWriteVirtualMemory( process
, addr
, buffer
, size
, bytes_written
);
2078 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
2083 /****************************************************************************
2084 * FlushInstructionCache (KERNEL32.@)
2086 BOOL WINAPI
FlushInstructionCache(HANDLE hProcess
, LPCVOID lpBaseAddress
, SIZE_T dwSize
)
2088 if (GetVersion() & 0x80000000) return TRUE
; /* not NT, always TRUE */
2089 FIXME("(%p,%p,0x%08lx): stub\n",hProcess
, lpBaseAddress
, dwSize
);
2094 /******************************************************************
2095 * GetProcessIoCounters (KERNEL32.@)
2097 BOOL WINAPI
GetProcessIoCounters(HANDLE hProcess
, PIO_COUNTERS ioc
)
2101 status
= NtQueryInformationProcess(hProcess
, ProcessIoCounters
,
2102 ioc
, sizeof(*ioc
), NULL
);
2103 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
2107 /***********************************************************************
2108 * ProcessIdToSessionId (KERNEL32.@)
2109 * This function is available on Terminal Server 4SP4 and Windows 2000
2111 BOOL WINAPI
ProcessIdToSessionId( DWORD procid
, DWORD
*sessionid_ptr
)
2113 /* According to MSDN, if the calling process is not in a terminal
2114 * services environment, then the sessionid returned is zero.
2121 /***********************************************************************
2122 * RegisterServiceProcess (KERNEL.491)
2123 * RegisterServiceProcess (KERNEL32.@)
2125 * A service process calls this function to ensure that it continues to run
2126 * even after a user logged off.
2128 DWORD WINAPI
RegisterServiceProcess(DWORD dwProcessId
, DWORD dwType
)
2130 /* I don't think that Wine needs to do anything in that function */
2131 return 1; /* success */
2135 /**************************************************************************
2136 * SetFileApisToOEM (KERNEL32.@)
2138 VOID WINAPI
SetFileApisToOEM(void)
2140 oem_file_apis
= TRUE
;
2144 /**************************************************************************
2145 * SetFileApisToANSI (KERNEL32.@)
2147 VOID WINAPI
SetFileApisToANSI(void)
2149 oem_file_apis
= FALSE
;
2153 /******************************************************************************
2154 * AreFileApisANSI [KERNEL32.@] Determines if file functions are using ANSI
2157 * TRUE: Set of file functions is using ANSI code page
2158 * FALSE: Set of file functions is using OEM code page
2160 BOOL WINAPI
AreFileApisANSI(void)
2162 return !oem_file_apis
;
2166 /***********************************************************************
2167 * GetCurrentProcess (KERNEL32.@)
2169 #undef GetCurrentProcess
2170 HANDLE WINAPI
GetCurrentProcess(void)
2172 return (HANDLE
)0xffffffff;