4 * Copyright 1996, 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
31 #include "wine/winbase16.h"
32 #include "wine/winuser16.h"
40 #include "kernel_private.h"
41 #include "wine/server.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(process
);
45 WINE_DECLARE_DEBUG_CHANNEL(server
);
46 WINE_DECLARE_DEBUG_CHANNEL(relay
);
47 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
49 /* Win32 process database */
52 LONG header
[2]; /* 00 Kernel object header */
53 HMODULE module
; /* 08 Main exe module (NT) */
54 PPEB_LDR_DATA LdrData
; /* 0c Pointer to loader information */
55 RTL_USER_PROCESS_PARAMETERS
*ProcessParameters
; /* 10 Process parameters */
56 DWORD unknown2
; /* 14 Unknown */
57 HANDLE heap
; /* 18 Default process heap */
58 HANDLE mem_context
; /* 1c Process memory context */
59 DWORD flags
; /* 20 Flags */
60 void *pdb16
; /* 24 DOS PSP */
61 WORD PSP_sel
; /* 28 Selector to DOS PSP */
62 WORD imte
; /* 2a IMTE for the process module */
63 WORD threads
; /* 2c Number of threads */
64 WORD running_threads
; /* 2e Number of running threads */
65 WORD free_lib_count
; /* 30 Recursion depth of FreeLibrary calls */
66 WORD ring0_threads
; /* 32 Number of ring 0 threads */
67 HANDLE system_heap
; /* 34 System heap to allocate handles */
68 HTASK task
; /* 38 Win16 task */
69 void *mem_map_files
; /* 3c Pointer to mem-mapped files */
70 struct _ENVDB
*env_db
; /* 40 Environment database */
71 void *handle_table
; /* 44 Handle table */
72 struct _PDB
*parent
; /* 48 Parent process */
73 void *modref_list
; /* 4c MODREF list */
74 void *thread_list
; /* 50 List of threads */
75 void *debuggee_CB
; /* 54 Debuggee context block */
76 void *local_heap_free
; /* 58 Head of local heap free list */
77 DWORD unknown4
; /* 5c Unknown */
78 CRITICAL_SECTION crit_section
; /* 60 Critical section */
79 DWORD unknown5
[3]; /* 78 Unknown */
80 void *console
; /* 84 Console */
81 DWORD tls_bits
[2]; /* 88 TLS in-use bits */
82 DWORD process_dword
; /* 90 Unknown */
83 struct _PDB
*group
; /* 94 Process group */
84 void *exe_modref
; /* 98 MODREF for the process EXE */
85 void *top_filter
; /* 9c Top exception filter */
86 DWORD priority
; /* a0 Priority level */
87 HANDLE heap_list
; /* a4 Head of process heap list */
88 void *heap_handles
; /* a8 Head of heap handles list */
89 DWORD unknown6
; /* ac Unknown */
90 void *console_provider
; /* b0 Console provider (??) */
91 WORD env_selector
; /* b4 Selector to process environment */
92 WORD error_mode
; /* b6 Error mode */
93 HANDLE load_done_evt
; /* b8 Event for process loading done */
94 void *UTState
; /* bc Head of Univeral Thunk list */
95 DWORD unknown8
; /* c0 Unknown (NT) */
96 LCID locale
; /* c4 Locale to be queried by GetThreadLocale (NT) */
101 static RTL_USER_PROCESS_PARAMETERS process_pmts
;
102 static PEB_LDR_DATA process_ldr
;
104 static char main_exe_name
[MAX_PATH
];
105 static char *main_exe_name_ptr
= main_exe_name
;
106 static HANDLE main_exe_file
;
107 static DWORD shutdown_flags
= 0;
108 static DWORD shutdown_priority
= 0x280;
109 static DWORD process_dword
;
110 static BOOL oem_file_apis
;
112 extern unsigned int server_startticks
;
113 int main_create_flags
= 0;
116 #define PDB32_DEBUGGED 0x0001 /* Process is being debugged */
117 #define PDB32_WIN16_PROC 0x0008 /* Win16 process */
118 #define PDB32_DOS_PROC 0x0010 /* Dos process */
119 #define PDB32_CONSOLE_PROC 0x0020 /* Console process */
120 #define PDB32_FILE_APIS_OEM 0x0040 /* File APIs are OEM */
121 #define PDB32_WIN32S_PROC 0x8000 /* Win32s process */
123 /* dlls/ntdll/env.c */
124 extern BOOL
init_user_process_pmts( size_t, char*, size_t );
125 extern BOOL
build_command_line( char **argv
);
127 extern void RELAY_InitDebugLists(void);
128 extern void SHELL_LoadRegistry(void);
129 extern void VERSION_Init( const char *appname
);
131 /***********************************************************************
134 inline static const char *get_basename( const char *name
)
138 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
139 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
144 /***********************************************************************
145 * open_builtin_exe_file
147 * Open an exe file for a builtin exe.
149 static void *open_builtin_exe_file( const char *name
, char *error
, int error_size
,
150 int test_only
, int *file_exists
)
152 char exename
[MAX_PATH
], *p
;
153 const char *basename
= get_basename(name
);
155 if (strlen(basename
) >= sizeof(exename
)) return NULL
;
156 strcpy( exename
, basename
);
157 for (p
= exename
; *p
; p
++) *p
= FILE_tolower(*p
);
158 return wine_dll_load_main_exe( exename
, error
, error_size
, test_only
, file_exists
);
162 /***********************************************************************
165 * Open a specific exe file, taking load order into account.
166 * Returns the file handle or 0 for a builtin exe.
168 static HANDLE
open_exe_file( const char *name
)
170 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
171 char buffer
[MAX_PATH
];
175 TRACE("looking for %s\n", debugstr_a(name
) );
177 if ((handle
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
,
178 NULL
, OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
180 /* file doesn't exist, check for builtin */
181 if (!FILE_contains_path( name
)) goto error
;
182 if (!MODULE_GetBuiltinPath( name
, "", buffer
, sizeof(buffer
) )) goto error
;
186 MODULE_GetLoadOrder( loadorder
, name
, TRUE
);
188 for(i
= 0; i
< LOADORDER_NTYPES
; i
++)
190 if (loadorder
[i
] == LOADORDER_INVALID
) break;
194 TRACE( "Trying native exe %s\n", debugstr_a(name
) );
195 if (handle
!= INVALID_HANDLE_VALUE
) return handle
;
198 TRACE( "Trying built-in exe %s\n", debugstr_a(name
) );
199 open_builtin_exe_file( name
, NULL
, 0, 1, &file_exists
);
202 if (handle
!= INVALID_HANDLE_VALUE
) CloseHandle(handle
);
209 if (handle
!= INVALID_HANDLE_VALUE
) CloseHandle(handle
);
212 SetLastError( ERROR_FILE_NOT_FOUND
);
213 return INVALID_HANDLE_VALUE
;
217 /***********************************************************************
220 * Open an exe file, and return the full name and file handle.
221 * Returns FALSE if file could not be found.
222 * If file exists but cannot be opened, returns TRUE and set handle to INVALID_HANDLE_VALUE.
223 * If file is a builtin exe, returns TRUE and sets handle to 0.
225 static BOOL
find_exe_file( const char *name
, char *buffer
, int buflen
, HANDLE
*handle
)
227 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
230 TRACE("looking for %s\n", debugstr_a(name
) );
232 if (!SearchPathA( NULL
, name
, ".exe", buflen
, buffer
, NULL
) &&
233 !MODULE_GetBuiltinPath( name
, ".exe", buffer
, buflen
))
235 /* no builtin found, try native without extension in case it is a Unix app */
237 if (SearchPathA( NULL
, name
, NULL
, buflen
, buffer
, NULL
))
239 TRACE( "Trying native/Unix binary %s\n", debugstr_a(buffer
) );
240 if ((*handle
= CreateFileA( buffer
, GENERIC_READ
, FILE_SHARE_READ
,
241 NULL
, OPEN_EXISTING
, 0, 0 )) != INVALID_HANDLE_VALUE
)
247 MODULE_GetLoadOrder( loadorder
, buffer
, TRUE
);
249 for(i
= 0; i
< LOADORDER_NTYPES
; i
++)
251 if (loadorder
[i
] == LOADORDER_INVALID
) break;
255 TRACE( "Trying native exe %s\n", debugstr_a(buffer
) );
256 if ((*handle
= CreateFileA( buffer
, GENERIC_READ
, FILE_SHARE_READ
,
257 NULL
, OPEN_EXISTING
, 0, 0 )) != INVALID_HANDLE_VALUE
)
259 if (GetLastError() != ERROR_FILE_NOT_FOUND
) return TRUE
;
262 TRACE( "Trying built-in exe %s\n", debugstr_a(buffer
) );
263 open_builtin_exe_file( buffer
, NULL
, 0, 1, &file_exists
);
274 SetLastError( ERROR_FILE_NOT_FOUND
);
279 /***********************************************************************
282 * Main process initialisation code
284 static BOOL
process_init( char *argv
[] )
287 size_t info_size
= 0;
291 setlocale(LC_CTYPE
,"");
293 /* store the program name */
296 /* Fill the initial process structure */
297 current_process
.threads
= 1;
298 current_process
.running_threads
= 1;
299 current_process
.ring0_threads
= 1;
300 current_process
.group
= ¤t_process
;
301 current_process
.priority
= 8; /* Normal */
302 current_process
.ProcessParameters
= &process_pmts
;
303 current_process
.LdrData
= &process_ldr
;
304 InitializeListHead(&process_ldr
.InLoadOrderModuleList
);
305 InitializeListHead(&process_ldr
.InMemoryOrderModuleList
);
306 InitializeListHead(&process_ldr
.InInitializationOrderModuleList
);
308 /* Setup the server connection */
309 wine_server_init_thread();
311 /* Retrieve startup info from the server */
312 SERVER_START_REQ( init_process
)
314 req
->ldt_copy
= &wine_ldt_copy
;
315 if ((ret
= !wine_server_call_err( req
)))
317 main_exe_file
= reply
->exe_file
;
318 main_create_flags
= reply
->create_flags
;
319 info_size
= reply
->info_size
;
320 server_startticks
= reply
->server_start
;
321 process_pmts
.hStdInput
= reply
->hstdin
;
322 process_pmts
.hStdOutput
= reply
->hstdout
;
323 process_pmts
.hStdError
= reply
->hstderr
;
327 if (!ret
) return FALSE
;
329 /* Create the process heap */
330 current_process
.heap
= RtlCreateHeap( HEAP_GROWABLE
, NULL
, 0, 0, NULL
, NULL
);
334 /* This is wine specific: we have no parent (we're started from unix)
335 * so, create a simple console with bare handles to unix stdio
336 * input & output streams (aka simple console)
338 wine_server_fd_to_handle( 0, GENERIC_READ
|SYNCHRONIZE
, TRUE
, &process_pmts
.hStdInput
);
339 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, TRUE
, &process_pmts
.hStdOutput
);
340 wine_server_fd_to_handle( 2, GENERIC_WRITE
|SYNCHRONIZE
, TRUE
, &process_pmts
.hStdError
);
344 /* convert value from server:
345 * + 0 => INVALID_HANDLE_VALUE
346 * + console handle need to be mapped
348 if (!process_pmts
.hStdInput
)
349 process_pmts
.hStdInput
= INVALID_HANDLE_VALUE
;
350 else if (VerifyConsoleIoHandle(console_handle_map(process_pmts
.hStdInput
)))
351 process_pmts
.hStdInput
= console_handle_map(process_pmts
.hStdInput
);
352 if (!process_pmts
.hStdOutput
)
353 process_pmts
.hStdOutput
= INVALID_HANDLE_VALUE
;
354 else if (VerifyConsoleIoHandle(console_handle_map(process_pmts
.hStdOutput
)))
355 process_pmts
.hStdOutput
= console_handle_map(process_pmts
.hStdOutput
);
356 if (!process_pmts
.hStdError
)
357 process_pmts
.hStdError
= INVALID_HANDLE_VALUE
;
358 else if (VerifyConsoleIoHandle(console_handle_map(process_pmts
.hStdError
)))
359 process_pmts
.hStdError
= console_handle_map(process_pmts
.hStdError
);
362 /* Copy the parent environment */
363 if (!init_user_process_pmts( info_size
, main_exe_name
, sizeof(main_exe_name
) ))
366 /* Parse command line arguments */
367 OPTIONS_ParseOptions( !info_size
? argv
: NULL
);
369 /* <hack: to be changed later on> */
370 process_pmts
.CurrentDirectoryName
.Length
= 3 * sizeof(WCHAR
);
371 process_pmts
.CurrentDirectoryName
.MaximumLength
= RtlGetLongestNtPathLength() * sizeof(WCHAR
);
372 process_pmts
.CurrentDirectoryName
.Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, process_pmts
.CurrentDirectoryName
.MaximumLength
);
373 process_pmts
.CurrentDirectoryName
.Buffer
[0] = 'C';
374 process_pmts
.CurrentDirectoryName
.Buffer
[1] = ':';
375 process_pmts
.CurrentDirectoryName
.Buffer
[2] = '\\';
376 process_pmts
.CurrentDirectoryName
.Buffer
[3] = '\0';
377 /* </hack: to be changed later on> */
379 /* initialise DOS drives */
380 if (!DRIVE_Init()) return FALSE
;
382 /* initialise DOS directories */
383 if (!DIR_Init()) return FALSE
;
385 /* registry initialisation */
386 SHELL_LoadRegistry();
388 /* global boot finished, the rest is process-local */
389 SERVER_START_REQ( boot_done
)
391 req
->debug_level
= TRACE_ON(server
);
392 wine_server_call( req
);
396 if (TRACE_ON(relay
) || TRACE_ON(snoop
)) RELAY_InitDebugLists();
402 /***********************************************************************
405 * Startup routine of a new process. Runs on the new process stack.
407 static void start_process( void *arg
)
411 LPTHREAD_START_ROUTINE entry
;
412 HANDLE main_file
= main_exe_file
;
413 IMAGE_NT_HEADERS
*nt
;
414 PEB
*peb
= NtCurrentTeb()->Peb
;
418 UINT drive_type
= GetDriveTypeA( main_exe_name
);
419 /* don't keep the file handle open on removable media */
420 if (drive_type
== DRIVE_REMOVABLE
|| drive_type
== DRIVE_CDROM
) main_file
= 0;
423 /* Retrieve entry point address */
424 nt
= RtlImageNtHeader( peb
->ImageBaseAddress
);
425 entry
= (LPTHREAD_START_ROUTINE
)((char*)peb
->ImageBaseAddress
+
426 nt
->OptionalHeader
.AddressOfEntryPoint
);
428 /* Install signal handlers; this cannot be done before, since we cannot
429 * send exceptions to the debugger before the create process event that
430 * is sent by REQ_INIT_PROCESS_DONE.
431 * We do need the handlers in place by the time the request is over, so
432 * we set them up here. If we segfault between here and the server call
433 * something is very wrong... */
434 if (!SIGNAL_Init()) goto error
;
436 /* Signal the parent process to continue */
437 SERVER_START_REQ( init_process_done
)
439 req
->module
= peb
->ImageBaseAddress
;
440 req
->module_size
= nt
->OptionalHeader
.SizeOfImage
;
442 /* API requires a double indirection */
443 req
->name
= &main_exe_name_ptr
;
444 req
->exe_file
= main_file
;
445 req
->gui
= (nt
->OptionalHeader
.Subsystem
!= IMAGE_SUBSYSTEM_WINDOWS_CUI
);
446 wine_server_add_data( req
, main_exe_name
, strlen(main_exe_name
) );
447 wine_server_call( req
);
448 peb
->BeingDebugged
= reply
->debugged
;
452 /* create the main modref and load dependencies */
453 if (!PE_CreateModule( peb
->ImageBaseAddress
, main_exe_name
, 0, 0, FALSE
)) goto error
;
455 if (main_exe_file
) CloseHandle( main_exe_file
); /* we no longer need it */
457 MODULE_DllProcessAttach( NULL
, (LPVOID
)1 );
460 DPRINTF( "%04lx:Starting process %s (entryproc=%p)\n",
461 GetCurrentThreadId(), main_exe_name
, entry
);
462 if (peb
->BeingDebugged
) DbgBreakPoint();
463 SetLastError(0); /* clear error code */
464 ExitThread( entry( NtCurrentTeb()->Peb
) );
467 ExitProcess( GetLastError() );
469 __EXCEPT(UnhandledExceptionFilter
)
471 TerminateThread( GetCurrentThread(), GetExceptionCode() );
477 /***********************************************************************
478 * __wine_process_init
480 * Wine initialisation: load and start the main exe file.
482 void __wine_process_init( int argc
, char *argv
[] )
484 char error
[1024], *p
;
485 DWORD stack_size
= 0;
488 /* Initialize everything */
489 if (!process_init( argv
)) exit(1);
491 argv
++; /* remove argv[0] (wine itself) */
493 TRACE( "starting process name=%s file=%p argv[0]=%s\n",
494 debugstr_a(main_exe_name
), main_exe_file
, debugstr_a(argv
[0]) );
496 if (!main_exe_name
[0])
498 if (!argv
[0]) OPTIONS_Usage();
500 if (!find_exe_file( argv
[0], main_exe_name
, sizeof(main_exe_name
), &main_exe_file
))
502 MESSAGE( "%s: cannot find '%s'\n", argv0
, argv
[0] );
505 if (main_exe_file
== INVALID_HANDLE_VALUE
)
507 MESSAGE( "%s: cannot open '%s'\n", argv0
, main_exe_name
);
512 if (!main_exe_file
) /* no file handle -> Winelib app */
514 TRACE( "starting Winelib app %s\n", debugstr_a(main_exe_name
) );
515 if (open_builtin_exe_file( main_exe_name
, error
, sizeof(error
), 0, &file_exists
))
517 MESSAGE( "%s: cannot open builtin library for '%s': %s\n", argv0
, main_exe_name
, error
);
520 VERSION_Init( main_exe_name
);
522 switch( MODULE_GetBinaryType( main_exe_file
))
525 TRACE( "starting Win32 binary %s\n", debugstr_a(main_exe_name
) );
526 if ((current_process
.module
= PE_LoadImage( main_exe_file
, main_exe_name
, 0 ))) goto found
;
527 MESSAGE( "%s: could not load '%s' as Win32 binary\n", argv0
, main_exe_name
);
530 MESSAGE( "%s: '%s' is a DLL, not an executable\n", argv0
, main_exe_name
);
533 /* check for .com extension */
534 if (!(p
= strrchr( main_exe_name
, '.' )) || FILE_strcasecmp( p
, ".com" ))
536 MESSAGE( "%s: cannot determine executable type for '%s'\n", argv0
, main_exe_name
);
542 TRACE( "starting Win16/DOS binary %s\n", debugstr_a(main_exe_name
) );
543 CloseHandle( main_exe_file
);
546 argv
[0] = "winevdm.exe";
547 if (open_builtin_exe_file( "winevdm.exe", error
, sizeof(error
), 0, &file_exists
))
549 MESSAGE( "%s: trying to run '%s', cannot open builtin library for 'winevdm.exe': %s\n",
550 argv0
, main_exe_name
, error
);
553 MESSAGE( "%s: '%s' is an OS/2 binary, not supported\n", argv0
, main_exe_name
);
555 case BINARY_UNIX_EXE
:
556 MESSAGE( "%s: '%s' is a Unix binary, not supported\n", argv0
, main_exe_name
);
558 case BINARY_UNIX_LIB
:
560 DOS_FULL_NAME full_name
;
561 const char *name
= main_exe_name
;
562 UNICODE_STRING nameW
;
564 TRACE( "starting Winelib app %s\n", debugstr_a(main_exe_name
) );
565 RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
566 if (DOSFS_GetFullName( nameW
.Buffer
, TRUE
, &full_name
)) name
= full_name
.long_name
;
567 RtlFreeUnicodeString(&nameW
);
568 CloseHandle( main_exe_file
);
570 if (wine_dlopen( name
, RTLD_NOW
, error
, sizeof(error
) ))
572 if ((p
= strrchr( main_exe_name
, '.' )) && !strcmp( p
, ".so" )) *p
= 0;
575 MESSAGE( "%s: could not load '%s': %s\n", argv0
, main_exe_name
, error
);
581 /* build command line */
582 if (!build_command_line( argv
)) goto error
;
584 /* create 32-bit module for main exe */
585 if (!(current_process
.module
= BUILTIN32_LoadExeModule( current_process
.module
))) goto error
;
586 stack_size
= RtlImageNtHeader(current_process
.module
)->OptionalHeader
.SizeOfStackReserve
;
588 /* allocate main thread stack */
589 if (!THREAD_InitStack( NtCurrentTeb(), stack_size
)) goto error
;
591 /* switch to the new stack */
592 wine_switch_to_stack( start_process
, NULL
, NtCurrentTeb()->Tib
.StackBase
);
595 ExitProcess( GetLastError() );
599 /***********************************************************************
602 * Build an argv array from a command-line.
603 * The command-line is modified to insert nulls.
604 * 'reserved' is the number of args to reserve before the first one.
606 static char **build_argv( char *cmdline
, int reserved
)
611 int in_quotes
,bcount
;
618 if (*s
=='\0' || ((*s
==' ' || *s
=='\t') && !in_quotes
)) {
621 /* skip the remaining spaces */
622 while (*s
==' ' || *s
=='\t') {
629 } else if (*s
=='\\') {
630 /* '\', count them */
632 } else if ((*s
=='"') && ((bcount
& 1)==0)) {
634 in_quotes
=!in_quotes
;
637 /* a regular character */
642 argv
=malloc(argc
*sizeof(*argv
));
651 if ((*s
==' ' || *s
=='\t') && !in_quotes
) {
652 /* Close the argument and copy it */
656 /* skip the remaining spaces */
659 } while (*s
==' ' || *s
=='\t');
661 /* Start with a new argument */
664 } else if (*s
=='\\') {
668 } else if (*s
=='"') {
670 if ((bcount
& 1)==0) {
671 /* Preceeded by an even number of '\', this is half that
672 * number of '\', plus a '"' which we discard.
676 in_quotes
=!in_quotes
;
678 /* Preceeded by an odd number of '\', this is half that
679 * number of '\' followed by a '"'
687 /* a regular character */
702 /***********************************************************************
705 * Build the environment of a new child process.
707 static char **build_envp( const char *env
, const char *extra_env
)
713 if (extra_env
) for (p
= extra_env
; *p
; count
++) p
+= strlen(p
) + 1;
714 for (p
= env
; *p
; count
++) p
+= strlen(p
) + 1;
717 if ((envp
= malloc( count
* sizeof(*envp
) )))
719 extern char **environ
;
720 char **envptr
= envp
;
721 char **unixptr
= environ
;
722 /* first the extra strings */
723 if (extra_env
) for (p
= extra_env
; *p
; p
+= strlen(p
) + 1) *envptr
++ = (char *)p
;
724 /* then put PATH, HOME and WINEPREFIX from the unix env */
725 for (unixptr
= environ
; unixptr
&& *unixptr
; unixptr
++)
726 if (!memcmp( *unixptr
, "PATH=", 5 ) ||
727 !memcmp( *unixptr
, "HOME=", 5 ) ||
728 !memcmp( *unixptr
, "WINEPREFIX=", 11 )) *envptr
++ = *unixptr
;
729 /* now put the Windows environment strings */
730 for (p
= env
; *p
; p
+= strlen(p
) + 1)
732 if (!memcmp( p
, "PATH=", 5 )) /* store PATH as WINEPATH */
734 char *winepath
= malloc( strlen(p
) + 5 );
735 strcpy( winepath
, "WINE" );
736 strcpy( winepath
+ 4, p
);
737 *envptr
++ = winepath
;
739 else if (memcmp( p
, "HOME=", 5 ) &&
740 memcmp( p
, "WINEPATH=", 9 ) &&
741 memcmp( p
, "WINEPREFIX=", 11 )) *envptr
++ = (char *)p
;
749 /***********************************************************************
752 * Locate the Wine binary to exec for a new Win32 process.
754 static void exec_wine_binary( char **argv
, char **envp
)
756 const char *path
, *pos
, *ptr
;
758 /* first, try for a WINELOADER environment variable */
759 argv
[0] = getenv("WINELOADER");
761 execve( argv
[0], argv
, envp
);
763 /* next, try bin directory */
764 argv
[0] = BINDIR
"/wine";
765 execve( argv
[0], argv
, envp
);
767 /* now try the path of argv0 of the current binary */
768 if (!(argv
[0] = malloc( strlen(full_argv0
) + 6 ))) return;
769 if ((ptr
= strrchr( full_argv0
, '/' )))
771 memcpy( argv
[0], full_argv0
, ptr
- full_argv0
);
772 strcpy( argv
[0] + (ptr
- full_argv0
), "/wine" );
773 execve( argv
[0], argv
, envp
);
777 /* now search in the Unix path */
778 if ((path
= getenv( "PATH" )))
780 if (!(argv
[0] = malloc( strlen(path
) + 6 ))) return;
784 while (*pos
== ':') pos
++;
786 if (!(ptr
= strchr( pos
, ':' ))) ptr
= pos
+ strlen(pos
);
787 memcpy( argv
[0], pos
, ptr
- pos
);
788 strcpy( argv
[0] + (ptr
- pos
), "/wine" );
789 execve( argv
[0], argv
, envp
);
797 /***********************************************************************
800 * Fork and exec a new Unix binary, checking for errors.
802 static int fork_and_exec( const char *filename
, char *cmdline
,
803 const char *env
, const char *newdir
)
808 if (!env
) env
= GetEnvironmentStringsA();
815 fcntl( fd
[1], F_SETFD
, 1 ); /* set close on exec */
816 if (!(pid
= fork())) /* child */
818 char **argv
= build_argv( cmdline
, 0 );
819 char **envp
= build_envp( env
, NULL
);
822 /* Reset signals that we previously set to SIG_IGN */
823 signal( SIGPIPE
, SIG_DFL
);
824 signal( SIGCHLD
, SIG_DFL
);
826 if (newdir
) chdir(newdir
);
828 if (argv
&& envp
) execve( filename
, argv
, envp
);
830 write( fd
[1], &err
, sizeof(err
) );
834 if ((pid
!= -1) && (read( fd
[0], &err
, sizeof(err
) ) > 0)) /* exec failed */
839 if (pid
== -1) FILE_SetDosError();
845 /***********************************************************************
848 * Create a new process. If hFile is a valid handle we have an exe
849 * file, otherwise it is a Winelib app.
851 static BOOL
create_process( HANDLE hFile
, LPCSTR filename
, LPSTR cmd_line
, LPCSTR env
,
852 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
853 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
854 LPPROCESS_INFORMATION info
, LPCSTR unixdir
)
856 BOOL ret
, success
= FALSE
;
858 startup_info_t startup_info
;
859 char *extra_env
= NULL
;
868 env
= GetEnvironmentStringsA();
869 extra_env
= DRIVE_BuildEnv();
872 /* create the synchronization pipes */
874 if (pipe( startfd
) == -1)
879 if (pipe( execfd
) == -1)
886 fcntl( execfd
[1], F_SETFD
, 1 ); /* set close on exec */
888 /* create the child process */
890 if (!(pid
= fork())) /* child */
892 char **argv
= build_argv( cmd_line
, 1 );
893 char **envp
= build_envp( env
, extra_env
);
898 /* wait for parent to tell us to start */
899 if (read( startfd
[0], &dummy
, 1 ) != 1) _exit(1);
902 /* Reset signals that we previously set to SIG_IGN */
903 signal( SIGPIPE
, SIG_DFL
);
904 signal( SIGCHLD
, SIG_DFL
);
906 if (unixdir
) chdir(unixdir
);
908 if (argv
&& envp
) exec_wine_binary( argv
, envp
);
911 write( execfd
[1], &err
, sizeof(err
) );
915 /* this is the parent */
919 if (extra_env
) HeapFree( GetProcessHeap(), 0, extra_env
);
928 /* fill the startup info structure */
930 startup_info
.size
= sizeof(startup_info
);
931 /* startup_info.filename_len is set below */
932 startup_info
.cmdline_len
= cmd_line
? strlen(cmd_line
) : 0;
933 startup_info
.desktop_len
= startup
->lpDesktop
? strlen(startup
->lpDesktop
) : 0;
934 startup_info
.title_len
= startup
->lpTitle
? strlen(startup
->lpTitle
) : 0;
935 startup_info
.x
= startup
->dwX
;
936 startup_info
.y
= startup
->dwY
;
937 startup_info
.cx
= startup
->dwXSize
;
938 startup_info
.cy
= startup
->dwYSize
;
939 startup_info
.x_chars
= startup
->dwXCountChars
;
940 startup_info
.y_chars
= startup
->dwYCountChars
;
941 startup_info
.attribute
= startup
->dwFillAttribute
;
942 startup_info
.cmd_show
= startup
->wShowWindow
;
943 startup_info
.flags
= startup
->dwFlags
;
945 /* create the process on the server side */
947 SERVER_START_REQ( new_process
)
952 req
->inherit_all
= inherit
;
953 req
->create_flags
= flags
;
955 req
->exe_file
= hFile
;
956 if (startup
->dwFlags
& STARTF_USESTDHANDLES
)
958 req
->hstdin
= startup
->hStdInput
;
959 req
->hstdout
= startup
->hStdOutput
;
960 req
->hstderr
= startup
->hStdError
;
964 req
->hstdin
= GetStdHandle( STD_INPUT_HANDLE
);
965 req
->hstdout
= GetStdHandle( STD_OUTPUT_HANDLE
);
966 req
->hstderr
= GetStdHandle( STD_ERROR_HANDLE
);
969 if ((flags
& (CREATE_NEW_CONSOLE
| DETACHED_PROCESS
)) != 0)
971 /* this is temporary (for console handles). We have no way to control that the handle is invalid in child process otherwise */
972 if (is_console_handle(req
->hstdin
)) req
->hstdin
= INVALID_HANDLE_VALUE
;
973 if (is_console_handle(req
->hstdout
)) req
->hstdout
= INVALID_HANDLE_VALUE
;
974 if (is_console_handle(req
->hstderr
)) req
->hstderr
= INVALID_HANDLE_VALUE
;
978 if (is_console_handle(req
->hstdin
)) req
->hstdin
= console_handle_unmap(req
->hstdin
);
979 if (is_console_handle(req
->hstdout
)) req
->hstdout
= console_handle_unmap(req
->hstdout
);
980 if (is_console_handle(req
->hstderr
)) req
->hstderr
= console_handle_unmap(req
->hstderr
);
983 if (GetLongPathNameA( filename
, buf
, MAX_PATH
))
988 startup_info
.filename_len
= strlen(nameptr
);
989 wine_server_add_data( req
, &startup_info
, sizeof(startup_info
) );
990 wine_server_add_data( req
, nameptr
, startup_info
.filename_len
);
991 wine_server_add_data( req
, cmd_line
, startup_info
.cmdline_len
);
992 wine_server_add_data( req
, startup
->lpDesktop
, startup_info
.desktop_len
);
993 wine_server_add_data( req
, startup
->lpTitle
, startup_info
.title_len
);
995 ret
= !wine_server_call_err( req
);
996 process_info
= reply
->info
;
1002 close( startfd
[1] );
1007 /* tell child to start and wait for it to exec */
1009 write( startfd
[1], &dummy
, 1 );
1010 close( startfd
[1] );
1012 if (read( execfd
[0], &err
, sizeof(err
) ) > 0) /* exec failed */
1017 CloseHandle( process_info
);
1021 /* wait for the new process info to be ready */
1023 WaitForSingleObject( process_info
, INFINITE
);
1024 SERVER_START_REQ( get_new_process_info
)
1026 req
->info
= process_info
;
1027 req
->pinherit
= (psa
&& (psa
->nLength
>= sizeof(*psa
)) && psa
->bInheritHandle
);
1028 req
->tinherit
= (tsa
&& (tsa
->nLength
>= sizeof(*tsa
)) && tsa
->bInheritHandle
);
1029 if ((ret
= !wine_server_call_err( req
)))
1031 info
->dwProcessId
= (DWORD
)reply
->pid
;
1032 info
->dwThreadId
= (DWORD
)reply
->tid
;
1033 info
->hProcess
= reply
->phandle
;
1034 info
->hThread
= reply
->thandle
;
1035 success
= reply
->success
;
1040 if (ret
&& !success
) /* new process failed to start */
1043 if (GetExitCodeProcess( info
->hProcess
, &exitcode
)) SetLastError( exitcode
);
1044 CloseHandle( info
->hThread
);
1045 CloseHandle( info
->hProcess
);
1048 CloseHandle( process_info
);
1053 /***********************************************************************
1054 * create_vdm_process
1056 * Create a new VDM process for a 16-bit or DOS application.
1058 static BOOL
create_vdm_process( LPCSTR filename
, LPSTR cmd_line
, LPCSTR env
,
1059 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
1060 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
1061 LPPROCESS_INFORMATION info
, LPCSTR unixdir
)
1064 LPSTR new_cmd_line
= HeapAlloc( GetProcessHeap(), 0, strlen(filename
) + strlen(cmd_line
) + 30 );
1068 SetLastError( ERROR_OUTOFMEMORY
);
1071 sprintf( new_cmd_line
, "winevdm.exe --app-name \"%s\" %s", filename
, cmd_line
);
1072 ret
= create_process( 0, "winevdm.exe", new_cmd_line
, env
, psa
, tsa
, inherit
,
1073 flags
, startup
, info
, unixdir
);
1074 HeapFree( GetProcessHeap(), 0, new_cmd_line
);
1079 /*************************************************************************
1082 * Helper for CreateProcess: retrieve the file name to load from the
1083 * app name and command line. Store the file name in buffer, and
1084 * return a possibly modified command line.
1085 * Also returns a handle to the opened file if it's a Windows binary.
1087 static LPSTR
get_file_name( LPCSTR appname
, LPSTR cmdline
, LPSTR buffer
,
1088 int buflen
, HANDLE
*handle
)
1090 char *name
, *pos
, *ret
= NULL
;
1093 /* if we have an app name, everything is easy */
1097 /* use the unmodified app name as file name */
1098 lstrcpynA( buffer
, appname
, buflen
);
1099 *handle
= open_exe_file( buffer
);
1100 if (!(ret
= cmdline
) || !cmdline
[0])
1102 /* no command-line, create one */
1103 if ((ret
= HeapAlloc( GetProcessHeap(), 0, strlen(appname
) + 3 )))
1104 sprintf( ret
, "\"%s\"", appname
);
1111 SetLastError( ERROR_INVALID_PARAMETER
);
1115 /* first check for a quoted file name */
1117 if ((cmdline
[0] == '"') && ((p
= strchr( cmdline
+ 1, '"' ))))
1119 int len
= p
- cmdline
- 1;
1120 /* extract the quoted portion as file name */
1121 if (!(name
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 ))) return NULL
;
1122 memcpy( name
, cmdline
+ 1, len
);
1125 if (find_exe_file( name
, buffer
, buflen
, handle
))
1126 ret
= cmdline
; /* no change necessary */
1130 /* now try the command-line word by word */
1132 if (!(name
= HeapAlloc( GetProcessHeap(), 0, strlen(cmdline
) + 1 ))) return NULL
;
1138 do *pos
++ = *p
++; while (*p
&& *p
!= ' ');
1140 if (find_exe_file( name
, buffer
, buflen
, handle
))
1147 if (!ret
|| !strchr( name
, ' ' )) goto done
; /* no change necessary */
1149 /* now build a new command-line with quotes */
1151 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, strlen(cmdline
) + 3 ))) goto done
;
1152 sprintf( ret
, "\"%s\"%s", name
, p
);
1155 HeapFree( GetProcessHeap(), 0, name
);
1160 /**********************************************************************
1161 * CreateProcessA (KERNEL32.@)
1163 BOOL WINAPI
CreateProcessA( LPCSTR app_name
, LPSTR cmd_line
, LPSECURITY_ATTRIBUTES process_attr
,
1164 LPSECURITY_ATTRIBUTES thread_attr
, BOOL inherit
,
1165 DWORD flags
, LPVOID env
, LPCSTR cur_dir
,
1166 LPSTARTUPINFOA startup_info
, LPPROCESS_INFORMATION info
)
1170 const char *unixdir
= NULL
;
1171 DOS_FULL_NAME full_dir
;
1172 char name
[MAX_PATH
];
1176 /* Process the AppName and/or CmdLine to get module name and path */
1178 TRACE("app %s cmdline %s\n", debugstr_a(app_name
), debugstr_a(cmd_line
) );
1180 if (!(tidy_cmdline
= get_file_name( app_name
, cmd_line
, name
, sizeof(name
), &hFile
)))
1182 if (hFile
== INVALID_HANDLE_VALUE
) goto done
;
1184 /* Warn if unsupported features are used */
1186 if (flags
& NORMAL_PRIORITY_CLASS
)
1187 FIXME("(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name
);
1188 if (flags
& IDLE_PRIORITY_CLASS
)
1189 FIXME("(%s,...): IDLE_PRIORITY_CLASS ignored\n", name
);
1190 if (flags
& HIGH_PRIORITY_CLASS
)
1191 FIXME("(%s,...): HIGH_PRIORITY_CLASS ignored\n", name
);
1192 if (flags
& REALTIME_PRIORITY_CLASS
)
1193 FIXME("(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name
);
1194 if (flags
& CREATE_NEW_PROCESS_GROUP
)
1195 FIXME("(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name
);
1196 if (flags
& CREATE_UNICODE_ENVIRONMENT
)
1197 FIXME("(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name
);
1198 if (flags
& CREATE_SEPARATE_WOW_VDM
)
1199 FIXME("(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name
);
1200 if (flags
& CREATE_SHARED_WOW_VDM
)
1201 FIXME("(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name
);
1202 if (flags
& CREATE_DEFAULT_ERROR_MODE
)
1203 FIXME("(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name
);
1204 if (flags
& CREATE_NO_WINDOW
)
1205 FIXME("(%s,...): CREATE_NO_WINDOW ignored\n", name
);
1206 if (flags
& PROFILE_USER
)
1207 FIXME("(%s,...): PROFILE_USER ignored\n", name
);
1208 if (flags
& PROFILE_KERNEL
)
1209 FIXME("(%s,...): PROFILE_KERNEL ignored\n", name
);
1210 if (flags
& PROFILE_SERVER
)
1211 FIXME("(%s,...): PROFILE_SERVER ignored\n", name
);
1212 if (startup_info
->lpDesktop
)
1213 FIXME("(%s,...): startup_info->lpDesktop %s ignored\n",
1214 name
, debugstr_a(startup_info
->lpDesktop
));
1215 if (startup_info
->dwFlags
& STARTF_RUNFULLSCREEN
)
1216 FIXME("(%s,...): STARTF_RUNFULLSCREEN ignored\n", name
);
1217 if (startup_info
->dwFlags
& STARTF_FORCEONFEEDBACK
)
1218 FIXME("(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name
);
1219 if (startup_info
->dwFlags
& STARTF_FORCEOFFFEEDBACK
)
1220 FIXME("(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name
);
1221 if (startup_info
->dwFlags
& STARTF_USEHOTKEY
)
1222 FIXME("(%s,...): STARTF_USEHOTKEY ignored\n", name
);
1226 UNICODE_STRING cur_dirW
;
1227 RtlCreateUnicodeStringFromAsciiz(&cur_dirW
, cur_dir
);
1228 if (DOSFS_GetFullName( cur_dirW
.Buffer
, TRUE
, &full_dir
))
1229 unixdir
= full_dir
.long_name
;
1230 RtlFreeUnicodeString(&cur_dirW
);
1234 WCHAR buf
[MAX_PATH
];
1235 if (GetCurrentDirectoryW(MAX_PATH
, buf
))
1237 if (DOSFS_GetFullName( buf
, TRUE
, &full_dir
)) unixdir
= full_dir
.long_name
;
1241 info
->hThread
= info
->hProcess
= 0;
1242 info
->dwProcessId
= info
->dwThreadId
= 0;
1244 /* Determine executable type */
1246 if (!hFile
) /* builtin exe */
1248 TRACE( "starting %s as Winelib app\n", debugstr_a(name
) );
1249 retv
= create_process( 0, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1250 inherit
, flags
, startup_info
, info
, unixdir
);
1254 switch( MODULE_GetBinaryType( hFile
))
1257 TRACE( "starting %s as Win32 binary\n", debugstr_a(name
) );
1258 retv
= create_process( hFile
, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1259 inherit
, flags
, startup_info
, info
, unixdir
);
1263 TRACE( "starting %s as Win16/DOS binary\n", debugstr_a(name
) );
1264 retv
= create_vdm_process( name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1265 inherit
, flags
, startup_info
, info
, unixdir
);
1268 FIXME( "%s is OS/2 binary, not supported\n", debugstr_a(name
) );
1269 SetLastError( ERROR_BAD_EXE_FORMAT
);
1272 TRACE( "not starting %s since it is a dll\n", debugstr_a(name
) );
1273 SetLastError( ERROR_BAD_EXE_FORMAT
);
1275 case BINARY_UNIX_LIB
:
1276 TRACE( "%s is a Unix library, starting as Winelib app\n", debugstr_a(name
) );
1277 retv
= create_process( hFile
, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1278 inherit
, flags
, startup_info
, info
, unixdir
);
1280 case BINARY_UNKNOWN
:
1281 /* check for .com or .bat extension */
1282 if ((p
= strrchr( name
, '.' )))
1284 if (!FILE_strcasecmp( p
, ".com" ))
1286 TRACE( "starting %s as DOS binary\n", debugstr_a(name
) );
1287 retv
= create_vdm_process( name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1288 inherit
, flags
, startup_info
, info
, unixdir
);
1291 if (!FILE_strcasecmp( p
, ".bat" ))
1293 char comspec
[MAX_PATH
];
1294 if (GetEnvironmentVariableA("COMSPEC", comspec
, sizeof(comspec
)))
1297 if ((newcmdline
= HeapAlloc( GetProcessHeap(), 0,
1298 strlen(comspec
) + 4 + strlen(tidy_cmdline
) + 1)))
1300 sprintf( newcmdline
, "%s /c %s", comspec
, tidy_cmdline
);
1301 TRACE( "starting %s as batch binary: %s\n",
1302 debugstr_a(name
), debugstr_a(newcmdline
) );
1303 retv
= CreateProcessA( comspec
, newcmdline
, process_attr
, thread_attr
,
1304 inherit
, flags
, env
, cur_dir
, startup_info
, info
);
1305 HeapFree( GetProcessHeap(), 0, newcmdline
);
1312 case BINARY_UNIX_EXE
:
1314 /* unknown file, try as unix executable */
1315 UNICODE_STRING nameW
;
1316 DOS_FULL_NAME full_name
;
1317 const char *unixfilename
= name
;
1319 TRACE( "starting %s as Unix binary\n", debugstr_a(name
) );
1321 RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
1322 if (DOSFS_GetFullName( nameW
.Buffer
, TRUE
, &full_name
)) unixfilename
= full_name
.long_name
;
1323 RtlFreeUnicodeString(&nameW
);
1324 retv
= (fork_and_exec( unixfilename
, tidy_cmdline
, env
, unixdir
) != -1);
1328 CloseHandle( hFile
);
1331 if (tidy_cmdline
!= cmd_line
) HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1336 /**********************************************************************
1337 * CreateProcessW (KERNEL32.@)
1339 * lpReserved is not converted
1341 BOOL WINAPI
CreateProcessW( LPCWSTR app_name
, LPWSTR cmd_line
, LPSECURITY_ATTRIBUTES process_attr
,
1342 LPSECURITY_ATTRIBUTES thread_attr
, BOOL inherit
, DWORD flags
,
1343 LPVOID env
, LPCWSTR cur_dir
, LPSTARTUPINFOW startup_info
,
1344 LPPROCESS_INFORMATION info
)
1347 STARTUPINFOA StartupInfoA
;
1349 LPSTR app_nameA
= HEAP_strdupWtoA (GetProcessHeap(),0,app_name
);
1350 LPSTR cmd_lineA
= HEAP_strdupWtoA (GetProcessHeap(),0,cmd_line
);
1351 LPSTR cur_dirA
= HEAP_strdupWtoA (GetProcessHeap(),0,cur_dir
);
1353 memcpy (&StartupInfoA
, startup_info
, sizeof(STARTUPINFOA
));
1354 StartupInfoA
.lpDesktop
= HEAP_strdupWtoA (GetProcessHeap(),0,startup_info
->lpDesktop
);
1355 StartupInfoA
.lpTitle
= HEAP_strdupWtoA (GetProcessHeap(),0,startup_info
->lpTitle
);
1357 TRACE("(%s,%s,...)\n", debugstr_w(app_name
), debugstr_w(cmd_line
));
1359 if (startup_info
->lpReserved
)
1360 FIXME("StartupInfo.lpReserved is used, please report (%s)\n",
1361 debugstr_w(startup_info
->lpReserved
));
1363 ret
= CreateProcessA( app_nameA
, cmd_lineA
, process_attr
, thread_attr
,
1364 inherit
, flags
, env
, cur_dirA
, &StartupInfoA
, info
);
1366 HeapFree( GetProcessHeap(), 0, cur_dirA
);
1367 HeapFree( GetProcessHeap(), 0, cmd_lineA
);
1368 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpDesktop
);
1369 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpTitle
);
1375 /***********************************************************************
1378 * Wrapper to call WaitForInputIdle USER function
1380 typedef DWORD (WINAPI
*WaitForInputIdle_ptr
)( HANDLE hProcess
, DWORD dwTimeOut
);
1382 static DWORD
wait_input_idle( HANDLE process
, DWORD timeout
)
1384 HMODULE mod
= GetModuleHandleA( "user32.dll" );
1387 WaitForInputIdle_ptr ptr
= (WaitForInputIdle_ptr
)GetProcAddress( mod
, "WaitForInputIdle" );
1388 if (ptr
) return ptr( process
, timeout
);
1394 /***********************************************************************
1395 * WinExec (KERNEL32.@)
1397 UINT WINAPI
WinExec( LPCSTR lpCmdLine
, UINT nCmdShow
)
1399 PROCESS_INFORMATION info
;
1400 STARTUPINFOA startup
;
1404 memset( &startup
, 0, sizeof(startup
) );
1405 startup
.cb
= sizeof(startup
);
1406 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
1407 startup
.wShowWindow
= nCmdShow
;
1409 /* cmdline needs to be writeable for CreateProcess */
1410 if (!(cmdline
= HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine
)+1 ))) return 0;
1411 strcpy( cmdline
, lpCmdLine
);
1413 if (CreateProcessA( NULL
, cmdline
, NULL
, NULL
, FALSE
,
1414 0, NULL
, NULL
, &startup
, &info
))
1416 /* Give 30 seconds to the app to come up */
1417 if (wait_input_idle( info
.hProcess
, 30000 ) == 0xFFFFFFFF)
1418 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1420 /* Close off the handles */
1421 CloseHandle( info
.hThread
);
1422 CloseHandle( info
.hProcess
);
1424 else if ((ret
= GetLastError()) >= 32)
1426 FIXME("Strange error set by CreateProcess: %d\n", ret
);
1429 HeapFree( GetProcessHeap(), 0, cmdline
);
1434 /**********************************************************************
1435 * LoadModule (KERNEL32.@)
1437 HINSTANCE WINAPI
LoadModule( LPCSTR name
, LPVOID paramBlock
)
1439 LOADPARAMS
*params
= (LOADPARAMS
*)paramBlock
;
1440 PROCESS_INFORMATION info
;
1441 STARTUPINFOA startup
;
1442 HINSTANCE hInstance
;
1444 char filename
[MAX_PATH
];
1447 if (!name
) return (HINSTANCE
)ERROR_FILE_NOT_FOUND
;
1449 if (!SearchPathA( NULL
, name
, ".exe", sizeof(filename
), filename
, NULL
) &&
1450 !SearchPathA( NULL
, name
, NULL
, sizeof(filename
), filename
, NULL
))
1451 return (HINSTANCE
)GetLastError();
1453 len
= (BYTE
)params
->lpCmdLine
[0];
1454 if (!(cmdline
= HeapAlloc( GetProcessHeap(), 0, strlen(filename
) + len
+ 2 )))
1455 return (HINSTANCE
)ERROR_NOT_ENOUGH_MEMORY
;
1457 strcpy( cmdline
, filename
);
1458 p
= cmdline
+ strlen(cmdline
);
1460 memcpy( p
, params
->lpCmdLine
+ 1, len
);
1463 memset( &startup
, 0, sizeof(startup
) );
1464 startup
.cb
= sizeof(startup
);
1465 if (params
->lpCmdShow
)
1467 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
1468 startup
.wShowWindow
= params
->lpCmdShow
[1];
1471 if (CreateProcessA( filename
, cmdline
, NULL
, NULL
, FALSE
, 0,
1472 params
->lpEnvAddress
, NULL
, &startup
, &info
))
1474 /* Give 30 seconds to the app to come up */
1475 if (wait_input_idle( info
.hProcess
, 30000 ) == 0xFFFFFFFF )
1476 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1477 hInstance
= (HINSTANCE
)33;
1478 /* Close off the handles */
1479 CloseHandle( info
.hThread
);
1480 CloseHandle( info
.hProcess
);
1482 else if ((hInstance
= (HINSTANCE
)GetLastError()) >= (HINSTANCE
)32)
1484 FIXME("Strange error set by CreateProcess: %p\n", hInstance
);
1485 hInstance
= (HINSTANCE
)11;
1488 HeapFree( GetProcessHeap(), 0, cmdline
);
1493 /******************************************************************************
1494 * TerminateProcess (KERNEL32.@)
1496 BOOL WINAPI
TerminateProcess( HANDLE handle
, DWORD exit_code
)
1498 NTSTATUS status
= NtTerminateProcess( handle
, exit_code
);
1499 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1504 /***********************************************************************
1505 * GetExitCodeProcess [KERNEL32.@]
1507 * Gets termination status of specified process
1513 BOOL WINAPI
GetExitCodeProcess(
1514 HANDLE hProcess
, /* [in] handle to the process */
1515 LPDWORD lpExitCode
) /* [out] address to receive termination status */
1518 SERVER_START_REQ( get_process_info
)
1520 req
->handle
= hProcess
;
1521 ret
= !wine_server_call_err( req
);
1522 if (ret
&& lpExitCode
) *lpExitCode
= reply
->exit_code
;
1529 /***********************************************************************
1530 * SetErrorMode (KERNEL32.@)
1532 UINT WINAPI
SetErrorMode( UINT mode
)
1534 UINT old
= current_process
.error_mode
;
1535 current_process
.error_mode
= mode
;
1540 /**********************************************************************
1541 * TlsAlloc [KERNEL32.@] Allocates a TLS index.
1543 * Allocates a thread local storage index
1546 * Success: TLS Index
1547 * Failure: 0xFFFFFFFF
1549 DWORD WINAPI
TlsAlloc( void )
1551 DWORD i
, mask
, ret
= 0;
1552 DWORD
*bits
= current_process
.tls_bits
;
1553 RtlAcquirePebLock();
1554 if (*bits
== 0xffffffff)
1558 if (*bits
== 0xffffffff)
1560 RtlReleasePebLock();
1561 SetLastError( ERROR_NO_MORE_ITEMS
);
1565 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) if (!(*bits
& mask
)) break;
1567 RtlReleasePebLock();
1568 NtCurrentTeb()->TlsSlots
[ret
+i
] = 0; /* clear the value */
1573 /**********************************************************************
1574 * TlsFree [KERNEL32.@] Releases a TLS index.
1576 * Releases a thread local storage index, making it available for reuse
1582 BOOL WINAPI
TlsFree(
1583 DWORD index
) /* [in] TLS Index to free */
1585 DWORD mask
= (1 << (index
& 31));
1586 DWORD
*bits
= current_process
.tls_bits
;
1589 SetLastError( ERROR_INVALID_PARAMETER
);
1592 if (index
>= 32) bits
++;
1593 RtlAcquirePebLock();
1594 if (!(*bits
& mask
)) /* already free? */
1596 RtlReleasePebLock();
1597 SetLastError( ERROR_INVALID_PARAMETER
);
1601 NtSetInformationThread( GetCurrentThread(), ThreadZeroTlsCell
, &index
, sizeof(index
) );
1602 RtlReleasePebLock();
1607 /**********************************************************************
1608 * TlsGetValue [KERNEL32.@] Gets value in a thread's TLS slot
1611 * Success: Value stored in calling thread's TLS slot for index
1612 * Failure: 0 and GetLastError returns NO_ERROR
1614 LPVOID WINAPI
TlsGetValue(
1615 DWORD index
) /* [in] TLS index to retrieve value for */
1619 SetLastError( ERROR_INVALID_PARAMETER
);
1622 SetLastError( ERROR_SUCCESS
);
1623 return NtCurrentTeb()->TlsSlots
[index
];
1627 /**********************************************************************
1628 * TlsSetValue [KERNEL32.@] Stores a value in the thread's TLS slot.
1634 BOOL WINAPI
TlsSetValue(
1635 DWORD index
, /* [in] TLS index to set value for */
1636 LPVOID value
) /* [in] Value to be stored */
1640 SetLastError( ERROR_INVALID_PARAMETER
);
1643 NtCurrentTeb()->TlsSlots
[index
] = value
;
1648 /***********************************************************************
1649 * GetProcessFlags (KERNEL32.@)
1651 DWORD WINAPI
GetProcessFlags( DWORD processid
)
1653 IMAGE_NT_HEADERS
*nt
;
1656 if (processid
&& processid
!= GetCurrentProcessId()) return 0;
1658 if ((nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
)))
1660 if (nt
->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
)
1661 flags
|= PDB32_CONSOLE_PROC
;
1663 if (!AreFileApisANSI()) flags
|= PDB32_FILE_APIS_OEM
;
1664 if (IsDebuggerPresent()) flags
|= PDB32_DEBUGGED
;
1669 /***********************************************************************
1670 * GetProcessDword (KERNEL.485)
1671 * GetProcessDword (KERNEL32.18)
1672 * 'Of course you cannot directly access Windows internal structures'
1674 DWORD WINAPI
GetProcessDword( DWORD dwProcessID
, INT offset
)
1679 TRACE("(%ld, %d)\n", dwProcessID
, offset
);
1681 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
1683 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
1689 case GPD_APP_COMPAT_FLAGS
:
1690 return GetAppCompatFlags16(0);
1691 case GPD_LOAD_DONE_EVENT
:
1693 case GPD_HINSTANCE16
:
1694 return GetTaskDS16();
1695 case GPD_WINDOWS_VERSION
:
1696 return GetExeVersion16();
1698 return (DWORD
)NtCurrentTeb() - 0x10 /* FIXME */;
1700 return (DWORD
)NtCurrentTeb()->Peb
;
1701 case GPD_STARTF_SHELLDATA
: /* return stdoutput handle from startupinfo ??? */
1702 GetStartupInfoW(&siw
);
1703 return (DWORD
)siw
.hStdOutput
;
1704 case GPD_STARTF_HOTKEY
: /* return stdinput handle from startupinfo ??? */
1705 GetStartupInfoW(&siw
);
1706 return (DWORD
)siw
.hStdInput
;
1707 case GPD_STARTF_SHOWWINDOW
:
1708 GetStartupInfoW(&siw
);
1709 return siw
.wShowWindow
;
1710 case GPD_STARTF_SIZE
:
1711 GetStartupInfoW(&siw
);
1713 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
1715 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
1716 return MAKELONG( x
, y
);
1717 case GPD_STARTF_POSITION
:
1718 GetStartupInfoW(&siw
);
1720 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
1722 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
1723 return MAKELONG( x
, y
);
1724 case GPD_STARTF_FLAGS
:
1725 GetStartupInfoW(&siw
);
1730 return GetProcessFlags(0);
1732 return process_dword
;
1734 ERR("Unknown offset %d\n", offset
);
1739 /***********************************************************************
1740 * SetProcessDword (KERNEL.484)
1741 * 'Of course you cannot directly access Windows internal structures'
1743 void WINAPI
SetProcessDword( DWORD dwProcessID
, INT offset
, DWORD value
)
1745 TRACE("(%ld, %d)\n", dwProcessID
, offset
);
1747 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
1749 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
1755 case GPD_APP_COMPAT_FLAGS
:
1756 case GPD_LOAD_DONE_EVENT
:
1757 case GPD_HINSTANCE16
:
1758 case GPD_WINDOWS_VERSION
:
1761 case GPD_STARTF_SHELLDATA
:
1762 case GPD_STARTF_HOTKEY
:
1763 case GPD_STARTF_SHOWWINDOW
:
1764 case GPD_STARTF_SIZE
:
1765 case GPD_STARTF_POSITION
:
1766 case GPD_STARTF_FLAGS
:
1769 ERR("Not allowed to modify offset %d\n", offset
);
1772 process_dword
= value
;
1775 ERR("Unknown offset %d\n", offset
);
1781 /***********************************************************************
1782 * ExitProcess (KERNEL.466)
1784 void WINAPI
ExitProcess16( WORD status
)
1787 ReleaseThunkLock( &count
);
1788 ExitProcess( status
);
1792 /*********************************************************************
1793 * OpenProcess (KERNEL32.@)
1795 HANDLE WINAPI
OpenProcess( DWORD access
, BOOL inherit
, DWORD id
)
1798 SERVER_START_REQ( open_process
)
1801 req
->access
= access
;
1802 req
->inherit
= inherit
;
1803 if (!wine_server_call_err( req
)) ret
= reply
->handle
;
1810 /*********************************************************************
1811 * MapProcessHandle (KERNEL.483)
1813 DWORD WINAPI
MapProcessHandle( HANDLE handle
)
1816 SERVER_START_REQ( get_process_info
)
1818 req
->handle
= handle
;
1819 if (!wine_server_call_err( req
)) ret
= reply
->pid
;
1826 /***********************************************************************
1827 * SetPriorityClass (KERNEL32.@)
1829 BOOL WINAPI
SetPriorityClass( HANDLE hprocess
, DWORD priorityclass
)
1832 SERVER_START_REQ( set_process_info
)
1834 req
->handle
= hprocess
;
1835 req
->priority
= priorityclass
;
1836 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
1837 ret
= !wine_server_call_err( req
);
1844 /***********************************************************************
1845 * GetPriorityClass (KERNEL32.@)
1847 DWORD WINAPI
GetPriorityClass(HANDLE hprocess
)
1850 SERVER_START_REQ( get_process_info
)
1852 req
->handle
= hprocess
;
1853 if (!wine_server_call_err( req
)) ret
= reply
->priority
;
1860 /***********************************************************************
1861 * SetProcessAffinityMask (KERNEL32.@)
1863 BOOL WINAPI
SetProcessAffinityMask( HANDLE hProcess
, DWORD affmask
)
1866 SERVER_START_REQ( set_process_info
)
1868 req
->handle
= hProcess
;
1869 req
->affinity
= affmask
;
1870 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
1871 ret
= !wine_server_call_err( req
);
1878 /**********************************************************************
1879 * GetProcessAffinityMask (KERNEL32.@)
1881 BOOL WINAPI
GetProcessAffinityMask( HANDLE hProcess
,
1882 LPDWORD lpProcessAffinityMask
,
1883 LPDWORD lpSystemAffinityMask
)
1886 SERVER_START_REQ( get_process_info
)
1888 req
->handle
= hProcess
;
1889 if (!wine_server_call_err( req
))
1891 if (lpProcessAffinityMask
) *lpProcessAffinityMask
= reply
->process_affinity
;
1892 if (lpSystemAffinityMask
) *lpSystemAffinityMask
= reply
->system_affinity
;
1901 /***********************************************************************
1902 * GetProcessVersion (KERNEL32.@)
1904 DWORD WINAPI
GetProcessVersion( DWORD processid
)
1906 IMAGE_NT_HEADERS
*nt
;
1908 if (processid
&& processid
!= GetCurrentProcessId())
1910 FIXME("should use ReadProcessMemory\n");
1913 if ((nt
= RtlImageNtHeader( NtCurrentTeb()->Peb
->ImageBaseAddress
)))
1914 return ((nt
->OptionalHeader
.MajorSubsystemVersion
<< 16) |
1915 nt
->OptionalHeader
.MinorSubsystemVersion
);
1920 /***********************************************************************
1921 * SetProcessWorkingSetSize [KERNEL32.@]
1922 * Sets the min/max working set sizes for a specified process.
1925 * hProcess [I] Handle to the process of interest
1926 * minset [I] Specifies minimum working set size
1927 * maxset [I] Specifies maximum working set size
1931 BOOL WINAPI
SetProcessWorkingSetSize(HANDLE hProcess
, SIZE_T minset
,
1934 FIXME("(%p,%ld,%ld): stub - harmless\n",hProcess
,minset
,maxset
);
1935 if(( minset
== (SIZE_T
)-1) && (maxset
== (SIZE_T
)-1)) {
1936 /* Trim the working set to zero */
1937 /* Swap the process out of physical RAM */
1942 /***********************************************************************
1943 * GetProcessWorkingSetSize (KERNEL32.@)
1945 BOOL WINAPI
GetProcessWorkingSetSize(HANDLE hProcess
, PSIZE_T minset
,
1948 FIXME("(%p,%p,%p): stub\n",hProcess
,minset
,maxset
);
1949 /* 32 MB working set size */
1950 if (minset
) *minset
= 32*1024*1024;
1951 if (maxset
) *maxset
= 32*1024*1024;
1956 /***********************************************************************
1957 * SetProcessShutdownParameters (KERNEL32.@)
1959 BOOL WINAPI
SetProcessShutdownParameters(DWORD level
, DWORD flags
)
1961 FIXME("(%08lx, %08lx): partial stub.\n", level
, flags
);
1962 shutdown_flags
= flags
;
1963 shutdown_priority
= level
;
1968 /***********************************************************************
1969 * GetProcessShutdownParameters (KERNEL32.@)
1972 BOOL WINAPI
GetProcessShutdownParameters( LPDWORD lpdwLevel
, LPDWORD lpdwFlags
)
1974 *lpdwLevel
= shutdown_priority
;
1975 *lpdwFlags
= shutdown_flags
;
1980 /***********************************************************************
1981 * GetProcessPriorityBoost (KERNEL32.@)
1983 BOOL WINAPI
GetProcessPriorityBoost(HANDLE hprocess
,PBOOL pDisablePriorityBoost
)
1985 FIXME("(%p,%p): semi-stub\n", hprocess
, pDisablePriorityBoost
);
1987 /* Report that no boost is present.. */
1988 *pDisablePriorityBoost
= FALSE
;
1993 /***********************************************************************
1994 * SetProcessPriorityBoost (KERNEL32.@)
1996 BOOL WINAPI
SetProcessPriorityBoost(HANDLE hprocess
,BOOL disableboost
)
1998 FIXME("(%p,%d): stub\n",hprocess
,disableboost
);
1999 /* Say we can do it. I doubt the program will notice that we don't. */
2004 /***********************************************************************
2005 * ReadProcessMemory (KERNEL32.@)
2007 BOOL WINAPI
ReadProcessMemory( HANDLE process
, LPCVOID addr
, LPVOID buffer
, SIZE_T size
,
2008 SIZE_T
*bytes_read
)
2010 NTSTATUS status
= NtReadVirtualMemory( process
, addr
, buffer
, size
, bytes_read
);
2011 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
2016 /***********************************************************************
2017 * WriteProcessMemory (KERNEL32.@)
2019 BOOL WINAPI
WriteProcessMemory( HANDLE process
, LPVOID addr
, LPCVOID buffer
, SIZE_T size
,
2020 SIZE_T
*bytes_written
)
2022 NTSTATUS status
= NtWriteVirtualMemory( process
, addr
, buffer
, size
, bytes_written
);
2023 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
2028 /***********************************************************************
2029 * ProcessIdToSessionId (KERNEL32.@)
2030 * This function is available on Terminal Server 4SP4 and Windows 2000
2032 BOOL WINAPI
ProcessIdToSessionId( DWORD procid
, DWORD
*sessionid_ptr
)
2034 /* According to MSDN, if the calling process is not in a terminal
2035 * services environment, then the sessionid returned is zero.
2042 /***********************************************************************
2043 * RegisterServiceProcess (KERNEL.491)
2044 * RegisterServiceProcess (KERNEL32.@)
2046 * A service process calls this function to ensure that it continues to run
2047 * even after a user logged off.
2049 DWORD WINAPI
RegisterServiceProcess(DWORD dwProcessId
, DWORD dwType
)
2051 /* I don't think that Wine needs to do anything in that function */
2052 return 1; /* success */
2056 /**************************************************************************
2057 * SetFileApisToOEM (KERNEL32.@)
2059 VOID WINAPI
SetFileApisToOEM(void)
2061 oem_file_apis
= TRUE
;
2065 /**************************************************************************
2066 * SetFileApisToANSI (KERNEL32.@)
2068 VOID WINAPI
SetFileApisToANSI(void)
2070 oem_file_apis
= FALSE
;
2074 /******************************************************************************
2075 * AreFileApisANSI [KERNEL32.@] Determines if file functions are using ANSI
2078 * TRUE: Set of file functions is using ANSI code page
2079 * FALSE: Set of file functions is using OEM code page
2081 BOOL WINAPI
AreFileApisANSI(void)
2083 return !oem_file_apis
;
2087 /***********************************************************************
2088 * GetCurrentProcess (KERNEL32.@)
2090 #undef GetCurrentProcess
2091 HANDLE WINAPI
GetCurrentProcess(void)
2093 return (HANDLE
)0xffffffff;