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"
33 #include "wine/winbase16.h"
34 #include "wine/winuser16.h"
35 #include "wine/exception.h"
36 #include "wine/library.h"
44 #include "wine/server.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(process
);
49 WINE_DECLARE_DEBUG_CHANNEL(relay
);
50 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
51 WINE_DECLARE_DEBUG_CHANNEL(win32
);
55 /* Win32 process database */
58 LONG header
[2]; /* 00 Kernel object header */
59 HMODULE module
; /* 08 Main exe module (NT) */
60 void *event
; /* 0c Pointer to an event object (unused) */
61 DWORD exit_code
; /* 10 Process exit code */
62 DWORD unknown2
; /* 14 Unknown */
63 HANDLE heap
; /* 18 Default process heap */
64 HANDLE mem_context
; /* 1c Process memory context */
65 DWORD flags
; /* 20 Flags */
66 void *pdb16
; /* 24 DOS PSP */
67 WORD PSP_sel
; /* 28 Selector to DOS PSP */
68 WORD imte
; /* 2a IMTE for the process module */
69 WORD threads
; /* 2c Number of threads */
70 WORD running_threads
; /* 2e Number of running threads */
71 WORD free_lib_count
; /* 30 Recursion depth of FreeLibrary calls */
72 WORD ring0_threads
; /* 32 Number of ring 0 threads */
73 HANDLE system_heap
; /* 34 System heap to allocate handles */
74 HTASK task
; /* 38 Win16 task */
75 void *mem_map_files
; /* 3c Pointer to mem-mapped files */
76 struct _ENVDB
*env_db
; /* 40 Environment database */
77 void *handle_table
; /* 44 Handle table */
78 struct _PDB
*parent
; /* 48 Parent process */
79 void *modref_list
; /* 4c MODREF list */
80 void *thread_list
; /* 50 List of threads */
81 void *debuggee_CB
; /* 54 Debuggee context block */
82 void *local_heap_free
; /* 58 Head of local heap free list */
83 DWORD unknown4
; /* 5c Unknown */
84 CRITICAL_SECTION crit_section
; /* 60 Critical section */
85 DWORD unknown5
[3]; /* 78 Unknown */
86 void *console
; /* 84 Console */
87 DWORD tls_bits
[2]; /* 88 TLS in-use bits */
88 DWORD process_dword
; /* 90 Unknown */
89 struct _PDB
*group
; /* 94 Process group */
90 void *exe_modref
; /* 98 MODREF for the process EXE */
91 void *top_filter
; /* 9c Top exception filter */
92 DWORD priority
; /* a0 Priority level */
93 HANDLE heap_list
; /* a4 Head of process heap list */
94 void *heap_handles
; /* a8 Head of heap handles list */
95 DWORD unknown6
; /* ac Unknown */
96 void *console_provider
; /* b0 Console provider (??) */
97 WORD env_selector
; /* b4 Selector to process environment */
98 WORD error_mode
; /* b6 Error mode */
99 HANDLE load_done_evt
; /* b8 Event for process loading done */
100 void *UTState
; /* bc Head of Univeral Thunk list */
101 DWORD unknown8
; /* c0 Unknown (NT) */
102 LCID locale
; /* c4 Locale to be queried by GetThreadLocale (NT) */
108 #define PDB32_DEBUGGED 0x0001 /* Process is being debugged */
109 #define PDB32_WIN16_PROC 0x0008 /* Win16 process */
110 #define PDB32_DOS_PROC 0x0010 /* Dos process */
111 #define PDB32_CONSOLE_PROC 0x0020 /* Console process */
112 #define PDB32_FILE_APIS_OEM 0x0040 /* File APIs are OEM */
113 #define PDB32_WIN32S_PROC 0x8000 /* Win32s process */
115 static char main_exe_name
[MAX_PATH
];
116 static char *main_exe_name_ptr
= main_exe_name
;
117 static HANDLE main_exe_file
;
118 static unsigned int server_startticks
;
120 int main_create_flags
= 0;
122 /* memory/environ.c */
123 extern struct _ENVDB
*ENV_InitStartupInfo( size_t info_size
, char *main_exe_name
,
124 size_t main_exe_size
);
125 extern BOOL
ENV_BuildCommandLine( char **argv
);
126 extern STARTUPINFOA current_startupinfo
;
128 /* scheduler/pthread.c */
129 extern void PTHREAD_init_done(void);
131 extern void RELAY_InitDebugLists(void);
132 extern BOOL
MAIN_MainInit(void);
134 typedef WORD (WINAPI
*pUserSignalProc
)( UINT
, DWORD
, DWORD
, HMODULE16
);
136 /***********************************************************************
137 * PROCESS_CallUserSignalProc
139 * FIXME: Some of the signals aren't sent correctly!
141 * The exact meaning of the USER signals is undocumented, but this
142 * should cover the basic idea:
144 * USIG_DLL_UNLOAD_WIN16
145 * This is sent when a 16-bit module is unloaded.
147 * USIG_DLL_UNLOAD_WIN32
148 * This is sent when a 32-bit module is unloaded.
150 * USIG_DLL_UNLOAD_ORPHANS
151 * This is sent after the last Win3.1 module is unloaded,
152 * to allow removal of orphaned menus.
154 * USIG_FAULT_DIALOG_PUSH
155 * USIG_FAULT_DIALOG_POP
156 * These are called to allow USER to prepare for displaying a
157 * fault dialog, even though the fault might have happened while
158 * inside a USER critical section.
161 * This is called from the context of a new thread, as soon as it
162 * has started to run.
165 * This is called, still in its context, just before a thread is
166 * about to terminate.
168 * USIG_PROCESS_CREATE
169 * This is called, in the parent process context, after a new process
173 * This is called in the new process context, just after the main thread
174 * has started execution (after the main thread's USIG_THREAD_INIT has
177 * USIG_PROCESS_LOADED
178 * This is called after the executable file has been loaded into the
179 * new process context.
181 * USIG_PROCESS_RUNNING
182 * This is called immediately before the main entry point is called.
185 * This is called in the context of a process that is about to
186 * terminate (but before the last thread's USIG_THREAD_EXIT has
189 * USIG_PROCESS_DESTROY
190 * This is called after a process has terminated.
193 * The meaning of the dwFlags bits is as follows:
196 * Current process is 32-bit.
199 * Current process is a (Win32) GUI process.
201 * USIG_FLAGS_FEEDBACK
202 * Current process needs 'feedback' (determined from the STARTUPINFO
203 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
206 * The signal is being sent due to a fault.
208 void PROCESS_CallUserSignalProc( UINT uCode
, HMODULE16 hModule
)
212 pUserSignalProc proc
;
214 if (!(user
= GetModuleHandleA( "user32.dll" ))) return;
215 if (!(proc
= (pUserSignalProc
)GetProcAddress( user
, "UserSignalProc" ))) return;
217 /* Determine dwFlags */
219 if ( !(current_process
.flags
& PDB32_WIN16_PROC
) ) dwFlags
|= USIG_FLAGS_WIN32
;
220 if ( !(current_process
.flags
& PDB32_CONSOLE_PROC
) ) dwFlags
|= USIG_FLAGS_GUI
;
222 if ( dwFlags
& USIG_FLAGS_GUI
)
224 /* Feedback defaults to ON */
225 if ( !(current_startupinfo
.dwFlags
& STARTF_FORCEOFFFEEDBACK
) )
226 dwFlags
|= USIG_FLAGS_FEEDBACK
;
230 /* Feedback defaults to OFF */
231 if (current_startupinfo
.dwFlags
& STARTF_FORCEONFEEDBACK
)
232 dwFlags
|= USIG_FLAGS_FEEDBACK
;
235 /* Call USER signal proc */
237 if ( uCode
== USIG_THREAD_INIT
|| uCode
== USIG_THREAD_EXIT
)
238 proc( uCode
, GetCurrentThreadId(), dwFlags
, hModule
);
240 proc( uCode
, GetCurrentProcessId(), dwFlags
, hModule
);
244 /***********************************************************************
247 inline static const char *get_basename( const char *name
)
251 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
252 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
257 /***********************************************************************
260 * Open an exe file, taking load order into account.
261 * Returns the file handle or 0 for a builtin exe.
263 static HANDLE
open_exe_file( const char *name
)
265 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
269 SetLastError( ERROR_FILE_NOT_FOUND
);
270 MODULE_GetLoadOrder( loadorder
, name
, TRUE
);
272 for(i
= 0; i
< LOADORDER_NTYPES
; i
++)
274 if (loadorder
[i
] == LOADORDER_INVALID
) break;
278 TRACE( "Trying native exe %s\n", debugstr_a(name
) );
279 if ((handle
= CreateFileA( name
, GENERIC_READ
, FILE_SHARE_READ
,
280 NULL
, OPEN_EXISTING
, 0, 0 )) != INVALID_HANDLE_VALUE
)
282 if (GetLastError() != ERROR_FILE_NOT_FOUND
) return INVALID_HANDLE_VALUE
;
285 TRACE( "Trying built-in exe %s\n", debugstr_a(name
) );
286 if (wine_dll_load_main_exe( get_basename(name
), NULL
, 0, 1 )) return 0;
292 return INVALID_HANDLE_VALUE
;
296 /***********************************************************************
299 * Open an exe file, and return the full name and file handle.
300 * Returns FALSE if file could not be found.
301 * If file exists but cannot be opened, returns TRUE and set handle to INVALID_HANDLE_VALUE.
302 * If file is a builtin exe, returns TRUE and sets handle to 0.
304 static BOOL
find_exe_file( const char *name
, char *buffer
, int buflen
, HANDLE
*handle
)
306 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
309 TRACE("looking for %s\n", debugstr_a(name
) );
311 if (SearchPathA( NULL
, name
, ".exe", buflen
, buffer
, NULL
))
313 *handle
= open_exe_file( buffer
);
317 /* no such file in path, try builtin with .exe extension */
319 lstrcpynA( buffer
, get_basename(name
), buflen
);
320 if (!strchr( buffer
, '.' ))
322 char *p
= buffer
+ strlen(buffer
);
323 lstrcpynA( p
, ".exe", buflen
- (p
- buffer
) );
326 MODULE_GetLoadOrder( loadorder
, buffer
, TRUE
);
327 for (i
= 0; i
< LOADORDER_NTYPES
; i
++)
329 if (loadorder
[i
] == LOADORDER_BI
)
331 TRACE( "Trying built-in exe %s\n", debugstr_a(buffer
) );
332 if (wine_dll_load_main_exe( buffer
, NULL
, 0, 1 ))
339 if (loadorder
[i
] == LOADORDER_INVALID
) break;
342 /* no builtin found, try native without extension in case it is a Unix app */
344 if (SearchPathA( NULL
, name
, NULL
, buflen
, buffer
, NULL
))
346 TRACE( "Trying native/Unix binary %s\n", debugstr_a(buffer
) );
347 if ((*handle
= CreateFileA( buffer
, GENERIC_READ
, FILE_SHARE_READ
,
348 NULL
, OPEN_EXISTING
, 0, 0 )) != INVALID_HANDLE_VALUE
)
355 /***********************************************************************
358 * Main process initialisation code
360 static BOOL
process_init( char *argv
[] )
363 size_t info_size
= 0;
365 /* store the program name */
368 /* Fill the initial process structure */
369 current_process
.exit_code
= STILL_ACTIVE
;
370 current_process
.threads
= 1;
371 current_process
.running_threads
= 1;
372 current_process
.ring0_threads
= 1;
373 current_process
.group
= ¤t_process
;
374 current_process
.priority
= 8; /* Normal */
376 /* Setup the server connection */
379 /* Retrieve startup info from the server */
380 SERVER_START_REQ( init_process
)
382 req
->ldt_copy
= &wine_ldt_copy
;
383 req
->ppid
= getppid();
384 if ((ret
= !wine_server_call_err( req
)))
386 main_exe_file
= reply
->exe_file
;
387 main_create_flags
= reply
->create_flags
;
388 info_size
= reply
->info_size
;
389 server_startticks
= reply
->server_start
;
390 current_startupinfo
.hStdInput
= reply
->hstdin
;
391 current_startupinfo
.hStdOutput
= reply
->hstdout
;
392 current_startupinfo
.hStdError
= reply
->hstderr
;
396 if (!ret
) return FALSE
;
398 /* Create the process heap */
399 current_process
.heap
= HeapCreate( HEAP_GROWABLE
, 0, 0 );
401 if (main_create_flags
== 0 &&
402 current_startupinfo
.hStdInput
== 0 &&
403 current_startupinfo
.hStdOutput
== 0 &&
404 current_startupinfo
.hStdError
== 0)
406 /* no parent, and no new console requested, create a simple console with bare handles to
407 * unix stdio input & output streams (aka simple console)
410 wine_server_fd_to_handle( 0, GENERIC_READ
|SYNCHRONIZE
, TRUE
, &handle
);
411 SetStdHandle( STD_INPUT_HANDLE
, handle
);
412 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, TRUE
, &handle
);
413 SetStdHandle( STD_OUTPUT_HANDLE
, handle
);
414 wine_server_fd_to_handle( 1, GENERIC_WRITE
|SYNCHRONIZE
, TRUE
, &handle
);
415 SetStdHandle( STD_ERROR_HANDLE
, handle
);
417 else if (!(main_create_flags
& (DETACHED_PROCESS
|CREATE_NEW_CONSOLE
)))
419 SetStdHandle( STD_INPUT_HANDLE
, current_startupinfo
.hStdInput
);
420 SetStdHandle( STD_OUTPUT_HANDLE
, current_startupinfo
.hStdOutput
);
421 SetStdHandle( STD_ERROR_HANDLE
, current_startupinfo
.hStdError
);
424 /* Now we can use the pthreads routines */
427 /* Copy the parent environment */
428 if (!(current_process
.env_db
= ENV_InitStartupInfo( info_size
, main_exe_name
,
429 sizeof(main_exe_name
) )))
432 /* Parse command line arguments */
433 OPTIONS_ParseOptions( !info_size
? argv
: NULL
);
435 ret
= MAIN_MainInit();
436 if (TRACE_ON(relay
) || TRACE_ON(snoop
)) RELAY_InitDebugLists();
442 /***********************************************************************
445 * Startup routine of a new process. Runs on the new process stack.
447 static void start_process(void)
449 int debugged
, console_app
;
450 LPTHREAD_START_ROUTINE entry
;
452 HANDLE main_file
= main_exe_file
;
453 IMAGE_NT_HEADERS
*nt
;
455 /* use original argv[0] as name for the main module */
456 if (!main_exe_name
[0])
458 if (!GetLongPathNameA( full_argv0
, main_exe_name
, sizeof(main_exe_name
) ))
459 lstrcpynA( main_exe_name
, full_argv0
, sizeof(main_exe_name
) );
464 UINT drive_type
= GetDriveTypeA( main_exe_name
);
465 /* don't keep the file handle open on removable media */
466 if (drive_type
== DRIVE_REMOVABLE
|| drive_type
== DRIVE_CDROM
) main_file
= 0;
469 /* Retrieve entry point address */
470 nt
= RtlImageNtHeader( current_process
.module
);
471 entry
= (LPTHREAD_START_ROUTINE
)((char*)current_process
.module
+
472 nt
->OptionalHeader
.AddressOfEntryPoint
);
473 console_app
= (nt
->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
);
474 if (console_app
) current_process
.flags
|= PDB32_CONSOLE_PROC
;
476 /* Signal the parent process to continue */
477 SERVER_START_REQ( init_process_done
)
479 req
->module
= (void *)current_process
.module
;
480 req
->module_size
= nt
->OptionalHeader
.SizeOfImage
;
482 /* API requires a double indirection */
483 req
->name
= &main_exe_name_ptr
;
484 req
->exe_file
= main_file
;
485 req
->gui
= !console_app
;
486 wine_server_add_data( req
, main_exe_name
, strlen(main_exe_name
) );
487 wine_server_call( req
);
488 debugged
= reply
->debugged
;
492 /* Install signal handlers; this cannot be done before, since we cannot
493 * send exceptions to the debugger before the create process event that
494 * is sent by REQ_INIT_PROCESS_DONE */
495 if (!SIGNAL_Init()) goto error
;
497 /* create the main modref and load dependencies */
498 if (!(wm
= PE_CreateModule( current_process
.module
, main_exe_name
, 0, 0, FALSE
)))
502 if (main_exe_file
) CloseHandle( main_exe_file
); /* we no longer need it */
504 MODULE_DllProcessAttach( NULL
, (LPVOID
)1 );
506 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
507 * context of the parent process. Actually, the USER signal proc
508 * doesn't really care about that, but it *does* require that the
509 * startup parameters are correctly set up, so that GetProcessDword
510 * works. Furthermore, before calling the USER signal proc the
511 * 16-bit stack must be set up, which it is only after TASK_Create
512 * in the case of a 16-bit process. Thus, we send the signal here.
514 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE
, 0 );
515 PROCESS_CallUserSignalProc( USIG_THREAD_INIT
, 0 );
516 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT
, 0 );
517 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED
, 0 );
518 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
519 if (console_app
) PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING
, 0 );
522 DPRINTF( "%08lx:Starting process %s (entryproc=%p)\n",
523 GetCurrentThreadId(), main_exe_name
, entry
);
524 if (debugged
) DbgBreakPoint();
525 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
526 * Dunno about other OSs */
527 SetLastError(0); /* clear error code */
528 ExitThread( entry(NULL
) );
531 ExitProcess( GetLastError() );
535 /***********************************************************************
538 * Wine initialisation: load and start the main exe file.
540 void PROCESS_InitWine( int argc
, char *argv
[], LPSTR win16_exe_name
, HANDLE
*win16_exe_file
)
543 DWORD stack_size
= 0;
545 /* Initialize everything */
546 if (!process_init( argv
)) exit(1);
548 argv
++; /* remove argv[0] (wine itself) */
550 TRACE( "starting process name=%s file=%x argv[0]=%s\n",
551 debugstr_a(main_exe_name
), main_exe_file
, debugstr_a(argv
[0]) );
553 if (!main_exe_name
[0])
555 if (!argv
[0]) OPTIONS_Usage();
557 if (!find_exe_file( argv
[0], main_exe_name
, sizeof(main_exe_name
), &main_exe_file
))
559 MESSAGE( "%s: cannot find '%s'\n", argv0
, argv
[0] );
562 if (main_exe_file
== INVALID_HANDLE_VALUE
)
564 MESSAGE( "%s: cannot open '%s'\n", argv0
, main_exe_name
);
569 if (!main_exe_file
) /* no file handle -> Winelib app */
571 TRACE( "starting Winelib app %s\n", debugstr_a(main_exe_name
) );
572 if (wine_dll_load_main_exe( get_basename(main_exe_name
), error
, sizeof(error
), 0 ))
574 MESSAGE( "%s: cannot open builtin library for '%s': %s\n", argv0
, main_exe_name
, error
);
578 switch( MODULE_GetBinaryType( main_exe_file
))
581 TRACE( "starting Win32 binary %s\n", debugstr_a(main_exe_name
) );
582 if ((current_process
.module
= PE_LoadImage( main_exe_file
, main_exe_name
, 0 ))) goto found
;
583 MESSAGE( "%s: could not load '%s' as Win32 binary\n", argv0
, main_exe_name
);
586 MESSAGE( "%s: '%s' is a DLL, not an executable\n", argv0
, main_exe_name
);
589 /* check for .com extension */
590 if (!(p
= strrchr( main_exe_name
, '.' )) || FILE_strcasecmp( p
, ".com" ))
592 MESSAGE( "%s: cannot determine executable type for '%s'\n", argv0
, main_exe_name
);
598 TRACE( "starting Win16/DOS binary %s\n", debugstr_a(main_exe_name
) );
599 NtCurrentTeb()->tibflags
&= ~TEBF_WIN32
;
600 current_process
.flags
|= PDB32_WIN16_PROC
;
601 strcpy( win16_exe_name
, main_exe_name
);
602 main_exe_name
[0] = 0;
603 *win16_exe_file
= main_exe_file
;
608 MESSAGE( "%s: '%s' is an OS/2 binary, not supported\n", argv0
, main_exe_name
);
610 case BINARY_UNIX_EXE
:
611 MESSAGE( "%s: '%s' is a Unix binary, not supported\n", argv0
, main_exe_name
);
613 case BINARY_UNIX_LIB
:
615 DOS_FULL_NAME full_name
;
616 const char *name
= main_exe_name
;
618 TRACE( "starting Winelib app %s\n", debugstr_a(main_exe_name
) );
619 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) name
= full_name
.long_name
;
620 CloseHandle( main_exe_file
);
622 if (wine_dlopen( name
, RTLD_NOW
, error
, sizeof(error
) ))
624 if ((p
= strrchr( main_exe_name
, '.' )) && !strcmp( p
, ".so" )) *p
= 0;
627 MESSAGE( "%s: could not load '%s': %s\n", argv0
, main_exe_name
, error
);
633 /* build command line */
634 if (!ENV_BuildCommandLine( argv
)) goto error
;
636 /* create 32-bit module for main exe */
637 if (!(current_process
.module
= BUILTIN32_LoadExeModule( current_process
.module
))) goto error
;
638 stack_size
= RtlImageNtHeader(current_process
.module
)->OptionalHeader
.SizeOfStackReserve
;
640 /* allocate main thread stack */
641 if (!THREAD_InitStack( NtCurrentTeb(), stack_size
)) goto error
;
643 /* switch to the new stack */
644 SYSDEPS_SwitchToThreadStack( start_process
);
647 ExitProcess( GetLastError() );
651 /***********************************************************************
654 * Build an argv array from a command-line.
655 * The command-line is modified to insert nulls.
656 * 'reserved' is the number of args to reserve before the first one.
658 static char **build_argv( char *cmdline
, int reserved
)
663 int in_quotes
,bcount
;
670 if (*s
=='\0' || ((*s
==' ' || *s
=='\t') && !in_quotes
)) {
673 /* skip the remaining spaces */
674 while (*s
==' ' || *s
=='\t') {
681 } else if (*s
=='\\') {
682 /* '\', count them */
684 } else if ((*s
=='"') && ((bcount
& 1)==0)) {
686 in_quotes
=!in_quotes
;
689 /* a regular character */
694 argv
=malloc(argc
*sizeof(*argv
));
703 if ((*s
==' ' || *s
=='\t') && !in_quotes
) {
704 /* Close the argument and copy it */
708 /* skip the remaining spaces */
711 } while (*s
==' ' || *s
=='\t');
713 /* Start with a new argument */
716 } else if (*s
=='\\') {
720 } else if (*s
=='"') {
722 if ((bcount
& 1)==0) {
723 /* Preceeded by an even number of '\', this is half that
724 * number of '\', plus a '"' which we discard.
728 in_quotes
=!in_quotes
;
730 /* Preceeded by an odd number of '\', this is half that
731 * number of '\' followed by a '"'
739 /* a regular character */
754 /***********************************************************************
757 * Build the environment of a new child process.
759 static char **build_envp( const char *env
, const char *extra_env
)
765 if (extra_env
) for (p
= extra_env
; *p
; count
++) p
+= strlen(p
) + 1;
766 for (p
= env
; *p
; count
++) p
+= strlen(p
) + 1;
769 if ((envp
= malloc( count
* sizeof(*envp
) )))
771 extern char **environ
;
772 char **envptr
= envp
;
773 char **unixptr
= environ
;
774 /* first the extra strings */
775 if (extra_env
) for (p
= extra_env
; *p
; p
+= strlen(p
) + 1) *envptr
++ = (char *)p
;
776 /* then put PATH, HOME and WINEPREFIX from the unix env */
777 for (unixptr
= environ
; unixptr
&& *unixptr
; unixptr
++)
778 if (!memcmp( *unixptr
, "PATH=", 5 ) ||
779 !memcmp( *unixptr
, "HOME=", 5 ) ||
780 !memcmp( *unixptr
, "WINEPREFIX=", 11 )) *envptr
++ = *unixptr
;
781 /* now put the Windows environment strings */
782 for (p
= env
; *p
; p
+= strlen(p
) + 1)
784 if (memcmp( p
, "PATH=", 5 ) &&
785 memcmp( p
, "HOME=", 5 ) &&
786 memcmp( p
, "WINEPREFIX=", 11 )) *envptr
++ = (char *)p
;
794 /***********************************************************************
797 * Locate the Wine binary to exec for a new Win32 process.
799 static void exec_wine_binary( char **argv
, char **envp
)
801 const char *path
, *pos
, *ptr
;
803 /* first, try for a WINELOADER environment variable */
804 argv
[0] = getenv("WINELOADER");
806 execve( argv
[0], argv
, envp
);
808 /* next, try bin directory */
809 argv
[0] = BINDIR
"/wine";
810 execve( argv
[0], argv
, envp
);
812 /* now try the path of argv0 of the current binary */
813 if (!(argv
[0] = malloc( strlen(full_argv0
) + 6 ))) return;
814 if ((ptr
= strrchr( full_argv0
, '/' )))
816 memcpy( argv
[0], full_argv0
, ptr
- full_argv0
);
817 strcpy( argv
[0] + (ptr
- full_argv0
), "/wine" );
818 execve( argv
[0], argv
, envp
);
822 /* now search in the Unix path */
823 if ((path
= getenv( "PATH" )))
825 if (!(argv
[0] = malloc( strlen(path
) + 6 ))) return;
829 while (*pos
== ':') pos
++;
831 if (!(ptr
= strchr( pos
, ':' ))) ptr
= pos
+ strlen(pos
);
832 memcpy( argv
[0], pos
, ptr
- pos
);
833 strcpy( argv
[0] + (ptr
- pos
), "/wine" );
834 execve( argv
[0], argv
, envp
);
842 /***********************************************************************
845 * Fork and exec a new Unix process, checking for errors.
847 static int fork_and_exec( const char *filename
, char *cmdline
,
848 const char *env
, const char *newdir
)
852 char *extra_env
= NULL
;
856 env
= GetEnvironmentStringsA();
857 extra_env
= DRIVE_BuildEnv();
865 fcntl( fd
[1], F_SETFD
, 1 ); /* set close on exec */
866 if (!(pid
= fork())) /* child */
868 char **argv
= build_argv( cmdline
, filename
? 0 : 1 );
869 char **envp
= build_envp( env
, extra_env
);
872 /* Reset signals that we previously set to SIG_IGN */
873 signal( SIGPIPE
, SIG_DFL
);
874 signal( SIGCHLD
, SIG_DFL
);
876 if (newdir
) chdir(newdir
);
880 if (!filename
) exec_wine_binary( argv
, envp
);
881 else execve( filename
, argv
, envp
);
884 write( fd
[1], &err
, sizeof(err
) );
888 if ((pid
!= -1) && (read( fd
[0], &err
, sizeof(err
) ) > 0)) /* exec failed */
893 if (pid
== -1) FILE_SetDosError();
895 if (extra_env
) HeapFree( GetProcessHeap(), 0, extra_env
);
900 /***********************************************************************
903 * Create a new process. If hFile is a valid handle we have an exe
904 * file, otherwise it is a Winelib app.
906 static BOOL
create_process( HANDLE hFile
, LPCSTR filename
, LPSTR cmd_line
, LPCSTR env
,
907 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
908 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
909 LPPROCESS_INFORMATION info
, LPCSTR unixdir
)
911 BOOL ret
, success
= FALSE
;
913 startup_info_t startup_info
;
915 /* fill the startup info structure */
917 startup_info
.size
= sizeof(startup_info
);
918 /* startup_info.filename_len is set below */
919 startup_info
.cmdline_len
= cmd_line
? strlen(cmd_line
) : 0;
920 startup_info
.desktop_len
= startup
->lpDesktop
? strlen(startup
->lpDesktop
) : 0;
921 startup_info
.title_len
= startup
->lpTitle
? strlen(startup
->lpTitle
) : 0;
922 startup_info
.x
= startup
->dwX
;
923 startup_info
.y
= startup
->dwY
;
924 startup_info
.cx
= startup
->dwXSize
;
925 startup_info
.cy
= startup
->dwYSize
;
926 startup_info
.x_chars
= startup
->dwXCountChars
;
927 startup_info
.y_chars
= startup
->dwYCountChars
;
928 startup_info
.attribute
= startup
->dwFillAttribute
;
929 startup_info
.cmd_show
= startup
->wShowWindow
;
930 startup_info
.flags
= startup
->dwFlags
;
932 /* create the process on the server side */
934 SERVER_START_REQ( new_process
)
939 req
->inherit_all
= inherit
;
940 req
->create_flags
= flags
;
941 req
->use_handles
= (startup
->dwFlags
& STARTF_USESTDHANDLES
) != 0;
942 req
->exe_file
= hFile
;
943 if (startup
->dwFlags
& STARTF_USESTDHANDLES
)
945 req
->hstdin
= startup
->hStdInput
;
946 req
->hstdout
= startup
->hStdOutput
;
947 req
->hstderr
= startup
->hStdError
;
951 req
->hstdin
= GetStdHandle( STD_INPUT_HANDLE
);
952 req
->hstdout
= GetStdHandle( STD_OUTPUT_HANDLE
);
953 req
->hstderr
= GetStdHandle( STD_ERROR_HANDLE
);
956 if (GetLongPathNameA( filename
, buf
, MAX_PATH
))
961 startup_info
.filename_len
= strlen(nameptr
);
962 wine_server_add_data( req
, &startup_info
, sizeof(startup_info
) );
963 wine_server_add_data( req
, nameptr
, startup_info
.filename_len
);
964 wine_server_add_data( req
, cmd_line
, startup_info
.cmdline_len
);
965 wine_server_add_data( req
, startup
->lpDesktop
, startup_info
.desktop_len
);
966 wine_server_add_data( req
, startup
->lpTitle
, startup_info
.title_len
);
968 ret
= !wine_server_call_err( req
);
969 process_info
= reply
->info
;
972 if (!ret
) return FALSE
;
974 /* fork and execute */
976 if (fork_and_exec( NULL
, cmd_line
, env
, unixdir
) == -1)
978 CloseHandle( process_info
);
982 /* wait for the new process info to be ready */
984 WaitForSingleObject( process_info
, INFINITE
);
985 SERVER_START_REQ( get_new_process_info
)
987 req
->info
= process_info
;
988 req
->pinherit
= (psa
&& (psa
->nLength
>= sizeof(*psa
)) && psa
->bInheritHandle
);
989 req
->tinherit
= (tsa
&& (tsa
->nLength
>= sizeof(*tsa
)) && tsa
->bInheritHandle
);
990 if ((ret
= !wine_server_call_err( req
)))
992 info
->dwProcessId
= (DWORD
)reply
->pid
;
993 info
->dwThreadId
= (DWORD
)reply
->tid
;
994 info
->hProcess
= reply
->phandle
;
995 info
->hThread
= reply
->thandle
;
996 success
= reply
->success
;
1001 if (ret
&& !success
) /* new process failed to start */
1004 if (GetExitCodeProcess( info
->hProcess
, &exitcode
)) SetLastError( exitcode
);
1005 CloseHandle( info
->hThread
);
1006 CloseHandle( info
->hProcess
);
1009 CloseHandle( process_info
);
1014 /*************************************************************************
1017 * Helper for CreateProcess: retrieve the file name to load from the
1018 * app name and command line. Store the file name in buffer, and
1019 * return a possibly modified command line.
1020 * Also returns a handle to the opened file if it's a Windows binary.
1022 static LPSTR
get_file_name( LPCSTR appname
, LPSTR cmdline
, LPSTR buffer
,
1023 int buflen
, HANDLE
*handle
)
1025 char *name
, *pos
, *ret
= NULL
;
1028 /* if we have an app name, everything is easy */
1032 /* use the unmodified app name as file name */
1033 lstrcpynA( buffer
, appname
, buflen
);
1034 *handle
= open_exe_file( buffer
);
1035 if (!(ret
= cmdline
))
1037 /* no command-line, create one */
1038 if ((ret
= HeapAlloc( GetProcessHeap(), 0, strlen(appname
) + 3 )))
1039 sprintf( ret
, "\"%s\"", appname
);
1046 SetLastError( ERROR_INVALID_PARAMETER
);
1050 /* first check for a quoted file name */
1052 if ((cmdline
[0] == '"') && ((p
= strchr( cmdline
+ 1, '"' ))))
1054 int len
= p
- cmdline
- 1;
1055 /* extract the quoted portion as file name */
1056 if (!(name
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 ))) return NULL
;
1057 memcpy( name
, cmdline
+ 1, len
);
1060 if (find_exe_file( name
, buffer
, buflen
, handle
))
1061 ret
= cmdline
; /* no change necessary */
1065 /* now try the command-line word by word */
1067 if (!(name
= HeapAlloc( GetProcessHeap(), 0, strlen(cmdline
) + 1 ))) return NULL
;
1073 do *pos
++ = *p
++; while (*p
&& *p
!= ' ');
1075 if (find_exe_file( name
, buffer
, buflen
, handle
))
1082 if (!ret
|| !strchr( name
, ' ' )) goto done
; /* no change necessary */
1084 /* now build a new command-line with quotes */
1086 if (!(ret
= HeapAlloc( GetProcessHeap(), 0, strlen(cmdline
) + 3 ))) goto done
;
1087 sprintf( ret
, "\"%s\"%s", name
, p
);
1090 HeapFree( GetProcessHeap(), 0, name
);
1095 /**********************************************************************
1096 * CreateProcessA (KERNEL32.@)
1098 BOOL WINAPI
CreateProcessA( LPCSTR app_name
, LPSTR cmd_line
, LPSECURITY_ATTRIBUTES process_attr
,
1099 LPSECURITY_ATTRIBUTES thread_attr
, BOOL inherit
,
1100 DWORD flags
, LPVOID env
, LPCSTR cur_dir
,
1101 LPSTARTUPINFOA startup_info
, LPPROCESS_INFORMATION info
)
1105 const char *unixdir
= NULL
;
1106 DOS_FULL_NAME full_dir
;
1107 char name
[MAX_PATH
];
1111 /* Process the AppName and/or CmdLine to get module name and path */
1113 TRACE("app %s cmdline %s\n", debugstr_a(app_name
), debugstr_a(cmd_line
) );
1115 if (!(tidy_cmdline
= get_file_name( app_name
, cmd_line
, name
, sizeof(name
), &hFile
)))
1117 if (hFile
== INVALID_HANDLE_VALUE
) goto done
;
1119 /* Warn if unsupported features are used */
1121 if (flags
& NORMAL_PRIORITY_CLASS
)
1122 FIXME("(%s,...): NORMAL_PRIORITY_CLASS ignored\n", name
);
1123 if (flags
& IDLE_PRIORITY_CLASS
)
1124 FIXME("(%s,...): IDLE_PRIORITY_CLASS ignored\n", name
);
1125 if (flags
& HIGH_PRIORITY_CLASS
)
1126 FIXME("(%s,...): HIGH_PRIORITY_CLASS ignored\n", name
);
1127 if (flags
& REALTIME_PRIORITY_CLASS
)
1128 FIXME("(%s,...): REALTIME_PRIORITY_CLASS ignored\n", name
);
1129 if (flags
& CREATE_NEW_PROCESS_GROUP
)
1130 FIXME("(%s,...): CREATE_NEW_PROCESS_GROUP ignored\n", name
);
1131 if (flags
& CREATE_UNICODE_ENVIRONMENT
)
1132 FIXME("(%s,...): CREATE_UNICODE_ENVIRONMENT ignored\n", name
);
1133 if (flags
& CREATE_SEPARATE_WOW_VDM
)
1134 FIXME("(%s,...): CREATE_SEPARATE_WOW_VDM ignored\n", name
);
1135 if (flags
& CREATE_SHARED_WOW_VDM
)
1136 FIXME("(%s,...): CREATE_SHARED_WOW_VDM ignored\n", name
);
1137 if (flags
& CREATE_DEFAULT_ERROR_MODE
)
1138 FIXME("(%s,...): CREATE_DEFAULT_ERROR_MODE ignored\n", name
);
1139 if (flags
& CREATE_NO_WINDOW
)
1140 FIXME("(%s,...): CREATE_NO_WINDOW ignored\n", name
);
1141 if (flags
& PROFILE_USER
)
1142 FIXME("(%s,...): PROFILE_USER ignored\n", name
);
1143 if (flags
& PROFILE_KERNEL
)
1144 FIXME("(%s,...): PROFILE_KERNEL ignored\n", name
);
1145 if (flags
& PROFILE_SERVER
)
1146 FIXME("(%s,...): PROFILE_SERVER ignored\n", name
);
1147 if (startup_info
->lpDesktop
)
1148 FIXME("(%s,...): startup_info->lpDesktop %s ignored\n",
1149 name
, debugstr_a(startup_info
->lpDesktop
));
1150 if (startup_info
->dwFlags
& STARTF_RUNFULLSCREEN
)
1151 FIXME("(%s,...): STARTF_RUNFULLSCREEN ignored\n", name
);
1152 if (startup_info
->dwFlags
& STARTF_FORCEONFEEDBACK
)
1153 FIXME("(%s,...): STARTF_FORCEONFEEDBACK ignored\n", name
);
1154 if (startup_info
->dwFlags
& STARTF_FORCEOFFFEEDBACK
)
1155 FIXME("(%s,...): STARTF_FORCEOFFFEEDBACK ignored\n", name
);
1156 if (startup_info
->dwFlags
& STARTF_USEHOTKEY
)
1157 FIXME("(%s,...): STARTF_USEHOTKEY ignored\n", name
);
1161 if (DOSFS_GetFullName( cur_dir
, TRUE
, &full_dir
))
1162 unixdir
= full_dir
.long_name
;
1167 if (GetCurrentDirectoryA(sizeof(buf
),buf
))
1169 if (DOSFS_GetFullName( buf
, TRUE
, &full_dir
)) unixdir
= full_dir
.long_name
;
1173 info
->hThread
= info
->hProcess
= 0;
1174 info
->dwProcessId
= info
->dwThreadId
= 0;
1176 /* Determine executable type */
1178 if (!hFile
) /* builtin exe */
1180 TRACE( "starting %s as Winelib app\n", debugstr_a(name
) );
1181 retv
= create_process( 0, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1182 inherit
, flags
, startup_info
, info
, unixdir
);
1186 switch( MODULE_GetBinaryType( hFile
))
1191 TRACE( "starting %s as Windows binary\n", debugstr_a(name
) );
1192 retv
= create_process( hFile
, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1193 inherit
, flags
, startup_info
, info
, unixdir
);
1196 FIXME( "%s is OS/2 binary, not supported\n", debugstr_a(name
) );
1197 SetLastError( ERROR_BAD_EXE_FORMAT
);
1200 TRACE( "not starting %s since it is a dll\n", debugstr_a(name
) );
1201 SetLastError( ERROR_BAD_EXE_FORMAT
);
1203 case BINARY_UNIX_LIB
:
1204 TRACE( "%s is a Unix library, starting as Winelib app\n", debugstr_a(name
) );
1205 retv
= create_process( hFile
, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1206 inherit
, flags
, startup_info
, info
, unixdir
);
1208 case BINARY_UNKNOWN
:
1209 /* check for .com extension */
1210 if ((p
= strrchr( name
, '.' )) && !FILE_strcasecmp( p
, ".com" ))
1212 TRACE( "starting %s as DOS binary\n", debugstr_a(name
) );
1213 retv
= create_process( hFile
, name
, tidy_cmdline
, env
, process_attr
, thread_attr
,
1214 inherit
, flags
, startup_info
, info
, unixdir
);
1218 case BINARY_UNIX_EXE
:
1220 DOS_FULL_NAME full_name
;
1221 const char *unixfilename
= name
;
1223 TRACE( "starting %s as Unix binary\n", debugstr_a(name
) );
1224 if (DOSFS_GetFullName( name
, TRUE
, &full_name
)) unixfilename
= full_name
.long_name
;
1225 retv
= (fork_and_exec( unixfilename
, tidy_cmdline
, env
, unixdir
) != -1);
1229 CloseHandle( hFile
);
1232 if (tidy_cmdline
!= cmd_line
) HeapFree( GetProcessHeap(), 0, tidy_cmdline
);
1237 /**********************************************************************
1238 * CreateProcessW (KERNEL32.@)
1240 * lpReserved is not converted
1242 BOOL WINAPI
CreateProcessW( LPCWSTR app_name
, LPWSTR cmd_line
, LPSECURITY_ATTRIBUTES process_attr
,
1243 LPSECURITY_ATTRIBUTES thread_attr
, BOOL inherit
, DWORD flags
,
1244 LPVOID env
, LPCWSTR cur_dir
, LPSTARTUPINFOW startup_info
,
1245 LPPROCESS_INFORMATION info
)
1248 STARTUPINFOA StartupInfoA
;
1250 LPSTR app_nameA
= HEAP_strdupWtoA (GetProcessHeap(),0,app_name
);
1251 LPSTR cmd_lineA
= HEAP_strdupWtoA (GetProcessHeap(),0,cmd_line
);
1252 LPSTR cur_dirA
= HEAP_strdupWtoA (GetProcessHeap(),0,cur_dir
);
1254 memcpy (&StartupInfoA
, startup_info
, sizeof(STARTUPINFOA
));
1255 StartupInfoA
.lpDesktop
= HEAP_strdupWtoA (GetProcessHeap(),0,startup_info
->lpDesktop
);
1256 StartupInfoA
.lpTitle
= HEAP_strdupWtoA (GetProcessHeap(),0,startup_info
->lpTitle
);
1258 TRACE_(win32
)("(%s,%s,...)\n", debugstr_w(app_name
), debugstr_w(cmd_line
));
1260 if (startup_info
->lpReserved
)
1261 FIXME_(win32
)("StartupInfo.lpReserved is used, please report (%s)\n",
1262 debugstr_w(startup_info
->lpReserved
));
1264 ret
= CreateProcessA( app_nameA
, cmd_lineA
, process_attr
, thread_attr
,
1265 inherit
, flags
, env
, cur_dirA
, &StartupInfoA
, info
);
1267 HeapFree( GetProcessHeap(), 0, cur_dirA
);
1268 HeapFree( GetProcessHeap(), 0, cmd_lineA
);
1269 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpDesktop
);
1270 HeapFree( GetProcessHeap(), 0, StartupInfoA
.lpTitle
);
1276 /***********************************************************************
1277 * ExitProcess (KERNEL32.@)
1279 void WINAPI
ExitProcess( DWORD status
)
1281 MODULE_DllProcessDetach( TRUE
, (LPVOID
)1 );
1282 SERVER_START_REQ( terminate_process
)
1284 /* send the exit code to the server */
1285 req
->handle
= GetCurrentProcess();
1286 req
->exit_code
= status
;
1287 wine_server_call( req
);
1293 /***********************************************************************
1294 * ExitProcess (KERNEL.466)
1296 void WINAPI
ExitProcess16( WORD status
)
1299 ReleaseThunkLock( &count
);
1300 ExitProcess( status
);
1303 /******************************************************************************
1304 * TerminateProcess (KERNEL32.@)
1306 BOOL WINAPI
TerminateProcess( HANDLE handle
, DWORD exit_code
)
1308 NTSTATUS status
= NtTerminateProcess( handle
, exit_code
);
1309 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1314 /***********************************************************************
1315 * GetProcessDword (KERNEL.485)
1316 * GetProcessDword (KERNEL32.18)
1317 * 'Of course you cannot directly access Windows internal structures'
1319 DWORD WINAPI
GetProcessDword( DWORD dwProcessID
, INT offset
)
1323 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
1325 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
1327 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
1333 case GPD_APP_COMPAT_FLAGS
:
1334 return GetAppCompatFlags16(0);
1336 case GPD_LOAD_DONE_EVENT
:
1337 return (DWORD
)current_process
.load_done_evt
;
1339 case GPD_HINSTANCE16
:
1340 return GetTaskDS16();
1342 case GPD_WINDOWS_VERSION
:
1343 return GetExeVersion16();
1346 return (DWORD
)NtCurrentTeb() - 0x10 /* FIXME */;
1349 return (DWORD
)¤t_process
;
1351 case GPD_STARTF_SHELLDATA
: /* return stdoutput handle from startupinfo ??? */
1352 return (DWORD
)current_startupinfo
.hStdOutput
;
1354 case GPD_STARTF_HOTKEY
: /* return stdinput handle from startupinfo ??? */
1355 return (DWORD
)current_startupinfo
.hStdInput
;
1357 case GPD_STARTF_SHOWWINDOW
:
1358 return current_startupinfo
.wShowWindow
;
1360 case GPD_STARTF_SIZE
:
1361 x
= current_startupinfo
.dwXSize
;
1362 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
1363 y
= current_startupinfo
.dwYSize
;
1364 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
1365 return MAKELONG( x
, y
);
1367 case GPD_STARTF_POSITION
:
1368 x
= current_startupinfo
.dwX
;
1369 if ( (INT
)x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
1370 y
= current_startupinfo
.dwY
;
1371 if ( (INT
)y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
1372 return MAKELONG( x
, y
);
1374 case GPD_STARTF_FLAGS
:
1375 return current_startupinfo
.dwFlags
;
1381 return current_process
.flags
;
1384 return current_process
.process_dword
;
1387 ERR_(win32
)("Unknown offset %d\n", offset
);
1392 /***********************************************************************
1393 * SetProcessDword (KERNEL.484)
1394 * 'Of course you cannot directly access Windows internal structures'
1396 void WINAPI
SetProcessDword( DWORD dwProcessID
, INT offset
, DWORD value
)
1398 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
1400 if (dwProcessID
&& dwProcessID
!= GetCurrentProcessId())
1402 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
1408 case GPD_APP_COMPAT_FLAGS
:
1409 case GPD_LOAD_DONE_EVENT
:
1410 case GPD_HINSTANCE16
:
1411 case GPD_WINDOWS_VERSION
:
1414 case GPD_STARTF_SHELLDATA
:
1415 case GPD_STARTF_HOTKEY
:
1416 case GPD_STARTF_SHOWWINDOW
:
1417 case GPD_STARTF_SIZE
:
1418 case GPD_STARTF_POSITION
:
1419 case GPD_STARTF_FLAGS
:
1422 ERR_(win32
)("Not allowed to modify offset %d\n", offset
);
1426 current_process
.process_dword
= value
;
1430 ERR_(win32
)("Unknown offset %d\n", offset
);
1436 /*********************************************************************
1437 * OpenProcess (KERNEL32.@)
1439 HANDLE WINAPI
OpenProcess( DWORD access
, BOOL inherit
, DWORD id
)
1442 SERVER_START_REQ( open_process
)
1444 req
->pid
= (void *)id
;
1445 req
->access
= access
;
1446 req
->inherit
= inherit
;
1447 if (!wine_server_call_err( req
)) ret
= reply
->handle
;
1453 /*********************************************************************
1454 * MapProcessHandle (KERNEL.483)
1456 DWORD WINAPI
MapProcessHandle( HANDLE handle
)
1459 SERVER_START_REQ( get_process_info
)
1461 req
->handle
= handle
;
1462 if (!wine_server_call_err( req
)) ret
= (DWORD
)reply
->pid
;
1468 /***********************************************************************
1469 * SetPriorityClass (KERNEL32.@)
1471 BOOL WINAPI
SetPriorityClass( HANDLE hprocess
, DWORD priorityclass
)
1474 SERVER_START_REQ( set_process_info
)
1476 req
->handle
= hprocess
;
1477 req
->priority
= priorityclass
;
1478 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
1479 ret
= !wine_server_call_err( req
);
1486 /***********************************************************************
1487 * GetPriorityClass (KERNEL32.@)
1489 DWORD WINAPI
GetPriorityClass(HANDLE hprocess
)
1492 SERVER_START_REQ( get_process_info
)
1494 req
->handle
= hprocess
;
1495 if (!wine_server_call_err( req
)) ret
= reply
->priority
;
1502 /***********************************************************************
1503 * SetProcessAffinityMask (KERNEL32.@)
1505 BOOL WINAPI
SetProcessAffinityMask( HANDLE hProcess
, DWORD affmask
)
1508 SERVER_START_REQ( set_process_info
)
1510 req
->handle
= hProcess
;
1511 req
->affinity
= affmask
;
1512 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
1513 ret
= !wine_server_call_err( req
);
1519 /**********************************************************************
1520 * GetProcessAffinityMask (KERNEL32.@)
1522 BOOL WINAPI
GetProcessAffinityMask( HANDLE hProcess
,
1523 LPDWORD lpProcessAffinityMask
,
1524 LPDWORD lpSystemAffinityMask
)
1527 SERVER_START_REQ( get_process_info
)
1529 req
->handle
= hProcess
;
1530 if (!wine_server_call_err( req
))
1532 if (lpProcessAffinityMask
) *lpProcessAffinityMask
= reply
->process_affinity
;
1533 if (lpSystemAffinityMask
) *lpSystemAffinityMask
= reply
->system_affinity
;
1542 /***********************************************************************
1543 * GetProcessVersion (KERNEL32.@)
1545 DWORD WINAPI
GetProcessVersion( DWORD processid
)
1547 IMAGE_NT_HEADERS
*nt
;
1549 if (processid
&& processid
!= GetCurrentProcessId())
1551 FIXME("should use ReadProcessMemory\n");
1554 if ((nt
= RtlImageNtHeader( current_process
.module
)))
1555 return ((nt
->OptionalHeader
.MajorSubsystemVersion
<< 16) |
1556 nt
->OptionalHeader
.MinorSubsystemVersion
);
1560 /***********************************************************************
1561 * GetProcessFlags (KERNEL32.@)
1563 DWORD WINAPI
GetProcessFlags( DWORD processid
)
1565 if (processid
&& processid
!= GetCurrentProcessId()) return 0;
1566 return current_process
.flags
;
1570 /***********************************************************************
1571 * SetProcessWorkingSetSize [KERNEL32.@]
1572 * Sets the min/max working set sizes for a specified process.
1575 * hProcess [I] Handle to the process of interest
1576 * minset [I] Specifies minimum working set size
1577 * maxset [I] Specifies maximum working set size
1581 BOOL WINAPI
SetProcessWorkingSetSize(HANDLE hProcess
,DWORD minset
,
1584 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess
,minset
,maxset
);
1585 if(( minset
== (DWORD
)-1) && (maxset
== (DWORD
)-1)) {
1586 /* Trim the working set to zero */
1587 /* Swap the process out of physical RAM */
1592 /***********************************************************************
1593 * GetProcessWorkingSetSize (KERNEL32.@)
1595 BOOL WINAPI
GetProcessWorkingSetSize(HANDLE hProcess
,LPDWORD minset
,
1598 FIXME("(0x%08x,%p,%p): stub\n",hProcess
,minset
,maxset
);
1599 /* 32 MB working set size */
1600 if (minset
) *minset
= 32*1024*1024;
1601 if (maxset
) *maxset
= 32*1024*1024;
1605 /***********************************************************************
1606 * SetProcessShutdownParameters (KERNEL32.@)
1608 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1609 * Now tracks changes made (but does not act on these changes)
1611 static DWORD shutdown_flags
= 0;
1612 static DWORD shutdown_priority
= 0x280;
1614 BOOL WINAPI
SetProcessShutdownParameters(DWORD level
, DWORD flags
)
1616 FIXME("(%08lx, %08lx): partial stub.\n", level
, flags
);
1617 shutdown_flags
= flags
;
1618 shutdown_priority
= level
;
1623 /***********************************************************************
1624 * GetProcessShutdownParameters (KERNEL32.@)
1627 BOOL WINAPI
GetProcessShutdownParameters( LPDWORD lpdwLevel
, LPDWORD lpdwFlags
)
1629 *lpdwLevel
= shutdown_priority
;
1630 *lpdwFlags
= shutdown_flags
;
1635 /***********************************************************************
1636 * SetProcessPriorityBoost (KERNEL32.@)
1638 BOOL WINAPI
SetProcessPriorityBoost(HANDLE hprocess
,BOOL disableboost
)
1640 FIXME("(%d,%d): stub\n",hprocess
,disableboost
);
1641 /* Say we can do it. I doubt the program will notice that we don't. */
1646 /***********************************************************************
1647 * ReadProcessMemory (KERNEL32.@)
1649 BOOL WINAPI
ReadProcessMemory( HANDLE process
, LPCVOID addr
, LPVOID buffer
, DWORD size
,
1650 LPDWORD bytes_read
)
1654 SERVER_START_REQ( read_process_memory
)
1656 req
->handle
= process
;
1657 req
->addr
= (void *)addr
;
1658 wine_server_set_reply( req
, buffer
, size
);
1659 if ((res
= wine_server_call_err( req
))) size
= 0;
1662 if (bytes_read
) *bytes_read
= size
;
1667 /***********************************************************************
1668 * WriteProcessMemory (KERNEL32.@)
1670 BOOL WINAPI
WriteProcessMemory( HANDLE process
, LPVOID addr
, LPCVOID buffer
, DWORD size
,
1671 LPDWORD bytes_written
)
1673 static const int zero
;
1674 unsigned int first_offset
, last_offset
, first_mask
, last_mask
;
1679 SetLastError( ERROR_INVALID_PARAMETER
);
1683 /* compute the mask for the first int */
1685 first_offset
= (unsigned int)addr
% sizeof(int);
1686 memset( &first_mask
, 0, first_offset
);
1688 /* compute the mask for the last int */
1689 last_offset
= (size
+ first_offset
) % sizeof(int);
1691 memset( &last_mask
, 0xff, last_offset
? last_offset
: sizeof(int) );
1693 SERVER_START_REQ( write_process_memory
)
1695 req
->handle
= process
;
1696 req
->addr
= (char *)addr
- first_offset
;
1697 req
->first_mask
= first_mask
;
1698 req
->last_mask
= last_mask
;
1699 if (first_offset
) wine_server_add_data( req
, &zero
, first_offset
);
1700 wine_server_add_data( req
, buffer
, size
);
1701 if (last_offset
) wine_server_add_data( req
, &zero
, sizeof(int) - last_offset
);
1703 if ((res
= wine_server_call_err( req
))) size
= 0;
1706 if (bytes_written
) *bytes_written
= size
;
1710 ReadProcessMemory( process
, addr
, dummy
, sizeof(dummy
), &read
);
1716 /***********************************************************************
1717 * RegisterServiceProcess (KERNEL.491)
1718 * RegisterServiceProcess (KERNEL32.@)
1720 * A service process calls this function to ensure that it continues to run
1721 * even after a user logged off.
1723 DWORD WINAPI
RegisterServiceProcess(DWORD dwProcessId
, DWORD dwType
)
1725 /* I don't think that Wine needs to do anything in that function */
1726 return 1; /* success */
1729 /***********************************************************************
1730 * GetExitCodeProcess [KERNEL32.@]
1732 * Gets termination status of specified process
1738 BOOL WINAPI
GetExitCodeProcess(
1739 HANDLE hProcess
, /* [in] handle to the process */
1740 LPDWORD lpExitCode
) /* [out] address to receive termination status */
1743 SERVER_START_REQ( get_process_info
)
1745 req
->handle
= hProcess
;
1746 ret
= !wine_server_call_err( req
);
1747 if (ret
&& lpExitCode
) *lpExitCode
= reply
->exit_code
;
1754 /***********************************************************************
1755 * SetErrorMode (KERNEL32.@)
1757 UINT WINAPI
SetErrorMode( UINT mode
)
1759 UINT old
= current_process
.error_mode
;
1760 current_process
.error_mode
= mode
;
1765 /**************************************************************************
1766 * SetFileApisToOEM (KERNEL32.@)
1768 VOID WINAPI
SetFileApisToOEM(void)
1770 current_process
.flags
|= PDB32_FILE_APIS_OEM
;
1774 /**************************************************************************
1775 * SetFileApisToANSI (KERNEL32.@)
1777 VOID WINAPI
SetFileApisToANSI(void)
1779 current_process
.flags
&= ~PDB32_FILE_APIS_OEM
;
1783 /******************************************************************************
1784 * AreFileApisANSI [KERNEL32.@] Determines if file functions are using ANSI
1787 * TRUE: Set of file functions is using ANSI code page
1788 * FALSE: Set of file functions is using OEM code page
1790 BOOL WINAPI
AreFileApisANSI(void)
1792 return !(current_process
.flags
& PDB32_FILE_APIS_OEM
);
1796 /***********************************************************************
1797 * GetTickCount (KERNEL32.@)
1799 * Returns the number of milliseconds, modulo 2^32, since the start
1800 * of the wineserver.
1802 DWORD WINAPI
GetTickCount(void)
1805 gettimeofday( &t
, NULL
);
1806 return ((t
.tv_sec
* 1000) + (t
.tv_usec
/ 1000)) - server_startticks
;
1810 /**********************************************************************
1811 * TlsAlloc [KERNEL32.@] Allocates a TLS index.
1813 * Allocates a thread local storage index
1816 * Success: TLS Index
1817 * Failure: 0xFFFFFFFF
1819 DWORD WINAPI
TlsAlloc( void )
1821 DWORD i
, mask
, ret
= 0;
1822 DWORD
*bits
= current_process
.tls_bits
;
1823 RtlAcquirePebLock();
1824 if (*bits
== 0xffffffff)
1828 if (*bits
== 0xffffffff)
1830 RtlReleasePebLock();
1831 SetLastError( ERROR_NO_MORE_ITEMS
);
1835 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) if (!(*bits
& mask
)) break;
1837 RtlReleasePebLock();
1838 NtCurrentTeb()->tls_array
[ret
+i
] = 0; /* clear the value */
1843 /**********************************************************************
1844 * TlsFree [KERNEL32.@] Releases a TLS index.
1846 * Releases a thread local storage index, making it available for reuse
1852 BOOL WINAPI
TlsFree(
1853 DWORD index
) /* [in] TLS Index to free */
1855 DWORD mask
= (1 << (index
& 31));
1856 DWORD
*bits
= current_process
.tls_bits
;
1859 SetLastError( ERROR_INVALID_PARAMETER
);
1862 if (index
>= 32) bits
++;
1863 RtlAcquirePebLock();
1864 if (!(*bits
& mask
)) /* already free? */
1866 RtlReleasePebLock();
1867 SetLastError( ERROR_INVALID_PARAMETER
);
1871 NtCurrentTeb()->tls_array
[index
] = 0;
1872 /* FIXME: should zero all other thread values */
1873 RtlReleasePebLock();
1878 /**********************************************************************
1879 * TlsGetValue [KERNEL32.@] Gets value in a thread's TLS slot
1882 * Success: Value stored in calling thread's TLS slot for index
1883 * Failure: 0 and GetLastError returns NO_ERROR
1885 LPVOID WINAPI
TlsGetValue(
1886 DWORD index
) /* [in] TLS index to retrieve value for */
1890 SetLastError( ERROR_INVALID_PARAMETER
);
1893 SetLastError( ERROR_SUCCESS
);
1894 return NtCurrentTeb()->tls_array
[index
];
1898 /**********************************************************************
1899 * TlsSetValue [KERNEL32.@] Stores a value in the thread's TLS slot.
1905 BOOL WINAPI
TlsSetValue(
1906 DWORD index
, /* [in] TLS index to set value for */
1907 LPVOID value
) /* [in] Value to be stored */
1911 SetLastError( ERROR_INVALID_PARAMETER
);
1914 NtCurrentTeb()->tls_array
[index
] = value
;
1919 /***********************************************************************
1920 * GetCurrentProcess (KERNEL32.@)
1922 #undef GetCurrentProcess
1923 HANDLE WINAPI
GetCurrentProcess(void)
1925 return (HANDLE
)0xffffffff;