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,
675 const char *env, int use_wine )
685 fcntl( fd[1], F_SETFD, 1 ); /* set close on exec */
686 if (!(pid = fork())) /* child */
688 char **argv = build_argv( (char *)cmdline, use_wine ? 2 : 0 );
689 char **envp = build_envp( env );
696 exec_wine_binary( argv, envp );
698 else execve( filename, argv, envp );
701 write( fd[1], &err, sizeof(err) );
705 if ((pid != -1) && (read( fd[0], &err, sizeof(err) ) > 0)) /* exec failed */
710 if (pid == -1) FILE_SetDosError();
716 /***********************************************************************
719 * Create a new process. If hFile is a valid handle we have an exe
720 * file, and we exec a new copy of wine to load it; otherwise we
721 * simply exec the specified filename as a Unix process.
723 BOOL PROCESS_Create( HFILE hFile, LPCSTR filename, LPCSTR cmd_line, LPCSTR env,
724 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
725 BOOL inherit, DWORD flags, LPSTARTUPINFOA startup,
726 LPPROCESS_INFORMATION info )
729 const char *unixfilename = filename;
730 DOS_FULL_NAME full_name;
731 HANDLE load_done_evt = -1;
732 struct new_process_request *req = get_req_buffer();
733 struct wait_process_request *wait_req = get_req_buffer();
735 info->hThread = info->hProcess = INVALID_HANDLE_VALUE;
737 if (DOSFS_GetFullName( filename, TRUE, &full_name )) unixfilename = full_name.long_name;
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;
759 lstrcpynA( req->filename, unixfilename, server_remaining(req->filename) );
760 if (server_call( REQ_NEW_PROCESS )) return FALSE;
762 /* fork and execute */
764 pid = fork_and_exec( unixfilename, cmd_line,
765 env ? env : GetEnvironmentStringsA(), (hFile != -1) );
767 wait_req->cancel = (pid == -1);
768 wait_req->pinherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
769 wait_req->tinherit = (tsa && (tsa->nLength >= sizeof(*tsa)) && tsa->bInheritHandle);
770 wait_req->timeout = 2000;
771 if (server_call( REQ_WAIT_PROCESS ) || (pid == -1)) goto error;
772 info->dwProcessId = (DWORD)wait_req->pid;
773 info->dwThreadId = (DWORD)wait_req->tid;
774 info->hProcess = wait_req->phandle;
775 info->hThread = wait_req->thandle;
776 load_done_evt = wait_req->event;
778 /* Wait until process is initialized (or initialization failed) */
779 if (load_done_evt != -1)
784 handles[0] = info->hProcess;
785 handles[1] = load_done_evt;
786 res = WaitForMultipleObjects( 2, handles, FALSE, INFINITE );
787 CloseHandle( load_done_evt );
788 if (res == STATUS_WAIT_0) /* the process died */
791 if (GetExitCodeProcess( info->hProcess, &exitcode )) SetLastError( exitcode );
792 CloseHandle( info->hThread );
793 CloseHandle( info->hProcess );
800 if (load_done_evt != -1) CloseHandle( load_done_evt );
801 if (info->hThread != INVALID_HANDLE_VALUE) CloseHandle( info->hThread );
802 if (info->hProcess != INVALID_HANDLE_VALUE) CloseHandle( info->hProcess );
807 /***********************************************************************
808 * ExitProcess (KERNEL32.100)
810 void WINAPI ExitProcess( DWORD status )
812 struct terminate_process_request *req = get_req_buffer();
814 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
815 /* send the exit code to the server */
816 req->handle = GetCurrentProcess();
817 req->exit_code = status;
818 server_call( REQ_TERMINATE_PROCESS );
822 /***********************************************************************
823 * ExitProcess16 (KERNEL.466)
825 void WINAPI ExitProcess16( WORD status )
827 SYSLEVEL_ReleaseWin16Lock();
828 ExitProcess( status );
831 /******************************************************************************
832 * TerminateProcess (KERNEL32.684)
834 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
837 struct terminate_process_request *req = get_req_buffer();
838 req->handle = handle;
839 req->exit_code = exit_code;
840 if ((ret = !server_call( REQ_TERMINATE_PROCESS )) && req->self) exit( exit_code );
845 /***********************************************************************
846 * GetProcessDword (KERNEL32.18) (KERNEL.485)
847 * 'Of course you cannot directly access Windows internal structures'
849 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
851 PDB *process = PROCESS_IdToPDB( dwProcessID );
855 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
858 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
864 case GPD_APP_COMPAT_FLAGS:
865 pTask = (TDB *)GlobalLock16( GetCurrentTask() );
866 return pTask? pTask->compat_flags : 0;
868 case GPD_LOAD_DONE_EVENT:
869 return process->load_done_evt;
871 case GPD_HINSTANCE16:
872 pTask = (TDB *)GlobalLock16( GetCurrentTask() );
873 return pTask? pTask->hInstance : 0;
875 case GPD_WINDOWS_VERSION:
876 pTask = (TDB *)GlobalLock16( GetCurrentTask() );
877 return pTask? pTask->version : 0;
880 if ( process != PROCESS_Current() ) return 0;
881 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
884 return (DWORD)process;
886 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
887 return process->env_db->startup_info->hStdOutput;
889 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
890 return process->env_db->startup_info->hStdInput;
892 case GPD_STARTF_SHOWWINDOW:
893 return process->env_db->startup_info->wShowWindow;
895 case GPD_STARTF_SIZE:
896 x = process->env_db->startup_info->dwXSize;
897 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
898 y = process->env_db->startup_info->dwYSize;
899 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
900 return MAKELONG( x, y );
902 case GPD_STARTF_POSITION:
903 x = process->env_db->startup_info->dwX;
904 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
905 y = process->env_db->startup_info->dwY;
906 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
907 return MAKELONG( x, y );
909 case GPD_STARTF_FLAGS:
910 return process->env_db->startup_info->dwFlags;
916 return process->flags;
919 return process->process_dword;
922 ERR_(win32)("Unknown offset %d\n", offset );
927 /***********************************************************************
928 * SetProcessDword (KERNEL.484)
929 * 'Of course you cannot directly access Windows internal structures'
931 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
933 PDB *process = PROCESS_IdToPDB( dwProcessID );
935 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
938 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
944 case GPD_APP_COMPAT_FLAGS:
945 case GPD_LOAD_DONE_EVENT:
946 case GPD_HINSTANCE16:
947 case GPD_WINDOWS_VERSION:
950 case GPD_STARTF_SHELLDATA:
951 case GPD_STARTF_HOTKEY:
952 case GPD_STARTF_SHOWWINDOW:
953 case GPD_STARTF_SIZE:
954 case GPD_STARTF_POSITION:
955 case GPD_STARTF_FLAGS:
958 ERR_(win32)("Not allowed to modify offset %d\n", offset );
962 process->process_dword = value;
966 ERR_(win32)("Unknown offset %d\n", offset );
972 /*********************************************************************
973 * OpenProcess (KERNEL32.543)
975 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
978 struct open_process_request *req = get_req_buffer();
980 req->pid = (void *)id;
981 req->access = access;
982 req->inherit = inherit;
983 if (!server_call( REQ_OPEN_PROCESS )) ret = req->handle;
987 /*********************************************************************
988 * MapProcessHandle (KERNEL.483)
990 DWORD WINAPI MapProcessHandle( HANDLE handle )
993 struct get_process_info_request *req = get_req_buffer();
994 req->handle = handle;
995 if (!server_call( REQ_GET_PROCESS_INFO )) ret = (DWORD)req->pid;
999 /***********************************************************************
1000 * GetThreadLocale (KERNEL32.295)
1002 LCID WINAPI GetThreadLocale(void)
1004 return PROCESS_Current()->locale;
1008 /***********************************************************************
1009 * SetPriorityClass (KERNEL32.503)
1011 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
1013 struct set_process_info_request *req = get_req_buffer();
1014 req->handle = hprocess;
1015 req->priority = priorityclass;
1016 req->mask = SET_PROCESS_INFO_PRIORITY;
1017 return !server_call( REQ_SET_PROCESS_INFO );
1021 /***********************************************************************
1022 * GetPriorityClass (KERNEL32.250)
1024 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
1027 struct get_process_info_request *req = get_req_buffer();
1028 req->handle = hprocess;
1029 if (!server_call( REQ_GET_PROCESS_INFO )) ret = req->priority;
1034 /***********************************************************************
1035 * SetProcessAffinityMask (KERNEL32.662)
1037 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
1039 struct set_process_info_request *req = get_req_buffer();
1040 req->handle = hProcess;
1041 req->affinity = affmask;
1042 req->mask = SET_PROCESS_INFO_AFFINITY;
1043 return !server_call( REQ_SET_PROCESS_INFO );
1046 /**********************************************************************
1047 * GetProcessAffinityMask (KERNEL32.373)
1049 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
1050 LPDWORD lpProcessAffinityMask,
1051 LPDWORD lpSystemAffinityMask )
1054 struct get_process_info_request *req = get_req_buffer();
1055 req->handle = hProcess;
1056 if (!server_call( REQ_GET_PROCESS_INFO ))
1058 if (lpProcessAffinityMask) *lpProcessAffinityMask = req->process_affinity;
1059 if (lpSystemAffinityMask) *lpSystemAffinityMask = req->system_affinity;
1066 /***********************************************************************
1067 * GetStdHandle (KERNEL32.276)
1069 HANDLE WINAPI GetStdHandle( DWORD std_handle )
1071 PDB *pdb = PROCESS_Current();
1075 case STD_INPUT_HANDLE: return pdb->env_db->hStdin;
1076 case STD_OUTPUT_HANDLE: return pdb->env_db->hStdout;
1077 case STD_ERROR_HANDLE: return pdb->env_db->hStderr;
1079 SetLastError( ERROR_INVALID_PARAMETER );
1080 return INVALID_HANDLE_VALUE;
1084 /***********************************************************************
1085 * SetStdHandle (KERNEL32.506)
1087 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
1089 PDB *pdb = PROCESS_Current();
1090 /* FIXME: should we close the previous handle? */
1093 case STD_INPUT_HANDLE:
1094 pdb->env_db->hStdin = handle;
1096 case STD_OUTPUT_HANDLE:
1097 pdb->env_db->hStdout = handle;
1099 case STD_ERROR_HANDLE:
1100 pdb->env_db->hStderr = handle;
1103 SetLastError( ERROR_INVALID_PARAMETER );
1107 /***********************************************************************
1108 * GetProcessVersion (KERNEL32)
1110 DWORD WINAPI GetProcessVersion( DWORD processid )
1113 PDB *pdb = PROCESS_IdToPDB( processid );
1116 if (!(pTask = (TDB *)GlobalLock16( pdb->task ))) return 0;
1117 return (pTask->version&0xff) | (((pTask->version >>8) & 0xff)<<16);
1120 /***********************************************************************
1121 * GetProcessFlags (KERNEL32)
1123 DWORD WINAPI GetProcessFlags( DWORD processid )
1125 PDB *pdb = PROCESS_IdToPDB( processid );
1130 /***********************************************************************
1131 * SetProcessWorkingSetSize [KERNEL32.662]
1132 * Sets the min/max working set sizes for a specified process.
1135 * hProcess [I] Handle to the process of interest
1136 * minset [I] Specifies minimum working set size
1137 * maxset [I] Specifies maximum working set size
1141 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess,DWORD minset,
1144 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
1145 if(( minset == -1) && (maxset == -1)) {
1146 /* Trim the working set to zero */
1147 /* Swap the process out of physical RAM */
1152 /***********************************************************************
1153 * GetProcessWorkingSetSize (KERNEL32)
1155 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess,LPDWORD minset,
1158 FIXME("(0x%08x,%p,%p): stub\n",hProcess,minset,maxset);
1159 /* 32 MB working set size */
1160 if (minset) *minset = 32*1024*1024;
1161 if (maxset) *maxset = 32*1024*1024;
1165 /***********************************************************************
1166 * SetProcessShutdownParameters (KERNEL32)
1168 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1169 * Now tracks changes made (but does not act on these changes)
1170 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
1171 * It really shouldn't be here, but I'll move it when it's been checked!
1173 #define SHUTDOWN_NORETRY 1
1174 static unsigned int shutdown_noretry = 0;
1175 static unsigned int shutdown_priority = 0x280L;
1176 BOOL WINAPI SetProcessShutdownParameters(DWORD level,DWORD flags)
1178 if (flags & SHUTDOWN_NORETRY)
1179 shutdown_noretry = 1;
1181 shutdown_noretry = 0;
1182 if (level > 0x100L && level < 0x3FFL)
1183 shutdown_priority = level;
1186 ERR("invalid priority level 0x%08lx\n", level);
1193 /***********************************************************************
1194 * GetProcessShutdownParameters (KERNEL32)
1197 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel,
1200 (*lpdwLevel) = shutdown_priority;
1201 (*lpdwFlags) = (shutdown_noretry * SHUTDOWN_NORETRY);
1204 /***********************************************************************
1205 * SetProcessPriorityBoost (KERNEL32)
1207 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
1209 FIXME("(%d,%d): stub\n",hprocess,disableboost);
1210 /* Say we can do it. I doubt the program will notice that we don't. */
1215 /***********************************************************************
1216 * ReadProcessMemory (KERNEL32)
1218 BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, DWORD size,
1219 LPDWORD bytes_read )
1221 struct read_process_memory_request *req = get_req_buffer();
1222 unsigned int offset = (unsigned int)addr % sizeof(int);
1223 unsigned int max = server_remaining( req->data ); /* max length in one request */
1226 if (bytes_read) *bytes_read = size;
1228 /* first time, read total length to check for permissions */
1229 req->handle = process;
1230 req->addr = (char *)addr - offset;
1231 req->len = (size + offset + sizeof(int) - 1) / sizeof(int);
1232 if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1234 if (size <= max - offset)
1236 memcpy( buffer, (char *)req->data + offset, size );
1240 /* now take care of the remaining data */
1241 memcpy( buffer, (char *)req->data + offset, max - offset );
1246 if (max > size) max = size;
1247 req->handle = process;
1248 req->addr = (char *)addr + pos;
1249 req->len = (max + sizeof(int) - 1) / sizeof(int);
1250 if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1251 memcpy( (char *)buffer + pos, (char *)req->data, max );
1258 if (bytes_read) *bytes_read = 0;
1263 /***********************************************************************
1264 * WriteProcessMemory (KERNEL32)
1266 BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPVOID buffer, DWORD size,
1267 LPDWORD bytes_written )
1269 unsigned int first_offset, last_offset;
1270 struct write_process_memory_request *req = get_req_buffer();
1271 unsigned int max = server_remaining( req->data ); /* max length in one request */
1272 unsigned int pos, last_mask;
1276 SetLastError( ERROR_INVALID_PARAMETER );
1279 if (bytes_written) *bytes_written = size;
1281 /* compute the mask for the first int */
1282 req->first_mask = ~0;
1283 first_offset = (unsigned int)addr % sizeof(int);
1284 memset( &req->first_mask, 0, first_offset );
1286 /* compute the mask for the last int */
1287 last_offset = (size + first_offset) % sizeof(int);
1289 memset( &last_mask, 0xff, last_offset ? last_offset : sizeof(int) );
1291 req->handle = process;
1292 req->addr = (char *)addr - first_offset;
1293 /* for the first request, use the total length */
1294 req->len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
1296 if (size + first_offset < max) /* we can do it in one round */
1298 memcpy( (char *)req->data + first_offset, buffer, size );
1299 req->last_mask = last_mask;
1300 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1304 /* needs multiple server calls */
1306 memcpy( (char *)req->data + first_offset, buffer, max - first_offset );
1307 req->last_mask = ~0;
1308 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1309 pos = max - first_offset;
1313 if (size <= max) /* last one */
1315 req->last_mask = last_mask;
1318 req->handle = process;
1319 req->addr = (char *)addr + pos;
1320 req->len = (max + sizeof(int) - 1) / sizeof(int);
1321 req->first_mask = ~0;
1322 memcpy( req->data, (char *) buffer + pos, max );
1323 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1330 if (bytes_written) *bytes_written = 0;
1336 /***********************************************************************
1337 * RegisterServiceProcess (KERNEL, KERNEL32)
1339 * A service process calls this function to ensure that it continues to run
1340 * even after a user logged off.
1342 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
1344 /* I don't think that Wine needs to do anything in that function */
1345 return 1; /* success */
1348 /***********************************************************************
1349 * GetExitCodeProcess [KERNEL32.325]
1351 * Gets termination status of specified process
1357 BOOL WINAPI GetExitCodeProcess(
1358 HANDLE hProcess, /* [I] handle to the process */
1359 LPDWORD lpExitCode) /* [O] address to receive termination status */
1362 struct get_process_info_request *req = get_req_buffer();
1363 req->handle = hProcess;
1364 if (!server_call( REQ_GET_PROCESS_INFO ))
1366 if (lpExitCode) *lpExitCode = req->exit_code;
1373 /***********************************************************************
1374 * SetErrorMode (KERNEL32.486)
1376 UINT WINAPI SetErrorMode( UINT mode )
1378 UINT old = PROCESS_Current()->error_mode;
1379 PROCESS_Current()->error_mode = mode;
1383 /***********************************************************************
1384 * GetCurrentProcess (KERNEL32.198)
1386 #undef GetCurrentProcess
1387 HANDLE WINAPI GetCurrentProcess(void)