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"
49 #include "ntdll_misc.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(process
);
52 WINE_DECLARE_DEBUG_CHANNEL(relay
);
53 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
54 WINE_DECLARE_DEBUG_CHANNEL(win32
);
58 /* Win32 process database */
61 LONG header
[2]; /* 00 Kernel object header */
62 HMODULE module
; /* 08 Main exe module (NT) */
63 void *event
; /* 0c Pointer to an event object (unused) */
64 DWORD exit_code
; /* 10 Process exit code */
65 DWORD unknown2
; /* 14 Unknown */
66 HANDLE heap
; /* 18 Default process heap */
67 HANDLE mem_context
; /* 1c Process memory context */
68 DWORD flags
; /* 20 Flags */
69 void *pdb16
; /* 24 DOS PSP */
70 WORD PSP_sel
; /* 28 Selector to DOS PSP */
71 WORD imte
; /* 2a IMTE for the process module */
72 WORD threads
; /* 2c Number of threads */
73 WORD running_threads
; /* 2e Number of running threads */
74 WORD free_lib_count
; /* 30 Recursion depth of FreeLibrary calls */
75 WORD ring0_threads
; /* 32 Number of ring 0 threads */
76 HANDLE system_heap
; /* 34 System heap to allocate handles */
77 HTASK task
; /* 38 Win16 task */
78 void *mem_map_files
; /* 3c Pointer to mem-mapped files */
79 struct _ENVDB
*env_db
; /* 40 Environment database */
80 void *handle_table
; /* 44 Handle table */
81 struct _PDB
*parent
; /* 48 Parent process */
82 void *modref_list
; /* 4c MODREF list */
83 void *thread_list
; /* 50 List of threads */
84 void *debuggee_CB
; /* 54 Debuggee context block */
85 void *local_heap_free
; /* 58 Head of local heap free list */
86 DWORD unknown4
; /* 5c Unknown */
87 CRITICAL_SECTION crit_section
; /* 60 Critical section */
88 DWORD unknown5
[3]; /* 78 Unknown */
89 void *console
; /* 84 Console */
90 DWORD tls_bits
[2]; /* 88 TLS in-use bits */
91 DWORD process_dword
; /* 90 Unknown */
92 struct _PDB
*group
; /* 94 Process group */
93 void *exe_modref
; /* 98 MODREF for the process EXE */
94 void *top_filter
; /* 9c Top exception filter */
95 DWORD priority
; /* a0 Priority level */
96 HANDLE heap_list
; /* a4 Head of process heap list */
97 void *heap_handles
; /* a8 Head of heap handles list */
98 DWORD unknown6
; /* ac Unknown */
99 void *console_provider
; /* b0 Console provider (??) */
100 WORD env_selector
; /* b4 Selector to process environment */
101 WORD error_mode
; /* b6 Error mode */
102 HANDLE load_done_evt
; /* b8 Event for process loading done */
103 void *UTState
; /* bc Head of Univeral Thunk list */
104 DWORD unknown8
; /* c0 Unknown (NT) */
105 LCID locale
; /* c4 Locale to be queried by GetThreadLocale (NT) */
110 RTL_USER_PROCESS_PARAMETERS process_pmts
;
113 #define PDB32_DEBUGGED 0x0001 /* Process is being debugged */
114 #define PDB32_WIN16_PROC 0x0008 /* Win16 process */
115 #define PDB32_DOS_PROC 0x0010 /* Dos process */
116 #define PDB32_CONSOLE_PROC 0x0020 /* Console process */
117 #define PDB32_FILE_APIS_OEM 0x0040 /* File APIs are OEM */
118 #define PDB32_WIN32S_PROC 0x8000 /* Win32s process */
120 static char main_exe_name
[MAX_PATH
];
121 static char *main_exe_name_ptr
= main_exe_name
;
122 static HANDLE main_exe_file
;
123 static unsigned int server_startticks
;
125 int main_create_flags
= 0;
127 /* memory/environ.c */
128 extern struct _ENVDB
*ENV_InitStartupInfo( size_t info_size
, char *main_exe_name
,
129 size_t main_exe_size
);
130 extern BOOL
ENV_BuildCommandLine( char **argv
);
131 extern STARTUPINFOA current_startupinfo
;
133 /* scheduler/pthread.c */
134 extern void PTHREAD_init_done(void);
136 extern void RELAY_InitDebugLists(void);
137 extern BOOL
MAIN_MainInit(void);
138 extern void VERSION_Init( const char *appname
);
140 typedef WORD (WINAPI
*pUserSignalProc
)( UINT
, DWORD
, DWORD
, HMODULE16
);
142 /***********************************************************************
143 * PROCESS_CallUserSignalProc
145 * FIXME: Some of the signals aren't sent correctly!
147 * The exact meaning of the USER signals is undocumented, but this
148 * should cover the basic idea:
150 * USIG_DLL_UNLOAD_WIN16
151 * This is sent when a 16-bit module is unloaded.
153 * USIG_DLL_UNLOAD_WIN32
154 * This is sent when a 32-bit module is unloaded.
156 * USIG_DLL_UNLOAD_ORPHANS
157 * This is sent after the last Win3.1 module is unloaded,
158 * to allow removal of orphaned menus.
160 * USIG_FAULT_DIALOG_PUSH
161 * USIG_FAULT_DIALOG_POP
162 * These are called to allow USER to prepare for displaying a
163 * fault dialog, even though the fault might have happened while
164 * inside a USER critical section.
167 * This is called from the context of a new thread, as soon as it
168 * has started to run.
171 * This is called, still in its context, just before a thread is
172 * about to terminate.
174 * USIG_PROCESS_CREATE
175 * This is called, in the parent process context, after a new process
179 * This is called in the new process context, just after the main thread
180 * has started execution (after the main thread's USIG_THREAD_INIT has
183 * USIG_PROCESS_LOADED
184 * This is called after the executable file has been loaded into the
185 * new process context.
187 * USIG_PROCESS_RUNNING
188 * This is called immediately before the main entry point is called.
191 * This is called in the context of a process that is about to
192 * terminate (but before the last thread's USIG_THREAD_EXIT has
195 * USIG_PROCESS_DESTROY
196 * This is called after a process has terminated.
199 * The meaning of the dwFlags bits is as follows:
202 * Current process is 32-bit.
205 * Current process is a (Win32) GUI process.
207 * USIG_FLAGS_FEEDBACK
208 * Current process needs 'feedback' (determined from the STARTUPINFO
209 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
212 * The signal is being sent due to a fault.
214 void PROCESS_CallUserSignalProc( UINT uCode
, HMODULE16 hModule
)
218 pUserSignalProc proc
;
220 if (!(user
= GetModuleHandleA( "user32.dll" ))) return;
221 if (!(proc
= (pUserSignalProc
)GetProcAddress( user
, "UserSignalProc" ))) return;
223 /* Determine dwFlags */
225 if ( !(current_process
.flags
& PDB32_WIN16_PROC
) ) dwFlags
|= USIG_FLAGS_WIN32
;
226 if ( !(current_process
.flags
& PDB32_CONSOLE_PROC
) ) dwFlags
|= USIG_FLAGS_GUI
;
228 if ( dwFlags
& USIG_FLAGS_GUI
)
230 /* Feedback defaults to ON */
231 if ( !(current_startupinfo
.dwFlags
& STARTF_FORCEOFFFEEDBACK
) )
232 dwFlags
|= USIG_FLAGS_FEEDBACK
;
236 /* Feedback defaults to OFF */
237 if (current_startupinfo
.dwFlags
& STARTF_FORCEONFEEDBACK
)
238 dwFlags
|= USIG_FLAGS_FEEDBACK
;
241 /* Call USER signal proc */
243 if ( uCode
== USIG_THREAD_INIT
|| uCode
== USIG_THREAD_EXIT
)
244 proc( uCode
, GetCurrentThreadId(), dwFlags
, hModule
);
246 proc( uCode
, GetCurrentProcessId(), dwFlags
, hModule
);
250 /***********************************************************************
253 inline static const char *get_basename( const char *name
)
257 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
258 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
263 /***********************************************************************
264 * open_builtin_exe_file
266 * Open an exe file for a builtin exe.
268 static void *open_builtin_exe_file( const char *name
, char *error
, int error_size
, int test_only
)
270 char exename
[MAX_PATH
], *p
;
271 const char *basename
= get_basename(name
);
273 if (strlen(basename
) >= sizeof(exename
)) return NULL
;
274 strcpy( exename
, basename
);
275 for (p
= exename
; *p
; p
++) *p
= FILE_tolower(*p
);
276 return wine_dll_load_main_exe( exename
, error
, error_size
, test_only
);
280 /***********************************************************************
283 * Open a specific exe file, taking load order into account.
284 * Returns the file handle or 0 for a builtin exe.
286 static HANDLE
open_exe_file( const char *name
)
288 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
289 char buffer
[MAX_PATH
];
293 TRACE("looking for %s\n", debugstr_a(name
) );
295 if ((handle
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
,
296 NULL
, OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
298 /* file doesn't exist, check for builtin */
299 if (!FILE_contains_path( name
)) goto error
;
300 if (!MODULE_GetBuiltinPath( name
, "", buffer
, sizeof(buffer
) )) goto error
;
304 MODULE_GetLoadOrder( loadorder
, name
, TRUE
);
306 for(i
= 0; i
< LOADORDER_NTYPES
; i
++)
308 if (loadorder
[i
] == LOADORDER_INVALID
) break;
312 TRACE( "Trying native exe %s\n", debugstr_a(name
) );
313 if (handle
!= INVALID_HANDLE_VALUE
) return handle
;
316 TRACE( "Trying built-in exe %s\n", debugstr_a(name
) );
317 if (open_builtin_exe_file( name
, NULL
, 0, 1 ))
319 if (handle
!= INVALID_HANDLE_VALUE
) CloseHandle(handle
);
326 if (handle
!= INVALID_HANDLE_VALUE
) CloseHandle(handle
);
329 SetLastError( ERROR_FILE_NOT_FOUND
);
330 return INVALID_HANDLE_VALUE
;
334 /***********************************************************************
337 * Open an exe file, and return the full name and file handle.
338 * Returns FALSE if file could not be found.
339 * If file exists but cannot be opened, returns TRUE and set handle to INVALID_HANDLE_VALUE.
340 * If file is a builtin exe, returns TRUE and sets handle to 0.
342 static BOOL
find_exe_file( const char *name
, char *buffer
, int buflen
, HANDLE
*handle
)
344 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
347 TRACE("looking for %s\n", debugstr_a(name
) );
349 if (!SearchPathA( NULL
, name
, ".exe", buflen
, buffer
, NULL
) &&
350 !MODULE_GetBuiltinPath( name
, ".exe", buffer
, buflen
))
352 /* no builtin found, try native without extension in case it is a Unix app */
354 if (SearchPathA( NULL
, name
, NULL
, buflen
, buffer
, NULL
))
356 TRACE( "Trying native/Unix binary %s\n", debugstr_a(buffer
) );
357 if ((*handle
= CreateFileA( buffer
, GENERIC_READ
, FILE_SHARE_READ
,
358 NULL
, OPEN_EXISTING
, 0, 0 )) != INVALID_HANDLE_VALUE
)
364 MODULE_GetLoadOrder( loadorder
, buffer
, TRUE
);
366 for(i
= 0; i
< LOADORDER_NTYPES
; i
++)
368 if (loadorder
[i
] == LOADORDER_INVALID
) break;
372 TRACE( "Trying native exe %s\n", debugstr_a(buffer
) );
373 if ((*handle
= CreateFileA( buffer
, GENERIC_READ
, FILE_SHARE_READ
,
374 NULL
, OPEN_EXISTING
, 0, 0 )) != INVALID_HANDLE_VALUE
)
376 if (GetLastError() != ERROR_FILE_NOT_FOUND
) return TRUE
;
379 TRACE( "Trying built-in exe %s\n", debugstr_a(buffer
) );
380 if (open_builtin_exe_file( buffer
, NULL
, 0, 1 ))
390 SetLastError( ERROR_FILE_NOT_FOUND
);
395 /***********************************************************************
398 * Main process initialisation code
400 static BOOL
process_init( char *argv
[] )
403 size_t info_size
= 0;
405 /* store the program name */
408 /* Fill the initial process structure */
409 current_process
.exit_code
= STILL_ACTIVE
;
410 current_process
.threads
= 1;
411 current_process
.running_threads
= 1;
412 current_process
.ring0_threads
= 1;
413 current_process
.group
= ¤t_process
;
414 current_process
.priority
= 8; /* Normal */
416 /* Setup the server connection */
419 /* Retrieve startup info from the server */
420 SERVER_START_REQ( init_process
)
422 req
->ldt_copy
= &wine_ldt_copy
;
423 if ((ret
= !wine_server_call_err( req
)))
425 main_exe_file
= reply
->exe_file
;
426 main_create_flags
= reply
->create_flags
;
427 info_size
= reply
->info_size
;
428 server_startticks
= reply
->server_start
;
429 current_startupinfo
.hStdInput
= reply
->hstdin
;
430 current_startupinfo
.hStdOutput
= reply
->hstdout
;
431 current_startupinfo
.hStdError
= reply
->hstderr
;
435 if (!ret
) return FALSE
;
437 /* Create the process heap */
438 current_process
.heap
= HeapCreate( HEAP_GROWABLE
, 0, 0 );
440 if (main_create_flags
== 0 &&
441 current_startupinfo
.hStdInput
== 0 &&
442 current_startupinfo
.hStdOutput
== 0 &&
443 current_startupinfo
.hStdError
== 0)
445 /* no parent, and no new console requested, create a simple console with bare handles to
446 * unix stdio input & output streams (aka simple console)
449 wine_server_fd_to_handle( 0, GENERIC_READ
|SYNCHRONIZE
, TRUE
, &handle
);
450 SetStdHandle( STD_INPUT_HANDLE
, handle
);
451 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, TRUE
, &handle
);
452 SetStdHandle( STD_OUTPUT_HANDLE
, handle
);
453 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, TRUE
, &handle
);
454 SetStdHandle( STD_ERROR_HANDLE
, handle
);
456 else if (!(main_create_flags
& (DETACHED_PROCESS
|CREATE_NEW_CONSOLE
)))
458 SetStdHandle( STD_INPUT_HANDLE
, current_startupinfo
.hStdInput
);
459 SetStdHandle( STD_OUTPUT_HANDLE
, current_startupinfo
.hStdOutput
);
460 SetStdHandle( STD_ERROR_HANDLE
, current_startupinfo
.hStdError
);
463 /* Now we can use the pthreads routines */
466 /* Copy the parent environment */
467 if (!(current_process
.env_db
= ENV_InitStartupInfo( info_size
, main_exe_name
,
468 sizeof(main_exe_name
) )))
471 /* Parse command line arguments */
472 OPTIONS_ParseOptions( !info_size
? argv
: NULL
);
474 ret
= MAIN_MainInit();
475 if (TRACE_ON(relay
) || TRACE_ON(snoop
)) RELAY_InitDebugLists();
481 /***********************************************************************
484 * Startup routine of a new process. Runs on the new process stack.
486 static void start_process(void)
488 int debugged
, console_app
;
489 LPTHREAD_START_ROUTINE entry
;
491 HANDLE main_file
= main_exe_file
;
492 IMAGE_NT_HEADERS
*nt
;
494 /* use original argv[0] as name for the main module */
495 if (!main_exe_name
[0])
497 if (!GetLongPathNameA( full_argv0
, main_exe_name
, sizeof(main_exe_name
) ))
498 lstrcpynA( main_exe_name
, full_argv0
, sizeof(main_exe_name
) );
503 UINT drive_type
= GetDriveTypeA( main_exe_name
);
504 /* don't keep the file handle open on removable media */
505 if (drive_type
== DRIVE_REMOVABLE
|| drive_type
== DRIVE_CDROM
) main_file
= 0;
508 /* Retrieve entry point address */
509 nt
= RtlImageNtHeader( current_process
.module
);
510 entry
= (LPTHREAD_START_ROUTINE
)((char*)current_process
.module
+
511 nt
->OptionalHeader
.AddressOfEntryPoint
);
512 console_app
= (nt
->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
);
513 if (console_app
) current_process
.flags
|= PDB32_CONSOLE_PROC
;
515 /* Install signal handlers; this cannot be done before, since we cannot
516 * send exceptions to the debugger before the create process event that
517 * is sent by REQ_INIT_PROCESS_DONE.
518 * We do need the handlers in place by the time the request is over, so
519 * we set them up here. If we segfault between here and the server call
520 * something is very wrong... */
521 if (!SIGNAL_Init()) goto error
;
523 /* Signal the parent process to continue */
524 SERVER_START_REQ( init_process_done
)
526 req
->module
= (void *)current_process
.module
;
527 req
->module_size
= nt
->OptionalHeader
.SizeOfImage
;
529 /* API requires a double indirection */
530 req
->name
= &main_exe_name_ptr
;
531 req
->exe_file
= main_file
;
532 req
->gui
= !console_app
;
533 wine_server_add_data( req
, main_exe_name
, strlen(main_exe_name
) );
534 wine_server_call( req
);
535 debugged
= reply
->debugged
;
539 /* create the main modref and load dependencies */
540 if (!(wm
= PE_CreateModule( current_process
.module
, main_exe_name
, 0, 0, FALSE
)))
544 if (main_exe_file
) CloseHandle( main_exe_file
); /* we no longer need it */
546 MODULE_DllProcessAttach( NULL
, (LPVOID
)1 );
548 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
549 * context of the parent process. Actually, the USER signal proc
550 * doesn't really care about that, but it *does* require that the
551 * startup parameters are correctly set up, so that GetProcessDword
552 * works. Furthermore, before calling the USER signal proc the
553 * 16-bit stack must be set up, which it is only after TASK_Create
554 * in the case of a 16-bit process. Thus, we send the signal here.
556 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE
, 0 );
557 PROCESS_CallUserSignalProc( USIG_THREAD_INIT
, 0 );
558 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT
, 0 );
559 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED
, 0 );
560 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
561 if (console_app
) PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING
, 0 );
564 DPRINTF( "%04lx:Starting process %s (entryproc=%p)\n",
565 GetCurrentThreadId(), main_exe_name
, entry
);
566 if (debugged
) DbgBreakPoint();
567 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
568 * Dunno about other OSs */
569 SetLastError(0); /* clear error code */
570 ExitThread( entry(NULL
) );
573 ExitProcess( GetLastError() );
577 /***********************************************************************
578 * __wine_process_init
580 * Wine initialisation: load and start the main exe file.
582 void __wine_process_init( int argc
, char *argv
[] )
584 char error
[1024], *p
;
585 DWORD stack_size
= 0;
587 /* Initialize everything */
588 if (!process_init( argv
)) exit(1);
590 argv
++; /* remove argv[0] (wine itself) */
592 TRACE( "starting process name=%s file=%p argv[0]=%s\n",
593 debugstr_a(main_exe_name
), main_exe_file
, debugstr_a(argv
[0]) );
595 if (!main_exe_name
[0])
597 if (!argv
[0]) OPTIONS_Usage();
599 if (!find_exe_file( argv
[0], main_exe_name
, sizeof(main_exe_name
), &main_exe_file
))
601 MESSAGE( "%s: cannot find '%s'\n", argv0
, argv
[0] );
604 if (main_exe_file
== INVALID_HANDLE_VALUE
)
606 MESSAGE( "%s: cannot open '%s'\n", argv0
, main_exe_name
);
611 if (!main_exe_file
) /* no file handle -> Winelib app */
613 TRACE( "starting Winelib app %s\n", debugstr_a(main_exe_name
) );
614 if (open_builtin_exe_file( main_exe_name
, error
, sizeof(error
), 0 ))
616 MESSAGE( "%s: cannot open builtin library for '%s': %s\n", argv0
, main_exe_name
, error
);
619 VERSION_Init( main_exe_name
);
621 switch( MODULE_GetBinaryType( main_exe_file
))
624 TRACE( "starting Win32 binary %s\n", debugstr_a(main_exe_name
) );
625 if ((current_process
.module
= PE_LoadImage( main_exe_file
, main_exe_name
, 0 ))) goto found
;
626 MESSAGE( "%s: could not load '%s' as Win32 binary\n", argv0
, main_exe_name
);
629 MESSAGE( "%s: '%s' is a DLL, not an executable\n", argv0
, main_exe_name
);
632 /* check for .com extension */
633 if (!(p
= strrchr( main_exe_name
, '.' )) || FILE_strcasecmp( p
, ".com" ))
635 MESSAGE( "%s: cannot determine executable type for '%s'\n", argv0
, main_exe_name
);
641 TRACE( "starting Win16/DOS binary %s\n", debugstr_a(main_exe_name
) );
642 CloseHandle( main_exe_file
);
645 argv
[0] = "winevdm.exe";
646 if (open_builtin_exe_file( "winevdm.exe", error
, sizeof(error
), 0 ))
648 MESSAGE( "%s: trying to run '%s', cannot open builtin library for 'winevdm.exe': %s\n",
649 argv0
, main_exe_name
, error
);
652 MESSAGE( "%s: '%s' is an OS/2 binary, not supported\n", argv0
, main_exe_name
);
654 case BINARY_UNIX_EXE
:
655 MESSAGE( "%s: '%s' is a Unix binary, not supported\n", argv0
, main_exe_name
);
657 case BINARY_UNIX_LIB
:
659 DOS_FULL_NAME full_name
;
660 const char *name
= main_exe_name
;
661 UNICODE_STRING nameW
;
663 TRACE( "starting Winelib app %s\n", debugstr_a(main_exe_name
) );
664 RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
665 if (DOSFS_GetFullName( nameW
.Buffer
, TRUE
, &full_name
)) name
= full_name
.long_name
;
666 RtlFreeUnicodeString(&nameW
);
667 CloseHandle( main_exe_file
);
669 if (wine_dlopen( name
, RTLD_NOW
, error
, sizeof(error
) ))
671 if ((p
= strrchr( main_exe_name
, '.' )) && !strcmp( p
, ".so" )) *p
= 0;
674 MESSAGE( "%s: could not load '%s': %s\n", argv0
, main_exe_name
, error
);
680 /* build command line */
681 if (!ENV_BuildCommandLine( argv
)) goto error
;
683 /* create 32-bit module for main exe */
684 if (!(current_process
.module
= BUILTIN32_LoadExeModule( current_process
.module
))) goto error
;
685 stack_size
= RtlImageNtHeader(current_process
.module
)->OptionalHeader
.SizeOfStackReserve
;
687 /* allocate main thread stack */
688 if (!THREAD_InitStack( NtCurrentTeb(), stack_size
)) goto error
;
690 /* switch to the new stack */
691 SYSDEPS_SwitchToThreadStack( start_process
);
694 ExitProcess( GetLastError() );
698 /***********************************************************************
701 * Build an argv array from a command-line.
702 * The command-line is modified to insert nulls.
703 * 'reserved' is the number of args to reserve before the first one.
705 static char **build_argv( char *cmdline
, int reserved
)
710 int in_quotes
,bcount
;
717 if (*s
=='\0' || ((*s
==' ' || *s
=='\t') && !in_quotes
)) {
720 /* skip the remaining spaces */
721 while (*s
==' ' || *s
=='\t') {
728 } else if (*s
=='\\') {
729 /* '\', count them */
731 } else if ((*s
=='"') && ((bcount
& 1)==0)) {
733 in_quotes
=!in_quotes
;
736 /* a regular character */
741 argv
=malloc(argc
*sizeof(*argv
));
750 if ((*s
==' ' || *s
=='\t') && !in_quotes
) {
751 /* Close the argument and copy it */
755 /* skip the remaining spaces */
758 } while (*s
==' ' || *s
=='\t');
760 /* Start with a new argument */
763 } else if (*s
=='\\') {
767 } else if (*s
=='"') {
769 if ((bcount
& 1)==0) {
770 /* Preceeded by an even number of '\', this is half that
771 * number of '\', plus a '"' which we discard.
775 in_quotes
=!in_quotes
;
777 /* Preceeded by an odd number of '\', this is half that
778 * number of '\' followed by a '"'
786 /* a regular character */
801 /***********************************************************************
804 * Build the environment of a new child process.
806 static char **build_envp( const char *env
, const char *extra_env
)
812 if (extra_env
) for (p
= extra_env
; *p
; count
++) p
+= strlen(p
) + 1;
813 for (p
= env
; *p
; count
++) p
+= strlen(p
) + 1;
816 if ((envp
= malloc( count
* sizeof(*envp
) )))
818 extern char **environ
;
819 char **envptr
= envp
;
820 char **unixptr
= environ
;
821 /* first the extra strings */
822 if (extra_env
) for (p
= extra_env
; *p
; p
+= strlen(p
) + 1) *envptr
++ = (char *)p
;
823 /* then put PATH, HOME and WINEPREFIX from the unix env */
824 for (unixptr
= environ
; unixptr
&& *unixptr
; unixptr
++)
825 if (!memcmp( *unixptr
, "PATH=", 5 ) ||
826 !memcmp( *unixptr
, "HOME=", 5 ) ||
827 !memcmp( *unixptr
, "WINEPREFIX=", 11 )) *envptr
++ = *unixptr
;
828 /* now put the Windows environment strings */
829 for (p
= env
; *p
; p
+= strlen(p
) + 1)
831 if (!memcmp( p
, "PATH=", 5 )) /* store PATH as WINEPATH */
833 char *winepath
= malloc( strlen(p
) + 5 );
834 strcpy( winepath
, "WINE" );
835 strcpy( winepath
+ 4, p
);
836 *envptr
++ = winepath
;
838 else if (memcmp( p
, "HOME=", 5 ) &&
839 memcmp( p
, "WINEPATH=", 9 ) &&
840 memcmp( p
, "WINEPREFIX=", 11 )) *envptr
++ = (char *)p
;
848 /***********************************************************************
851 * Locate the Wine binary to exec for a new Win32 process.
853 static void exec_wine_binary( char **argv
, char **envp
)
855 const char *path
, *pos
, *ptr
;
857 /* first, try for a WINELOADER environment variable */
858 argv
[0] = getenv("WINELOADER");
860 execve( argv
[0], argv
, envp
);
862 /* next, try bin directory */
863 argv
[0] = BINDIR
"/wine";
864 execve( argv
[0], argv
, envp
);
866 /* now try the path of argv0 of the current binary */
867 if (!(argv
[0] = malloc( strlen(full_argv0
) + 6 ))) return;
868 if ((ptr
= strrchr( full_argv0
, '/' )))
870 memcpy( argv
[0], full_argv0
, ptr
- full_argv0
);
871 strcpy( argv
[0] + (ptr
- full_argv0
), "/wine" );
872 execve( argv
[0], argv
, envp
);
876 /* now search in the Unix path */
877 if ((path
= getenv( "PATH" )))
879 if (!(argv
[0] = malloc( strlen(path
) + 6 ))) return;
883 while (*pos
== ':') pos
++;
885 if (!(ptr
= strchr( pos
, ':' ))) ptr
= pos
+ strlen(pos
);
886 memcpy( argv
[0], pos
, ptr
- pos
);
887 strcpy( argv
[0] + (ptr
- pos
), "/wine" );
888 execve( argv
[0], argv
, envp
);
896 /***********************************************************************
899 * Fork and exec a new Unix binary, checking for errors.
901 static int fork_and_exec( const char *filename
, char *cmdline
,
902 const char *env
, const char *newdir
)
907 if (!env
) env
= GetEnvironmentStringsA();
914 fcntl( fd
[1], F_SETFD
, 1 ); /* set close on exec */
915 if (!(pid
= fork())) /* child */
917 char **argv
= build_argv( cmdline
, 0 );
918 char **envp
= build_envp( env
, NULL
);
921 /* Reset signals that we previously set to SIG_IGN */
922 signal( SIGPIPE
, SIG_DFL
);
923 signal( SIGCHLD
, SIG_DFL
);
925 if (newdir
) chdir(newdir
);
927 if (argv
&& envp
) execve( filename
, argv
, envp
);
929 write( fd
[1], &err
, sizeof(err
) );
933 if ((pid
!= -1) && (read( fd
[0], &err
, sizeof(err
) ) > 0)) /* exec failed */
938 if (pid
== -1) FILE_SetDosError();
944 /***********************************************************************
947 * Create a new process. If hFile is a valid handle we have an exe
948 * file, otherwise it is a Winelib app.
950 static BOOL
create_process( HANDLE hFile
, LPCSTR filename
, LPSTR cmd_line
, LPCSTR env
,
951 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
952 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
953 LPPROCESS_INFORMATION info
, LPCSTR unixdir
)
955 BOOL ret
, success
= FALSE
;
957 startup_info_t startup_info
;
958 char *extra_env
= NULL
;
967 env
= GetEnvironmentStringsA();
968 extra_env
= DRIVE_BuildEnv();
971 /* create the synchronization pipes */
973 if (pipe( startfd
) == -1)
978 if (pipe( execfd
) == -1)
985 fcntl( execfd
[1], F_SETFD
, 1 ); /* set close on exec */
987 /* create the child process */
989 if (!(pid
= fork())) /* child */
991 char **argv
= build_argv( cmd_line
, 1 );
992 char **envp
= build_envp( env
, extra_env
);
997 /* wait for parent to tell us to start */
998 if (read( startfd
[0], &dummy
, 1 ) != 1) _exit(1);
1000 close( startfd
[0] );
1001 /* Reset signals that we previously set to SIG_IGN */
1002 signal( SIGPIPE
, SIG_DFL
);
1003 signal( SIGCHLD
, SIG_DFL
);
1005 if (unixdir
) chdir(unixdir
);
1007 if (argv
&& envp
) exec_wine_binary( argv
, envp
);
1010 write( execfd
[1], &err
, sizeof(err
) );
1014 /* this is the parent */
1016 close( startfd
[0] );
1018 if (extra_env
) HeapFree( GetProcessHeap(), 0, extra_env
);
1021 close( startfd
[1] );
1027 /* fill the startup info structure */
1029 startup_info
.size
= sizeof(startup_info
);
1030 /* startup_info.filename_len is set below */
1031 startup_info
.cmdline_len
= cmd_line
? strlen(cmd_line
) : 0;
1032 startup_info
.desktop_len
= startup
->lpDesktop
? strlen(startup
->lpDesktop
) : 0;
1033 startup_info
.title_len
= startup
->lpTitle
? strlen(startup
->lpTitle
) : 0;
1034 startup_info
.x
= startup
->dwX
;
1035 startup_info
.y
= startup
->dwY
;
1036 startup_info
.cx
= startup
->dwXSize
;
1037 startup_info
.cy
= startup
->dwYSize
;
1038 startup_info
.x_chars
= startup
->dwXCountChars
;
1039 startup_info
.y_chars
= startup
->dwYCountChars
;
1040 startup_info
.attribute
= startup
->dwFillAttribute
;
1041 startup_info
.cmd_show
= startup
->wShowWindow
;
1042 startup_info
.flags
= startup
->dwFlags
;
1044 /* create the process on the server side */
1046 SERVER_START_REQ( new_process
)
1051 req
->inherit_all
= inherit
;
1052 req
->create_flags
= flags
;
1053 req
->use_handles
= (startup
->dwFlags
& STARTF_USESTDHANDLES
) != 0;
1054 req
->unix_pid
= pid
;
1055 req
->exe_file
= hFile
;
1056 if (startup
->dwFlags
& STARTF_USESTDHANDLES
)
1058 req
->hstdin
= startup
->hStdInput
;
1059 req
->hstdout
= startup
->hStdOutput
;
1060 req
->hstderr
= startup
->hStdError
;
1064 req
->hstdin
= GetStdHandle( STD_INPUT_HANDLE
);
1065 req
->hstdout
= GetStdHandle( STD_OUTPUT_HANDLE
);
1066 req
->hstderr
= GetStdHandle( STD_ERROR_HANDLE
);
1069 if (GetLongPathNameA( filename
, buf
, MAX_PATH
))
1074 startup_info
.filename_len
= strlen(nameptr
);
1075 wine_server_add_data( req
, &startup_info
, sizeof(startup_info
) );
1076 wine_server_add_data( req
, nameptr
, startup_info
.filename_len
);
1077 wine_server_add_data( req
, cmd_line
, startup_info
.cmdline_len
);
1078 wine_server_add_data( req
, startup
->lpDesktop
, startup_info
.desktop_len
);
1079 wine_server_add_data( req
, startup
->lpTitle
, startup_info
.title_len
);
1081 ret
= !wine_server_call_err( req
);
1082 process_info
= reply
->info
;
1088 close( startfd
[1] );
1093 /* tell child to start and wait for it to exec */
1095 write( startfd
[1], &dummy
, 1 );
1096 close( startfd
[1] );
1098 if (read( execfd
[0], &err
, sizeof(err
) ) > 0) /* exec failed */
1103 CloseHandle( process_info
);
1107 /* wait for the new process info to be ready */
1109 WaitForSingleObject( process_info
, INFINITE
);
1110 SERVER_START_REQ( get_new_process_info
)
1112 req
->info
= process_info
;
1113 req
->pinherit
= (psa
&& (psa
->nLength
>= sizeof(*psa
)) && psa
->bInheritHandle
);
1114 req
->tinherit
= (tsa
&& (tsa
->nLength
>= sizeof(*tsa
)) && tsa
->bInheritHandle
);
1115 if ((ret
= !wine_server_call_err( req
)))
1117 info
->dwProcessId
= (DWORD
)reply
->pid
;
1118 info
->dwThreadId
= (DWORD
)reply
->tid
;
1119 info
->hProcess
= reply
->phandle
;
1120 info
->hThread
= reply
->thandle
;
1121 success
= reply
->success
;
1126 if (ret
&& !success
) /* new process failed to start */
1129 if (GetExitCodeProcess( info
->hProcess
, &exitcode
)) SetLastError( exitcode
);
1130 CloseHandle( info
->hThread
);
1131 CloseHandle( info
->hProcess
);
1134 CloseHandle( process_info
);
1139 /***********************************************************************
1140 * create_vdm_process
1142 * Create a new VDM process for a 16-bit or DOS application.
1144 static BOOL
create_vdm_process( LPCSTR filename
, LPSTR cmd_line
, LPCSTR env
,
1145 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
1146 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
1147 LPPROCESS_INFORMATION info
, LPCSTR unixdir
)
1150 LPSTR new_cmd_line
= HeapAlloc( GetProcessHeap(), 0, strlen(filename
) + strlen(cmd_line
) + 30 );
1154 SetLastError( ERROR_OUTOFMEMORY
);
1157 sprintf( new_cmd_line
, "winevdm.exe --app-name \"%s\" %s", filename
, cmd_line
);
1158 ret
= create_process( 0, "winevdm.exe", new_cmd_line
, env
, psa
, tsa
, inherit
,
1159 flags
, startup
, info
, unixdir
);
1160 HeapFree( GetProcessHeap(), 0, new_cmd_line
);
1165 /*************************************************************************
1168 * Helper for CreateProcess: retrieve the file name to load from the
1169 * app name and command line. Store the file name in buffer, and
1170 * return a possibly modified command line.
1171 * Also returns a handle to the opened file if it's a Windows binary.
1173 static LPSTR
get_file_name( LPCSTR appname
, LPSTR cmdline
, LPSTR buffer
,
1174 int buflen
, HANDLE
*handle
)
1176 char *name
, *pos
, *ret
= NULL
;
1179 /* if we have an app name, everything is easy */
1183 /* use the unmodified app name as file name */
1184 lstrcpynA( buffer
, appname
, buflen
);
1185 *handle
= open_exe_file( buffer
);
1186 if (!(ret
= cmdline
) || !cmdline
[0])
1188 /* no command-line, create one */
1189 if ((ret
= HeapAlloc( GetProcessHeap(), 0, strlen(appname
) + 3 )))
1190 sprintf( ret
, "\"%s\"", appname
);
1197 SetLastError( ERROR_INVALID_PARAMETER
);
1201 /* first check for a quoted file name */
1203 if ((cmdline
[0] == '"') && ((p
= strchr( cmdline
+ 1, '"' ))))
1205 int len
= p
- cmdline
- 1;
1206 /* extract the quoted portion as file name */
1207 if (!(name
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 ))) return NULL
;
1208 memcpy( name
, cmdline
+ 1, len
);
1211 if (find_exe_file( name
, buffer
, buflen
, handle
))
1212 ret
= cmdline
; /* no change necessary */
1216 /* now try the command-line word by word */
1218 if (!(name
= HeapAlloc( GetProcessHeap(), 0, strlen(cmdline
) + 1 ))) return NULL
;
1224 do *pos
++ = *p
++; while (*p
&& *p
!= ' ');
1226 if (find_exe_file( name
, buffer
, buflen
, handle
))
1233 if (!ret
|| !strchr( name
, ' ' )) goto done
; /* no change necessary */
1235 /* now build a new command-line with quotes */
1237 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, strlen(cmdline
) + 3 ))) goto done
;
1238 sprintf( ret
, "\"%s\"%s", name
, p
);
1241 HeapFree( GetProcessHeap(), 0, name
);
1246 /**********************************************************************
1247 * CreateProcessA (KERNEL32.@)
1249 BOOL WINAPI
CreateProcessA( LPCSTR app_name
, LPSTR cmd_line
, LPSECURITY_ATTRIBUTES process_attr
,
1250 LPSECURITY_ATTRIBUTES thread_attr
, BOOL inherit
,
1251 DWORD flags
, LPVOID env
, LPCSTR cur_dir
,
1252 LPSTARTUPINFOA startup_info
, LPPROCESS_INFORMATION info
)
1256 const char *unixdir
= NULL
;
1257 DOS_FULL_NAME full_dir
;
1258 char name
[MAX_PATH
];
1262 /* Process the AppName and/or CmdLine to get module name and path */
1264 TRACE("app %s cmdline %s\n", debugstr_a(app_name
), debugstr_a(cmd_line
) );
1266 if (!(tidy_cmdline
= get_file_name( app_name
, cmd_line
, name
, sizeof(name
), &hFile
)))
1268 if (hFile
== INVALID_HANDLE_VALUE
) goto done
;
1270 /* Warn if unsupported features are used */
1272 if (flags
& NORMAL_PRIORITY_CLASS
)
1273 FIXME("(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name
);
1274 if (flags
& IDLE_PRIORITY_CLASS
)
1275 FIXME("(%s,...): IDLE_PRIORITY_CLASS ignored\n", name
);
1276 if (flags
& HIGH_PRIORITY_CLASS
)
1277 FIXME("(%s,...): HIGH_PRIORITY_CLASS ignored\n", name
);
1278 if (flags
& REALTIME_PRIORITY_CLASS
)
1279 FIXME("(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name
);
1280 if (flags
& CREATE_NEW_PROCESS_GROUP
)
1281 FIXME("(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name
);
1282 if (flags
& CREATE_UNICODE_ENVIRONMENT
)
1283 FIXME("(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name
);
1284 if (flags
& CREATE_SEPARATE_WOW_VDM
)
1285 FIXME("(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name
);
1286 if (flags
& CREATE_SHARED_WOW_VDM
)
1287 FIXME("(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name
);
1288 if (flags
& CREATE_DEFAULT_ERROR_MODE
)
1289 FIXME("(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name
);
1290 if (flags
& CREATE_NO_WINDOW
)
1291 FIXME("(%s,...): CREATE_NO_WINDOW ignored\n", name
);
1292 if (flags
& PROFILE_USER
)
1293 FIXME("(%s,...): PROFILE_USER ignored\n", name
);
1294 if (flags
& PROFILE_KERNEL
)
1295 FIXME("(%s,...): PROFILE_KERNEL ignored\n", name
);
1296 if (flags
& PROFILE_SERVER
)
1297 FIXME("(%s,...): PROFILE_SERVER ignored\n", name
);
1298 if (startup_info
->lpDesktop
)
1299 FIXME("(%s,...): startup_info->lpDesktop %s ignored\n",
1300 name
, debugstr_a(startup_info
->lpDesktop
));
1301 if (startup_info
->dwFlags
& STARTF_RUNFULLSCREEN
)
1302 FIXME("(%s,...): STARTF_RUNFULLSCREEN ignored\n", name
);
1303 if (startup_info
->dwFlags
& STARTF_FORCEONFEEDBACK
)
1304 FIXME("(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name
);
1305 if (startup_info
->dwFlags
& STARTF_FORCEOFFFEEDBACK
)
1306 FIXME("(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name
);
1307 if (startup_info
->dwFlags
& STARTF_USEHOTKEY
)
1308 FIXME("(%s,...): STARTF_USEHOTKEY ignored\n", name
);
1312 UNICODE_STRING cur_dirW
;
1313 RtlCreateUnicodeStringFromAsciiz(&cur_dirW
, cur_dir
);
1314 if (DOSFS_GetFullName( cur_dirW
.Buffer
, TRUE
, &full_dir
))
1315 unixdir
= full_dir
.long_name
;
1316 RtlFreeUnicodeString(&cur_dirW
);
1320 WCHAR buf
[MAX_PATH
];
1321 if (GetCurrentDirectoryW(MAX_PATH
, buf
))
1323 if (DOSFS_GetFullName( buf
, TRUE
, &full_dir
)) unixdir
= full_dir
.long_name
;
1327 info
->hThread
= info
->hProcess
= 0;
1328 info
->dwProcessId
= info
->dwThreadId
= 0;
1330 /* Determine executable type */
1332 if (!hFile
) /* builtin exe */
1334 TRACE( "starting %s as Winelib app\n", debugstr_a(name
) );
1335 retv
= create_process( 0, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1336 inherit
, flags
, startup_info
, info
, unixdir
);
1340 switch( MODULE_GetBinaryType( hFile
))
1343 TRACE( "starting %s as Win32 binary\n", debugstr_a(name
) );
1344 retv
= create_process( hFile
, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1345 inherit
, flags
, startup_info
, info
, unixdir
);
1349 TRACE( "starting %s as Win16/DOS binary\n", debugstr_a(name
) );
1350 retv
= create_vdm_process( name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1351 inherit
, flags
, startup_info
, info
, unixdir
);
1354 FIXME( "%s is OS/2 binary, not supported\n", debugstr_a(name
) );
1355 SetLastError( ERROR_BAD_EXE_FORMAT
);
1358 TRACE( "not starting %s since it is a dll\n", debugstr_a(name
) );
1359 SetLastError( ERROR_BAD_EXE_FORMAT
);
1361 case BINARY_UNIX_LIB
:
1362 TRACE( "%s is a Unix library, starting as Winelib app\n", debugstr_a(name
) );
1363 retv
= create_process( hFile
, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1364 inherit
, flags
, startup_info
, info
, unixdir
);
1366 case BINARY_UNKNOWN
:
1367 /* check for .com or .bat extension */
1368 if ((p
= strrchr( name
, '.' )))
1370 if (!FILE_strcasecmp( p
, ".com" ))
1372 TRACE( "starting %s as DOS binary\n", debugstr_a(name
) );
1373 retv
= create_vdm_process( name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1374 inherit
, flags
, startup_info
, info
, unixdir
);
1377 if (!FILE_strcasecmp( p
, ".bat" ))
1379 char comspec
[MAX_PATH
];
1380 if (GetEnvironmentVariableA("COMSPEC", comspec
, sizeof(comspec
)))
1382 char *newcmdline
, *q
= strchr(cmd_line
, ' ');
1384 if ((newcmdline
= HeapAlloc( GetProcessHeap(), 0,
1385 strlen(comspec
) + strlen(name
) + strlen(q
) + 8)))
1387 sprintf( newcmdline
, "%s /c %s%s", comspec
, name
, q
);
1388 TRACE( "starting %s as batch binary: %s\n",
1389 debugstr_a(name
), debugstr_a(newcmdline
) );
1390 retv
= CreateProcessA( comspec
, newcmdline
, process_attr
, thread_attr
,
1391 inherit
, flags
, env
, cur_dir
, startup_info
, info
);
1392 HeapFree( GetProcessHeap(), 0, newcmdline
);
1399 case BINARY_UNIX_EXE
:
1401 /* unknown file, try as unix executable */
1402 UNICODE_STRING nameW
;
1403 DOS_FULL_NAME full_name
;
1404 const char *unixfilename
= name
;
1406 TRACE( "starting %s as Unix binary\n", debugstr_a(name
) );
1408 RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
1409 if (DOSFS_GetFullName( nameW
.Buffer
, TRUE
, &full_name
)) unixfilename
= full_name
.long_name
;
1410 RtlFreeUnicodeString(&nameW
);
1411 retv
= (fork_and_exec( unixfilename
, tidy_cmdline
, env
, unixdir
) != -1);
1415 CloseHandle( hFile
);
1418 if (tidy_cmdline
!= cmd_line
) HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1423 /**********************************************************************
1424 * CreateProcessW (KERNEL32.@)
1426 * lpReserved is not converted
1428 BOOL WINAPI
CreateProcessW( LPCWSTR app_name
, LPWSTR cmd_line
, LPSECURITY_ATTRIBUTES process_attr
,
1429 LPSECURITY_ATTRIBUTES thread_attr
, BOOL inherit
, DWORD flags
,
1430 LPVOID env
, LPCWSTR cur_dir
, LPSTARTUPINFOW startup_info
,
1431 LPPROCESS_INFORMATION info
)
1434 STARTUPINFOA StartupInfoA
;
1436 LPSTR app_nameA
= HEAP_strdupWtoA (GetProcessHeap(),0,app_name
);
1437 LPSTR cmd_lineA
= HEAP_strdupWtoA (GetProcessHeap(),0,cmd_line
);
1438 LPSTR cur_dirA
= HEAP_strdupWtoA (GetProcessHeap(),0,cur_dir
);
1440 memcpy (&StartupInfoA
, startup_info
, sizeof(STARTUPINFOA
));
1441 StartupInfoA
.lpDesktop
= HEAP_strdupWtoA (GetProcessHeap(),0,startup_info
->lpDesktop
);
1442 StartupInfoA
.lpTitle
= HEAP_strdupWtoA (GetProcessHeap(),0,startup_info
->lpTitle
);
1444 TRACE_(win32
)("(%s,%s,...)\n", debugstr_w(app_name
), debugstr_w(cmd_line
));
1446 if (startup_info
->lpReserved
)
1447 FIXME_(win32
)("StartupInfo.lpReserved is used, please report (%s)\n",
1448 debugstr_w(startup_info
->lpReserved
));
1450 ret
= CreateProcessA( app_nameA
, cmd_lineA
, process_attr
, thread_attr
,
1451 inherit
, flags
, env
, cur_dirA
, &StartupInfoA
, info
);
1453 HeapFree( GetProcessHeap(), 0, cur_dirA
);
1454 HeapFree( GetProcessHeap(), 0, cmd_lineA
);
1455 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpDesktop
);
1456 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpTitle
);
1462 /***********************************************************************
1463 * ExitProcess (KERNEL32.@)
1465 void WINAPI
ExitProcess( DWORD status
)
1467 LdrShutdownProcess();
1468 SERVER_START_REQ( terminate_process
)
1470 /* send the exit code to the server */
1471 req
->handle
= GetCurrentProcess();
1472 req
->exit_code
= status
;
1473 wine_server_call( req
);
1479 /***********************************************************************
1480 * ExitProcess (KERNEL.466)
1482 void WINAPI
ExitProcess16( WORD status
)
1485 ReleaseThunkLock( &count
);
1486 ExitProcess( status
);
1489 /******************************************************************************
1490 * TerminateProcess (KERNEL32.@)
1492 BOOL WINAPI
TerminateProcess( HANDLE handle
, DWORD exit_code
)
1494 NTSTATUS status
= NtTerminateProcess( handle
, exit_code
);
1495 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1500 /***********************************************************************
1501 * GetProcessDword (KERNEL.485)
1502 * GetProcessDword (KERNEL32.18)
1503 * 'Of course you cannot directly access Windows internal structures'
1505 DWORD WINAPI
GetProcessDword( DWORD dwProcessID
, INT offset
)
1509 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
1511 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
1513 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
1519 case GPD_APP_COMPAT_FLAGS
:
1520 return GetAppCompatFlags16(0);
1522 case GPD_LOAD_DONE_EVENT
:
1523 return (DWORD
)current_process
.load_done_evt
;
1525 case GPD_HINSTANCE16
:
1526 return GetTaskDS16();
1528 case GPD_WINDOWS_VERSION
:
1529 return GetExeVersion16();
1532 return (DWORD
)NtCurrentTeb() - 0x10 /* FIXME */;
1535 return (DWORD
)¤t_process
;
1537 case GPD_STARTF_SHELLDATA
: /* return stdoutput handle from startupinfo ??? */
1538 return (DWORD
)current_startupinfo
.hStdOutput
;
1540 case GPD_STARTF_HOTKEY
: /* return stdinput handle from startupinfo ??? */
1541 return (DWORD
)current_startupinfo
.hStdInput
;
1543 case GPD_STARTF_SHOWWINDOW
:
1544 return current_startupinfo
.wShowWindow
;
1546 case GPD_STARTF_SIZE
:
1547 x
= current_startupinfo
.dwXSize
;
1548 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
1549 y
= current_startupinfo
.dwYSize
;
1550 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
1551 return MAKELONG( x
, y
);
1553 case GPD_STARTF_POSITION
:
1554 x
= current_startupinfo
.dwX
;
1555 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
1556 y
= current_startupinfo
.dwY
;
1557 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
1558 return MAKELONG( x
, y
);
1560 case GPD_STARTF_FLAGS
:
1561 return current_startupinfo
.dwFlags
;
1567 return current_process
.flags
;
1570 return current_process
.process_dword
;
1573 ERR_(win32
)("Unknown offset %d\n", offset
);
1578 /***********************************************************************
1579 * SetProcessDword (KERNEL.484)
1580 * 'Of course you cannot directly access Windows internal structures'
1582 void WINAPI
SetProcessDword( DWORD dwProcessID
, INT offset
, DWORD value
)
1584 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
1586 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
1588 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
1594 case GPD_APP_COMPAT_FLAGS
:
1595 case GPD_LOAD_DONE_EVENT
:
1596 case GPD_HINSTANCE16
:
1597 case GPD_WINDOWS_VERSION
:
1600 case GPD_STARTF_SHELLDATA
:
1601 case GPD_STARTF_HOTKEY
:
1602 case GPD_STARTF_SHOWWINDOW
:
1603 case GPD_STARTF_SIZE
:
1604 case GPD_STARTF_POSITION
:
1605 case GPD_STARTF_FLAGS
:
1608 ERR_(win32
)("Not allowed to modify offset %d\n", offset
);
1612 current_process
.process_dword
= value
;
1616 ERR_(win32
)("Unknown offset %d\n", offset
);
1622 /*********************************************************************
1623 * OpenProcess (KERNEL32.@)
1625 HANDLE WINAPI
OpenProcess( DWORD access
, BOOL inherit
, DWORD id
)
1628 SERVER_START_REQ( open_process
)
1631 req
->access
= access
;
1632 req
->inherit
= inherit
;
1633 if (!wine_server_call_err( req
)) ret
= reply
->handle
;
1639 /*********************************************************************
1640 * MapProcessHandle (KERNEL.483)
1642 DWORD WINAPI
MapProcessHandle( HANDLE handle
)
1645 SERVER_START_REQ( get_process_info
)
1647 req
->handle
= handle
;
1648 if (!wine_server_call_err( req
)) ret
= (DWORD
)reply
->pid
;
1654 /***********************************************************************
1655 * SetPriorityClass (KERNEL32.@)
1657 BOOL WINAPI
SetPriorityClass( HANDLE hprocess
, DWORD priorityclass
)
1660 SERVER_START_REQ( set_process_info
)
1662 req
->handle
= hprocess
;
1663 req
->priority
= priorityclass
;
1664 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
1665 ret
= !wine_server_call_err( req
);
1672 /***********************************************************************
1673 * GetPriorityClass (KERNEL32.@)
1675 DWORD WINAPI
GetPriorityClass(HANDLE hprocess
)
1678 SERVER_START_REQ( get_process_info
)
1680 req
->handle
= hprocess
;
1681 if (!wine_server_call_err( req
)) ret
= reply
->priority
;
1688 /***********************************************************************
1689 * SetProcessAffinityMask (KERNEL32.@)
1691 BOOL WINAPI
SetProcessAffinityMask( HANDLE hProcess
, DWORD affmask
)
1694 SERVER_START_REQ( set_process_info
)
1696 req
->handle
= hProcess
;
1697 req
->affinity
= affmask
;
1698 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
1699 ret
= !wine_server_call_err( req
);
1705 /**********************************************************************
1706 * GetProcessAffinityMask (KERNEL32.@)
1708 BOOL WINAPI
GetProcessAffinityMask( HANDLE hProcess
,
1709 LPDWORD lpProcessAffinityMask
,
1710 LPDWORD lpSystemAffinityMask
)
1713 SERVER_START_REQ( get_process_info
)
1715 req
->handle
= hProcess
;
1716 if (!wine_server_call_err( req
))
1718 if (lpProcessAffinityMask
) *lpProcessAffinityMask
= reply
->process_affinity
;
1719 if (lpSystemAffinityMask
) *lpSystemAffinityMask
= reply
->system_affinity
;
1728 /***********************************************************************
1729 * GetProcessVersion (KERNEL32.@)
1731 DWORD WINAPI
GetProcessVersion( DWORD processid
)
1733 IMAGE_NT_HEADERS
*nt
;
1735 if (processid
&& processid
!= GetCurrentProcessId())
1737 FIXME("should use ReadProcessMemory\n");
1740 if ((nt
= RtlImageNtHeader( current_process
.module
)))
1741 return ((nt
->OptionalHeader
.MajorSubsystemVersion
<< 16) |
1742 nt
->OptionalHeader
.MinorSubsystemVersion
);
1746 /***********************************************************************
1747 * GetProcessFlags (KERNEL32.@)
1749 DWORD WINAPI
GetProcessFlags( DWORD processid
)
1751 if (processid
&& processid
!= GetCurrentProcessId()) return 0;
1752 return current_process
.flags
;
1756 /***********************************************************************
1757 * SetProcessWorkingSetSize [KERNEL32.@]
1758 * Sets the min/max working set sizes for a specified process.
1761 * hProcess [I] Handle to the process of interest
1762 * minset [I] Specifies minimum working set size
1763 * maxset [I] Specifies maximum working set size
1767 BOOL WINAPI
SetProcessWorkingSetSize(HANDLE hProcess
, SIZE_T minset
,
1770 FIXME("(%p,%ld,%ld): stub - harmless\n",hProcess
,minset
,maxset
);
1771 if(( minset
== (SIZE_T
)-1) && (maxset
== (SIZE_T
)-1)) {
1772 /* Trim the working set to zero */
1773 /* Swap the process out of physical RAM */
1778 /***********************************************************************
1779 * GetProcessWorkingSetSize (KERNEL32.@)
1781 BOOL WINAPI
GetProcessWorkingSetSize(HANDLE hProcess
, PSIZE_T minset
,
1784 FIXME("(%p,%p,%p): stub\n",hProcess
,minset
,maxset
);
1785 /* 32 MB working set size */
1786 if (minset
) *minset
= 32*1024*1024;
1787 if (maxset
) *maxset
= 32*1024*1024;
1791 /***********************************************************************
1792 * SetProcessShutdownParameters (KERNEL32.@)
1794 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1795 * Now tracks changes made (but does not act on these changes)
1797 static DWORD shutdown_flags
= 0;
1798 static DWORD shutdown_priority
= 0x280;
1800 BOOL WINAPI
SetProcessShutdownParameters(DWORD level
, DWORD flags
)
1802 FIXME("(%08lx, %08lx): partial stub.\n", level
, flags
);
1803 shutdown_flags
= flags
;
1804 shutdown_priority
= level
;
1809 /***********************************************************************
1810 * GetProcessShutdownParameters (KERNEL32.@)
1813 BOOL WINAPI
GetProcessShutdownParameters( LPDWORD lpdwLevel
, LPDWORD lpdwFlags
)
1815 *lpdwLevel
= shutdown_priority
;
1816 *lpdwFlags
= shutdown_flags
;
1821 /***********************************************************************
1822 * SetProcessPriorityBoost (KERNEL32.@)
1824 BOOL WINAPI
SetProcessPriorityBoost(HANDLE hprocess
,BOOL disableboost
)
1826 FIXME("(%p,%d): stub\n",hprocess
,disableboost
);
1827 /* Say we can do it. I doubt the program will notice that we don't. */
1832 /***********************************************************************
1833 * ReadProcessMemory (KERNEL32.@)
1835 BOOL WINAPI
ReadProcessMemory( HANDLE process
, LPCVOID addr
, LPVOID buffer
, SIZE_T size
,
1836 SIZE_T
*bytes_read
)
1840 SERVER_START_REQ( read_process_memory
)
1842 req
->handle
= process
;
1843 req
->addr
= (void *)addr
;
1844 wine_server_set_reply( req
, buffer
, size
);
1845 if ((res
= wine_server_call_err( req
))) size
= 0;
1848 if (bytes_read
) *bytes_read
= size
;
1853 /***********************************************************************
1854 * WriteProcessMemory (KERNEL32.@)
1856 BOOL WINAPI
WriteProcessMemory( HANDLE process
, LPVOID addr
, LPCVOID buffer
, SIZE_T size
,
1857 SIZE_T
*bytes_written
)
1859 static const int zero
;
1860 unsigned int first_offset
, last_offset
, first_mask
, last_mask
;
1865 SetLastError( ERROR_INVALID_PARAMETER
);
1869 /* compute the mask for the first int */
1871 first_offset
= (unsigned int)addr
% sizeof(int);
1872 memset( &first_mask
, 0, first_offset
);
1874 /* compute the mask for the last int */
1875 last_offset
= (size
+ first_offset
) % sizeof(int);
1877 memset( &last_mask
, 0xff, last_offset
? last_offset
: sizeof(int) );
1879 SERVER_START_REQ( write_process_memory
)
1881 req
->handle
= process
;
1882 req
->addr
= (char *)addr
- first_offset
;
1883 req
->first_mask
= first_mask
;
1884 req
->last_mask
= last_mask
;
1885 if (first_offset
) wine_server_add_data( req
, &zero
, first_offset
);
1886 wine_server_add_data( req
, buffer
, size
);
1887 if (last_offset
) wine_server_add_data( req
, &zero
, sizeof(int) - last_offset
);
1889 if ((res
= wine_server_call_err( req
))) size
= 0;
1892 if (bytes_written
) *bytes_written
= size
;
1896 ReadProcessMemory( process
, addr
, dummy
, sizeof(dummy
), &read
);
1902 /***********************************************************************
1903 * RegisterServiceProcess (KERNEL.491)
1904 * RegisterServiceProcess (KERNEL32.@)
1906 * A service process calls this function to ensure that it continues to run
1907 * even after a user logged off.
1909 DWORD WINAPI
RegisterServiceProcess(DWORD dwProcessId
, DWORD dwType
)
1911 /* I don't think that Wine needs to do anything in that function */
1912 return 1; /* success */
1915 /***********************************************************************
1916 * GetExitCodeProcess [KERNEL32.@]
1918 * Gets termination status of specified process
1924 BOOL WINAPI
GetExitCodeProcess(
1925 HANDLE hProcess
, /* [in] handle to the process */
1926 LPDWORD lpExitCode
) /* [out] address to receive termination status */
1929 SERVER_START_REQ( get_process_info
)
1931 req
->handle
= hProcess
;
1932 ret
= !wine_server_call_err( req
);
1933 if (ret
&& lpExitCode
) *lpExitCode
= reply
->exit_code
;
1940 /***********************************************************************
1941 * SetErrorMode (KERNEL32.@)
1943 UINT WINAPI
SetErrorMode( UINT mode
)
1945 UINT old
= current_process
.error_mode
;
1946 current_process
.error_mode
= mode
;
1951 /**************************************************************************
1952 * SetFileApisToOEM (KERNEL32.@)
1954 VOID WINAPI
SetFileApisToOEM(void)
1956 current_process
.flags
|= PDB32_FILE_APIS_OEM
;
1960 /**************************************************************************
1961 * SetFileApisToANSI (KERNEL32.@)
1963 VOID WINAPI
SetFileApisToANSI(void)
1965 current_process
.flags
&= ~PDB32_FILE_APIS_OEM
;
1969 /******************************************************************************
1970 * AreFileApisANSI [KERNEL32.@] Determines if file functions are using ANSI
1973 * TRUE: Set of file functions is using ANSI code page
1974 * FALSE: Set of file functions is using OEM code page
1976 BOOL WINAPI
AreFileApisANSI(void)
1978 return !(current_process
.flags
& PDB32_FILE_APIS_OEM
);
1982 /***********************************************************************
1983 * GetTickCount (KERNEL32.@)
1985 * Returns the number of milliseconds, modulo 2^32, since the start
1986 * of the wineserver.
1988 DWORD WINAPI
GetTickCount(void)
1991 gettimeofday( &t
, NULL
);
1992 return ((t
.tv_sec
* 1000) + (t
.tv_usec
/ 1000)) - server_startticks
;
1996 /**********************************************************************
1997 * TlsAlloc [KERNEL32.@] Allocates a TLS index.
1999 * Allocates a thread local storage index
2002 * Success: TLS Index
2003 * Failure: 0xFFFFFFFF
2005 DWORD WINAPI
TlsAlloc( void )
2007 DWORD i
, mask
, ret
= 0;
2008 DWORD
*bits
= current_process
.tls_bits
;
2009 RtlAcquirePebLock();
2010 if (*bits
== 0xffffffff)
2014 if (*bits
== 0xffffffff)
2016 RtlReleasePebLock();
2017 SetLastError( ERROR_NO_MORE_ITEMS
);
2021 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) if (!(*bits
& mask
)) break;
2023 RtlReleasePebLock();
2024 NtCurrentTeb()->tls_array
[ret
+i
] = 0; /* clear the value */
2029 /**********************************************************************
2030 * TlsFree [KERNEL32.@] Releases a TLS index.
2032 * Releases a thread local storage index, making it available for reuse
2038 BOOL WINAPI
TlsFree(
2039 DWORD index
) /* [in] TLS Index to free */
2041 DWORD mask
= (1 << (index
& 31));
2042 DWORD
*bits
= current_process
.tls_bits
;
2045 SetLastError( ERROR_INVALID_PARAMETER
);
2048 if (index
>= 32) bits
++;
2049 RtlAcquirePebLock();
2050 if (!(*bits
& mask
)) /* already free? */
2052 RtlReleasePebLock();
2053 SetLastError( ERROR_INVALID_PARAMETER
);
2057 NtCurrentTeb()->tls_array
[index
] = 0;
2058 /* FIXME: should zero all other thread values */
2059 RtlReleasePebLock();
2064 /**********************************************************************
2065 * TlsGetValue [KERNEL32.@] Gets value in a thread's TLS slot
2068 * Success: Value stored in calling thread's TLS slot for index
2069 * Failure: 0 and GetLastError returns NO_ERROR
2071 LPVOID WINAPI
TlsGetValue(
2072 DWORD index
) /* [in] TLS index to retrieve value for */
2076 SetLastError( ERROR_INVALID_PARAMETER
);
2079 SetLastError( ERROR_SUCCESS
);
2080 return NtCurrentTeb()->tls_array
[index
];
2084 /**********************************************************************
2085 * TlsSetValue [KERNEL32.@] Stores a value in the thread's TLS slot.
2091 BOOL WINAPI
TlsSetValue(
2092 DWORD index
, /* [in] TLS index to set value for */
2093 LPVOID value
) /* [in] Value to be stored */
2097 SetLastError( ERROR_INVALID_PARAMETER
);
2100 NtCurrentTeb()->tls_array
[index
] = value
;
2105 /***********************************************************************
2106 * GetCurrentProcess (KERNEL32.@)
2108 #undef GetCurrentProcess
2109 HANDLE WINAPI
GetCurrentProcess(void)
2111 return (HANDLE
)0xffffffff;