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"
35 #include "wine/winbase16.h"
36 #include "wine/winuser16.h"
37 #include "wine/exception.h"
38 #include "wine/library.h"
46 #include "wine/server.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(process
);
51 WINE_DECLARE_DEBUG_CHANNEL(relay
);
52 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
53 WINE_DECLARE_DEBUG_CHANNEL(win32
);
57 /* Win32 process database */
60 LONG header
[2]; /* 00 Kernel object header */
61 HMODULE module
; /* 08 Main exe module (NT) */
62 void *event
; /* 0c Pointer to an event object (unused) */
63 DWORD exit_code
; /* 10 Process exit code */
64 DWORD unknown2
; /* 14 Unknown */
65 HANDLE heap
; /* 18 Default process heap */
66 HANDLE mem_context
; /* 1c Process memory context */
67 DWORD flags
; /* 20 Flags */
68 void *pdb16
; /* 24 DOS PSP */
69 WORD PSP_sel
; /* 28 Selector to DOS PSP */
70 WORD imte
; /* 2a IMTE for the process module */
71 WORD threads
; /* 2c Number of threads */
72 WORD running_threads
; /* 2e Number of running threads */
73 WORD free_lib_count
; /* 30 Recursion depth of FreeLibrary calls */
74 WORD ring0_threads
; /* 32 Number of ring 0 threads */
75 HANDLE system_heap
; /* 34 System heap to allocate handles */
76 HTASK task
; /* 38 Win16 task */
77 void *mem_map_files
; /* 3c Pointer to mem-mapped files */
78 struct _ENVDB
*env_db
; /* 40 Environment database */
79 void *handle_table
; /* 44 Handle table */
80 struct _PDB
*parent
; /* 48 Parent process */
81 void *modref_list
; /* 4c MODREF list */
82 void *thread_list
; /* 50 List of threads */
83 void *debuggee_CB
; /* 54 Debuggee context block */
84 void *local_heap_free
; /* 58 Head of local heap free list */
85 DWORD unknown4
; /* 5c Unknown */
86 CRITICAL_SECTION crit_section
; /* 60 Critical section */
87 DWORD unknown5
[3]; /* 78 Unknown */
88 void *console
; /* 84 Console */
89 DWORD tls_bits
[2]; /* 88 TLS in-use bits */
90 DWORD process_dword
; /* 90 Unknown */
91 struct _PDB
*group
; /* 94 Process group */
92 void *exe_modref
; /* 98 MODREF for the process EXE */
93 void *top_filter
; /* 9c Top exception filter */
94 DWORD priority
; /* a0 Priority level */
95 HANDLE heap_list
; /* a4 Head of process heap list */
96 void *heap_handles
; /* a8 Head of heap handles list */
97 DWORD unknown6
; /* ac Unknown */
98 void *console_provider
; /* b0 Console provider (??) */
99 WORD env_selector
; /* b4 Selector to process environment */
100 WORD error_mode
; /* b6 Error mode */
101 HANDLE load_done_evt
; /* b8 Event for process loading done */
102 void *UTState
; /* bc Head of Univeral Thunk list */
103 DWORD unknown8
; /* c0 Unknown (NT) */
104 LCID locale
; /* c4 Locale to be queried by GetThreadLocale (NT) */
110 #define PDB32_DEBUGGED 0x0001 /* Process is being debugged */
111 #define PDB32_WIN16_PROC 0x0008 /* Win16 process */
112 #define PDB32_DOS_PROC 0x0010 /* Dos process */
113 #define PDB32_CONSOLE_PROC 0x0020 /* Console process */
114 #define PDB32_FILE_APIS_OEM 0x0040 /* File APIs are OEM */
115 #define PDB32_WIN32S_PROC 0x8000 /* Win32s process */
117 static char main_exe_name
[MAX_PATH
];
118 static char *main_exe_name_ptr
= main_exe_name
;
119 static HANDLE main_exe_file
;
120 static unsigned int server_startticks
;
122 int main_create_flags
= 0;
124 /* memory/environ.c */
125 extern struct _ENVDB
*ENV_InitStartupInfo( size_t info_size
, char *main_exe_name
,
126 size_t main_exe_size
);
127 extern BOOL
ENV_BuildCommandLine( char **argv
);
128 extern STARTUPINFOA current_startupinfo
;
130 /* scheduler/pthread.c */
131 extern void PTHREAD_init_done(void);
133 extern void RELAY_InitDebugLists(void);
134 extern BOOL
MAIN_MainInit(void);
135 extern void VERSION_Init( const char *appname
);
137 typedef WORD (WINAPI
*pUserSignalProc
)( UINT
, DWORD
, DWORD
, HMODULE16
);
139 /***********************************************************************
140 * PROCESS_CallUserSignalProc
142 * FIXME: Some of the signals aren't sent correctly!
144 * The exact meaning of the USER signals is undocumented, but this
145 * should cover the basic idea:
147 * USIG_DLL_UNLOAD_WIN16
148 * This is sent when a 16-bit module is unloaded.
150 * USIG_DLL_UNLOAD_WIN32
151 * This is sent when a 32-bit module is unloaded.
153 * USIG_DLL_UNLOAD_ORPHANS
154 * This is sent after the last Win3.1 module is unloaded,
155 * to allow removal of orphaned menus.
157 * USIG_FAULT_DIALOG_PUSH
158 * USIG_FAULT_DIALOG_POP
159 * These are called to allow USER to prepare for displaying a
160 * fault dialog, even though the fault might have happened while
161 * inside a USER critical section.
164 * This is called from the context of a new thread, as soon as it
165 * has started to run.
168 * This is called, still in its context, just before a thread is
169 * about to terminate.
171 * USIG_PROCESS_CREATE
172 * This is called, in the parent process context, after a new process
176 * This is called in the new process context, just after the main thread
177 * has started execution (after the main thread's USIG_THREAD_INIT has
180 * USIG_PROCESS_LOADED
181 * This is called after the executable file has been loaded into the
182 * new process context.
184 * USIG_PROCESS_RUNNING
185 * This is called immediately before the main entry point is called.
188 * This is called in the context of a process that is about to
189 * terminate (but before the last thread's USIG_THREAD_EXIT has
192 * USIG_PROCESS_DESTROY
193 * This is called after a process has terminated.
196 * The meaning of the dwFlags bits is as follows:
199 * Current process is 32-bit.
202 * Current process is a (Win32) GUI process.
204 * USIG_FLAGS_FEEDBACK
205 * Current process needs 'feedback' (determined from the STARTUPINFO
206 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
209 * The signal is being sent due to a fault.
211 void PROCESS_CallUserSignalProc( UINT uCode
, HMODULE16 hModule
)
215 pUserSignalProc proc
;
217 if (!(user
= GetModuleHandleA( "user32.dll" ))) return;
218 if (!(proc
= (pUserSignalProc
)GetProcAddress( user
, "UserSignalProc" ))) return;
220 /* Determine dwFlags */
222 if ( !(current_process
.flags
& PDB32_WIN16_PROC
) ) dwFlags
|= USIG_FLAGS_WIN32
;
223 if ( !(current_process
.flags
& PDB32_CONSOLE_PROC
) ) dwFlags
|= USIG_FLAGS_GUI
;
225 if ( dwFlags
& USIG_FLAGS_GUI
)
227 /* Feedback defaults to ON */
228 if ( !(current_startupinfo
.dwFlags
& STARTF_FORCEOFFFEEDBACK
) )
229 dwFlags
|= USIG_FLAGS_FEEDBACK
;
233 /* Feedback defaults to OFF */
234 if (current_startupinfo
.dwFlags
& STARTF_FORCEONFEEDBACK
)
235 dwFlags
|= USIG_FLAGS_FEEDBACK
;
238 /* Call USER signal proc */
240 if ( uCode
== USIG_THREAD_INIT
|| uCode
== USIG_THREAD_EXIT
)
241 proc( uCode
, GetCurrentThreadId(), dwFlags
, hModule
);
243 proc( uCode
, GetCurrentProcessId(), dwFlags
, hModule
);
247 /***********************************************************************
250 inline static const char *get_basename( const char *name
)
254 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
255 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
260 /***********************************************************************
261 * open_builtin_exe_file
263 * Open an exe file for a builtin exe.
265 static void *open_builtin_exe_file( const char *name
, char *error
, int error_size
, int test_only
)
267 char exename
[MAX_PATH
], *p
;
268 const char *basename
= get_basename(name
);
270 if (strlen(basename
) >= sizeof(exename
)) return NULL
;
271 strcpy( exename
, basename
);
272 for (p
= exename
; *p
; p
++) *p
= FILE_tolower(*p
);
273 return wine_dll_load_main_exe( exename
, error
, error_size
, test_only
);
277 /***********************************************************************
280 * Open a specific exe file, taking load order into account.
281 * Returns the file handle or 0 for a builtin exe.
283 static HANDLE
open_exe_file( const char *name
)
285 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
286 char buffer
[MAX_PATH
];
290 TRACE("looking for %s\n", debugstr_a(name
) );
292 if ((handle
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
,
293 NULL
, OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
295 /* file doesn't exist, check for builtin */
296 if (!FILE_contains_path( name
)) goto error
;
297 if (!MODULE_GetBuiltinPath( name
, "", buffer
, sizeof(buffer
) )) goto error
;
301 MODULE_GetLoadOrder( loadorder
, name
, TRUE
);
303 for(i
= 0; i
< LOADORDER_NTYPES
; i
++)
305 if (loadorder
[i
] == LOADORDER_INVALID
) break;
309 TRACE( "Trying native exe %s\n", debugstr_a(name
) );
310 if (handle
!= INVALID_HANDLE_VALUE
) return handle
;
313 TRACE( "Trying built-in exe %s\n", debugstr_a(name
) );
314 if (open_builtin_exe_file( name
, NULL
, 0, 1 ))
316 if (handle
!= INVALID_HANDLE_VALUE
) CloseHandle(handle
);
323 if (handle
!= INVALID_HANDLE_VALUE
) CloseHandle(handle
);
326 SetLastError( ERROR_FILE_NOT_FOUND
);
327 return INVALID_HANDLE_VALUE
;
331 /***********************************************************************
334 * Open an exe file, and return the full name and file handle.
335 * Returns FALSE if file could not be found.
336 * If file exists but cannot be opened, returns TRUE and set handle to INVALID_HANDLE_VALUE.
337 * If file is a builtin exe, returns TRUE and sets handle to 0.
339 static BOOL
find_exe_file( const char *name
, char *buffer
, int buflen
, HANDLE
*handle
)
341 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
344 TRACE("looking for %s\n", debugstr_a(name
) );
346 if (!SearchPathA( NULL
, name
, ".exe", buflen
, buffer
, NULL
) &&
347 !MODULE_GetBuiltinPath( name
, ".exe", buffer
, buflen
))
349 /* no builtin found, try native without extension in case it is a Unix app */
351 if (SearchPathA( NULL
, name
, NULL
, buflen
, buffer
, NULL
))
353 TRACE( "Trying native/Unix binary %s\n", debugstr_a(buffer
) );
354 if ((*handle
= CreateFileA( buffer
, GENERIC_READ
, FILE_SHARE_READ
,
355 NULL
, OPEN_EXISTING
, 0, 0 )) != INVALID_HANDLE_VALUE
)
361 MODULE_GetLoadOrder( loadorder
, buffer
, TRUE
);
363 for(i
= 0; i
< LOADORDER_NTYPES
; i
++)
365 if (loadorder
[i
] == LOADORDER_INVALID
) break;
369 TRACE( "Trying native exe %s\n", debugstr_a(buffer
) );
370 if ((*handle
= CreateFileA( buffer
, GENERIC_READ
, FILE_SHARE_READ
,
371 NULL
, OPEN_EXISTING
, 0, 0 )) != INVALID_HANDLE_VALUE
)
373 if (GetLastError() != ERROR_FILE_NOT_FOUND
) return TRUE
;
376 TRACE( "Trying built-in exe %s\n", debugstr_a(buffer
) );
377 if (open_builtin_exe_file( buffer
, NULL
, 0, 1 ))
387 SetLastError( ERROR_FILE_NOT_FOUND
);
392 /***********************************************************************
395 * Main process initialisation code
397 static BOOL
process_init( char *argv
[] )
400 size_t info_size
= 0;
402 /* store the program name */
405 /* Fill the initial process structure */
406 current_process
.exit_code
= STILL_ACTIVE
;
407 current_process
.threads
= 1;
408 current_process
.running_threads
= 1;
409 current_process
.ring0_threads
= 1;
410 current_process
.group
= ¤t_process
;
411 current_process
.priority
= 8; /* Normal */
413 /* Setup the server connection */
416 /* Retrieve startup info from the server */
417 SERVER_START_REQ( init_process
)
419 req
->ldt_copy
= &wine_ldt_copy
;
420 if ((ret
= !wine_server_call_err( req
)))
422 main_exe_file
= reply
->exe_file
;
423 main_create_flags
= reply
->create_flags
;
424 info_size
= reply
->info_size
;
425 server_startticks
= reply
->server_start
;
426 current_startupinfo
.hStdInput
= reply
->hstdin
;
427 current_startupinfo
.hStdOutput
= reply
->hstdout
;
428 current_startupinfo
.hStdError
= reply
->hstderr
;
432 if (!ret
) return FALSE
;
434 /* Create the process heap */
435 current_process
.heap
= HeapCreate( HEAP_GROWABLE
, 0, 0 );
437 if (main_create_flags
== 0 &&
438 current_startupinfo
.hStdInput
== 0 &&
439 current_startupinfo
.hStdOutput
== 0 &&
440 current_startupinfo
.hStdError
== 0)
442 /* no parent, and no new console requested, create a simple console with bare handles to
443 * unix stdio input & output streams (aka simple console)
446 wine_server_fd_to_handle( 0, GENERIC_READ
|SYNCHRONIZE
, TRUE
, &handle
);
447 SetStdHandle( STD_INPUT_HANDLE
, handle
);
448 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, TRUE
, &handle
);
449 SetStdHandle( STD_OUTPUT_HANDLE
, handle
);
450 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, TRUE
, &handle
);
451 SetStdHandle( STD_ERROR_HANDLE
, handle
);
453 else if (!(main_create_flags
& (DETACHED_PROCESS
|CREATE_NEW_CONSOLE
)))
455 SetStdHandle( STD_INPUT_HANDLE
, current_startupinfo
.hStdInput
);
456 SetStdHandle( STD_OUTPUT_HANDLE
, current_startupinfo
.hStdOutput
);
457 SetStdHandle( STD_ERROR_HANDLE
, current_startupinfo
.hStdError
);
460 /* Now we can use the pthreads routines */
463 /* Copy the parent environment */
464 if (!(current_process
.env_db
= ENV_InitStartupInfo( info_size
, main_exe_name
,
465 sizeof(main_exe_name
) )))
468 /* Parse command line arguments */
469 OPTIONS_ParseOptions( !info_size
? argv
: NULL
);
471 ret
= MAIN_MainInit();
472 if (TRACE_ON(relay
) || TRACE_ON(snoop
)) RELAY_InitDebugLists();
478 /***********************************************************************
481 * Startup routine of a new process. Runs on the new process stack.
483 static void start_process(void)
485 int debugged
, console_app
;
486 LPTHREAD_START_ROUTINE entry
;
488 HANDLE main_file
= main_exe_file
;
489 IMAGE_NT_HEADERS
*nt
;
491 /* use original argv[0] as name for the main module */
492 if (!main_exe_name
[0])
494 if (!GetLongPathNameA( full_argv0
, main_exe_name
, sizeof(main_exe_name
) ))
495 lstrcpynA( main_exe_name
, full_argv0
, sizeof(main_exe_name
) );
500 UINT drive_type
= GetDriveTypeA( main_exe_name
);
501 /* don't keep the file handle open on removable media */
502 if (drive_type
== DRIVE_REMOVABLE
|| drive_type
== DRIVE_CDROM
) main_file
= 0;
505 /* Retrieve entry point address */
506 nt
= RtlImageNtHeader( current_process
.module
);
507 entry
= (LPTHREAD_START_ROUTINE
)((char*)current_process
.module
+
508 nt
->OptionalHeader
.AddressOfEntryPoint
);
509 console_app
= (nt
->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
);
510 if (console_app
) current_process
.flags
|= PDB32_CONSOLE_PROC
;
512 /* Install signal handlers; this cannot be done before, since we cannot
513 * send exceptions to the debugger before the create process event that
514 * is sent by REQ_INIT_PROCESS_DONE.
515 * We do need the handlers in place by the time the request is over, so
516 * we set them up here. If we segfault between here and the server call
517 * something is very wrong... */
518 if (!SIGNAL_Init()) goto error
;
520 /* Signal the parent process to continue */
521 SERVER_START_REQ( init_process_done
)
523 req
->module
= (void *)current_process
.module
;
524 req
->module_size
= nt
->OptionalHeader
.SizeOfImage
;
526 /* API requires a double indirection */
527 req
->name
= &main_exe_name_ptr
;
528 req
->exe_file
= main_file
;
529 req
->gui
= !console_app
;
530 wine_server_add_data( req
, main_exe_name
, strlen(main_exe_name
) );
531 wine_server_call( req
);
532 debugged
= reply
->debugged
;
536 /* create the main modref and load dependencies */
537 if (!(wm
= PE_CreateModule( current_process
.module
, main_exe_name
, 0, 0, FALSE
)))
541 if (main_exe_file
) CloseHandle( main_exe_file
); /* we no longer need it */
543 MODULE_DllProcessAttach( NULL
, (LPVOID
)1 );
545 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
546 * context of the parent process. Actually, the USER signal proc
547 * doesn't really care about that, but it *does* require that the
548 * startup parameters are correctly set up, so that GetProcessDword
549 * works. Furthermore, before calling the USER signal proc the
550 * 16-bit stack must be set up, which it is only after TASK_Create
551 * in the case of a 16-bit process. Thus, we send the signal here.
553 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE
, 0 );
554 PROCESS_CallUserSignalProc( USIG_THREAD_INIT
, 0 );
555 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT
, 0 );
556 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED
, 0 );
557 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
558 if (console_app
) PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING
, 0 );
561 DPRINTF( "%04lx:Starting process %s (entryproc=%p)\n",
562 GetCurrentThreadId(), main_exe_name
, entry
);
563 if (debugged
) DbgBreakPoint();
564 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
565 * Dunno about other OSs */
566 SetLastError(0); /* clear error code */
567 ExitThread( entry(NULL
) );
570 ExitProcess( GetLastError() );
574 /***********************************************************************
575 * __wine_process_init
577 * Wine initialisation: load and start the main exe file.
579 void __wine_process_init( int argc
, char *argv
[] )
581 char error
[1024], *p
;
582 DWORD stack_size
= 0;
584 /* Initialize everything */
585 if (!process_init( argv
)) exit(1);
587 argv
++; /* remove argv[0] (wine itself) */
589 TRACE( "starting process name=%s file=%p argv[0]=%s\n",
590 debugstr_a(main_exe_name
), main_exe_file
, debugstr_a(argv
[0]) );
592 if (!main_exe_name
[0])
594 if (!argv
[0]) OPTIONS_Usage();
596 if (!find_exe_file( argv
[0], main_exe_name
, sizeof(main_exe_name
), &main_exe_file
))
598 MESSAGE( "%s: cannot find '%s'\n", argv0
, argv
[0] );
601 if (main_exe_file
== INVALID_HANDLE_VALUE
)
603 MESSAGE( "%s: cannot open '%s'\n", argv0
, main_exe_name
);
608 if (!main_exe_file
) /* no file handle -> Winelib app */
610 TRACE( "starting Winelib app %s\n", debugstr_a(main_exe_name
) );
611 if (open_builtin_exe_file( main_exe_name
, error
, sizeof(error
), 0 ))
613 MESSAGE( "%s: cannot open builtin library for '%s': %s\n", argv0
, main_exe_name
, error
);
616 VERSION_Init( main_exe_name
);
618 switch( MODULE_GetBinaryType( main_exe_file
))
621 TRACE( "starting Win32 binary %s\n", debugstr_a(main_exe_name
) );
622 if ((current_process
.module
= PE_LoadImage( main_exe_file
, main_exe_name
, 0 ))) goto found
;
623 MESSAGE( "%s: could not load '%s' as Win32 binary\n", argv0
, main_exe_name
);
626 MESSAGE( "%s: '%s' is a DLL, not an executable\n", argv0
, main_exe_name
);
629 /* check for .com extension */
630 if (!(p
= strrchr( main_exe_name
, '.' )) || FILE_strcasecmp( p
, ".com" ))
632 MESSAGE( "%s: cannot determine executable type for '%s'\n", argv0
, main_exe_name
);
638 TRACE( "starting Win16/DOS binary %s\n", debugstr_a(main_exe_name
) );
639 CloseHandle( main_exe_file
);
642 argv
[0] = "winevdm.exe";
643 if (open_builtin_exe_file( "winevdm.exe", error
, sizeof(error
), 0 ))
645 MESSAGE( "%s: trying to run '%s', cannot open builtin library for 'winevdm.exe': %s\n",
646 argv0
, main_exe_name
, error
);
649 MESSAGE( "%s: '%s' is an OS/2 binary, not supported\n", argv0
, main_exe_name
);
651 case BINARY_UNIX_EXE
:
652 MESSAGE( "%s: '%s' is a Unix binary, not supported\n", argv0
, main_exe_name
);
654 case BINARY_UNIX_LIB
:
656 DOS_FULL_NAME full_name
;
657 const char *name
= main_exe_name
;
658 UNICODE_STRING nameW
;
660 TRACE( "starting Winelib app %s\n", debugstr_a(main_exe_name
) );
661 RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
662 if (DOSFS_GetFullName( nameW
.Buffer
, TRUE
, &full_name
)) name
= full_name
.long_name
;
663 RtlFreeUnicodeString(&nameW
);
664 CloseHandle( main_exe_file
);
666 if (wine_dlopen( name
, RTLD_NOW
, error
, sizeof(error
) ))
668 if ((p
= strrchr( main_exe_name
, '.' )) && !strcmp( p
, ".so" )) *p
= 0;
671 MESSAGE( "%s: could not load '%s': %s\n", argv0
, main_exe_name
, error
);
677 /* build command line */
678 if (!ENV_BuildCommandLine( argv
)) goto error
;
680 /* create 32-bit module for main exe */
681 if (!(current_process
.module
= BUILTIN32_LoadExeModule( current_process
.module
))) goto error
;
682 stack_size
= RtlImageNtHeader(current_process
.module
)->OptionalHeader
.SizeOfStackReserve
;
684 /* allocate main thread stack */
685 if (!THREAD_InitStack( NtCurrentTeb(), stack_size
)) goto error
;
687 /* switch to the new stack */
688 SYSDEPS_SwitchToThreadStack( start_process
);
691 ExitProcess( GetLastError() );
695 /***********************************************************************
698 * Build an argv array from a command-line.
699 * The command-line is modified to insert nulls.
700 * 'reserved' is the number of args to reserve before the first one.
702 static char **build_argv( char *cmdline
, int reserved
)
707 int in_quotes
,bcount
;
714 if (*s
=='\0' || ((*s
==' ' || *s
=='\t') && !in_quotes
)) {
717 /* skip the remaining spaces */
718 while (*s
==' ' || *s
=='\t') {
725 } else if (*s
=='\\') {
726 /* '\', count them */
728 } else if ((*s
=='"') && ((bcount
& 1)==0)) {
730 in_quotes
=!in_quotes
;
733 /* a regular character */
738 argv
=malloc(argc
*sizeof(*argv
));
747 if ((*s
==' ' || *s
=='\t') && !in_quotes
) {
748 /* Close the argument and copy it */
752 /* skip the remaining spaces */
755 } while (*s
==' ' || *s
=='\t');
757 /* Start with a new argument */
760 } else if (*s
=='\\') {
764 } else if (*s
=='"') {
766 if ((bcount
& 1)==0) {
767 /* Preceeded by an even number of '\', this is half that
768 * number of '\', plus a '"' which we discard.
772 in_quotes
=!in_quotes
;
774 /* Preceeded by an odd number of '\', this is half that
775 * number of '\' followed by a '"'
783 /* a regular character */
798 /***********************************************************************
801 * Build the environment of a new child process.
803 static char **build_envp( const char *env
, const char *extra_env
)
809 if (extra_env
) for (p
= extra_env
; *p
; count
++) p
+= strlen(p
) + 1;
810 for (p
= env
; *p
; count
++) p
+= strlen(p
) + 1;
813 if ((envp
= malloc( count
* sizeof(*envp
) )))
815 extern char **environ
;
816 char **envptr
= envp
;
817 char **unixptr
= environ
;
818 /* first the extra strings */
819 if (extra_env
) for (p
= extra_env
; *p
; p
+= strlen(p
) + 1) *envptr
++ = (char *)p
;
820 /* then put PATH, HOME and WINEPREFIX from the unix env */
821 for (unixptr
= environ
; unixptr
&& *unixptr
; unixptr
++)
822 if (!memcmp( *unixptr
, "PATH=", 5 ) ||
823 !memcmp( *unixptr
, "HOME=", 5 ) ||
824 !memcmp( *unixptr
, "WINEPREFIX=", 11 )) *envptr
++ = *unixptr
;
825 /* now put the Windows environment strings */
826 for (p
= env
; *p
; p
+= strlen(p
) + 1)
828 if (!memcmp( p
, "PATH=", 5 )) /* store PATH as WINEPATH */
830 char *winepath
= malloc( strlen(p
) + 5 );
831 strcpy( winepath
, "WINE" );
832 strcpy( winepath
+ 4, p
);
833 *envptr
++ = winepath
;
835 else if (memcmp( p
, "HOME=", 5 ) &&
836 memcmp( p
, "WINEPATH=", 9 ) &&
837 memcmp( p
, "WINEPREFIX=", 11 )) *envptr
++ = (char *)p
;
845 /***********************************************************************
848 * Locate the Wine binary to exec for a new Win32 process.
850 static void exec_wine_binary( char **argv
, char **envp
)
852 const char *path
, *pos
, *ptr
;
854 /* first, try for a WINELOADER environment variable */
855 argv
[0] = getenv("WINELOADER");
857 execve( argv
[0], argv
, envp
);
859 /* next, try bin directory */
860 argv
[0] = BINDIR
"/wine";
861 execve( argv
[0], argv
, envp
);
863 /* now try the path of argv0 of the current binary */
864 if (!(argv
[0] = malloc( strlen(full_argv0
) + 6 ))) return;
865 if ((ptr
= strrchr( full_argv0
, '/' )))
867 memcpy( argv
[0], full_argv0
, ptr
- full_argv0
);
868 strcpy( argv
[0] + (ptr
- full_argv0
), "/wine" );
869 execve( argv
[0], argv
, envp
);
873 /* now search in the Unix path */
874 if ((path
= getenv( "PATH" )))
876 if (!(argv
[0] = malloc( strlen(path
) + 6 ))) return;
880 while (*pos
== ':') pos
++;
882 if (!(ptr
= strchr( pos
, ':' ))) ptr
= pos
+ strlen(pos
);
883 memcpy( argv
[0], pos
, ptr
- pos
);
884 strcpy( argv
[0] + (ptr
- pos
), "/wine" );
885 execve( argv
[0], argv
, envp
);
893 /***********************************************************************
896 * Fork and exec a new Unix binary, checking for errors.
898 static int fork_and_exec( const char *filename
, char *cmdline
,
899 const char *env
, const char *newdir
)
904 if (!env
) env
= GetEnvironmentStringsA();
911 fcntl( fd
[1], F_SETFD
, 1 ); /* set close on exec */
912 if (!(pid
= fork())) /* child */
914 char **argv
= build_argv( cmdline
, 0 );
915 char **envp
= build_envp( env
, NULL
);
918 /* Reset signals that we previously set to SIG_IGN */
919 signal( SIGPIPE
, SIG_DFL
);
920 signal( SIGCHLD
, SIG_DFL
);
922 if (newdir
) chdir(newdir
);
924 if (argv
&& envp
) execve( filename
, argv
, envp
);
926 write( fd
[1], &err
, sizeof(err
) );
930 if ((pid
!= -1) && (read( fd
[0], &err
, sizeof(err
) ) > 0)) /* exec failed */
935 if (pid
== -1) FILE_SetDosError();
941 /***********************************************************************
944 * Create a new process. If hFile is a valid handle we have an exe
945 * file, otherwise it is a Winelib app.
947 static BOOL
create_process( HANDLE hFile
, LPCSTR filename
, LPSTR cmd_line
, LPCSTR env
,
948 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
949 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
950 LPPROCESS_INFORMATION info
, LPCSTR unixdir
)
952 BOOL ret
, success
= FALSE
;
954 startup_info_t startup_info
;
955 char *extra_env
= NULL
;
964 env
= GetEnvironmentStringsA();
965 extra_env
= DRIVE_BuildEnv();
968 /* create the synchronization pipes */
970 if (pipe( startfd
) == -1)
975 if (pipe( execfd
) == -1)
982 fcntl( execfd
[1], F_SETFD
, 1 ); /* set close on exec */
984 /* create the child process */
986 if (!(pid
= fork())) /* child */
988 char **argv
= build_argv( cmd_line
, 1 );
989 char **envp
= build_envp( env
, extra_env
);
994 /* wait for parent to tell us to start */
995 if (read( startfd
[0], &dummy
, 1 ) != 1) _exit(1);
998 /* Reset signals that we previously set to SIG_IGN */
999 signal( SIGPIPE
, SIG_DFL
);
1000 signal( SIGCHLD
, SIG_DFL
);
1002 if (unixdir
) chdir(unixdir
);
1004 if (argv
&& envp
) exec_wine_binary( argv
, envp
);
1007 write( execfd
[1], &err
, sizeof(err
) );
1011 /* this is the parent */
1013 close( startfd
[0] );
1015 if (extra_env
) HeapFree( GetProcessHeap(), 0, extra_env
);
1018 close( startfd
[1] );
1024 /* fill the startup info structure */
1026 startup_info
.size
= sizeof(startup_info
);
1027 /* startup_info.filename_len is set below */
1028 startup_info
.cmdline_len
= cmd_line
? strlen(cmd_line
) : 0;
1029 startup_info
.desktop_len
= startup
->lpDesktop
? strlen(startup
->lpDesktop
) : 0;
1030 startup_info
.title_len
= startup
->lpTitle
? strlen(startup
->lpTitle
) : 0;
1031 startup_info
.x
= startup
->dwX
;
1032 startup_info
.y
= startup
->dwY
;
1033 startup_info
.cx
= startup
->dwXSize
;
1034 startup_info
.cy
= startup
->dwYSize
;
1035 startup_info
.x_chars
= startup
->dwXCountChars
;
1036 startup_info
.y_chars
= startup
->dwYCountChars
;
1037 startup_info
.attribute
= startup
->dwFillAttribute
;
1038 startup_info
.cmd_show
= startup
->wShowWindow
;
1039 startup_info
.flags
= startup
->dwFlags
;
1041 /* create the process on the server side */
1043 SERVER_START_REQ( new_process
)
1048 req
->inherit_all
= inherit
;
1049 req
->create_flags
= flags
;
1050 req
->use_handles
= (startup
->dwFlags
& STARTF_USESTDHANDLES
) != 0;
1051 req
->unix_pid
= pid
;
1052 req
->exe_file
= hFile
;
1053 if (startup
->dwFlags
& STARTF_USESTDHANDLES
)
1055 req
->hstdin
= startup
->hStdInput
;
1056 req
->hstdout
= startup
->hStdOutput
;
1057 req
->hstderr
= startup
->hStdError
;
1061 req
->hstdin
= GetStdHandle( STD_INPUT_HANDLE
);
1062 req
->hstdout
= GetStdHandle( STD_OUTPUT_HANDLE
);
1063 req
->hstderr
= GetStdHandle( STD_ERROR_HANDLE
);
1066 if (GetLongPathNameA( filename
, buf
, MAX_PATH
))
1071 startup_info
.filename_len
= strlen(nameptr
);
1072 wine_server_add_data( req
, &startup_info
, sizeof(startup_info
) );
1073 wine_server_add_data( req
, nameptr
, startup_info
.filename_len
);
1074 wine_server_add_data( req
, cmd_line
, startup_info
.cmdline_len
);
1075 wine_server_add_data( req
, startup
->lpDesktop
, startup_info
.desktop_len
);
1076 wine_server_add_data( req
, startup
->lpTitle
, startup_info
.title_len
);
1078 ret
= !wine_server_call_err( req
);
1079 process_info
= reply
->info
;
1085 close( startfd
[1] );
1090 /* tell child to start and wait for it to exec */
1092 write( startfd
[1], &dummy
, 1 );
1093 close( startfd
[1] );
1095 if (read( execfd
[0], &err
, sizeof(err
) ) > 0) /* exec failed */
1100 CloseHandle( process_info
);
1104 /* wait for the new process info to be ready */
1106 WaitForSingleObject( process_info
, INFINITE
);
1107 SERVER_START_REQ( get_new_process_info
)
1109 req
->info
= process_info
;
1110 req
->pinherit
= (psa
&& (psa
->nLength
>= sizeof(*psa
)) && psa
->bInheritHandle
);
1111 req
->tinherit
= (tsa
&& (tsa
->nLength
>= sizeof(*tsa
)) && tsa
->bInheritHandle
);
1112 if ((ret
= !wine_server_call_err( req
)))
1114 info
->dwProcessId
= (DWORD
)reply
->pid
;
1115 info
->dwThreadId
= (DWORD
)reply
->tid
;
1116 info
->hProcess
= reply
->phandle
;
1117 info
->hThread
= reply
->thandle
;
1118 success
= reply
->success
;
1123 if (ret
&& !success
) /* new process failed to start */
1126 if (GetExitCodeProcess( info
->hProcess
, &exitcode
)) SetLastError( exitcode
);
1127 CloseHandle( info
->hThread
);
1128 CloseHandle( info
->hProcess
);
1131 CloseHandle( process_info
);
1136 /***********************************************************************
1137 * create_vdm_process
1139 * Create a new VDM process for a 16-bit or DOS application.
1141 static BOOL
create_vdm_process( LPCSTR filename
, LPSTR cmd_line
, LPCSTR env
,
1142 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
1143 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
1144 LPPROCESS_INFORMATION info
, LPCSTR unixdir
)
1147 LPSTR new_cmd_line
= HeapAlloc( GetProcessHeap(), 0, strlen(filename
) + strlen(cmd_line
) + 30 );
1151 SetLastError( ERROR_OUTOFMEMORY
);
1154 sprintf( new_cmd_line
, "winevdm.exe --app-name \"%s\" %s", filename
, cmd_line
);
1155 ret
= create_process( 0, "winevdm.exe", new_cmd_line
, env
, psa
, tsa
, inherit
,
1156 flags
, startup
, info
, unixdir
);
1157 HeapFree( GetProcessHeap(), 0, new_cmd_line
);
1162 /*************************************************************************
1165 * Helper for CreateProcess: retrieve the file name to load from the
1166 * app name and command line. Store the file name in buffer, and
1167 * return a possibly modified command line.
1168 * Also returns a handle to the opened file if it's a Windows binary.
1170 static LPSTR
get_file_name( LPCSTR appname
, LPSTR cmdline
, LPSTR buffer
,
1171 int buflen
, HANDLE
*handle
)
1173 char *name
, *pos
, *ret
= NULL
;
1176 /* if we have an app name, everything is easy */
1180 /* use the unmodified app name as file name */
1181 lstrcpynA( buffer
, appname
, buflen
);
1182 *handle
= open_exe_file( buffer
);
1183 if (!(ret
= cmdline
) || !cmdline
[0])
1185 /* no command-line, create one */
1186 if ((ret
= HeapAlloc( GetProcessHeap(), 0, strlen(appname
) + 3 )))
1187 sprintf( ret
, "\"%s\"", appname
);
1194 SetLastError( ERROR_INVALID_PARAMETER
);
1198 /* first check for a quoted file name */
1200 if ((cmdline
[0] == '"') && ((p
= strchr( cmdline
+ 1, '"' ))))
1202 int len
= p
- cmdline
- 1;
1203 /* extract the quoted portion as file name */
1204 if (!(name
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 ))) return NULL
;
1205 memcpy( name
, cmdline
+ 1, len
);
1208 if (find_exe_file( name
, buffer
, buflen
, handle
))
1209 ret
= cmdline
; /* no change necessary */
1213 /* now try the command-line word by word */
1215 if (!(name
= HeapAlloc( GetProcessHeap(), 0, strlen(cmdline
) + 1 ))) return NULL
;
1221 do *pos
++ = *p
++; while (*p
&& *p
!= ' ');
1223 if (find_exe_file( name
, buffer
, buflen
, handle
))
1230 if (!ret
|| !strchr( name
, ' ' )) goto done
; /* no change necessary */
1232 /* now build a new command-line with quotes */
1234 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, strlen(cmdline
) + 3 ))) goto done
;
1235 sprintf( ret
, "\"%s\"%s", name
, p
);
1238 HeapFree( GetProcessHeap(), 0, name
);
1243 /**********************************************************************
1244 * CreateProcessA (KERNEL32.@)
1246 BOOL WINAPI
CreateProcessA( LPCSTR app_name
, LPSTR cmd_line
, LPSECURITY_ATTRIBUTES process_attr
,
1247 LPSECURITY_ATTRIBUTES thread_attr
, BOOL inherit
,
1248 DWORD flags
, LPVOID env
, LPCSTR cur_dir
,
1249 LPSTARTUPINFOA startup_info
, LPPROCESS_INFORMATION info
)
1253 const char *unixdir
= NULL
;
1254 DOS_FULL_NAME full_dir
;
1255 char name
[MAX_PATH
];
1259 /* Process the AppName and/or CmdLine to get module name and path */
1261 TRACE("app %s cmdline %s\n", debugstr_a(app_name
), debugstr_a(cmd_line
) );
1263 if (!(tidy_cmdline
= get_file_name( app_name
, cmd_line
, name
, sizeof(name
), &hFile
)))
1265 if (hFile
== INVALID_HANDLE_VALUE
) goto done
;
1267 /* Warn if unsupported features are used */
1269 if (flags
& NORMAL_PRIORITY_CLASS
)
1270 FIXME("(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name
);
1271 if (flags
& IDLE_PRIORITY_CLASS
)
1272 FIXME("(%s,...): IDLE_PRIORITY_CLASS ignored\n", name
);
1273 if (flags
& HIGH_PRIORITY_CLASS
)
1274 FIXME("(%s,...): HIGH_PRIORITY_CLASS ignored\n", name
);
1275 if (flags
& REALTIME_PRIORITY_CLASS
)
1276 FIXME("(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name
);
1277 if (flags
& CREATE_NEW_PROCESS_GROUP
)
1278 FIXME("(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name
);
1279 if (flags
& CREATE_UNICODE_ENVIRONMENT
)
1280 FIXME("(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name
);
1281 if (flags
& CREATE_SEPARATE_WOW_VDM
)
1282 FIXME("(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name
);
1283 if (flags
& CREATE_SHARED_WOW_VDM
)
1284 FIXME("(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name
);
1285 if (flags
& CREATE_DEFAULT_ERROR_MODE
)
1286 FIXME("(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name
);
1287 if (flags
& CREATE_NO_WINDOW
)
1288 FIXME("(%s,...): CREATE_NO_WINDOW ignored\n", name
);
1289 if (flags
& PROFILE_USER
)
1290 FIXME("(%s,...): PROFILE_USER ignored\n", name
);
1291 if (flags
& PROFILE_KERNEL
)
1292 FIXME("(%s,...): PROFILE_KERNEL ignored\n", name
);
1293 if (flags
& PROFILE_SERVER
)
1294 FIXME("(%s,...): PROFILE_SERVER ignored\n", name
);
1295 if (startup_info
->lpDesktop
)
1296 FIXME("(%s,...): startup_info->lpDesktop %s ignored\n",
1297 name
, debugstr_a(startup_info
->lpDesktop
));
1298 if (startup_info
->dwFlags
& STARTF_RUNFULLSCREEN
)
1299 FIXME("(%s,...): STARTF_RUNFULLSCREEN ignored\n", name
);
1300 if (startup_info
->dwFlags
& STARTF_FORCEONFEEDBACK
)
1301 FIXME("(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name
);
1302 if (startup_info
->dwFlags
& STARTF_FORCEOFFFEEDBACK
)
1303 FIXME("(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name
);
1304 if (startup_info
->dwFlags
& STARTF_USEHOTKEY
)
1305 FIXME("(%s,...): STARTF_USEHOTKEY ignored\n", name
);
1309 UNICODE_STRING cur_dirW
;
1310 RtlCreateUnicodeStringFromAsciiz(&cur_dirW
, cur_dir
);
1311 if (DOSFS_GetFullName( cur_dirW
.Buffer
, TRUE
, &full_dir
))
1312 unixdir
= full_dir
.long_name
;
1313 RtlFreeUnicodeString(&cur_dirW
);
1317 WCHAR buf
[MAX_PATH
];
1318 if (GetCurrentDirectoryW(MAX_PATH
, buf
))
1320 if (DOSFS_GetFullName( buf
, TRUE
, &full_dir
)) unixdir
= full_dir
.long_name
;
1324 info
->hThread
= info
->hProcess
= 0;
1325 info
->dwProcessId
= info
->dwThreadId
= 0;
1327 /* Determine executable type */
1329 if (!hFile
) /* builtin exe */
1331 TRACE( "starting %s as Winelib app\n", debugstr_a(name
) );
1332 retv
= create_process( 0, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1333 inherit
, flags
, startup_info
, info
, unixdir
);
1337 switch( MODULE_GetBinaryType( hFile
))
1340 TRACE( "starting %s as Win32 binary\n", debugstr_a(name
) );
1341 retv
= create_process( hFile
, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1342 inherit
, flags
, startup_info
, info
, unixdir
);
1346 TRACE( "starting %s as Win16/DOS binary\n", debugstr_a(name
) );
1347 retv
= create_vdm_process( name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1348 inherit
, flags
, startup_info
, info
, unixdir
);
1351 FIXME( "%s is OS/2 binary, not supported\n", debugstr_a(name
) );
1352 SetLastError( ERROR_BAD_EXE_FORMAT
);
1355 TRACE( "not starting %s since it is a dll\n", debugstr_a(name
) );
1356 SetLastError( ERROR_BAD_EXE_FORMAT
);
1358 case BINARY_UNIX_LIB
:
1359 TRACE( "%s is a Unix library, starting as Winelib app\n", debugstr_a(name
) );
1360 retv
= create_process( hFile
, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1361 inherit
, flags
, startup_info
, info
, unixdir
);
1363 case BINARY_UNKNOWN
:
1364 /* check for .com or .bat extension */
1365 if ((p
= strrchr( name
, '.' )))
1367 if (!FILE_strcasecmp( p
, ".com" ))
1369 TRACE( "starting %s as DOS binary\n", debugstr_a(name
) );
1370 retv
= create_vdm_process( name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1371 inherit
, flags
, startup_info
, info
, unixdir
);
1374 if (!FILE_strcasecmp( p
, ".bat" ))
1376 char comspec
[MAX_PATH
];
1377 if (GetEnvironmentVariableA("COMSPEC", comspec
, sizeof(comspec
)))
1379 char *newcmdline
, *q
= strchr(cmd_line
, ' ');
1381 if ((newcmdline
= HeapAlloc( GetProcessHeap(), 0,
1382 strlen(comspec
) + strlen(name
) + strlen(q
) + 8)))
1384 sprintf( newcmdline
, "%s /c %s%s", comspec
, name
, q
);
1385 TRACE( "starting %s as batch binary: %s\n",
1386 debugstr_a(name
), debugstr_a(newcmdline
) );
1387 retv
= CreateProcessA( comspec
, newcmdline
, process_attr
, thread_attr
,
1388 inherit
, flags
, env
, cur_dir
, startup_info
, info
);
1389 HeapFree( GetProcessHeap(), 0, newcmdline
);
1396 case BINARY_UNIX_EXE
:
1398 /* unknown file, try as unix executable */
1399 UNICODE_STRING nameW
;
1400 DOS_FULL_NAME full_name
;
1401 const char *unixfilename
= name
;
1403 TRACE( "starting %s as Unix binary\n", debugstr_a(name
) );
1405 RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
1406 if (DOSFS_GetFullName( nameW
.Buffer
, TRUE
, &full_name
)) unixfilename
= full_name
.long_name
;
1407 RtlFreeUnicodeString(&nameW
);
1408 retv
= (fork_and_exec( unixfilename
, tidy_cmdline
, env
, unixdir
) != -1);
1412 CloseHandle( hFile
);
1415 if (tidy_cmdline
!= cmd_line
) HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1420 /**********************************************************************
1421 * CreateProcessW (KERNEL32.@)
1423 * lpReserved is not converted
1425 BOOL WINAPI
CreateProcessW( LPCWSTR app_name
, LPWSTR cmd_line
, LPSECURITY_ATTRIBUTES process_attr
,
1426 LPSECURITY_ATTRIBUTES thread_attr
, BOOL inherit
, DWORD flags
,
1427 LPVOID env
, LPCWSTR cur_dir
, LPSTARTUPINFOW startup_info
,
1428 LPPROCESS_INFORMATION info
)
1431 STARTUPINFOA StartupInfoA
;
1433 LPSTR app_nameA
= HEAP_strdupWtoA (GetProcessHeap(),0,app_name
);
1434 LPSTR cmd_lineA
= HEAP_strdupWtoA (GetProcessHeap(),0,cmd_line
);
1435 LPSTR cur_dirA
= HEAP_strdupWtoA (GetProcessHeap(),0,cur_dir
);
1437 memcpy (&StartupInfoA
, startup_info
, sizeof(STARTUPINFOA
));
1438 StartupInfoA
.lpDesktop
= HEAP_strdupWtoA (GetProcessHeap(),0,startup_info
->lpDesktop
);
1439 StartupInfoA
.lpTitle
= HEAP_strdupWtoA (GetProcessHeap(),0,startup_info
->lpTitle
);
1441 TRACE_(win32
)("(%s,%s,...)\n", debugstr_w(app_name
), debugstr_w(cmd_line
));
1443 if (startup_info
->lpReserved
)
1444 FIXME_(win32
)("StartupInfo.lpReserved is used, please report (%s)\n",
1445 debugstr_w(startup_info
->lpReserved
));
1447 ret
= CreateProcessA( app_nameA
, cmd_lineA
, process_attr
, thread_attr
,
1448 inherit
, flags
, env
, cur_dirA
, &StartupInfoA
, info
);
1450 HeapFree( GetProcessHeap(), 0, cur_dirA
);
1451 HeapFree( GetProcessHeap(), 0, cmd_lineA
);
1452 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpDesktop
);
1453 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpTitle
);
1459 /***********************************************************************
1460 * ExitProcess (KERNEL32.@)
1462 void WINAPI
ExitProcess( DWORD status
)
1464 LdrShutdownProcess();
1465 SERVER_START_REQ( terminate_process
)
1467 /* send the exit code to the server */
1468 req
->handle
= GetCurrentProcess();
1469 req
->exit_code
= status
;
1470 wine_server_call( req
);
1476 /***********************************************************************
1477 * ExitProcess (KERNEL.466)
1479 void WINAPI
ExitProcess16( WORD status
)
1482 ReleaseThunkLock( &count
);
1483 ExitProcess( status
);
1486 /******************************************************************************
1487 * TerminateProcess (KERNEL32.@)
1489 BOOL WINAPI
TerminateProcess( HANDLE handle
, DWORD exit_code
)
1491 NTSTATUS status
= NtTerminateProcess( handle
, exit_code
);
1492 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1497 /***********************************************************************
1498 * GetProcessDword (KERNEL.485)
1499 * GetProcessDword (KERNEL32.18)
1500 * 'Of course you cannot directly access Windows internal structures'
1502 DWORD WINAPI
GetProcessDword( DWORD dwProcessID
, INT offset
)
1506 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
1508 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
1510 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
1516 case GPD_APP_COMPAT_FLAGS
:
1517 return GetAppCompatFlags16(0);
1519 case GPD_LOAD_DONE_EVENT
:
1520 return (DWORD
)current_process
.load_done_evt
;
1522 case GPD_HINSTANCE16
:
1523 return GetTaskDS16();
1525 case GPD_WINDOWS_VERSION
:
1526 return GetExeVersion16();
1529 return (DWORD
)NtCurrentTeb() - 0x10 /* FIXME */;
1532 return (DWORD
)¤t_process
;
1534 case GPD_STARTF_SHELLDATA
: /* return stdoutput handle from startupinfo ??? */
1535 return (DWORD
)current_startupinfo
.hStdOutput
;
1537 case GPD_STARTF_HOTKEY
: /* return stdinput handle from startupinfo ??? */
1538 return (DWORD
)current_startupinfo
.hStdInput
;
1540 case GPD_STARTF_SHOWWINDOW
:
1541 return current_startupinfo
.wShowWindow
;
1543 case GPD_STARTF_SIZE
:
1544 x
= current_startupinfo
.dwXSize
;
1545 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
1546 y
= current_startupinfo
.dwYSize
;
1547 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
1548 return MAKELONG( x
, y
);
1550 case GPD_STARTF_POSITION
:
1551 x
= current_startupinfo
.dwX
;
1552 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
1553 y
= current_startupinfo
.dwY
;
1554 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
1555 return MAKELONG( x
, y
);
1557 case GPD_STARTF_FLAGS
:
1558 return current_startupinfo
.dwFlags
;
1564 return current_process
.flags
;
1567 return current_process
.process_dword
;
1570 ERR_(win32
)("Unknown offset %d\n", offset
);
1575 /***********************************************************************
1576 * SetProcessDword (KERNEL.484)
1577 * 'Of course you cannot directly access Windows internal structures'
1579 void WINAPI
SetProcessDword( DWORD dwProcessID
, INT offset
, DWORD value
)
1581 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
1583 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
1585 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
1591 case GPD_APP_COMPAT_FLAGS
:
1592 case GPD_LOAD_DONE_EVENT
:
1593 case GPD_HINSTANCE16
:
1594 case GPD_WINDOWS_VERSION
:
1597 case GPD_STARTF_SHELLDATA
:
1598 case GPD_STARTF_HOTKEY
:
1599 case GPD_STARTF_SHOWWINDOW
:
1600 case GPD_STARTF_SIZE
:
1601 case GPD_STARTF_POSITION
:
1602 case GPD_STARTF_FLAGS
:
1605 ERR_(win32
)("Not allowed to modify offset %d\n", offset
);
1609 current_process
.process_dword
= value
;
1613 ERR_(win32
)("Unknown offset %d\n", offset
);
1619 /*********************************************************************
1620 * OpenProcess (KERNEL32.@)
1622 HANDLE WINAPI
OpenProcess( DWORD access
, BOOL inherit
, DWORD id
)
1625 SERVER_START_REQ( open_process
)
1628 req
->access
= access
;
1629 req
->inherit
= inherit
;
1630 if (!wine_server_call_err( req
)) ret
= reply
->handle
;
1636 /*********************************************************************
1637 * MapProcessHandle (KERNEL.483)
1639 DWORD WINAPI
MapProcessHandle( HANDLE handle
)
1642 SERVER_START_REQ( get_process_info
)
1644 req
->handle
= handle
;
1645 if (!wine_server_call_err( req
)) ret
= (DWORD
)reply
->pid
;
1651 /***********************************************************************
1652 * SetPriorityClass (KERNEL32.@)
1654 BOOL WINAPI
SetPriorityClass( HANDLE hprocess
, DWORD priorityclass
)
1657 SERVER_START_REQ( set_process_info
)
1659 req
->handle
= hprocess
;
1660 req
->priority
= priorityclass
;
1661 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
1662 ret
= !wine_server_call_err( req
);
1669 /***********************************************************************
1670 * GetPriorityClass (KERNEL32.@)
1672 DWORD WINAPI
GetPriorityClass(HANDLE hprocess
)
1675 SERVER_START_REQ( get_process_info
)
1677 req
->handle
= hprocess
;
1678 if (!wine_server_call_err( req
)) ret
= reply
->priority
;
1685 /***********************************************************************
1686 * SetProcessAffinityMask (KERNEL32.@)
1688 BOOL WINAPI
SetProcessAffinityMask( HANDLE hProcess
, DWORD affmask
)
1691 SERVER_START_REQ( set_process_info
)
1693 req
->handle
= hProcess
;
1694 req
->affinity
= affmask
;
1695 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
1696 ret
= !wine_server_call_err( req
);
1702 /**********************************************************************
1703 * GetProcessAffinityMask (KERNEL32.@)
1705 BOOL WINAPI
GetProcessAffinityMask( HANDLE hProcess
,
1706 LPDWORD lpProcessAffinityMask
,
1707 LPDWORD lpSystemAffinityMask
)
1710 SERVER_START_REQ( get_process_info
)
1712 req
->handle
= hProcess
;
1713 if (!wine_server_call_err( req
))
1715 if (lpProcessAffinityMask
) *lpProcessAffinityMask
= reply
->process_affinity
;
1716 if (lpSystemAffinityMask
) *lpSystemAffinityMask
= reply
->system_affinity
;
1725 /***********************************************************************
1726 * GetProcessVersion (KERNEL32.@)
1728 DWORD WINAPI
GetProcessVersion( DWORD processid
)
1730 IMAGE_NT_HEADERS
*nt
;
1732 if (processid
&& processid
!= GetCurrentProcessId())
1734 FIXME("should use ReadProcessMemory\n");
1737 if ((nt
= RtlImageNtHeader( current_process
.module
)))
1738 return ((nt
->OptionalHeader
.MajorSubsystemVersion
<< 16) |
1739 nt
->OptionalHeader
.MinorSubsystemVersion
);
1743 /***********************************************************************
1744 * GetProcessFlags (KERNEL32.@)
1746 DWORD WINAPI
GetProcessFlags( DWORD processid
)
1748 if (processid
&& processid
!= GetCurrentProcessId()) return 0;
1749 return current_process
.flags
;
1753 /***********************************************************************
1754 * SetProcessWorkingSetSize [KERNEL32.@]
1755 * Sets the min/max working set sizes for a specified process.
1758 * hProcess [I] Handle to the process of interest
1759 * minset [I] Specifies minimum working set size
1760 * maxset [I] Specifies maximum working set size
1764 BOOL WINAPI
SetProcessWorkingSetSize(HANDLE hProcess
, SIZE_T minset
,
1767 FIXME("(%p,%ld,%ld): stub - harmless\n",hProcess
,minset
,maxset
);
1768 if(( minset
== (SIZE_T
)-1) && (maxset
== (SIZE_T
)-1)) {
1769 /* Trim the working set to zero */
1770 /* Swap the process out of physical RAM */
1775 /***********************************************************************
1776 * GetProcessWorkingSetSize (KERNEL32.@)
1778 BOOL WINAPI
GetProcessWorkingSetSize(HANDLE hProcess
, PSIZE_T minset
,
1781 FIXME("(%p,%p,%p): stub\n",hProcess
,minset
,maxset
);
1782 /* 32 MB working set size */
1783 if (minset
) *minset
= 32*1024*1024;
1784 if (maxset
) *maxset
= 32*1024*1024;
1788 /***********************************************************************
1789 * SetProcessShutdownParameters (KERNEL32.@)
1791 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1792 * Now tracks changes made (but does not act on these changes)
1794 static DWORD shutdown_flags
= 0;
1795 static DWORD shutdown_priority
= 0x280;
1797 BOOL WINAPI
SetProcessShutdownParameters(DWORD level
, DWORD flags
)
1799 FIXME("(%08lx, %08lx): partial stub.\n", level
, flags
);
1800 shutdown_flags
= flags
;
1801 shutdown_priority
= level
;
1806 /***********************************************************************
1807 * GetProcessShutdownParameters (KERNEL32.@)
1810 BOOL WINAPI
GetProcessShutdownParameters( LPDWORD lpdwLevel
, LPDWORD lpdwFlags
)
1812 *lpdwLevel
= shutdown_priority
;
1813 *lpdwFlags
= shutdown_flags
;
1818 /***********************************************************************
1819 * SetProcessPriorityBoost (KERNEL32.@)
1821 BOOL WINAPI
SetProcessPriorityBoost(HANDLE hprocess
,BOOL disableboost
)
1823 FIXME("(%p,%d): stub\n",hprocess
,disableboost
);
1824 /* Say we can do it. I doubt the program will notice that we don't. */
1829 /***********************************************************************
1830 * ReadProcessMemory (KERNEL32.@)
1832 BOOL WINAPI
ReadProcessMemory( HANDLE process
, LPCVOID addr
, LPVOID buffer
, SIZE_T size
,
1833 SIZE_T
*bytes_read
)
1837 SERVER_START_REQ( read_process_memory
)
1839 req
->handle
= process
;
1840 req
->addr
= (void *)addr
;
1841 wine_server_set_reply( req
, buffer
, size
);
1842 if ((res
= wine_server_call_err( req
))) size
= 0;
1845 if (bytes_read
) *bytes_read
= size
;
1850 /***********************************************************************
1851 * WriteProcessMemory (KERNEL32.@)
1853 BOOL WINAPI
WriteProcessMemory( HANDLE process
, LPVOID addr
, LPCVOID buffer
, SIZE_T size
,
1854 SIZE_T
*bytes_written
)
1856 static const int zero
;
1857 unsigned int first_offset
, last_offset
, first_mask
, last_mask
;
1862 SetLastError( ERROR_INVALID_PARAMETER
);
1866 /* compute the mask for the first int */
1868 first_offset
= (unsigned int)addr
% sizeof(int);
1869 memset( &first_mask
, 0, first_offset
);
1871 /* compute the mask for the last int */
1872 last_offset
= (size
+ first_offset
) % sizeof(int);
1874 memset( &last_mask
, 0xff, last_offset
? last_offset
: sizeof(int) );
1876 SERVER_START_REQ( write_process_memory
)
1878 req
->handle
= process
;
1879 req
->addr
= (char *)addr
- first_offset
;
1880 req
->first_mask
= first_mask
;
1881 req
->last_mask
= last_mask
;
1882 if (first_offset
) wine_server_add_data( req
, &zero
, first_offset
);
1883 wine_server_add_data( req
, buffer
, size
);
1884 if (last_offset
) wine_server_add_data( req
, &zero
, sizeof(int) - last_offset
);
1886 if ((res
= wine_server_call_err( req
))) size
= 0;
1889 if (bytes_written
) *bytes_written
= size
;
1893 ReadProcessMemory( process
, addr
, dummy
, sizeof(dummy
), &read
);
1899 /***********************************************************************
1900 * RegisterServiceProcess (KERNEL.491)
1901 * RegisterServiceProcess (KERNEL32.@)
1903 * A service process calls this function to ensure that it continues to run
1904 * even after a user logged off.
1906 DWORD WINAPI
RegisterServiceProcess(DWORD dwProcessId
, DWORD dwType
)
1908 /* I don't think that Wine needs to do anything in that function */
1909 return 1; /* success */
1912 /***********************************************************************
1913 * GetExitCodeProcess [KERNEL32.@]
1915 * Gets termination status of specified process
1921 BOOL WINAPI
GetExitCodeProcess(
1922 HANDLE hProcess
, /* [in] handle to the process */
1923 LPDWORD lpExitCode
) /* [out] address to receive termination status */
1926 SERVER_START_REQ( get_process_info
)
1928 req
->handle
= hProcess
;
1929 ret
= !wine_server_call_err( req
);
1930 if (ret
&& lpExitCode
) *lpExitCode
= reply
->exit_code
;
1937 /***********************************************************************
1938 * SetErrorMode (KERNEL32.@)
1940 UINT WINAPI
SetErrorMode( UINT mode
)
1942 UINT old
= current_process
.error_mode
;
1943 current_process
.error_mode
= mode
;
1948 /**************************************************************************
1949 * SetFileApisToOEM (KERNEL32.@)
1951 VOID WINAPI
SetFileApisToOEM(void)
1953 current_process
.flags
|= PDB32_FILE_APIS_OEM
;
1957 /**************************************************************************
1958 * SetFileApisToANSI (KERNEL32.@)
1960 VOID WINAPI
SetFileApisToANSI(void)
1962 current_process
.flags
&= ~PDB32_FILE_APIS_OEM
;
1966 /******************************************************************************
1967 * AreFileApisANSI [KERNEL32.@] Determines if file functions are using ANSI
1970 * TRUE: Set of file functions is using ANSI code page
1971 * FALSE: Set of file functions is using OEM code page
1973 BOOL WINAPI
AreFileApisANSI(void)
1975 return !(current_process
.flags
& PDB32_FILE_APIS_OEM
);
1979 /***********************************************************************
1980 * GetTickCount (KERNEL32.@)
1982 * Returns the number of milliseconds, modulo 2^32, since the start
1983 * of the wineserver.
1985 DWORD WINAPI
GetTickCount(void)
1988 gettimeofday( &t
, NULL
);
1989 return ((t
.tv_sec
* 1000) + (t
.tv_usec
/ 1000)) - server_startticks
;
1993 /**********************************************************************
1994 * TlsAlloc [KERNEL32.@] Allocates a TLS index.
1996 * Allocates a thread local storage index
1999 * Success: TLS Index
2000 * Failure: 0xFFFFFFFF
2002 DWORD WINAPI
TlsAlloc( void )
2004 DWORD i
, mask
, ret
= 0;
2005 DWORD
*bits
= current_process
.tls_bits
;
2006 RtlAcquirePebLock();
2007 if (*bits
== 0xffffffff)
2011 if (*bits
== 0xffffffff)
2013 RtlReleasePebLock();
2014 SetLastError( ERROR_NO_MORE_ITEMS
);
2018 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) if (!(*bits
& mask
)) break;
2020 RtlReleasePebLock();
2021 NtCurrentTeb()->tls_array
[ret
+i
] = 0; /* clear the value */
2026 /**********************************************************************
2027 * TlsFree [KERNEL32.@] Releases a TLS index.
2029 * Releases a thread local storage index, making it available for reuse
2035 BOOL WINAPI
TlsFree(
2036 DWORD index
) /* [in] TLS Index to free */
2038 DWORD mask
= (1 << (index
& 31));
2039 DWORD
*bits
= current_process
.tls_bits
;
2042 SetLastError( ERROR_INVALID_PARAMETER
);
2045 if (index
>= 32) bits
++;
2046 RtlAcquirePebLock();
2047 if (!(*bits
& mask
)) /* already free? */
2049 RtlReleasePebLock();
2050 SetLastError( ERROR_INVALID_PARAMETER
);
2054 NtCurrentTeb()->tls_array
[index
] = 0;
2055 /* FIXME: should zero all other thread values */
2056 RtlReleasePebLock();
2061 /**********************************************************************
2062 * TlsGetValue [KERNEL32.@] Gets value in a thread's TLS slot
2065 * Success: Value stored in calling thread's TLS slot for index
2066 * Failure: 0 and GetLastError returns NO_ERROR
2068 LPVOID WINAPI
TlsGetValue(
2069 DWORD index
) /* [in] TLS index to retrieve value for */
2073 SetLastError( ERROR_INVALID_PARAMETER
);
2076 SetLastError( ERROR_SUCCESS
);
2077 return NtCurrentTeb()->tls_array
[index
];
2081 /**********************************************************************
2082 * TlsSetValue [KERNEL32.@] Stores a value in the thread's TLS slot.
2088 BOOL WINAPI
TlsSetValue(
2089 DWORD index
, /* [in] TLS index to set value for */
2090 LPVOID value
) /* [in] Value to be stored */
2094 SetLastError( ERROR_INVALID_PARAMETER
);
2097 NtCurrentTeb()->tls_array
[index
] = value
;
2102 /***********************************************************************
2103 * GetCurrentProcess (KERNEL32.@)
2105 #undef GetCurrentProcess
2106 HANDLE WINAPI
GetCurrentProcess(void)
2108 return (HANDLE
)0xffffffff;