4 * Copyright 1996, 1998 Alexandre Julliard
15 #include "wine/winbase16.h"
16 #include "wine/exception.h"
33 #include "debugtools.h"
35 DEFAULT_DEBUG_CHANNEL(process
);
36 DECLARE_DEBUG_CHANNEL(relay
);
37 DECLARE_DEBUG_CHANNEL(win32
);
40 static ENVDB initial_envdb
;
41 static STARTUPINFOA initial_startup
;
42 static char **main_exe_argv
;
43 static char *main_exe_name
;
44 static HFILE main_exe_file
= -1;
47 /***********************************************************************
50 * Convert a process id to a PDB, making sure it is valid.
52 PDB
*PROCESS_IdToPDB( DWORD pid
)
54 if (!pid
|| pid
== GetCurrentProcessId()) return PROCESS_Current();
59 /***********************************************************************
60 * PROCESS_CallUserSignalProc
62 * FIXME: Some of the signals aren't sent correctly!
64 * The exact meaning of the USER signals is undocumented, but this
65 * should cover the basic idea:
67 * USIG_DLL_UNLOAD_WIN16
68 * This is sent when a 16-bit module is unloaded.
70 * USIG_DLL_UNLOAD_WIN32
71 * This is sent when a 32-bit module is unloaded.
73 * USIG_DLL_UNLOAD_ORPHANS
74 * This is sent after the last Win3.1 module is unloaded,
75 * to allow removal of orphaned menus.
77 * USIG_FAULT_DIALOG_PUSH
78 * USIG_FAULT_DIALOG_POP
79 * These are called to allow USER to prepare for displaying a
80 * fault dialog, even though the fault might have happened while
81 * inside a USER critical section.
84 * This is called from the context of a new thread, as soon as it
88 * This is called, still in its context, just before a thread is
92 * This is called, in the parent process context, after a new process
96 * This is called in the new process context, just after the main thread
97 * has started execution (after the main thread's USIG_THREAD_INIT has
100 * USIG_PROCESS_LOADED
101 * This is called after the executable file has been loaded into the
102 * new process context.
104 * USIG_PROCESS_RUNNING
105 * This is called immediately before the main entry point is called.
108 * This is called in the context of a process that is about to
109 * terminate (but before the last thread's USIG_THREAD_EXIT has
112 * USIG_PROCESS_DESTROY
113 * This is called after a process has terminated.
116 * The meaning of the dwFlags bits is as follows:
119 * Current process is 32-bit.
122 * Current process is a (Win32) GUI process.
124 * USIG_FLAGS_FEEDBACK
125 * Current process needs 'feedback' (determined from the STARTUPINFO
126 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
129 * The signal is being sent due to a fault.
131 void PROCESS_CallUserSignalProc( UINT uCode
, HMODULE hModule
)
133 DWORD flags
= PROCESS_Current()->flags
;
134 DWORD startup_flags
= PROCESS_Current()->env_db
->startup_info
->dwFlags
;
137 /* Determine dwFlags */
139 if ( !(flags
& PDB32_WIN16_PROC
) ) dwFlags
|= USIG_FLAGS_WIN32
;
141 if ( !(flags
& PDB32_CONSOLE_PROC
) ) dwFlags
|= USIG_FLAGS_GUI
;
143 if ( dwFlags
& USIG_FLAGS_GUI
)
145 /* Feedback defaults to ON */
146 if ( !(startup_flags
& STARTF_FORCEOFFFEEDBACK
) )
147 dwFlags
|= USIG_FLAGS_FEEDBACK
;
151 /* Feedback defaults to OFF */
152 if (startup_flags
& STARTF_FORCEONFEEDBACK
)
153 dwFlags
|= USIG_FLAGS_FEEDBACK
;
156 /* Convert module handle to 16-bit */
158 if ( HIWORD( hModule
) )
159 hModule
= MapHModuleLS( hModule
);
161 /* Call USER signal proc */
163 if ( Callout
.UserSignalProc
)
165 if ( uCode
== USIG_THREAD_INIT
|| uCode
== USIG_THREAD_EXIT
)
166 Callout
.UserSignalProc( uCode
, GetCurrentThreadId(), dwFlags
, hModule
);
168 Callout
.UserSignalProc( uCode
, GetCurrentProcessId(), dwFlags
, hModule
);
173 /***********************************************************************
176 * Main process initialisation code
178 static BOOL
process_init( char *argv
[] )
180 struct init_process_request
*req
;
181 PDB
*pdb
= PROCESS_Current();
183 /* store the program name */
186 /* Fill the initial process structure */
187 pdb
->exit_code
= STILL_ACTIVE
;
189 pdb
->running_threads
= 1;
190 pdb
->ring0_threads
= 1;
191 pdb
->env_db
= &initial_envdb
;
193 pdb
->priority
= 8; /* Normal */
194 pdb
->winver
= 0xffff; /* to be determined */
195 initial_envdb
.startup_info
= &initial_startup
;
197 /* Setup the server connection */
198 NtCurrentTeb()->socket
= CLIENT_InitServer();
199 if (CLIENT_InitThread()) return FALSE
;
201 /* Retrieve startup info from the server */
202 req
= get_req_buffer();
203 req
->ldt_copy
= ldt_copy
;
204 req
->ldt_flags
= ldt_flags_copy
;
205 req
->ppid
= getppid();
206 if (server_call( REQ_INIT_PROCESS
)) return FALSE
;
207 main_exe_file
= req
->exe_file
;
208 if (req
->filename
[0]) main_exe_name
= strdup( req
->filename
);
209 initial_startup
.dwFlags
= req
->start_flags
;
210 initial_startup
.wShowWindow
= req
->cmd_show
;
211 initial_envdb
.hStdin
= initial_startup
.hStdInput
= req
->hstdin
;
212 initial_envdb
.hStdout
= initial_startup
.hStdOutput
= req
->hstdout
;
213 initial_envdb
.hStderr
= initial_startup
.hStdError
= req
->hstderr
;
215 /* Remember TEB selector of initial process for emergency use */
216 SYSLEVEL_EmergencyTeb
= NtCurrentTeb()->teb_sel
;
218 /* Create the system and process heaps */
219 if (!HEAP_CreateSystemHeap()) return FALSE
;
220 pdb
->heap
= HeapCreate( HEAP_GROWABLE
, 0, 0 );
222 /* Copy the parent environment */
223 if (!ENV_BuildEnvironment()) return FALSE
;
225 /* Create the SEGPTR heap */
226 if (!(SegptrHeap
= HeapCreate( HEAP_WINE_SEGPTR
, 0, 0 ))) return FALSE
;
228 /* Initialize the critical sections */
229 InitializeCriticalSection( &pdb
->crit_section
);
230 InitializeCriticalSection( &initial_envdb
.section
);
232 /* Initialize syslevel handling */
235 /* Parse command line arguments */
236 OPTIONS_ParseOptions( argv
);
238 return MAIN_MainInit();
242 /***********************************************************************
245 * Load system DLLs into the initial process (and initialize them)
247 static int load_system_dlls(void)
249 /* Get pointers to USER routines called by KERNEL */
252 /* Call FinalUserInit routine */
253 Callout
.FinalUserInit16();
255 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
256 * context of the parent process. Actually, the USER signal proc
257 * doesn't really care about that, but it *does* require that the
258 * startup parameters are correctly set up, so that GetProcessDword
259 * works. Furthermore, before calling the USER signal proc the
260 * 16-bit stack must be set up, which it is only after TASK_Create
261 * in the case of a 16-bit process. Thus, we send the signal here.
263 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE
, 0 );
264 PROCESS_CallUserSignalProc( USIG_THREAD_INIT
, 0 );
265 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT
, 0 );
266 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED
, 0 );
272 /***********************************************************************
275 * Build the command-line of a process from the argv array.
277 static inline char *build_command_line( char **argv
)
280 char *cmdline
, *p
, **arg
;
282 for (arg
= argv
, len
= 0; *arg
; arg
++) len
+= strlen(*arg
) + 1;
283 if ((quote
= (strchr( argv
[0], ' ' ) != NULL
))) len
+= 2;
284 if (!(p
= cmdline
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
302 if (p
> cmdline
) p
--; /* remove last space */
308 /***********************************************************************
311 * Startup routine of a new process. Runs on the new process stack.
313 static void start_process(void)
315 struct init_process_done_request
*req
= get_req_buffer();
316 int debugged
, console_app
;
317 UINT cmdShow
= SW_SHOWNORMAL
;
318 LPTHREAD_START_ROUTINE entry
;
319 PDB
*pdb
= PROCESS_Current();
320 HMODULE module
= pdb
->exe_modref
->module
;
322 /* Increment EXE refcount */
323 pdb
->exe_modref
->refCount
++;
325 /* build command line */
326 if (!(pdb
->env_db
->cmd_line
= build_command_line( main_exe_argv
))) goto error
;
328 /* Retrieve entry point address */
329 entry
= (LPTHREAD_START_ROUTINE
)((char*)module
+ PE_HEADER(module
)->OptionalHeader
.AddressOfEntryPoint
);
330 console_app
= (PE_HEADER(module
)->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
);
332 if (console_app
) pdb
->flags
|= PDB32_CONSOLE_PROC
;
334 /* Signal the parent process to continue */
335 req
->module
= (void *)module
;
337 req
->name
= &pdb
->exe_modref
->filename
;
338 req
->gui
= !console_app
;
339 server_call( REQ_INIT_PROCESS_DONE
);
340 debugged
= req
->debugged
;
342 /* Install signal handlers; this cannot be done before, since we cannot
343 * send exceptions to the debugger before the create process event that
344 * is sent by REQ_INIT_PROCESS_DONE */
345 if (!SIGNAL_Init()) goto error
;
347 /* Load KERNEL (necessary for TASK_Create) */
348 if (!LoadLibraryA( "KERNEL32" )) goto error
;
350 /* Create 16-bit task */
351 if (pdb
->env_db
->startup_info
->dwFlags
& STARTF_USESHOWWINDOW
)
352 cmdShow
= pdb
->env_db
->startup_info
->wShowWindow
;
353 if (!TASK_Create( (NE_MODULE
*)GlobalLock16( MapHModuleLS(module
) ), cmdShow
,
354 NtCurrentTeb(), NULL
, 0 ))
357 /* Load the system dlls */
358 if (!load_system_dlls()) goto error
;
360 EnterCriticalSection( &pdb
->crit_section
);
362 MODULE_DllProcessAttach( pdb
->exe_modref
, (LPVOID
)1 );
363 LeaveCriticalSection( &pdb
->crit_section
);
365 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
366 if (console_app
) PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING
, 0 );
368 TRACE_(relay
)( "Starting Win32 process (entryproc=%p)\n", entry
);
369 if (debugged
) DbgBreakPoint();
370 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
371 * Dunno about other OSs */
372 ExitThread( entry(NULL
) );
375 ExitProcess( GetLastError() );
379 /***********************************************************************
382 * Startup routine of a new Win32 process once the main module has been loaded.
383 * The filename is free'd by this routine.
385 static void PROCESS_Start( HMODULE main_module
, HFILE hFile
, LPSTR filename
) WINE_NORETURN
;
386 static void PROCESS_Start( HMODULE main_module
, HFILE hFile
, LPSTR filename
)
390 /* if no explicit filename, use argv[0] */
391 if (!(filename
= malloc( MAX_PATH
))) ExitProcess(1);
392 if (!GetLongPathNameA( full_argv0
, filename
, MAX_PATH
))
393 lstrcpynA( filename
, full_argv0
, MAX_PATH
);
396 /* load main module */
397 if (PE_HEADER(main_module
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
398 ExitProcess( ERROR_BAD_EXE_FORMAT
);
400 /* Create 32-bit MODREF */
401 if (!PE_CreateModule( main_module
, filename
, 0, hFile
, FALSE
))
405 /* allocate main thread stack */
406 if (!THREAD_InitStack( NtCurrentTeb(),
407 PE_HEADER(main_module
)->OptionalHeader
.SizeOfStackReserve
, TRUE
))
410 /* switch to the new stack */
411 SYSDEPS_SwitchToThreadStack( start_process
);
414 ExitProcess( GetLastError() );
418 /***********************************************************************
421 * Wine initialisation: load and start the main exe file.
423 void PROCESS_InitWine( int argc
, char *argv
[] )
427 /* Initialize everything */
428 if (!process_init( argv
)) exit(1);
430 main_exe_argv
= ++argv
; /* remove argv[0] (wine itself) */
434 char buffer
[MAX_PATH
];
435 if (!argv
[0]) OPTIONS_Usage();
437 /* open the exe file */
438 if (!SearchPathA( NULL
, argv
[0], ".exe", sizeof(buffer
), buffer
, NULL
) &&
439 !SearchPathA( NULL
, argv
[0], NULL
, sizeof(buffer
), buffer
, NULL
))
441 MESSAGE( "%s: cannot find '%s'\n", argv0
, argv
[0] );
444 if (!(main_exe_name
= strdup(buffer
)))
446 MESSAGE( "%s: out of memory\n", argv0
);
451 if (main_exe_file
== INVALID_HANDLE_VALUE
)
453 if ((main_exe_file
= CreateFileA( main_exe_name
, GENERIC_READ
, FILE_SHARE_READ
,
454 NULL
, OPEN_EXISTING
, 0, -1 )) == INVALID_HANDLE_VALUE
)
456 MESSAGE( "%s: cannot open '%s'\n", argv0
, main_exe_name
);
461 if (!MODULE_GetBinaryType( main_exe_file
, main_exe_name
, &type
))
463 MESSAGE( "%s: unrecognized executable '%s'\n", argv0
, main_exe_name
);
469 case SCS_32BIT_BINARY
:
471 HMODULE main_module
= PE_LoadImage( main_exe_file
, main_exe_name
, 0 );
472 if (main_module
) PROCESS_Start( main_module
, main_exe_file
, main_exe_name
);
479 /* create 32-bit module for main exe */
480 if (!(main_module
= BUILTIN32_LoadExeModule())) goto error
;
481 NtCurrentTeb()->tibflags
&= ~TEBF_WIN32
;
482 PROCESS_Current()->flags
|= PDB32_WIN16_PROC
;
483 SYSLEVEL_EnterWin16Lock();
484 PROCESS_Start( main_module
, -1, NULL
);
491 /* create 32-bit module for main exe */
492 if (!(main_module
= BUILTIN32_LoadExeModule())) goto error
;
493 NtCurrentTeb()->tibflags
&= ~TEBF_WIN32
;
494 if (!MZ_LoadImage( main_module
, main_exe_file
, main_exe_name
)) goto error
;
495 PROCESS_Start( main_module
, main_exe_file
, NULL
);
500 case SCS_POSIX_BINARY
:
501 case SCS_OS216_BINARY
:
503 MESSAGE( "%s: unrecognized executable '%s'\n", argv0
, main_exe_name
);
504 SetLastError( ERROR_BAD_FORMAT
);
508 ExitProcess( GetLastError() );
512 /***********************************************************************
513 * PROCESS_InitWinelib
515 * Initialisation of a new Winelib process.
517 void PROCESS_InitWinelib( int argc
, char *argv
[] )
521 if (!process_init( argv
)) exit(1);
523 /* create 32-bit module for main exe */
524 if (!(main_module
= BUILTIN32_LoadExeModule())) ExitProcess( GetLastError() );
526 main_exe_argv
= argv
;
527 PROCESS_Start( main_module
, -1, NULL
);
531 /***********************************************************************
534 * Build an argv array from a command-line.
535 * The command-line is modified to insert nulls.
536 * 'reserved' is the number of args to reserve before the first one.
538 static char **build_argv( char *cmdline
, int reserved
)
541 int count
= reserved
+ 1;
544 /* if first word is quoted store it as a single arg */
545 if (*cmdline
== '\"')
547 if ((p
= strchr( cmdline
+ 1, '\"' )))
556 while (*p
&& isspace(*p
)) p
++;
559 while (*p
&& !isspace(*p
)) p
++;
562 if ((argv
= malloc( count
* sizeof(*argv
) )))
564 char **argvptr
= argv
+ reserved
;
566 if (*cmdline
== '\"')
568 if ((p
= strchr( cmdline
+ 1, '\"' )))
570 *argvptr
++ = cmdline
+ 1;
577 while (*p
&& isspace(*p
)) *p
++ = 0;
580 while (*p
&& !isspace(*p
)) p
++;
588 /***********************************************************************
591 * Build the environment of a new child process.
593 static char **build_envp( const char *env
)
599 for (p
= env
, count
= 0; *p
; count
++) p
+= strlen(p
) + 1;
601 if ((envp
= malloc( count
* sizeof(*envp
) )))
603 extern char **environ
;
604 char **envptr
= envp
;
605 char **unixptr
= environ
;
606 /* first put PATH, HOME and WINEPREFIX from the unix env */
607 for (unixptr
= environ
; unixptr
&& *unixptr
; unixptr
++)
608 if (!memcmp( *unixptr
, "PATH=", 5 ) ||
609 !memcmp( *unixptr
, "HOME=", 5 ) ||
610 !memcmp( *unixptr
, "WINEPREFIX=", 11 )) *envptr
++ = *unixptr
;
611 /* now put the Windows environment strings */
612 for (p
= env
; *p
; p
+= strlen(p
) + 1)
614 if (memcmp( p
, "PATH=", 5 ) &&
615 memcmp( p
, "HOME=", 5 ) &&
616 memcmp( p
, "WINEPREFIX=", 11 )) *envptr
++ = (char *)p
;
624 /***********************************************************************
627 * Locate the Wine binary to exec for a new Win32 process.
629 static void exec_wine_binary( char **argv
, char **envp
)
631 const char *path
, *pos
, *ptr
;
633 /* first try bin directory */
634 argv
[0] = BINDIR
"/wine";
635 execve( argv
[0], argv
, envp
);
637 /* now try the path of argv0 of the current binary */
638 if (!(argv
[0] = malloc( strlen(full_argv0
) + 6 ))) return;
639 if ((ptr
= strrchr( full_argv0
, '/' )))
641 memcpy( argv
[0], full_argv0
, ptr
- full_argv0
);
642 strcpy( argv
[0] + (ptr
- full_argv0
), "/wine" );
643 execve( argv
[0], argv
, envp
);
647 /* now search in the Unix path */
648 if ((path
= getenv( "PATH" )))
650 if (!(argv
[0] = malloc( strlen(path
) + 6 ))) return;
654 while (*pos
== ':') pos
++;
656 if (!(ptr
= strchr( pos
, ':' ))) ptr
= pos
+ strlen(pos
);
657 memcpy( argv
[0], pos
, ptr
- pos
);
658 strcpy( argv
[0] + (ptr
- pos
), "/wine" );
659 execve( argv
[0], argv
, envp
);
665 /* finally try the current directory */
667 execve( argv
[0], argv
, envp
);
671 /***********************************************************************
674 * Fork and exec a new Unix process, checking for errors.
676 static int fork_and_exec( const char *filename
, char *cmdline
,
677 const char *env
, const char *newdir
)
687 fcntl( fd
[1], F_SETFD
, 1 ); /* set close on exec */
688 if (!(pid
= fork())) /* child */
690 char **argv
= build_argv( cmdline
, filename
? 0 : 2 );
691 char **envp
= build_envp( env
);
694 if (newdir
) chdir(newdir
);
701 exec_wine_binary( argv
, envp
);
703 else execve( filename
, argv
, envp
);
706 write( fd
[1], &err
, sizeof(err
) );
710 if ((pid
!= -1) && (read( fd
[0], &err
, sizeof(err
) ) > 0)) /* exec failed */
715 if (pid
== -1) FILE_SetDosError();
721 /***********************************************************************
724 * Create a new process. If hFile is a valid handle we have an exe
725 * file, and we exec a new copy of wine to load it; otherwise we
726 * simply exec the specified filename as a Unix process.
728 BOOL
PROCESS_Create( HFILE hFile
, LPCSTR filename
, LPSTR cmd_line
, LPCSTR env
,
729 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
730 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
731 LPPROCESS_INFORMATION info
, LPCSTR lpCurrentDirectory
)
734 const char *unixfilename
= NULL
;
735 const char *unixdir
= NULL
;
736 DOS_FULL_NAME full_name
;
737 HANDLE load_done_evt
= -1;
738 struct new_process_request
*req
= get_req_buffer();
739 struct wait_process_request
*wait_req
= get_req_buffer();
741 info
->hThread
= info
->hProcess
= INVALID_HANDLE_VALUE
;
743 /* create the process on the server side */
745 req
->inherit_all
= inherit
;
746 req
->create_flags
= flags
;
747 req
->start_flags
= startup
->dwFlags
;
748 req
->exe_file
= hFile
;
749 if (startup
->dwFlags
& STARTF_USESTDHANDLES
)
751 req
->hstdin
= startup
->hStdInput
;
752 req
->hstdout
= startup
->hStdOutput
;
753 req
->hstderr
= startup
->hStdError
;
757 req
->hstdin
= GetStdHandle( STD_INPUT_HANDLE
);
758 req
->hstdout
= GetStdHandle( STD_OUTPUT_HANDLE
);
759 req
->hstderr
= GetStdHandle( STD_ERROR_HANDLE
);
761 req
->cmd_show
= startup
->wShowWindow
;
764 if (lpCurrentDirectory
) {
765 if (DOSFS_GetFullName( lpCurrentDirectory
, TRUE
, &full_name
))
766 unixdir
= full_name
.long_name
;
769 if (GetCurrentDirectoryA(sizeof(buf
),buf
)) {
770 if (DOSFS_GetFullName( buf
, TRUE
, &full_name
))
771 unixdir
= full_name
.long_name
;
775 if (hFile
== -1) /* unix process */
777 unixfilename
= filename
;
778 if (DOSFS_GetFullName( filename
, TRUE
, &full_name
)) unixfilename
= full_name
.long_name
;
779 req
->filename
[0] = 0;
781 else /* new wine process */
783 if (!GetLongPathNameA( filename
, req
->filename
, server_remaining(req
->filename
) ))
784 lstrcpynA( req
->filename
, filename
, server_remaining(req
->filename
) );
786 if (server_call( REQ_NEW_PROCESS
)) return FALSE
;
788 /* fork and execute */
790 pid
= fork_and_exec( unixfilename
, cmd_line
, env
? env
: GetEnvironmentStringsA(), unixdir
);
792 wait_req
->cancel
= (pid
== -1);
793 wait_req
->pinherit
= (psa
&& (psa
->nLength
>= sizeof(*psa
)) && psa
->bInheritHandle
);
794 wait_req
->tinherit
= (tsa
&& (tsa
->nLength
>= sizeof(*tsa
)) && tsa
->bInheritHandle
);
795 wait_req
->timeout
= 2000;
796 if (server_call( REQ_WAIT_PROCESS
) || (pid
== -1)) goto error
;
797 info
->dwProcessId
= (DWORD
)wait_req
->pid
;
798 info
->dwThreadId
= (DWORD
)wait_req
->tid
;
799 info
->hProcess
= wait_req
->phandle
;
800 info
->hThread
= wait_req
->thandle
;
801 load_done_evt
= wait_req
->event
;
803 /* Wait until process is initialized (or initialization failed) */
804 if (load_done_evt
!= -1)
809 handles
[0] = info
->hProcess
;
810 handles
[1] = load_done_evt
;
811 res
= WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
);
812 CloseHandle( load_done_evt
);
813 if (res
== STATUS_WAIT_0
) /* the process died */
816 if (GetExitCodeProcess( info
->hProcess
, &exitcode
)) SetLastError( exitcode
);
817 CloseHandle( info
->hThread
);
818 CloseHandle( info
->hProcess
);
825 if (load_done_evt
!= -1) CloseHandle( load_done_evt
);
826 if (info
->hThread
!= INVALID_HANDLE_VALUE
) CloseHandle( info
->hThread
);
827 if (info
->hProcess
!= INVALID_HANDLE_VALUE
) CloseHandle( info
->hProcess
);
832 /***********************************************************************
833 * ExitProcess (KERNEL32.100)
835 void WINAPI
ExitProcess( DWORD status
)
837 struct terminate_process_request
*req
= get_req_buffer();
839 MODULE_DllProcessDetach( TRUE
, (LPVOID
)1 );
840 /* send the exit code to the server */
841 req
->handle
= GetCurrentProcess();
842 req
->exit_code
= status
;
843 server_call( REQ_TERMINATE_PROCESS
);
847 /***********************************************************************
848 * ExitProcess16 (KERNEL.466)
850 void WINAPI
ExitProcess16( WORD status
)
852 SYSLEVEL_ReleaseWin16Lock();
853 ExitProcess( status
);
856 /******************************************************************************
857 * TerminateProcess (KERNEL32.684)
859 BOOL WINAPI
TerminateProcess( HANDLE handle
, DWORD exit_code
)
862 struct terminate_process_request
*req
= get_req_buffer();
863 req
->handle
= handle
;
864 req
->exit_code
= exit_code
;
865 if ((ret
= !server_call( REQ_TERMINATE_PROCESS
)) && req
->self
) exit( exit_code
);
870 /***********************************************************************
871 * GetProcessDword (KERNEL32.18) (KERNEL.485)
872 * 'Of course you cannot directly access Windows internal structures'
874 DWORD WINAPI
GetProcessDword( DWORD dwProcessID
, INT offset
)
876 PDB
*process
= PROCESS_IdToPDB( dwProcessID
);
880 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
883 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
889 case GPD_APP_COMPAT_FLAGS
:
890 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
891 return pTask
? pTask
->compat_flags
: 0;
893 case GPD_LOAD_DONE_EVENT
:
894 return process
->load_done_evt
;
896 case GPD_HINSTANCE16
:
897 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
898 return pTask
? pTask
->hInstance
: 0;
900 case GPD_WINDOWS_VERSION
:
901 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
902 return pTask
? pTask
->version
: 0;
905 if ( process
!= PROCESS_Current() ) return 0;
906 return (DWORD
)NtCurrentTeb() - 0x10 /* FIXME */;
909 return (DWORD
)process
;
911 case GPD_STARTF_SHELLDATA
: /* return stdoutput handle from startupinfo ??? */
912 return process
->env_db
->startup_info
->hStdOutput
;
914 case GPD_STARTF_HOTKEY
: /* return stdinput handle from startupinfo ??? */
915 return process
->env_db
->startup_info
->hStdInput
;
917 case GPD_STARTF_SHOWWINDOW
:
918 return process
->env_db
->startup_info
->wShowWindow
;
920 case GPD_STARTF_SIZE
:
921 x
= process
->env_db
->startup_info
->dwXSize
;
922 if ( x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
923 y
= process
->env_db
->startup_info
->dwYSize
;
924 if ( y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
925 return MAKELONG( x
, y
);
927 case GPD_STARTF_POSITION
:
928 x
= process
->env_db
->startup_info
->dwX
;
929 if ( x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
930 y
= process
->env_db
->startup_info
->dwY
;
931 if ( y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
932 return MAKELONG( x
, y
);
934 case GPD_STARTF_FLAGS
:
935 return process
->env_db
->startup_info
->dwFlags
;
941 return process
->flags
;
944 return process
->process_dword
;
947 ERR_(win32
)("Unknown offset %d\n", offset
);
952 /***********************************************************************
953 * SetProcessDword (KERNEL.484)
954 * 'Of course you cannot directly access Windows internal structures'
956 void WINAPI
SetProcessDword( DWORD dwProcessID
, INT offset
, DWORD value
)
958 PDB
*process
= PROCESS_IdToPDB( dwProcessID
);
960 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
963 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
969 case GPD_APP_COMPAT_FLAGS
:
970 case GPD_LOAD_DONE_EVENT
:
971 case GPD_HINSTANCE16
:
972 case GPD_WINDOWS_VERSION
:
975 case GPD_STARTF_SHELLDATA
:
976 case GPD_STARTF_HOTKEY
:
977 case GPD_STARTF_SHOWWINDOW
:
978 case GPD_STARTF_SIZE
:
979 case GPD_STARTF_POSITION
:
980 case GPD_STARTF_FLAGS
:
983 ERR_(win32
)("Not allowed to modify offset %d\n", offset
);
987 process
->process_dword
= value
;
991 ERR_(win32
)("Unknown offset %d\n", offset
);
997 /*********************************************************************
998 * OpenProcess (KERNEL32.543)
1000 HANDLE WINAPI
OpenProcess( DWORD access
, BOOL inherit
, DWORD id
)
1003 struct open_process_request
*req
= get_req_buffer();
1005 req
->pid
= (void *)id
;
1006 req
->access
= access
;
1007 req
->inherit
= inherit
;
1008 if (!server_call( REQ_OPEN_PROCESS
)) ret
= req
->handle
;
1012 /*********************************************************************
1013 * MapProcessHandle (KERNEL.483)
1015 DWORD WINAPI
MapProcessHandle( HANDLE handle
)
1018 struct get_process_info_request
*req
= get_req_buffer();
1019 req
->handle
= handle
;
1020 if (!server_call( REQ_GET_PROCESS_INFO
)) ret
= (DWORD
)req
->pid
;
1024 /***********************************************************************
1025 * SetPriorityClass (KERNEL32.503)
1027 BOOL WINAPI
SetPriorityClass( HANDLE hprocess
, DWORD priorityclass
)
1029 struct set_process_info_request
*req
= get_req_buffer();
1030 req
->handle
= hprocess
;
1031 req
->priority
= priorityclass
;
1032 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
1033 return !server_call( REQ_SET_PROCESS_INFO
);
1037 /***********************************************************************
1038 * GetPriorityClass (KERNEL32.250)
1040 DWORD WINAPI
GetPriorityClass(HANDLE hprocess
)
1043 struct get_process_info_request
*req
= get_req_buffer();
1044 req
->handle
= hprocess
;
1045 if (!server_call( REQ_GET_PROCESS_INFO
)) ret
= req
->priority
;
1050 /***********************************************************************
1051 * SetProcessAffinityMask (KERNEL32.662)
1053 BOOL WINAPI
SetProcessAffinityMask( HANDLE hProcess
, DWORD affmask
)
1055 struct set_process_info_request
*req
= get_req_buffer();
1056 req
->handle
= hProcess
;
1057 req
->affinity
= affmask
;
1058 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
1059 return !server_call( REQ_SET_PROCESS_INFO
);
1062 /**********************************************************************
1063 * GetProcessAffinityMask (KERNEL32.373)
1065 BOOL WINAPI
GetProcessAffinityMask( HANDLE hProcess
,
1066 LPDWORD lpProcessAffinityMask
,
1067 LPDWORD lpSystemAffinityMask
)
1070 struct get_process_info_request
*req
= get_req_buffer();
1071 req
->handle
= hProcess
;
1072 if (!server_call( REQ_GET_PROCESS_INFO
))
1074 if (lpProcessAffinityMask
) *lpProcessAffinityMask
= req
->process_affinity
;
1075 if (lpSystemAffinityMask
) *lpSystemAffinityMask
= req
->system_affinity
;
1082 /***********************************************************************
1083 * GetStdHandle (KERNEL32.276)
1085 HANDLE WINAPI
GetStdHandle( DWORD std_handle
)
1087 PDB
*pdb
= PROCESS_Current();
1091 case STD_INPUT_HANDLE
: return pdb
->env_db
->hStdin
;
1092 case STD_OUTPUT_HANDLE
: return pdb
->env_db
->hStdout
;
1093 case STD_ERROR_HANDLE
: return pdb
->env_db
->hStderr
;
1095 SetLastError( ERROR_INVALID_PARAMETER
);
1096 return INVALID_HANDLE_VALUE
;
1100 /***********************************************************************
1101 * SetStdHandle (KERNEL32.506)
1103 BOOL WINAPI
SetStdHandle( DWORD std_handle
, HANDLE handle
)
1105 PDB
*pdb
= PROCESS_Current();
1106 /* FIXME: should we close the previous handle? */
1109 case STD_INPUT_HANDLE
:
1110 pdb
->env_db
->hStdin
= handle
;
1112 case STD_OUTPUT_HANDLE
:
1113 pdb
->env_db
->hStdout
= handle
;
1115 case STD_ERROR_HANDLE
:
1116 pdb
->env_db
->hStderr
= handle
;
1119 SetLastError( ERROR_INVALID_PARAMETER
);
1123 /***********************************************************************
1124 * GetProcessVersion (KERNEL32)
1126 DWORD WINAPI
GetProcessVersion( DWORD processid
)
1129 PDB
*pdb
= PROCESS_IdToPDB( processid
);
1132 if (!(pTask
= (TDB
*)GlobalLock16( pdb
->task
))) return 0;
1133 return (pTask
->version
&0xff) | (((pTask
->version
>>8) & 0xff)<<16);
1136 /***********************************************************************
1137 * GetProcessFlags (KERNEL32)
1139 DWORD WINAPI
GetProcessFlags( DWORD processid
)
1141 PDB
*pdb
= PROCESS_IdToPDB( processid
);
1146 /***********************************************************************
1147 * SetProcessWorkingSetSize [KERNEL32.662]
1148 * Sets the min/max working set sizes for a specified process.
1151 * hProcess [I] Handle to the process of interest
1152 * minset [I] Specifies minimum working set size
1153 * maxset [I] Specifies maximum working set size
1157 BOOL WINAPI
SetProcessWorkingSetSize(HANDLE hProcess
,DWORD minset
,
1160 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess
,minset
,maxset
);
1161 if(( minset
== -1) && (maxset
== -1)) {
1162 /* Trim the working set to zero */
1163 /* Swap the process out of physical RAM */
1168 /***********************************************************************
1169 * GetProcessWorkingSetSize (KERNEL32)
1171 BOOL WINAPI
GetProcessWorkingSetSize(HANDLE hProcess
,LPDWORD minset
,
1174 FIXME("(0x%08x,%p,%p): stub\n",hProcess
,minset
,maxset
);
1175 /* 32 MB working set size */
1176 if (minset
) *minset
= 32*1024*1024;
1177 if (maxset
) *maxset
= 32*1024*1024;
1181 /***********************************************************************
1182 * SetProcessShutdownParameters (KERNEL32)
1184 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1185 * Now tracks changes made (but does not act on these changes)
1186 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
1187 * It really shouldn't be here, but I'll move it when it's been checked!
1189 #define SHUTDOWN_NORETRY 1
1190 static unsigned int shutdown_noretry
= 0;
1191 static unsigned int shutdown_priority
= 0x280L
;
1192 BOOL WINAPI
SetProcessShutdownParameters(DWORD level
,DWORD flags
)
1194 if (flags
& SHUTDOWN_NORETRY
)
1195 shutdown_noretry
= 1;
1197 shutdown_noretry
= 0;
1198 if (level
> 0x100L
&& level
< 0x3FFL
)
1199 shutdown_priority
= level
;
1202 ERR("invalid priority level 0x%08lx\n", level
);
1209 /***********************************************************************
1210 * GetProcessShutdownParameters (KERNEL32)
1213 BOOL WINAPI
GetProcessShutdownParameters( LPDWORD lpdwLevel
,
1216 (*lpdwLevel
) = shutdown_priority
;
1217 (*lpdwFlags
) = (shutdown_noretry
* SHUTDOWN_NORETRY
);
1220 /***********************************************************************
1221 * SetProcessPriorityBoost (KERNEL32)
1223 BOOL WINAPI
SetProcessPriorityBoost(HANDLE hprocess
,BOOL disableboost
)
1225 FIXME("(%d,%d): stub\n",hprocess
,disableboost
);
1226 /* Say we can do it. I doubt the program will notice that we don't. */
1231 /***********************************************************************
1232 * ReadProcessMemory (KERNEL32)
1234 BOOL WINAPI
ReadProcessMemory( HANDLE process
, LPCVOID addr
, LPVOID buffer
, DWORD size
,
1235 LPDWORD bytes_read
)
1237 struct read_process_memory_request
*req
= get_req_buffer();
1238 unsigned int offset
= (unsigned int)addr
% sizeof(int);
1239 unsigned int max
= server_remaining( req
->data
); /* max length in one request */
1242 if (bytes_read
) *bytes_read
= size
;
1244 /* first time, read total length to check for permissions */
1245 req
->handle
= process
;
1246 req
->addr
= (char *)addr
- offset
;
1247 req
->len
= (size
+ offset
+ sizeof(int) - 1) / sizeof(int);
1248 if (server_call( REQ_READ_PROCESS_MEMORY
)) goto error
;
1250 if (size
<= max
- offset
)
1252 memcpy( buffer
, (char *)req
->data
+ offset
, size
);
1256 /* now take care of the remaining data */
1257 memcpy( buffer
, (char *)req
->data
+ offset
, max
- offset
);
1262 if (max
> size
) max
= size
;
1263 req
->handle
= process
;
1264 req
->addr
= (char *)addr
+ pos
;
1265 req
->len
= (max
+ sizeof(int) - 1) / sizeof(int);
1266 if (server_call( REQ_READ_PROCESS_MEMORY
)) goto error
;
1267 memcpy( (char *)buffer
+ pos
, (char *)req
->data
, max
);
1274 if (bytes_read
) *bytes_read
= 0;
1279 /***********************************************************************
1280 * WriteProcessMemory (KERNEL32)
1282 BOOL WINAPI
WriteProcessMemory( HANDLE process
, LPVOID addr
, LPVOID buffer
, DWORD size
,
1283 LPDWORD bytes_written
)
1285 unsigned int first_offset
, last_offset
;
1286 struct write_process_memory_request
*req
= get_req_buffer();
1287 unsigned int max
= server_remaining( req
->data
); /* max length in one request */
1288 unsigned int pos
, last_mask
;
1292 SetLastError( ERROR_INVALID_PARAMETER
);
1295 if (bytes_written
) *bytes_written
= size
;
1297 /* compute the mask for the first int */
1298 req
->first_mask
= ~0;
1299 first_offset
= (unsigned int)addr
% sizeof(int);
1300 memset( &req
->first_mask
, 0, first_offset
);
1302 /* compute the mask for the last int */
1303 last_offset
= (size
+ first_offset
) % sizeof(int);
1305 memset( &last_mask
, 0xff, last_offset
? last_offset
: sizeof(int) );
1307 req
->handle
= process
;
1308 req
->addr
= (char *)addr
- first_offset
;
1309 /* for the first request, use the total length */
1310 req
->len
= (size
+ first_offset
+ sizeof(int) - 1) / sizeof(int);
1312 if (size
+ first_offset
< max
) /* we can do it in one round */
1314 memcpy( (char *)req
->data
+ first_offset
, buffer
, size
);
1315 req
->last_mask
= last_mask
;
1316 if (server_call( REQ_WRITE_PROCESS_MEMORY
)) goto error
;
1320 /* needs multiple server calls */
1322 memcpy( (char *)req
->data
+ first_offset
, buffer
, max
- first_offset
);
1323 req
->last_mask
= ~0;
1324 if (server_call( REQ_WRITE_PROCESS_MEMORY
)) goto error
;
1325 pos
= max
- first_offset
;
1329 if (size
<= max
) /* last one */
1331 req
->last_mask
= last_mask
;
1334 req
->handle
= process
;
1335 req
->addr
= (char *)addr
+ pos
;
1336 req
->len
= (max
+ sizeof(int) - 1) / sizeof(int);
1337 req
->first_mask
= ~0;
1338 memcpy( req
->data
, (char *) buffer
+ pos
, max
);
1339 if (server_call( REQ_WRITE_PROCESS_MEMORY
)) goto error
;
1346 if (bytes_written
) *bytes_written
= 0;
1352 /***********************************************************************
1353 * RegisterServiceProcess (KERNEL, KERNEL32)
1355 * A service process calls this function to ensure that it continues to run
1356 * even after a user logged off.
1358 DWORD WINAPI
RegisterServiceProcess(DWORD dwProcessId
, DWORD dwType
)
1360 /* I don't think that Wine needs to do anything in that function */
1361 return 1; /* success */
1364 /***********************************************************************
1365 * GetExitCodeProcess [KERNEL32.325]
1367 * Gets termination status of specified process
1373 BOOL WINAPI
GetExitCodeProcess(
1374 HANDLE hProcess
, /* [I] handle to the process */
1375 LPDWORD lpExitCode
) /* [O] address to receive termination status */
1378 struct get_process_info_request
*req
= get_req_buffer();
1379 req
->handle
= hProcess
;
1380 if (!server_call( REQ_GET_PROCESS_INFO
))
1382 if (lpExitCode
) *lpExitCode
= req
->exit_code
;
1389 /***********************************************************************
1390 * SetErrorMode (KERNEL32.486)
1392 UINT WINAPI
SetErrorMode( UINT mode
)
1394 UINT old
= PROCESS_Current()->error_mode
;
1395 PROCESS_Current()->error_mode
= mode
;
1399 /***********************************************************************
1400 * GetCurrentProcess (KERNEL32.198)
1402 #undef GetCurrentProcess
1403 HANDLE WINAPI
GetCurrentProcess(void)