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 BOOL
PROCESS_Init(void)
178 struct init_process_request
*req
;
179 PDB
*pdb
= PROCESS_Current();
181 /* Fill the initial process structure */
182 pdb
->exit_code
= STILL_ACTIVE
;
184 pdb
->running_threads
= 1;
185 pdb
->ring0_threads
= 1;
186 pdb
->env_db
= &initial_envdb
;
188 pdb
->priority
= 8; /* Normal */
189 pdb
->winver
= 0xffff; /* to be determined */
190 initial_envdb
.startup_info
= &initial_startup
;
192 /* Setup the server connection */
193 NtCurrentTeb()->socket
= CLIENT_InitServer();
194 if (CLIENT_InitThread()) return FALSE
;
196 /* Retrieve startup info from the server */
197 req
= get_req_buffer();
198 req
->ldt_copy
= ldt_copy
;
199 req
->ldt_flags
= ldt_flags_copy
;
200 req
->ppid
= getppid();
201 if (server_call( REQ_INIT_PROCESS
)) return FALSE
;
202 main_exe_file
= req
->exe_file
;
203 if (req
->filename
[0]) main_exe_name
= strdup( req
->filename
);
204 initial_startup
.dwFlags
= req
->start_flags
;
205 initial_startup
.wShowWindow
= req
->cmd_show
;
206 initial_envdb
.hStdin
= initial_startup
.hStdInput
= req
->hstdin
;
207 initial_envdb
.hStdout
= initial_startup
.hStdOutput
= req
->hstdout
;
208 initial_envdb
.hStderr
= initial_startup
.hStdError
= req
->hstderr
;
210 /* Initialize signal handling */
211 if (!SIGNAL_Init()) return FALSE
;
213 /* Remember TEB selector of initial process for emergency use */
214 SYSLEVEL_EmergencyTeb
= NtCurrentTeb()->teb_sel
;
216 /* Create the system and process heaps */
217 if (!HEAP_CreateSystemHeap()) return FALSE
;
218 pdb
->heap
= HeapCreate( HEAP_GROWABLE
, 0, 0 );
220 /* Copy the parent environment */
221 if (!ENV_BuildEnvironment()) return FALSE
;
223 /* Create the SEGPTR heap */
224 if (!(SegptrHeap
= HeapCreate( HEAP_WINE_SEGPTR
, 0, 0 ))) return FALSE
;
226 /* Initialize the critical sections */
227 InitializeCriticalSection( &pdb
->crit_section
);
228 InitializeCriticalSection( &initial_envdb
.section
);
230 /* Initialize syslevel handling */
237 /***********************************************************************
240 * Load system DLLs into the initial process (and initialize them)
242 static int load_system_dlls(void)
244 char driver
[MAX_PATH
];
246 if (!LoadLibraryA( "KERNEL32" )) return 0;
248 PROFILE_GetWineIniString( "Wine", "GraphicsDriver", "x11drv", driver
, sizeof(driver
) );
249 if (!LoadLibraryA( driver
))
251 MESSAGE( "Could not load graphics driver '%s'\n", driver
);
255 if (!LoadLibraryA("USER32.DLL")) return 0;
257 /* Get pointers to USER routines called by KERNEL */
260 /* Call FinalUserInit routine */
261 Callout
.FinalUserInit16();
263 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
264 * context of the parent process. Actually, the USER signal proc
265 * doesn't really care about that, but it *does* require that the
266 * startup parameters are correctly set up, so that GetProcessDword
267 * works. Furthermore, before calling the USER signal proc the
268 * 16-bit stack must be set up, which it is only after TASK_Create
269 * in the case of a 16-bit process. Thus, we send the signal here.
271 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE
, 0 );
272 PROCESS_CallUserSignalProc( USIG_THREAD_INIT
, 0 );
273 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT
, 0 );
274 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED
, 0 );
280 /***********************************************************************
283 * Build the command-line of a process from the argv array.
285 static inline char *build_command_line( char **argv
)
288 char *cmdline
, *p
, **arg
;
290 for (arg
= argv
, len
= 0; *arg
; arg
++) len
+= strlen(*arg
) + 1;
291 if ((quote
= (strchr( argv
[0], ' ' ) != NULL
))) len
+= 2;
292 if (!(p
= cmdline
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
310 if (p
> cmdline
) p
--; /* remove last space */
316 /***********************************************************************
319 * Startup routine of a new process. Runs on the new process stack.
321 static void start_process(void)
325 struct init_process_done_request
*req
= get_req_buffer();
326 int debugged
, console_app
;
328 UINT cmdShow
= SW_SHOWNORMAL
;
329 LPTHREAD_START_ROUTINE entry
;
330 PDB
*pdb
= PROCESS_Current();
331 HMODULE module
= pdb
->exe_modref
->module
;
333 /* Increment EXE refcount */
334 pdb
->exe_modref
->refCount
++;
336 /* build command line */
337 if (!(pdb
->env_db
->cmd_line
= build_command_line( main_exe_argv
))) goto error
;
339 /* Retrieve entry point address */
340 entry
= (LPTHREAD_START_ROUTINE
)RVA_PTR( module
, OptionalHeader
.AddressOfEntryPoint
);
341 console_app
= (PE_HEADER(module
)->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
);
343 /* Create 16-bit dummy module */
344 if ((hModule16
= MODULE_CreateDummyModule( pdb
->exe_modref
->filename
, module
)) < 32)
345 ExitProcess( hModule16
);
347 if (pdb
->env_db
->startup_info
->dwFlags
& STARTF_USESHOWWINDOW
)
348 cmdShow
= pdb
->env_db
->startup_info
->wShowWindow
;
349 if (!TASK_Create( (NE_MODULE
*)GlobalLock16( hModule16
), cmdShow
,
350 NtCurrentTeb(), NULL
, 0 ))
353 if (console_app
) pdb
->flags
|= PDB32_CONSOLE_PROC
;
355 /* Signal the parent process to continue */
356 req
->module
= (void *)module
;
358 req
->gui
= !console_app
;
359 server_call( REQ_INIT_PROCESS_DONE
);
360 debugged
= req
->debugged
;
362 /* Load the system dlls */
363 if (!load_system_dlls()) goto error
;
365 EnterCriticalSection( &pdb
->crit_section
);
367 MODULE_DllProcessAttach( pdb
->exe_modref
, (LPVOID
)1 );
368 LeaveCriticalSection( &pdb
->crit_section
);
370 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
371 if (console_app
) PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING
, 0 );
373 TRACE_(relay
)( "Starting Win32 process (entryproc=%p)\n", entry
);
374 if (debugged
) DbgBreakPoint();
375 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
376 * Dunno about other OSs */
377 ExitThread( entry(NULL
) );
380 ExitProcess( GetLastError() );
383 __EXCEPT(UnhandledExceptionFilter
)
385 TerminateThread( GetCurrentThread(), GetExceptionCode() );
391 /***********************************************************************
394 * Startup routine of a new Win32 process once the main module has been loaded.
396 static void PROCESS_Start( HMODULE main_module
, LPCSTR filename
) WINE_NORETURN
;
397 static void PROCESS_Start( HMODULE main_module
, LPCSTR filename
)
399 /* load main module */
400 if (PE_HEADER(main_module
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
401 ExitProcess( ERROR_BAD_EXE_FORMAT
);
403 /* Create 32-bit MODREF */
404 if (!PE_CreateModule( main_module
, filename
, 0, FALSE
))
405 ExitProcess( GetLastError() );
407 /* allocate main thread stack */
408 if (!THREAD_InitStack( NtCurrentTeb(),
409 PE_HEADER(main_module
)->OptionalHeader
.SizeOfStackReserve
, TRUE
))
410 ExitProcess( GetLastError() );
412 SIGNAL_Init(); /* reinitialize signal stack */
414 /* switch to the new stack */
415 CALL32_Init( &IF1632_CallLargeStack
, start_process
, NtCurrentTeb()->stack_top
);
419 /***********************************************************************
422 * Wine initialisation: load and start the main exe file.
424 void PROCESS_InitWine( int argc
, char *argv
[] )
428 /* Initialize everything */
429 if (!MAIN_MainInit( argv
)) exit(1);
431 main_exe_argv
= ++argv
; /* remove argv[0] (wine itself) */
435 char buffer
[MAX_PATH
];
436 if (!argv
[0]) OPTIONS_Usage();
438 /* open the exe file */
439 if (!SearchPathA( NULL
, argv
[0], ".exe", sizeof(buffer
), buffer
, NULL
) &&
440 !SearchPathA( NULL
, argv
[0], NULL
, sizeof(buffer
), buffer
, NULL
))
442 MESSAGE( "%s: cannot find '%s'\n", argv0
, argv
[0] );
445 if (!(main_exe_name
= strdup(buffer
)))
447 MESSAGE( "%s: out of memory\n", argv0
);
452 if (main_exe_file
== INVALID_HANDLE_VALUE
)
454 if ((main_exe_file
= CreateFileA( main_exe_name
, GENERIC_READ
, FILE_SHARE_READ
,
455 NULL
, OPEN_EXISTING
, 0, -1 )) == INVALID_HANDLE_VALUE
)
457 MESSAGE( "%s: cannot open '%s'\n", argv0
, main_exe_name
);
462 if (!MODULE_GetBinaryType( main_exe_file
, main_exe_name
, &type
))
464 MESSAGE( "%s: unrecognized executable '%s'\n", argv0
, main_exe_name
);
470 case SCS_32BIT_BINARY
:
472 HMODULE main_module
= PE_LoadImage( main_exe_file
, main_exe_name
);
473 if (main_module
) PROCESS_Start( main_module
, main_exe_name
);
481 /* create 32-bit module for main exe */
482 if (!(main_module
= BUILTIN32_LoadExeModule( &filename
))) goto error
;
483 NtCurrentTeb()->tibflags
&= ~TEBF_WIN32
;
484 PROCESS_Current()->flags
|= PDB32_WIN16_PROC
;
485 SYSLEVEL_EnterWin16Lock();
486 PROCESS_Start( main_module
, filename
);
491 FIXME( "DOS binaries support is broken at the moment; feel free to fix it...\n" );
492 SetLastError( ERROR_BAD_FORMAT
);
496 case SCS_POSIX_BINARY
:
497 case SCS_OS216_BINARY
:
499 MESSAGE( "%s: unrecognized executable '%s'\n", argv0
, main_exe_name
);
500 SetLastError( ERROR_BAD_FORMAT
);
504 ExitProcess( GetLastError() );
508 /***********************************************************************
509 * PROCESS_InitWinelib
511 * Initialisation of a new Winelib process.
513 void PROCESS_InitWinelib( int argc
, char *argv
[] )
518 if (!MAIN_MainInit( argv
)) exit(1);
520 main_exe_argv
= argv
;
522 /* create 32-bit module for main exe */
523 if (!(main_module
= BUILTIN32_LoadExeModule( &filename
))) ExitProcess( GetLastError() );
525 PROCESS_Start( main_module
, filename
);
529 /***********************************************************************
532 * Build an argv array from a command-line.
533 * The command-line is modified to insert nulls.
534 * 'reserved' is the number of args to reserve before the first one.
536 static char **build_argv( char *cmdline
, int reserved
)
539 int count
= reserved
+ 1;
542 /* if first word is quoted store it as a single arg */
543 if (*cmdline
== '\"')
545 if ((p
= strchr( cmdline
+ 1, '\"' )))
554 while (*p
&& isspace(*p
)) p
++;
557 while (*p
&& !isspace(*p
)) p
++;
560 if ((argv
= malloc( count
* sizeof(*argv
) )))
562 char **argvptr
= argv
+ reserved
;
564 if (*cmdline
== '\"')
566 if ((p
= strchr( cmdline
+ 1, '\"' )))
568 *argvptr
++ = cmdline
+ 1;
575 while (*p
&& isspace(*p
)) *p
++ = 0;
578 while (*p
&& !isspace(*p
)) p
++;
586 /***********************************************************************
589 * Build the environment of a new child process.
591 static char **build_envp( const char *env
)
597 for (p
= env
, count
= 0; *p
; count
++) p
+= strlen(p
) + 1;
599 if ((envp
= malloc( count
* sizeof(*envp
) )))
601 extern char **environ
;
602 char **envptr
= envp
;
603 char **unixptr
= environ
;
604 /* first put PATH, HOME and WINEPREFIX from the unix env */
605 for (unixptr
= environ
; unixptr
&& *unixptr
; unixptr
++)
606 if (!memcmp( *unixptr
, "PATH=", 5 ) ||
607 !memcmp( *unixptr
, "HOME=", 5 ) ||
608 !memcmp( *unixptr
, "WINEPREFIX=", 11 )) *envptr
++ = *unixptr
;
609 /* now put the Windows environment strings */
610 for (p
= env
; *p
; p
+= strlen(p
) + 1)
612 if (memcmp( p
, "PATH=", 5 ) &&
613 memcmp( p
, "HOME=", 5 ) &&
614 memcmp( p
, "WINEPREFIX=", 11 )) *envptr
++ = (char *)p
;
622 /***********************************************************************
625 * Locate the Wine binary to exec for a new Win32 process.
627 static void exec_wine_binary( char **argv
, char **envp
)
629 const char *path
, *pos
, *ptr
;
631 /* first try bin directory */
632 argv
[0] = BINDIR
"/wine";
633 execve( argv
[0], argv
, envp
);
635 /* now try the path of argv0 of the current binary */
636 if (!(argv
[0] = malloc( strlen(argv0
) + 6 ))) return;
637 if ((ptr
= strrchr( argv0
, '/' )))
639 memcpy( argv
[0], argv0
, ptr
- argv0
);
640 strcpy( argv
[0] + (ptr
- argv0
), "/wine" );
641 execve( argv
[0], argv
, envp
);
645 /* now search in the Unix path */
646 if ((path
= getenv( "PATH" )))
648 if (!(argv
[0] = malloc( strlen(path
) + 6 ))) return;
652 while (*pos
== ':') pos
++;
654 if (!(ptr
= strchr( pos
, ':' ))) ptr
= pos
+ strlen(pos
);
655 memcpy( argv
[0], pos
, ptr
- pos
);
656 strcpy( argv
[0] + (ptr
- pos
), "/wine" );
657 execve( argv
[0], argv
, envp
);
663 /* finally try the current directory */
665 execve( argv
[0], argv
, envp
);
669 /***********************************************************************
672 * Fork and exec a new Unix process, checking for errors.
674 static int fork_and_exec( const char *filename
, const char *cmdline
, const char *env
)
684 fcntl( fd
[1], F_SETFD
, 1 ); /* set close on exec */
685 if (!(pid
= fork())) /* child */
687 char **argv
= build_argv( (char *)cmdline
, filename
? 0 : 2 );
688 char **envp
= build_envp( env
);
695 exec_wine_binary( argv
, envp
);
697 else execve( filename
, argv
, envp
);
700 write( fd
[1], &err
, sizeof(err
) );
704 if ((pid
!= -1) && (read( fd
[0], &err
, sizeof(err
) ) > 0)) /* exec failed */
709 if (pid
== -1) FILE_SetDosError();
715 /***********************************************************************
718 * Create a new process. If hFile is a valid handle we have an exe
719 * file, and we exec a new copy of wine to load it; otherwise we
720 * simply exec the specified filename as a Unix process.
722 BOOL
PROCESS_Create( HFILE hFile
, LPCSTR filename
, LPCSTR cmd_line
, LPCSTR env
,
723 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
724 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
725 LPPROCESS_INFORMATION info
)
728 const char *unixfilename
= NULL
;
729 DOS_FULL_NAME full_name
;
730 HANDLE load_done_evt
= -1;
731 struct new_process_request
*req
= get_req_buffer();
732 struct wait_process_request
*wait_req
= get_req_buffer();
734 info
->hThread
= info
->hProcess
= INVALID_HANDLE_VALUE
;
736 /* create the process on the server side */
738 req
->inherit_all
= inherit
;
739 req
->create_flags
= flags
;
740 req
->start_flags
= startup
->dwFlags
;
741 req
->exe_file
= hFile
;
742 if (startup
->dwFlags
& STARTF_USESTDHANDLES
)
744 req
->hstdin
= startup
->hStdInput
;
745 req
->hstdout
= startup
->hStdOutput
;
746 req
->hstderr
= startup
->hStdError
;
750 req
->hstdin
= GetStdHandle( STD_INPUT_HANDLE
);
751 req
->hstdout
= GetStdHandle( STD_OUTPUT_HANDLE
);
752 req
->hstderr
= GetStdHandle( STD_ERROR_HANDLE
);
754 req
->cmd_show
= startup
->wShowWindow
;
757 if (hFile
== -1) /* unix process */
759 unixfilename
= filename
;
760 if (DOSFS_GetFullName( filename
, TRUE
, &full_name
)) unixfilename
= full_name
.long_name
;
761 req
->filename
[0] = 0;
763 else /* new wine process */
765 if (!GetFullPathNameA( filename
, server_remaining(req
->filename
), req
->filename
, NULL
))
766 lstrcpynA( req
->filename
, filename
, server_remaining(req
->filename
) );
768 if (server_call( REQ_NEW_PROCESS
)) return FALSE
;
770 /* fork and execute */
772 pid
= fork_and_exec( unixfilename
, cmd_line
, env
? env
: GetEnvironmentStringsA() );
774 wait_req
->cancel
= (pid
== -1);
775 wait_req
->pinherit
= (psa
&& (psa
->nLength
>= sizeof(*psa
)) && psa
->bInheritHandle
);
776 wait_req
->tinherit
= (tsa
&& (tsa
->nLength
>= sizeof(*tsa
)) && tsa
->bInheritHandle
);
777 wait_req
->timeout
= 2000;
778 if (server_call( REQ_WAIT_PROCESS
) || (pid
== -1)) goto error
;
779 info
->dwProcessId
= (DWORD
)wait_req
->pid
;
780 info
->dwThreadId
= (DWORD
)wait_req
->tid
;
781 info
->hProcess
= wait_req
->phandle
;
782 info
->hThread
= wait_req
->thandle
;
783 load_done_evt
= wait_req
->event
;
785 /* Wait until process is initialized (or initialization failed) */
786 if (load_done_evt
!= -1)
791 handles
[0] = info
->hProcess
;
792 handles
[1] = load_done_evt
;
793 res
= WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
);
794 CloseHandle( load_done_evt
);
795 if (res
== STATUS_WAIT_0
) /* the process died */
798 if (GetExitCodeProcess( info
->hProcess
, &exitcode
)) SetLastError( exitcode
);
799 CloseHandle( info
->hThread
);
800 CloseHandle( info
->hProcess
);
807 if (load_done_evt
!= -1) CloseHandle( load_done_evt
);
808 if (info
->hThread
!= INVALID_HANDLE_VALUE
) CloseHandle( info
->hThread
);
809 if (info
->hProcess
!= INVALID_HANDLE_VALUE
) CloseHandle( info
->hProcess
);
814 /***********************************************************************
815 * ExitProcess (KERNEL32.100)
817 void WINAPI
ExitProcess( DWORD status
)
819 struct terminate_process_request
*req
= get_req_buffer();
821 MODULE_DllProcessDetach( TRUE
, (LPVOID
)1 );
822 /* send the exit code to the server */
823 req
->handle
= GetCurrentProcess();
824 req
->exit_code
= status
;
825 server_call( REQ_TERMINATE_PROCESS
);
829 /***********************************************************************
830 * ExitProcess16 (KERNEL.466)
832 void WINAPI
ExitProcess16( WORD status
)
834 SYSLEVEL_ReleaseWin16Lock();
835 ExitProcess( status
);
838 /******************************************************************************
839 * TerminateProcess (KERNEL32.684)
841 BOOL WINAPI
TerminateProcess( HANDLE handle
, DWORD exit_code
)
844 struct terminate_process_request
*req
= get_req_buffer();
845 req
->handle
= handle
;
846 req
->exit_code
= exit_code
;
847 if ((ret
= !server_call( REQ_TERMINATE_PROCESS
)) && req
->self
) exit( exit_code
);
852 /***********************************************************************
853 * GetProcessDword (KERNEL32.18) (KERNEL.485)
854 * 'Of course you cannot directly access Windows internal structures'
856 DWORD WINAPI
GetProcessDword( DWORD dwProcessID
, INT offset
)
858 PDB
*process
= PROCESS_IdToPDB( dwProcessID
);
862 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
865 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
871 case GPD_APP_COMPAT_FLAGS
:
872 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
873 return pTask
? pTask
->compat_flags
: 0;
875 case GPD_LOAD_DONE_EVENT
:
876 return process
->load_done_evt
;
878 case GPD_HINSTANCE16
:
879 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
880 return pTask
? pTask
->hInstance
: 0;
882 case GPD_WINDOWS_VERSION
:
883 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
884 return pTask
? pTask
->version
: 0;
887 if ( process
!= PROCESS_Current() ) return 0;
888 return (DWORD
)NtCurrentTeb() - 0x10 /* FIXME */;
891 return (DWORD
)process
;
893 case GPD_STARTF_SHELLDATA
: /* return stdoutput handle from startupinfo ??? */
894 return process
->env_db
->startup_info
->hStdOutput
;
896 case GPD_STARTF_HOTKEY
: /* return stdinput handle from startupinfo ??? */
897 return process
->env_db
->startup_info
->hStdInput
;
899 case GPD_STARTF_SHOWWINDOW
:
900 return process
->env_db
->startup_info
->wShowWindow
;
902 case GPD_STARTF_SIZE
:
903 x
= process
->env_db
->startup_info
->dwXSize
;
904 if ( x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
905 y
= process
->env_db
->startup_info
->dwYSize
;
906 if ( y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
907 return MAKELONG( x
, y
);
909 case GPD_STARTF_POSITION
:
910 x
= process
->env_db
->startup_info
->dwX
;
911 if ( x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
912 y
= process
->env_db
->startup_info
->dwY
;
913 if ( y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
914 return MAKELONG( x
, y
);
916 case GPD_STARTF_FLAGS
:
917 return process
->env_db
->startup_info
->dwFlags
;
923 return process
->flags
;
926 return process
->process_dword
;
929 ERR_(win32
)("Unknown offset %d\n", offset
);
934 /***********************************************************************
935 * SetProcessDword (KERNEL.484)
936 * 'Of course you cannot directly access Windows internal structures'
938 void WINAPI
SetProcessDword( DWORD dwProcessID
, INT offset
, DWORD value
)
940 PDB
*process
= PROCESS_IdToPDB( dwProcessID
);
942 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
945 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
951 case GPD_APP_COMPAT_FLAGS
:
952 case GPD_LOAD_DONE_EVENT
:
953 case GPD_HINSTANCE16
:
954 case GPD_WINDOWS_VERSION
:
957 case GPD_STARTF_SHELLDATA
:
958 case GPD_STARTF_HOTKEY
:
959 case GPD_STARTF_SHOWWINDOW
:
960 case GPD_STARTF_SIZE
:
961 case GPD_STARTF_POSITION
:
962 case GPD_STARTF_FLAGS
:
965 ERR_(win32
)("Not allowed to modify offset %d\n", offset
);
969 process
->process_dword
= value
;
973 ERR_(win32
)("Unknown offset %d\n", offset
);
979 /*********************************************************************
980 * OpenProcess (KERNEL32.543)
982 HANDLE WINAPI
OpenProcess( DWORD access
, BOOL inherit
, DWORD id
)
985 struct open_process_request
*req
= get_req_buffer();
987 req
->pid
= (void *)id
;
988 req
->access
= access
;
989 req
->inherit
= inherit
;
990 if (!server_call( REQ_OPEN_PROCESS
)) ret
= req
->handle
;
994 /*********************************************************************
995 * MapProcessHandle (KERNEL.483)
997 DWORD WINAPI
MapProcessHandle( HANDLE handle
)
1000 struct get_process_info_request
*req
= get_req_buffer();
1001 req
->handle
= handle
;
1002 if (!server_call( REQ_GET_PROCESS_INFO
)) ret
= (DWORD
)req
->pid
;
1006 /***********************************************************************
1007 * GetThreadLocale (KERNEL32.295)
1009 LCID WINAPI
GetThreadLocale(void)
1011 return PROCESS_Current()->locale
;
1015 /***********************************************************************
1016 * SetPriorityClass (KERNEL32.503)
1018 BOOL WINAPI
SetPriorityClass( HANDLE hprocess
, DWORD priorityclass
)
1020 struct set_process_info_request
*req
= get_req_buffer();
1021 req
->handle
= hprocess
;
1022 req
->priority
= priorityclass
;
1023 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
1024 return !server_call( REQ_SET_PROCESS_INFO
);
1028 /***********************************************************************
1029 * GetPriorityClass (KERNEL32.250)
1031 DWORD WINAPI
GetPriorityClass(HANDLE hprocess
)
1034 struct get_process_info_request
*req
= get_req_buffer();
1035 req
->handle
= hprocess
;
1036 if (!server_call( REQ_GET_PROCESS_INFO
)) ret
= req
->priority
;
1041 /***********************************************************************
1042 * SetProcessAffinityMask (KERNEL32.662)
1044 BOOL WINAPI
SetProcessAffinityMask( HANDLE hProcess
, DWORD affmask
)
1046 struct set_process_info_request
*req
= get_req_buffer();
1047 req
->handle
= hProcess
;
1048 req
->affinity
= affmask
;
1049 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
1050 return !server_call( REQ_SET_PROCESS_INFO
);
1053 /**********************************************************************
1054 * GetProcessAffinityMask (KERNEL32.373)
1056 BOOL WINAPI
GetProcessAffinityMask( HANDLE hProcess
,
1057 LPDWORD lpProcessAffinityMask
,
1058 LPDWORD lpSystemAffinityMask
)
1061 struct get_process_info_request
*req
= get_req_buffer();
1062 req
->handle
= hProcess
;
1063 if (!server_call( REQ_GET_PROCESS_INFO
))
1065 if (lpProcessAffinityMask
) *lpProcessAffinityMask
= req
->process_affinity
;
1066 if (lpSystemAffinityMask
) *lpSystemAffinityMask
= req
->system_affinity
;
1073 /***********************************************************************
1074 * GetStdHandle (KERNEL32.276)
1076 HANDLE WINAPI
GetStdHandle( DWORD std_handle
)
1078 PDB
*pdb
= PROCESS_Current();
1082 case STD_INPUT_HANDLE
: return pdb
->env_db
->hStdin
;
1083 case STD_OUTPUT_HANDLE
: return pdb
->env_db
->hStdout
;
1084 case STD_ERROR_HANDLE
: return pdb
->env_db
->hStderr
;
1086 SetLastError( ERROR_INVALID_PARAMETER
);
1087 return INVALID_HANDLE_VALUE
;
1091 /***********************************************************************
1092 * SetStdHandle (KERNEL32.506)
1094 BOOL WINAPI
SetStdHandle( DWORD std_handle
, HANDLE handle
)
1096 PDB
*pdb
= PROCESS_Current();
1097 /* FIXME: should we close the previous handle? */
1100 case STD_INPUT_HANDLE
:
1101 pdb
->env_db
->hStdin
= handle
;
1103 case STD_OUTPUT_HANDLE
:
1104 pdb
->env_db
->hStdout
= handle
;
1106 case STD_ERROR_HANDLE
:
1107 pdb
->env_db
->hStderr
= handle
;
1110 SetLastError( ERROR_INVALID_PARAMETER
);
1114 /***********************************************************************
1115 * GetProcessVersion (KERNEL32)
1117 DWORD WINAPI
GetProcessVersion( DWORD processid
)
1120 PDB
*pdb
= PROCESS_IdToPDB( processid
);
1123 if (!(pTask
= (TDB
*)GlobalLock16( pdb
->task
))) return 0;
1124 return (pTask
->version
&0xff) | (((pTask
->version
>>8) & 0xff)<<16);
1127 /***********************************************************************
1128 * GetProcessFlags (KERNEL32)
1130 DWORD WINAPI
GetProcessFlags( DWORD processid
)
1132 PDB
*pdb
= PROCESS_IdToPDB( processid
);
1137 /***********************************************************************
1138 * SetProcessWorkingSetSize [KERNEL32.662]
1139 * Sets the min/max working set sizes for a specified process.
1142 * hProcess [I] Handle to the process of interest
1143 * minset [I] Specifies minimum working set size
1144 * maxset [I] Specifies maximum working set size
1148 BOOL WINAPI
SetProcessWorkingSetSize(HANDLE hProcess
,DWORD minset
,
1151 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess
,minset
,maxset
);
1152 if(( minset
== -1) && (maxset
== -1)) {
1153 /* Trim the working set to zero */
1154 /* Swap the process out of physical RAM */
1159 /***********************************************************************
1160 * GetProcessWorkingSetSize (KERNEL32)
1162 BOOL WINAPI
GetProcessWorkingSetSize(HANDLE hProcess
,LPDWORD minset
,
1165 FIXME("(0x%08x,%p,%p): stub\n",hProcess
,minset
,maxset
);
1166 /* 32 MB working set size */
1167 if (minset
) *minset
= 32*1024*1024;
1168 if (maxset
) *maxset
= 32*1024*1024;
1172 /***********************************************************************
1173 * SetProcessShutdownParameters (KERNEL32)
1175 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1176 * Now tracks changes made (but does not act on these changes)
1177 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
1178 * It really shouldn't be here, but I'll move it when it's been checked!
1180 #define SHUTDOWN_NORETRY 1
1181 static unsigned int shutdown_noretry
= 0;
1182 static unsigned int shutdown_priority
= 0x280L
;
1183 BOOL WINAPI
SetProcessShutdownParameters(DWORD level
,DWORD flags
)
1185 if (flags
& SHUTDOWN_NORETRY
)
1186 shutdown_noretry
= 1;
1188 shutdown_noretry
= 0;
1189 if (level
> 0x100L
&& level
< 0x3FFL
)
1190 shutdown_priority
= level
;
1193 ERR("invalid priority level 0x%08lx\n", level
);
1200 /***********************************************************************
1201 * GetProcessShutdownParameters (KERNEL32)
1204 BOOL WINAPI
GetProcessShutdownParameters( LPDWORD lpdwLevel
,
1207 (*lpdwLevel
) = shutdown_priority
;
1208 (*lpdwFlags
) = (shutdown_noretry
* SHUTDOWN_NORETRY
);
1211 /***********************************************************************
1212 * SetProcessPriorityBoost (KERNEL32)
1214 BOOL WINAPI
SetProcessPriorityBoost(HANDLE hprocess
,BOOL disableboost
)
1216 FIXME("(%d,%d): stub\n",hprocess
,disableboost
);
1217 /* Say we can do it. I doubt the program will notice that we don't. */
1222 /***********************************************************************
1223 * ReadProcessMemory (KERNEL32)
1225 BOOL WINAPI
ReadProcessMemory( HANDLE process
, LPCVOID addr
, LPVOID buffer
, DWORD size
,
1226 LPDWORD bytes_read
)
1228 struct read_process_memory_request
*req
= get_req_buffer();
1229 unsigned int offset
= (unsigned int)addr
% sizeof(int);
1230 unsigned int max
= server_remaining( req
->data
); /* max length in one request */
1233 if (bytes_read
) *bytes_read
= size
;
1235 /* first time, read total length to check for permissions */
1236 req
->handle
= process
;
1237 req
->addr
= (char *)addr
- offset
;
1238 req
->len
= (size
+ offset
+ sizeof(int) - 1) / sizeof(int);
1239 if (server_call( REQ_READ_PROCESS_MEMORY
)) goto error
;
1241 if (size
<= max
- offset
)
1243 memcpy( buffer
, (char *)req
->data
+ offset
, size
);
1247 /* now take care of the remaining data */
1248 memcpy( buffer
, (char *)req
->data
+ offset
, max
- offset
);
1253 if (max
> size
) max
= size
;
1254 req
->handle
= process
;
1255 req
->addr
= (char *)addr
+ pos
;
1256 req
->len
= (max
+ sizeof(int) - 1) / sizeof(int);
1257 if (server_call( REQ_READ_PROCESS_MEMORY
)) goto error
;
1258 memcpy( (char *)buffer
+ pos
, (char *)req
->data
, max
);
1265 if (bytes_read
) *bytes_read
= 0;
1270 /***********************************************************************
1271 * WriteProcessMemory (KERNEL32)
1273 BOOL WINAPI
WriteProcessMemory( HANDLE process
, LPVOID addr
, LPVOID buffer
, DWORD size
,
1274 LPDWORD bytes_written
)
1276 unsigned int first_offset
, last_offset
;
1277 struct write_process_memory_request
*req
= get_req_buffer();
1278 unsigned int max
= server_remaining( req
->data
); /* max length in one request */
1279 unsigned int pos
, last_mask
;
1283 SetLastError( ERROR_INVALID_PARAMETER
);
1286 if (bytes_written
) *bytes_written
= size
;
1288 /* compute the mask for the first int */
1289 req
->first_mask
= ~0;
1290 first_offset
= (unsigned int)addr
% sizeof(int);
1291 memset( &req
->first_mask
, 0, first_offset
);
1293 /* compute the mask for the last int */
1294 last_offset
= (size
+ first_offset
) % sizeof(int);
1296 memset( &last_mask
, 0xff, last_offset
? last_offset
: sizeof(int) );
1298 req
->handle
= process
;
1299 req
->addr
= (char *)addr
- first_offset
;
1300 /* for the first request, use the total length */
1301 req
->len
= (size
+ first_offset
+ sizeof(int) - 1) / sizeof(int);
1303 if (size
+ first_offset
< max
) /* we can do it in one round */
1305 memcpy( (char *)req
->data
+ first_offset
, buffer
, size
);
1306 req
->last_mask
= last_mask
;
1307 if (server_call( REQ_WRITE_PROCESS_MEMORY
)) goto error
;
1311 /* needs multiple server calls */
1313 memcpy( (char *)req
->data
+ first_offset
, buffer
, max
- first_offset
);
1314 req
->last_mask
= ~0;
1315 if (server_call( REQ_WRITE_PROCESS_MEMORY
)) goto error
;
1316 pos
= max
- first_offset
;
1320 if (size
<= max
) /* last one */
1322 req
->last_mask
= last_mask
;
1325 req
->handle
= process
;
1326 req
->addr
= (char *)addr
+ pos
;
1327 req
->len
= (max
+ sizeof(int) - 1) / sizeof(int);
1328 req
->first_mask
= ~0;
1329 memcpy( req
->data
, (char *) buffer
+ pos
, max
);
1330 if (server_call( REQ_WRITE_PROCESS_MEMORY
)) goto error
;
1337 if (bytes_written
) *bytes_written
= 0;
1343 /***********************************************************************
1344 * RegisterServiceProcess (KERNEL, KERNEL32)
1346 * A service process calls this function to ensure that it continues to run
1347 * even after a user logged off.
1349 DWORD WINAPI
RegisterServiceProcess(DWORD dwProcessId
, DWORD dwType
)
1351 /* I don't think that Wine needs to do anything in that function */
1352 return 1; /* success */
1355 /***********************************************************************
1356 * GetExitCodeProcess [KERNEL32.325]
1358 * Gets termination status of specified process
1364 BOOL WINAPI
GetExitCodeProcess(
1365 HANDLE hProcess
, /* [I] handle to the process */
1366 LPDWORD lpExitCode
) /* [O] address to receive termination status */
1369 struct get_process_info_request
*req
= get_req_buffer();
1370 req
->handle
= hProcess
;
1371 if (!server_call( REQ_GET_PROCESS_INFO
))
1373 if (lpExitCode
) *lpExitCode
= req
->exit_code
;
1380 /***********************************************************************
1381 * SetErrorMode (KERNEL32.486)
1383 UINT WINAPI
SetErrorMode( UINT mode
)
1385 UINT old
= PROCESS_Current()->error_mode
;
1386 PROCESS_Current()->error_mode
= mode
;
1390 /***********************************************************************
1391 * GetCurrentProcess (KERNEL32.198)
1393 #undef GetCurrentProcess
1394 HANDLE WINAPI
GetCurrentProcess(void)