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 /* Remember TEB selector of initial process for emergency use */
211 SYSLEVEL_EmergencyTeb
= NtCurrentTeb()->teb_sel
;
213 /* Create the system and process heaps */
214 if (!HEAP_CreateSystemHeap()) return FALSE
;
215 pdb
->heap
= HeapCreate( HEAP_GROWABLE
, 0, 0 );
217 /* Copy the parent environment */
218 if (!ENV_BuildEnvironment()) return FALSE
;
220 /* Create the SEGPTR heap */
221 if (!(SegptrHeap
= HeapCreate( HEAP_WINE_SEGPTR
, 0, 0 ))) return FALSE
;
223 /* Initialize the critical sections */
224 InitializeCriticalSection( &pdb
->crit_section
);
225 InitializeCriticalSection( &initial_envdb
.section
);
227 /* Initialize syslevel handling */
234 /***********************************************************************
237 * Load system DLLs into the initial process (and initialize them)
239 static int load_system_dlls(void)
241 char driver
[MAX_PATH
];
243 PROFILE_GetWineIniString( "Wine", "GraphicsDriver", "x11drv", driver
, sizeof(driver
) );
244 if (!LoadLibraryA( driver
))
246 MESSAGE( "Could not load graphics driver '%s'\n", driver
);
250 if (!LoadLibraryA("USER32.DLL")) return 0;
252 /* Get pointers to USER routines called by KERNEL */
255 /* Call FinalUserInit routine */
256 Callout
.FinalUserInit16();
258 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
259 * context of the parent process. Actually, the USER signal proc
260 * doesn't really care about that, but it *does* require that the
261 * startup parameters are correctly set up, so that GetProcessDword
262 * works. Furthermore, before calling the USER signal proc the
263 * 16-bit stack must be set up, which it is only after TASK_Create
264 * in the case of a 16-bit process. Thus, we send the signal here.
266 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE
, 0 );
267 PROCESS_CallUserSignalProc( USIG_THREAD_INIT
, 0 );
268 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT
, 0 );
269 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED
, 0 );
275 /***********************************************************************
278 * Build the command-line of a process from the argv array.
280 static inline char *build_command_line( char **argv
)
283 char *cmdline
, *p
, **arg
;
285 for (arg
= argv
, len
= 0; *arg
; arg
++) len
+= strlen(*arg
) + 1;
286 if ((quote
= (strchr( argv
[0], ' ' ) != NULL
))) len
+= 2;
287 if (!(p
= cmdline
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
305 if (p
> cmdline
) p
--; /* remove last space */
311 /***********************************************************************
314 * Startup routine of a new process. Runs on the new process stack.
316 static void start_process(void)
318 struct init_process_done_request
*req
= get_req_buffer();
319 int debugged
, console_app
;
321 UINT cmdShow
= SW_SHOWNORMAL
;
322 LPTHREAD_START_ROUTINE entry
;
323 PDB
*pdb
= PROCESS_Current();
324 HMODULE module
= pdb
->exe_modref
->module
;
326 /* Increment EXE refcount */
327 pdb
->exe_modref
->refCount
++;
329 /* build command line */
330 if (!(pdb
->env_db
->cmd_line
= build_command_line( main_exe_argv
))) goto error
;
332 /* Retrieve entry point address */
333 entry
= (LPTHREAD_START_ROUTINE
)RVA_PTR( module
, OptionalHeader
.AddressOfEntryPoint
);
334 console_app
= (PE_HEADER(module
)->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
);
336 if (console_app
) pdb
->flags
|= PDB32_CONSOLE_PROC
;
338 /* Signal the parent process to continue */
339 req
->module
= (void *)module
;
341 req
->name
= &pdb
->exe_modref
->filename
;
342 req
->gui
= !console_app
;
343 server_call( REQ_INIT_PROCESS_DONE
);
344 debugged
= req
->debugged
;
346 /* Install signal handlers; this cannot be done before, since we cannot
347 * send exceptions to the debugger before the create process event that
348 * is sent by REQ_INIT_PROCESS_DONE */
349 if (!SIGNAL_Init()) goto error
;
351 /* Load KERNEL (necessary for TASK_Create) */
352 if (!LoadLibraryA( "KERNEL32" )) goto error
;
354 /* Create 16-bit dummy module */
355 if ((hModule16
= MODULE_CreateDummyModule( pdb
->exe_modref
->filename
, module
)) < 32)
356 ExitProcess( hModule16
);
358 if (pdb
->env_db
->startup_info
->dwFlags
& STARTF_USESHOWWINDOW
)
359 cmdShow
= pdb
->env_db
->startup_info
->wShowWindow
;
360 if (!TASK_Create( (NE_MODULE
*)GlobalLock16( hModule16
), cmdShow
,
361 NtCurrentTeb(), NULL
, 0 ))
364 /* Load the system dlls */
365 if (!load_system_dlls()) goto error
;
367 EnterCriticalSection( &pdb
->crit_section
);
369 MODULE_DllProcessAttach( pdb
->exe_modref
, (LPVOID
)1 );
370 LeaveCriticalSection( &pdb
->crit_section
);
372 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
373 if (console_app
) PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING
, 0 );
375 TRACE_(relay
)( "Starting Win32 process (entryproc=%p)\n", entry
);
376 if (debugged
) DbgBreakPoint();
377 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
378 * Dunno about other OSs */
379 ExitThread( entry(NULL
) );
382 ExitProcess( GetLastError() );
386 /***********************************************************************
389 * Startup routine of a new Win32 process once the main module has been loaded.
390 * The filename is free'd by this routine.
392 static void PROCESS_Start( HMODULE main_module
, LPSTR filename
) WINE_NORETURN
;
393 static void PROCESS_Start( HMODULE main_module
, LPSTR filename
)
397 /* if no explicit filename, use argv[0] */
398 if (!(filename
= malloc( MAX_PATH
))) ExitProcess(1);
399 if (!GetFullPathNameA( argv0
, MAX_PATH
, filename
, NULL
))
400 lstrcpynA( filename
, argv0
, MAX_PATH
);
403 /* load main module */
404 if (PE_HEADER(main_module
)->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
405 ExitProcess( ERROR_BAD_EXE_FORMAT
);
407 /* Create 32-bit MODREF */
408 if (!PE_CreateModule( main_module
, filename
, 0, FALSE
))
412 /* allocate main thread stack */
413 if (!THREAD_InitStack( NtCurrentTeb(),
414 PE_HEADER(main_module
)->OptionalHeader
.SizeOfStackReserve
, TRUE
))
417 /* switch to the new stack */
418 SYSDEPS_SwitchToThreadStack( start_process
);
421 ExitProcess( GetLastError() );
425 /***********************************************************************
428 * Wine initialisation: load and start the main exe file.
430 void PROCESS_InitWine( int argc
, char *argv
[] )
434 /* Initialize everything */
435 if (!MAIN_MainInit( argv
)) exit(1);
437 main_exe_argv
= ++argv
; /* remove argv[0] (wine itself) */
441 char buffer
[MAX_PATH
];
442 if (!argv
[0]) OPTIONS_Usage();
444 /* open the exe file */
445 if (!SearchPathA( NULL
, argv
[0], ".exe", sizeof(buffer
), buffer
, NULL
) &&
446 !SearchPathA( NULL
, argv
[0], NULL
, sizeof(buffer
), buffer
, NULL
))
448 MESSAGE( "%s: cannot find '%s'\n", argv0
, argv
[0] );
451 if (!(main_exe_name
= strdup(buffer
)))
453 MESSAGE( "%s: out of memory\n", argv0
);
458 if (main_exe_file
== INVALID_HANDLE_VALUE
)
460 if ((main_exe_file
= CreateFileA( main_exe_name
, GENERIC_READ
, FILE_SHARE_READ
,
461 NULL
, OPEN_EXISTING
, 0, -1 )) == INVALID_HANDLE_VALUE
)
463 MESSAGE( "%s: cannot open '%s'\n", argv0
, main_exe_name
);
468 if (!MODULE_GetBinaryType( main_exe_file
, main_exe_name
, &type
))
470 MESSAGE( "%s: unrecognized executable '%s'\n", argv0
, main_exe_name
);
476 case SCS_32BIT_BINARY
:
478 HMODULE main_module
= PE_LoadImage( main_exe_file
, main_exe_name
);
479 if (main_module
) PROCESS_Start( main_module
, main_exe_name
);
486 /* create 32-bit module for main exe */
487 if (!(main_module
= BUILTIN32_LoadExeModule())) goto error
;
488 NtCurrentTeb()->tibflags
&= ~TEBF_WIN32
;
489 PROCESS_Current()->flags
|= PDB32_WIN16_PROC
;
490 SYSLEVEL_EnterWin16Lock();
491 PROCESS_Start( main_module
, NULL
);
496 FIXME( "DOS binaries support is broken at the moment; feel free to fix it...\n" );
497 SetLastError( ERROR_BAD_FORMAT
);
501 case SCS_POSIX_BINARY
:
502 case SCS_OS216_BINARY
:
504 MESSAGE( "%s: unrecognized executable '%s'\n", argv0
, main_exe_name
);
505 SetLastError( ERROR_BAD_FORMAT
);
509 ExitProcess( GetLastError() );
513 /***********************************************************************
514 * PROCESS_InitWinelib
516 * Initialisation of a new Winelib process.
518 void PROCESS_InitWinelib( int argc
, char *argv
[] )
522 if (!MAIN_MainInit( argv
)) exit(1);
524 /* create 32-bit module for main exe */
525 if (!(main_module
= BUILTIN32_LoadExeModule())) ExitProcess( GetLastError() );
527 main_exe_argv
= argv
;
528 PROCESS_Start( main_module
, NULL
);
532 /***********************************************************************
535 * Build an argv array from a command-line.
536 * The command-line is modified to insert nulls.
537 * 'reserved' is the number of args to reserve before the first one.
539 static char **build_argv( char *cmdline
, int reserved
)
542 int count
= reserved
+ 1;
545 /* if first word is quoted store it as a single arg */
546 if (*cmdline
== '\"')
548 if ((p
= strchr( cmdline
+ 1, '\"' )))
557 while (*p
&& isspace(*p
)) p
++;
560 while (*p
&& !isspace(*p
)) p
++;
563 if ((argv
= malloc( count
* sizeof(*argv
) )))
565 char **argvptr
= argv
+ reserved
;
567 if (*cmdline
== '\"')
569 if ((p
= strchr( cmdline
+ 1, '\"' )))
571 *argvptr
++ = cmdline
+ 1;
578 while (*p
&& isspace(*p
)) *p
++ = 0;
581 while (*p
&& !isspace(*p
)) p
++;
589 /***********************************************************************
592 * Build the environment of a new child process.
594 static char **build_envp( const char *env
)
600 for (p
= env
, count
= 0; *p
; count
++) p
+= strlen(p
) + 1;
602 if ((envp
= malloc( count
* sizeof(*envp
) )))
604 extern char **environ
;
605 char **envptr
= envp
;
606 char **unixptr
= environ
;
607 /* first put PATH, HOME and WINEPREFIX from the unix env */
608 for (unixptr
= environ
; unixptr
&& *unixptr
; unixptr
++)
609 if (!memcmp( *unixptr
, "PATH=", 5 ) ||
610 !memcmp( *unixptr
, "HOME=", 5 ) ||
611 !memcmp( *unixptr
, "WINEPREFIX=", 11 )) *envptr
++ = *unixptr
;
612 /* now put the Windows environment strings */
613 for (p
= env
; *p
; p
+= strlen(p
) + 1)
615 if (memcmp( p
, "PATH=", 5 ) &&
616 memcmp( p
, "HOME=", 5 ) &&
617 memcmp( p
, "WINEPREFIX=", 11 )) *envptr
++ = (char *)p
;
625 /***********************************************************************
628 * Locate the Wine binary to exec for a new Win32 process.
630 static void exec_wine_binary( char **argv
, char **envp
)
632 const char *path
, *pos
, *ptr
;
634 /* first try bin directory */
635 argv
[0] = BINDIR
"/wine";
636 execve( argv
[0], argv
, envp
);
638 /* now try the path of argv0 of the current binary */
639 if (!(argv
[0] = malloc( strlen(argv0
) + 6 ))) return;
640 if ((ptr
= strrchr( argv0
, '/' )))
642 memcpy( argv
[0], argv0
, ptr
- argv0
);
643 strcpy( argv
[0] + (ptr
- argv0
), "/wine" );
644 execve( argv
[0], argv
, envp
);
648 /* now search in the Unix path */
649 if ((path
= getenv( "PATH" )))
651 if (!(argv
[0] = malloc( strlen(path
) + 6 ))) return;
655 while (*pos
== ':') pos
++;
657 if (!(ptr
= strchr( pos
, ':' ))) ptr
= pos
+ strlen(pos
);
658 memcpy( argv
[0], pos
, ptr
- pos
);
659 strcpy( argv
[0] + (ptr
- pos
), "/wine" );
660 execve( argv
[0], argv
, envp
);
666 /* finally try the current directory */
668 execve( argv
[0], argv
, envp
);
672 /***********************************************************************
675 * Fork and exec a new Unix process, checking for errors.
677 static int fork_and_exec( const char *filename
, const char *cmdline
, const char *env
)
687 fcntl( fd
[1], F_SETFD
, 1 ); /* set close on exec */
688 if (!(pid
= fork())) /* child */
690 char **argv
= build_argv( (char *)cmdline
, filename
? 0 : 2 );
691 char **envp
= build_envp( env
);
698 exec_wine_binary( argv
, envp
);
700 else execve( filename
, argv
, envp
);
703 write( fd
[1], &err
, sizeof(err
) );
707 if ((pid
!= -1) && (read( fd
[0], &err
, sizeof(err
) ) > 0)) /* exec failed */
712 if (pid
== -1) FILE_SetDosError();
718 /***********************************************************************
721 * Create a new process. If hFile is a valid handle we have an exe
722 * file, and we exec a new copy of wine to load it; otherwise we
723 * simply exec the specified filename as a Unix process.
725 BOOL
PROCESS_Create( HFILE hFile
, LPCSTR filename
, LPCSTR cmd_line
, LPCSTR env
,
726 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
727 BOOL inherit
, DWORD flags
, LPSTARTUPINFOA startup
,
728 LPPROCESS_INFORMATION info
)
731 const char *unixfilename
= NULL
;
732 DOS_FULL_NAME full_name
;
733 HANDLE load_done_evt
= -1;
734 struct new_process_request
*req
= get_req_buffer();
735 struct wait_process_request
*wait_req
= get_req_buffer();
737 info
->hThread
= info
->hProcess
= INVALID_HANDLE_VALUE
;
739 /* create the process on the server side */
741 req
->inherit_all
= inherit
;
742 req
->create_flags
= flags
;
743 req
->start_flags
= startup
->dwFlags
;
744 req
->exe_file
= hFile
;
745 if (startup
->dwFlags
& STARTF_USESTDHANDLES
)
747 req
->hstdin
= startup
->hStdInput
;
748 req
->hstdout
= startup
->hStdOutput
;
749 req
->hstderr
= startup
->hStdError
;
753 req
->hstdin
= GetStdHandle( STD_INPUT_HANDLE
);
754 req
->hstdout
= GetStdHandle( STD_OUTPUT_HANDLE
);
755 req
->hstderr
= GetStdHandle( STD_ERROR_HANDLE
);
757 req
->cmd_show
= startup
->wShowWindow
;
760 if (hFile
== -1) /* unix process */
762 unixfilename
= filename
;
763 if (DOSFS_GetFullName( filename
, TRUE
, &full_name
)) unixfilename
= full_name
.long_name
;
764 req
->filename
[0] = 0;
766 else /* new wine process */
768 if (!GetFullPathNameA( filename
, server_remaining(req
->filename
), req
->filename
, NULL
))
769 lstrcpynA( req
->filename
, filename
, server_remaining(req
->filename
) );
771 if (server_call( REQ_NEW_PROCESS
)) return FALSE
;
773 /* fork and execute */
775 pid
= fork_and_exec( unixfilename
, cmd_line
, env
? env
: GetEnvironmentStringsA() );
777 wait_req
->cancel
= (pid
== -1);
778 wait_req
->pinherit
= (psa
&& (psa
->nLength
>= sizeof(*psa
)) && psa
->bInheritHandle
);
779 wait_req
->tinherit
= (tsa
&& (tsa
->nLength
>= sizeof(*tsa
)) && tsa
->bInheritHandle
);
780 wait_req
->timeout
= 2000;
781 if (server_call( REQ_WAIT_PROCESS
) || (pid
== -1)) goto error
;
782 info
->dwProcessId
= (DWORD
)wait_req
->pid
;
783 info
->dwThreadId
= (DWORD
)wait_req
->tid
;
784 info
->hProcess
= wait_req
->phandle
;
785 info
->hThread
= wait_req
->thandle
;
786 load_done_evt
= wait_req
->event
;
788 /* Wait until process is initialized (or initialization failed) */
789 if (load_done_evt
!= -1)
794 handles
[0] = info
->hProcess
;
795 handles
[1] = load_done_evt
;
796 res
= WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
);
797 CloseHandle( load_done_evt
);
798 if (res
== STATUS_WAIT_0
) /* the process died */
801 if (GetExitCodeProcess( info
->hProcess
, &exitcode
)) SetLastError( exitcode
);
802 CloseHandle( info
->hThread
);
803 CloseHandle( info
->hProcess
);
810 if (load_done_evt
!= -1) CloseHandle( load_done_evt
);
811 if (info
->hThread
!= INVALID_HANDLE_VALUE
) CloseHandle( info
->hThread
);
812 if (info
->hProcess
!= INVALID_HANDLE_VALUE
) CloseHandle( info
->hProcess
);
817 /***********************************************************************
818 * ExitProcess (KERNEL32.100)
820 void WINAPI
ExitProcess( DWORD status
)
822 struct terminate_process_request
*req
= get_req_buffer();
824 MODULE_DllProcessDetach( TRUE
, (LPVOID
)1 );
825 /* send the exit code to the server */
826 req
->handle
= GetCurrentProcess();
827 req
->exit_code
= status
;
828 server_call( REQ_TERMINATE_PROCESS
);
832 /***********************************************************************
833 * ExitProcess16 (KERNEL.466)
835 void WINAPI
ExitProcess16( WORD status
)
837 SYSLEVEL_ReleaseWin16Lock();
838 ExitProcess( status
);
841 /******************************************************************************
842 * TerminateProcess (KERNEL32.684)
844 BOOL WINAPI
TerminateProcess( HANDLE handle
, DWORD exit_code
)
847 struct terminate_process_request
*req
= get_req_buffer();
848 req
->handle
= handle
;
849 req
->exit_code
= exit_code
;
850 if ((ret
= !server_call( REQ_TERMINATE_PROCESS
)) && req
->self
) exit( exit_code
);
855 /***********************************************************************
856 * GetProcessDword (KERNEL32.18) (KERNEL.485)
857 * 'Of course you cannot directly access Windows internal structures'
859 DWORD WINAPI
GetProcessDword( DWORD dwProcessID
, INT offset
)
861 PDB
*process
= PROCESS_IdToPDB( dwProcessID
);
865 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
868 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
874 case GPD_APP_COMPAT_FLAGS
:
875 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
876 return pTask
? pTask
->compat_flags
: 0;
878 case GPD_LOAD_DONE_EVENT
:
879 return process
->load_done_evt
;
881 case GPD_HINSTANCE16
:
882 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
883 return pTask
? pTask
->hInstance
: 0;
885 case GPD_WINDOWS_VERSION
:
886 pTask
= (TDB
*)GlobalLock16( GetCurrentTask() );
887 return pTask
? pTask
->version
: 0;
890 if ( process
!= PROCESS_Current() ) return 0;
891 return (DWORD
)NtCurrentTeb() - 0x10 /* FIXME */;
894 return (DWORD
)process
;
896 case GPD_STARTF_SHELLDATA
: /* return stdoutput handle from startupinfo ??? */
897 return process
->env_db
->startup_info
->hStdOutput
;
899 case GPD_STARTF_HOTKEY
: /* return stdinput handle from startupinfo ??? */
900 return process
->env_db
->startup_info
->hStdInput
;
902 case GPD_STARTF_SHOWWINDOW
:
903 return process
->env_db
->startup_info
->wShowWindow
;
905 case GPD_STARTF_SIZE
:
906 x
= process
->env_db
->startup_info
->dwXSize
;
907 if ( x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
908 y
= process
->env_db
->startup_info
->dwYSize
;
909 if ( y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
910 return MAKELONG( x
, y
);
912 case GPD_STARTF_POSITION
:
913 x
= process
->env_db
->startup_info
->dwX
;
914 if ( x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
915 y
= process
->env_db
->startup_info
->dwY
;
916 if ( y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
917 return MAKELONG( x
, y
);
919 case GPD_STARTF_FLAGS
:
920 return process
->env_db
->startup_info
->dwFlags
;
926 return process
->flags
;
929 return process
->process_dword
;
932 ERR_(win32
)("Unknown offset %d\n", offset
);
937 /***********************************************************************
938 * SetProcessDword (KERNEL.484)
939 * 'Of course you cannot directly access Windows internal structures'
941 void WINAPI
SetProcessDword( DWORD dwProcessID
, INT offset
, DWORD value
)
943 PDB
*process
= PROCESS_IdToPDB( dwProcessID
);
945 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
948 ERR("%d: process %lx not accessible\n", offset
, dwProcessID
);
954 case GPD_APP_COMPAT_FLAGS
:
955 case GPD_LOAD_DONE_EVENT
:
956 case GPD_HINSTANCE16
:
957 case GPD_WINDOWS_VERSION
:
960 case GPD_STARTF_SHELLDATA
:
961 case GPD_STARTF_HOTKEY
:
962 case GPD_STARTF_SHOWWINDOW
:
963 case GPD_STARTF_SIZE
:
964 case GPD_STARTF_POSITION
:
965 case GPD_STARTF_FLAGS
:
968 ERR_(win32
)("Not allowed to modify offset %d\n", offset
);
972 process
->process_dword
= value
;
976 ERR_(win32
)("Unknown offset %d\n", offset
);
982 /*********************************************************************
983 * OpenProcess (KERNEL32.543)
985 HANDLE WINAPI
OpenProcess( DWORD access
, BOOL inherit
, DWORD id
)
988 struct open_process_request
*req
= get_req_buffer();
990 req
->pid
= (void *)id
;
991 req
->access
= access
;
992 req
->inherit
= inherit
;
993 if (!server_call( REQ_OPEN_PROCESS
)) ret
= req
->handle
;
997 /*********************************************************************
998 * MapProcessHandle (KERNEL.483)
1000 DWORD WINAPI
MapProcessHandle( HANDLE handle
)
1003 struct get_process_info_request
*req
= get_req_buffer();
1004 req
->handle
= handle
;
1005 if (!server_call( REQ_GET_PROCESS_INFO
)) ret
= (DWORD
)req
->pid
;
1009 /***********************************************************************
1010 * GetThreadLocale (KERNEL32.295)
1012 LCID WINAPI
GetThreadLocale(void)
1014 return PROCESS_Current()->locale
;
1018 /***********************************************************************
1019 * SetPriorityClass (KERNEL32.503)
1021 BOOL WINAPI
SetPriorityClass( HANDLE hprocess
, DWORD priorityclass
)
1023 struct set_process_info_request
*req
= get_req_buffer();
1024 req
->handle
= hprocess
;
1025 req
->priority
= priorityclass
;
1026 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
1027 return !server_call( REQ_SET_PROCESS_INFO
);
1031 /***********************************************************************
1032 * GetPriorityClass (KERNEL32.250)
1034 DWORD WINAPI
GetPriorityClass(HANDLE hprocess
)
1037 struct get_process_info_request
*req
= get_req_buffer();
1038 req
->handle
= hprocess
;
1039 if (!server_call( REQ_GET_PROCESS_INFO
)) ret
= req
->priority
;
1044 /***********************************************************************
1045 * SetProcessAffinityMask (KERNEL32.662)
1047 BOOL WINAPI
SetProcessAffinityMask( HANDLE hProcess
, DWORD affmask
)
1049 struct set_process_info_request
*req
= get_req_buffer();
1050 req
->handle
= hProcess
;
1051 req
->affinity
= affmask
;
1052 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
1053 return !server_call( REQ_SET_PROCESS_INFO
);
1056 /**********************************************************************
1057 * GetProcessAffinityMask (KERNEL32.373)
1059 BOOL WINAPI
GetProcessAffinityMask( HANDLE hProcess
,
1060 LPDWORD lpProcessAffinityMask
,
1061 LPDWORD lpSystemAffinityMask
)
1064 struct get_process_info_request
*req
= get_req_buffer();
1065 req
->handle
= hProcess
;
1066 if (!server_call( REQ_GET_PROCESS_INFO
))
1068 if (lpProcessAffinityMask
) *lpProcessAffinityMask
= req
->process_affinity
;
1069 if (lpSystemAffinityMask
) *lpSystemAffinityMask
= req
->system_affinity
;
1076 /***********************************************************************
1077 * GetStdHandle (KERNEL32.276)
1079 HANDLE WINAPI
GetStdHandle( DWORD std_handle
)
1081 PDB
*pdb
= PROCESS_Current();
1085 case STD_INPUT_HANDLE
: return pdb
->env_db
->hStdin
;
1086 case STD_OUTPUT_HANDLE
: return pdb
->env_db
->hStdout
;
1087 case STD_ERROR_HANDLE
: return pdb
->env_db
->hStderr
;
1089 SetLastError( ERROR_INVALID_PARAMETER
);
1090 return INVALID_HANDLE_VALUE
;
1094 /***********************************************************************
1095 * SetStdHandle (KERNEL32.506)
1097 BOOL WINAPI
SetStdHandle( DWORD std_handle
, HANDLE handle
)
1099 PDB
*pdb
= PROCESS_Current();
1100 /* FIXME: should we close the previous handle? */
1103 case STD_INPUT_HANDLE
:
1104 pdb
->env_db
->hStdin
= handle
;
1106 case STD_OUTPUT_HANDLE
:
1107 pdb
->env_db
->hStdout
= handle
;
1109 case STD_ERROR_HANDLE
:
1110 pdb
->env_db
->hStderr
= handle
;
1113 SetLastError( ERROR_INVALID_PARAMETER
);
1117 /***********************************************************************
1118 * GetProcessVersion (KERNEL32)
1120 DWORD WINAPI
GetProcessVersion( DWORD processid
)
1123 PDB
*pdb
= PROCESS_IdToPDB( processid
);
1126 if (!(pTask
= (TDB
*)GlobalLock16( pdb
->task
))) return 0;
1127 return (pTask
->version
&0xff) | (((pTask
->version
>>8) & 0xff)<<16);
1130 /***********************************************************************
1131 * GetProcessFlags (KERNEL32)
1133 DWORD WINAPI
GetProcessFlags( DWORD processid
)
1135 PDB
*pdb
= PROCESS_IdToPDB( processid
);
1140 /***********************************************************************
1141 * SetProcessWorkingSetSize [KERNEL32.662]
1142 * Sets the min/max working set sizes for a specified process.
1145 * hProcess [I] Handle to the process of interest
1146 * minset [I] Specifies minimum working set size
1147 * maxset [I] Specifies maximum working set size
1151 BOOL WINAPI
SetProcessWorkingSetSize(HANDLE hProcess
,DWORD minset
,
1154 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess
,minset
,maxset
);
1155 if(( minset
== -1) && (maxset
== -1)) {
1156 /* Trim the working set to zero */
1157 /* Swap the process out of physical RAM */
1162 /***********************************************************************
1163 * GetProcessWorkingSetSize (KERNEL32)
1165 BOOL WINAPI
GetProcessWorkingSetSize(HANDLE hProcess
,LPDWORD minset
,
1168 FIXME("(0x%08x,%p,%p): stub\n",hProcess
,minset
,maxset
);
1169 /* 32 MB working set size */
1170 if (minset
) *minset
= 32*1024*1024;
1171 if (maxset
) *maxset
= 32*1024*1024;
1175 /***********************************************************************
1176 * SetProcessShutdownParameters (KERNEL32)
1178 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1179 * Now tracks changes made (but does not act on these changes)
1180 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
1181 * It really shouldn't be here, but I'll move it when it's been checked!
1183 #define SHUTDOWN_NORETRY 1
1184 static unsigned int shutdown_noretry
= 0;
1185 static unsigned int shutdown_priority
= 0x280L
;
1186 BOOL WINAPI
SetProcessShutdownParameters(DWORD level
,DWORD flags
)
1188 if (flags
& SHUTDOWN_NORETRY
)
1189 shutdown_noretry
= 1;
1191 shutdown_noretry
= 0;
1192 if (level
> 0x100L
&& level
< 0x3FFL
)
1193 shutdown_priority
= level
;
1196 ERR("invalid priority level 0x%08lx\n", level
);
1203 /***********************************************************************
1204 * GetProcessShutdownParameters (KERNEL32)
1207 BOOL WINAPI
GetProcessShutdownParameters( LPDWORD lpdwLevel
,
1210 (*lpdwLevel
) = shutdown_priority
;
1211 (*lpdwFlags
) = (shutdown_noretry
* SHUTDOWN_NORETRY
);
1214 /***********************************************************************
1215 * SetProcessPriorityBoost (KERNEL32)
1217 BOOL WINAPI
SetProcessPriorityBoost(HANDLE hprocess
,BOOL disableboost
)
1219 FIXME("(%d,%d): stub\n",hprocess
,disableboost
);
1220 /* Say we can do it. I doubt the program will notice that we don't. */
1225 /***********************************************************************
1226 * ReadProcessMemory (KERNEL32)
1228 BOOL WINAPI
ReadProcessMemory( HANDLE process
, LPCVOID addr
, LPVOID buffer
, DWORD size
,
1229 LPDWORD bytes_read
)
1231 struct read_process_memory_request
*req
= get_req_buffer();
1232 unsigned int offset
= (unsigned int)addr
% sizeof(int);
1233 unsigned int max
= server_remaining( req
->data
); /* max length in one request */
1236 if (bytes_read
) *bytes_read
= size
;
1238 /* first time, read total length to check for permissions */
1239 req
->handle
= process
;
1240 req
->addr
= (char *)addr
- offset
;
1241 req
->len
= (size
+ offset
+ sizeof(int) - 1) / sizeof(int);
1242 if (server_call( REQ_READ_PROCESS_MEMORY
)) goto error
;
1244 if (size
<= max
- offset
)
1246 memcpy( buffer
, (char *)req
->data
+ offset
, size
);
1250 /* now take care of the remaining data */
1251 memcpy( buffer
, (char *)req
->data
+ offset
, max
- offset
);
1256 if (max
> size
) max
= size
;
1257 req
->handle
= process
;
1258 req
->addr
= (char *)addr
+ pos
;
1259 req
->len
= (max
+ sizeof(int) - 1) / sizeof(int);
1260 if (server_call( REQ_READ_PROCESS_MEMORY
)) goto error
;
1261 memcpy( (char *)buffer
+ pos
, (char *)req
->data
, max
);
1268 if (bytes_read
) *bytes_read
= 0;
1273 /***********************************************************************
1274 * WriteProcessMemory (KERNEL32)
1276 BOOL WINAPI
WriteProcessMemory( HANDLE process
, LPVOID addr
, LPVOID buffer
, DWORD size
,
1277 LPDWORD bytes_written
)
1279 unsigned int first_offset
, last_offset
;
1280 struct write_process_memory_request
*req
= get_req_buffer();
1281 unsigned int max
= server_remaining( req
->data
); /* max length in one request */
1282 unsigned int pos
, last_mask
;
1286 SetLastError( ERROR_INVALID_PARAMETER
);
1289 if (bytes_written
) *bytes_written
= size
;
1291 /* compute the mask for the first int */
1292 req
->first_mask
= ~0;
1293 first_offset
= (unsigned int)addr
% sizeof(int);
1294 memset( &req
->first_mask
, 0, first_offset
);
1296 /* compute the mask for the last int */
1297 last_offset
= (size
+ first_offset
) % sizeof(int);
1299 memset( &last_mask
, 0xff, last_offset
? last_offset
: sizeof(int) );
1301 req
->handle
= process
;
1302 req
->addr
= (char *)addr
- first_offset
;
1303 /* for the first request, use the total length */
1304 req
->len
= (size
+ first_offset
+ sizeof(int) - 1) / sizeof(int);
1306 if (size
+ first_offset
< max
) /* we can do it in one round */
1308 memcpy( (char *)req
->data
+ first_offset
, buffer
, size
);
1309 req
->last_mask
= last_mask
;
1310 if (server_call( REQ_WRITE_PROCESS_MEMORY
)) goto error
;
1314 /* needs multiple server calls */
1316 memcpy( (char *)req
->data
+ first_offset
, buffer
, max
- first_offset
);
1317 req
->last_mask
= ~0;
1318 if (server_call( REQ_WRITE_PROCESS_MEMORY
)) goto error
;
1319 pos
= max
- first_offset
;
1323 if (size
<= max
) /* last one */
1325 req
->last_mask
= last_mask
;
1328 req
->handle
= process
;
1329 req
->addr
= (char *)addr
+ pos
;
1330 req
->len
= (max
+ sizeof(int) - 1) / sizeof(int);
1331 req
->first_mask
= ~0;
1332 memcpy( req
->data
, (char *) buffer
+ pos
, max
);
1333 if (server_call( REQ_WRITE_PROCESS_MEMORY
)) goto error
;
1340 if (bytes_written
) *bytes_written
= 0;
1346 /***********************************************************************
1347 * RegisterServiceProcess (KERNEL, KERNEL32)
1349 * A service process calls this function to ensure that it continues to run
1350 * even after a user logged off.
1352 DWORD WINAPI
RegisterServiceProcess(DWORD dwProcessId
, DWORD dwType
)
1354 /* I don't think that Wine needs to do anything in that function */
1355 return 1; /* success */
1358 /***********************************************************************
1359 * GetExitCodeProcess [KERNEL32.325]
1361 * Gets termination status of specified process
1367 BOOL WINAPI
GetExitCodeProcess(
1368 HANDLE hProcess
, /* [I] handle to the process */
1369 LPDWORD lpExitCode
) /* [O] address to receive termination status */
1372 struct get_process_info_request
*req
= get_req_buffer();
1373 req
->handle
= hProcess
;
1374 if (!server_call( REQ_GET_PROCESS_INFO
))
1376 if (lpExitCode
) *lpExitCode
= req
->exit_code
;
1383 /***********************************************************************
1384 * SetErrorMode (KERNEL32.486)
1386 UINT WINAPI
SetErrorMode( UINT mode
)
1388 UINT old
= PROCESS_Current()->error_mode
;
1389 PROCESS_Current()->error_mode
= mode
;
1393 /***********************************************************************
1394 * GetCurrentProcess (KERNEL32.198)
1396 #undef GetCurrentProcess
1397 HANDLE WINAPI
GetCurrentProcess(void)