4 * Copyright 1996, 1998 Alexandre Julliard
15 #include "wine/winbase16.h"
16 #include "wine/exception.h"
17 #include "wine/library.h"
34 #include "debugtools.h"
36 DEFAULT_DEBUG_CHANNEL(process);
37 DECLARE_DEBUG_CHANNEL(relay);
38 DECLARE_DEBUG_CHANNEL(win32);
42 static char **main_exe_argv;
43 static char main_exe_name[MAX_PATH];
44 static HANDLE main_exe_file = INVALID_HANDLE_VALUE;
45 static HMODULE main_module;
47 unsigned int server_startticks;
49 /***********************************************************************
50 * PROCESS_CallUserSignalProc
52 * FIXME: Some of the signals aren't sent correctly!
54 * The exact meaning of the USER signals is undocumented, but this
55 * should cover the basic idea:
57 * USIG_DLL_UNLOAD_WIN16
58 * This is sent when a 16-bit module is unloaded.
60 * USIG_DLL_UNLOAD_WIN32
61 * This is sent when a 32-bit module is unloaded.
63 * USIG_DLL_UNLOAD_ORPHANS
64 * This is sent after the last Win3.1 module is unloaded,
65 * to allow removal of orphaned menus.
67 * USIG_FAULT_DIALOG_PUSH
68 * USIG_FAULT_DIALOG_POP
69 * These are called to allow USER to prepare for displaying a
70 * fault dialog, even though the fault might have happened while
71 * inside a USER critical section.
74 * This is called from the context of a new thread, as soon as it
78 * This is called, still in its context, just before a thread is
82 * This is called, in the parent process context, after a new process
86 * This is called in the new process context, just after the main thread
87 * has started execution (after the main thread's USIG_THREAD_INIT has
91 * This is called after the executable file has been loaded into the
92 * new process context.
94 * USIG_PROCESS_RUNNING
95 * This is called immediately before the main entry point is called.
98 * This is called in the context of a process that is about to
99 * terminate (but before the last thread's USIG_THREAD_EXIT has
102 * USIG_PROCESS_DESTROY
103 * This is called after a process has terminated.
106 * The meaning of the dwFlags bits is as follows:
109 * Current process is 32-bit.
112 * Current process is a (Win32) GUI process.
114 * USIG_FLAGS_FEEDBACK
115 * Current process needs 'feedback' (determined from the STARTUPINFO
116 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
119 * The signal is being sent due to a fault.
121 void PROCESS_CallUserSignalProc( UINT uCode, HMODULE hModule )
123 DWORD flags = current_process.flags;
124 DWORD startup_flags = current_startupinfo.dwFlags;
127 /* Determine dwFlags */
129 if ( !(flags & PDB32_WIN16_PROC) ) dwFlags |= USIG_FLAGS_WIN32;
131 if ( !(flags & PDB32_CONSOLE_PROC) ) dwFlags |= USIG_FLAGS_GUI;
133 if ( dwFlags & USIG_FLAGS_GUI )
135 /* Feedback defaults to ON */
136 if ( !(startup_flags & STARTF_FORCEOFFFEEDBACK) )
137 dwFlags |= USIG_FLAGS_FEEDBACK;
141 /* Feedback defaults to OFF */
142 if (startup_flags & STARTF_FORCEONFEEDBACK)
143 dwFlags |= USIG_FLAGS_FEEDBACK;
146 /* Convert module handle to 16-bit */
148 if ( HIWORD( hModule ) )
149 hModule = MapHModuleLS( hModule );
151 /* Call USER signal proc */
153 if ( Callout.UserSignalProc )
155 if ( uCode == USIG_THREAD_INIT || uCode == USIG_THREAD_EXIT )
156 Callout.UserSignalProc( uCode, GetCurrentThreadId(), dwFlags, hModule );
158 Callout.UserSignalProc( uCode, GetCurrentProcessId(), dwFlags, hModule );
163 /***********************************************************************
166 * Main process initialisation code
168 static BOOL process_init( char *argv[] )
172 /* store the program name */
174 main_exe_argv = argv;
176 /* Fill the initial process structure */
177 current_process.exit_code = STILL_ACTIVE;
178 current_process.threads = 1;
179 current_process.running_threads = 1;
180 current_process.ring0_threads = 1;
181 current_process.group = ¤t_process;
182 current_process.priority = 8; /* Normal */
183 current_process.env_db = ¤t_envdb;
185 /* Setup the server connection */
186 NtCurrentTeb()->socket = CLIENT_InitServer();
187 if (CLIENT_InitThread()) return FALSE;
189 /* Retrieve startup info from the server */
192 struct init_process_request *req = server_alloc_req( sizeof(*req),
193 sizeof(main_exe_name)-1 );
195 req->ldt_copy = ldt_copy;
196 req->ldt_flags = ldt_flags_copy;
197 req->ppid = getppid();
198 if ((ret = !server_call( REQ_INIT_PROCESS )))
200 size_t len = server_data_size( req );
201 memcpy( main_exe_name, server_data_ptr(req), len );
202 main_exe_name[len] = 0;
203 main_exe_file = req->exe_file;
204 current_startupinfo.dwFlags = req->start_flags;
205 server_startticks = req->server_start;
206 current_startupinfo.wShowWindow = req->cmd_show;
207 current_envdb.hStdin = current_startupinfo.hStdInput = req->hstdin;
208 current_envdb.hStdout = current_startupinfo.hStdOutput = req->hstdout;
209 current_envdb.hStderr = current_startupinfo.hStdError = req->hstderr;
213 if (!ret) return FALSE;
215 /* Remember TEB selector of initial process for emergency use */
216 SYSLEVEL_EmergencyTeb = NtCurrentTeb()->teb_sel;
218 /* Create the system and process heaps */
219 if (!HEAP_CreateSystemHeap()) return FALSE;
220 current_process.heap = HeapCreate( HEAP_GROWABLE, 0, 0 );
222 /* Copy the parent environment */
223 if (!ENV_BuildEnvironment()) return FALSE;
225 /* Create the SEGPTR heap */
226 if (!(SegptrHeap = HeapCreate( HEAP_WINE_SEGPTR, 0, 0 ))) return FALSE;
228 /* Initialize the critical sections */
229 InitializeCriticalSection( ¤t_process.crit_section );
231 /* Initialize syslevel handling */
234 /* Parse command line arguments */
235 OPTIONS_ParseOptions( argv );
237 return MAIN_MainInit();
241 /***********************************************************************
244 * Build the command line of a process from the argv array.
246 * Note that it does NOT necessarily include the file name.
247 * Sometimes we don't even have any command line options at all.
249 static inline char *build_command_line( char **argv )
252 char *cmdline, *p, **arg;
254 for (arg = argv, len = 0; *arg; arg++) len += strlen(*arg) + 1;
255 if ((argv[0]) && (quote = (strchr( argv[0], ' ' ) != NULL))) len += 2;
256 if (!(p = cmdline = HeapAlloc( GetProcessHeap(), 0, len ))) return NULL;
274 if (p > cmdline) p--; /* remove last space */
280 /***********************************************************************
283 * Startup routine of a new process. Runs on the new process stack.
285 static void start_process(void)
287 int debugged, console_app;
288 LPTHREAD_START_ROUTINE entry;
291 /* build command line */
292 if (!(current_envdb.cmd_line = build_command_line( main_exe_argv ))) goto error;
294 /* create 32-bit module for main exe */
295 if (!(main_module = BUILTIN32_LoadExeModule( main_module ))) goto error;
297 /* use original argv[0] as name for the main module */
298 if (!main_exe_name[0])
300 if (!GetLongPathNameA( full_argv0, main_exe_name, sizeof(main_exe_name) ))
301 lstrcpynA( main_exe_name, full_argv0, sizeof(main_exe_name) );
304 /* Retrieve entry point address */
305 entry = (LPTHREAD_START_ROUTINE)((char*)main_module +
306 PE_HEADER(main_module)->OptionalHeader.AddressOfEntryPoint);
307 console_app = (PE_HEADER(main_module)->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI);
308 if (console_app) current_process.flags |= PDB32_CONSOLE_PROC;
310 /* Signal the parent process to continue */
313 struct init_process_done_request *req = server_alloc_req( sizeof(*req), 0 );
314 req->module = (void *)main_module;
316 req->name = main_exe_name;
317 req->gui = !console_app;
318 server_call( REQ_INIT_PROCESS_DONE );
319 debugged = req->debugged;
323 /* Install signal handlers; this cannot be done before, since we cannot
324 * send exceptions to the debugger before the create process event that
325 * is sent by REQ_INIT_PROCESS_DONE */
326 if (!SIGNAL_Init()) goto error;
328 /* create the main modref and load dependencies */
329 if (!(wm = PE_CreateModule( main_module, main_exe_name, 0, main_exe_file, FALSE )))
333 EnterCriticalSection( ¤t_process.crit_section );
335 MODULE_DllProcessAttach( current_process.exe_modref, (LPVOID)1 );
336 LeaveCriticalSection( ¤t_process.crit_section );
338 /* Get pointers to USER routines called by KERNEL */
341 /* Call FinalUserInit routine */
342 if (Callout.FinalUserInit16) Callout.FinalUserInit16();
344 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
345 * context of the parent process. Actually, the USER signal proc
346 * doesn't really care about that, but it *does* require that the
347 * startup parameters are correctly set up, so that GetProcessDword
348 * works. Furthermore, before calling the USER signal proc the
349 * 16-bit stack must be set up, which it is only after TASK_Create
350 * in the case of a 16-bit process. Thus, we send the signal here.
352 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE, 0 );
353 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
354 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT, 0 );
355 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED, 0 );
356 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
357 if (console_app) PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING, 0 );
359 TRACE_(relay)( "Starting Win32 process (entryproc=%p)\n", entry );
360 if (debugged) DbgBreakPoint();
361 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
362 * Dunno about other OSs */
363 SetLastError(0); /* clear error code */
364 ExitThread( entry(NULL) );
367 ExitProcess( GetLastError() );
371 /***********************************************************************
374 * Try to open the Winelib app .so file based on argv[0] or WINEPRELOAD.
376 void *open_winelib_app( const char *argv0 )
382 if ((name = getenv( "WINEPRELOAD" )))
384 ret = wine_dll_load_main_exe( name, 0 );
388 /* if argv[0] is "wine", don't try to load anything */
389 if (!(name = strrchr( argv0, '/' ))) name = argv0;
391 if (!strcmp( name, "wine" )) return NULL;
393 /* now try argv[0] with ".so" appended */
394 if ((tmp = HeapAlloc( GetProcessHeap(), 0, strlen(argv0) + 4 )))
396 strcpy( tmp, argv0 );
397 strcat( tmp, ".so" );
398 /* search in PATH only if there was no '/' in argv[0] */
399 ret = wine_dll_load_main_exe( tmp, (name == argv0) );
400 HeapFree( GetProcessHeap(), 0, tmp );
406 /***********************************************************************
409 * Wine initialisation: load and start the main exe file.
411 void PROCESS_InitWine( int argc, char *argv[] )
413 DWORD stack_size = 0;
415 /* Initialize everything */
416 if (!process_init( argv )) exit(1);
418 if (open_winelib_app( argv[0] )) goto found; /* try to open argv[0] as a winelib app */
420 main_exe_argv = ++argv; /* remove argv[0] (wine itself) */
422 if (!main_exe_name[0])
424 if (!argv[0]) OPTIONS_Usage();
426 /* open the exe file */
427 if (!SearchPathA( NULL, argv[0], ".exe", sizeof(main_exe_name), main_exe_name, NULL ) &&
428 !SearchPathA( NULL, argv[0], NULL, sizeof(main_exe_name), main_exe_name, NULL ))
430 MESSAGE( "%s: cannot find '%s'\n", argv0, argv[0] );
435 if (main_exe_file == INVALID_HANDLE_VALUE)
437 if ((main_exe_file = CreateFileA( main_exe_name, GENERIC_READ, FILE_SHARE_READ,
438 NULL, OPEN_EXISTING, 0, -1 )) == INVALID_HANDLE_VALUE)
440 MESSAGE( "%s: cannot open '%s'\n", argv0, main_exe_name );
445 /* first try Win32 format; this will fail if the file is not a PE binary */
446 if ((main_module = PE_LoadImage( main_exe_file, main_exe_name, 0 )))
448 if (PE_HEADER(main_module)->FileHeader.Characteristics & IMAGE_FILE_DLL)
449 ExitProcess( ERROR_BAD_EXE_FORMAT );
450 stack_size = PE_HEADER(main_module)->OptionalHeader.SizeOfStackReserve;
454 /* it must be 16-bit or DOS format */
455 NtCurrentTeb()->tibflags &= ~TEBF_WIN32;
456 current_process.flags |= PDB32_WIN16_PROC;
457 main_exe_name[0] = 0;
458 CloseHandle( main_exe_file );
459 main_exe_file = INVALID_HANDLE_VALUE;
460 SYSLEVEL_EnterWin16Lock();
463 /* allocate main thread stack */
464 if (!THREAD_InitStack( NtCurrentTeb(), stack_size, TRUE )) goto error;
466 /* switch to the new stack */
467 SYSDEPS_SwitchToThreadStack( start_process );
470 ExitProcess( GetLastError() );
474 /***********************************************************************
475 * PROCESS_InitWinelib
477 * Initialisation of a new Winelib process.
479 void PROCESS_InitWinelib( int argc, char *argv[] )
481 if (!process_init( argv )) exit(1);
483 /* allocate main thread stack */
484 if (!THREAD_InitStack( NtCurrentTeb(), 0, TRUE )) ExitProcess( GetLastError() );
486 /* switch to the new stack */
487 SYSDEPS_SwitchToThreadStack( start_process );
491 /***********************************************************************
494 * Build an argv array from a command-line.
495 * The command-line is modified to insert nulls.
496 * 'reserved' is the number of args to reserve before the first one.
498 static char **build_argv( char *cmdline, int reserved )
501 int count = reserved + 1;
504 /* if first word is quoted store it as a single arg */
505 if (*cmdline == '\"')
507 if ((p = strchr( cmdline + 1, '\"' )))
516 while (*p && isspace(*p)) p++;
519 while (*p && !isspace(*p)) p++;
522 if ((argv = malloc( count * sizeof(*argv) )))
524 char **argvptr = argv + reserved;
526 if (*cmdline == '\"')
528 if ((p = strchr( cmdline + 1, '\"' )))
530 *argvptr++ = cmdline + 1;
537 while (*p && isspace(*p)) *p++ = 0;
540 while (*p && !isspace(*p)) p++;
548 /***********************************************************************
551 * Build the environment of a new child process.
553 static char **build_envp( const char *env, const char *extra_env )
559 if (extra_env) for (p = extra_env; *p; count++) p += strlen(p) + 1;
560 for (p = env; *p; count++) p += strlen(p) + 1;
563 if ((envp = malloc( count * sizeof(*envp) )))
565 extern char **environ;
566 char **envptr = envp;
567 char **unixptr = environ;
568 /* first the extra strings */
569 if (extra_env) for (p = extra_env; *p; p += strlen(p) + 1) *envptr++ = (char *)p;
570 /* then put PATH, HOME and WINEPREFIX from the unix env */
571 for (unixptr = environ; unixptr && *unixptr; unixptr++)
572 if (!memcmp( *unixptr, "PATH=", 5 ) ||
573 !memcmp( *unixptr, "HOME=", 5 ) ||
574 !memcmp( *unixptr, "WINEPREFIX=", 11 )) *envptr++ = *unixptr;
575 /* now put the Windows environment strings */
576 for (p = env; *p; p += strlen(p) + 1)
578 if (memcmp( p, "PATH=", 5 ) &&
579 memcmp( p, "HOME=", 5 ) &&
580 memcmp( p, "WINEPREFIX=", 11 )) *envptr++ = (char *)p;
588 /***********************************************************************
591 * Locate the Wine binary to exec for a new Win32 process.
593 static void exec_wine_binary( char **argv, char **envp )
595 const char *path, *pos, *ptr;
597 /* first, try for a WINELOADER environment variable */
598 argv[0] = getenv("WINELOADER");
600 execve( argv[0], argv, envp );
602 /* next, try bin directory */
603 argv[0] = BINDIR "/wine";
604 execve( argv[0], argv, envp );
606 /* now try the path of argv0 of the current binary */
607 if (!(argv[0] = malloc( strlen(full_argv0) + 6 ))) return;
608 if ((ptr = strrchr( full_argv0, '/' )))
610 memcpy( argv[0], full_argv0, ptr - full_argv0 );
611 strcpy( argv[0] + (ptr - full_argv0), "/wine" );
612 execve( argv[0], argv, envp );
616 /* now search in the Unix path */
617 if ((path = getenv( "PATH" )))
619 if (!(argv[0] = malloc( strlen(path) + 6 ))) return;
623 while (*pos == ':') pos++;
625 if (!(ptr = strchr( pos, ':' ))) ptr = pos + strlen(pos);
626 memcpy( argv[0], pos, ptr - pos );
627 strcpy( argv[0] + (ptr - pos), "/wine" );
628 execve( argv[0], argv, envp );
634 /* finally try the current directory */
636 execve( argv[0], argv, envp );
640 /***********************************************************************
643 * Fork and exec a new Unix process, checking for errors.
645 static int fork_and_exec( const char *filename, char *cmdline,
646 const char *env, const char *newdir )
650 char *extra_env = NULL;
654 env = GetEnvironmentStringsA();
655 extra_env = DRIVE_BuildEnv();
663 fcntl( fd[1], F_SETFD, 1 ); /* set close on exec */
664 if (!(pid = fork())) /* child */
666 char **argv = build_argv( cmdline, filename ? 0 : 2 );
667 char **envp = build_envp( env, extra_env );
670 if (newdir) chdir(newdir);
677 exec_wine_binary( argv, envp );
679 else execve( filename, argv, envp );
682 write( fd[1], &err, sizeof(err) );
686 if ((pid != -1) && (read( fd[0], &err, sizeof(err) ) > 0)) /* exec failed */
691 if (pid == -1) FILE_SetDosError();
693 if (extra_env) HeapFree( GetProcessHeap(), 0, extra_env );
698 /***********************************************************************
701 * Create a new process. If hFile is a valid handle we have an exe
702 * file, and we exec a new copy of wine to load it; otherwise we
703 * simply exec the specified filename as a Unix process.
705 BOOL PROCESS_Create( HFILE hFile, LPCSTR filename, LPSTR cmd_line, LPCSTR env,
706 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
707 BOOL inherit, DWORD flags, LPSTARTUPINFOA startup,
708 LPPROCESS_INFORMATION info, LPCSTR lpCurrentDirectory )
712 const char *unixfilename = NULL;
713 const char *unixdir = NULL;
714 DOS_FULL_NAME full_name;
715 HANDLE load_done_evt = (HANDLE)-1;
717 info->hThread = info->hProcess = INVALID_HANDLE_VALUE;
719 if (lpCurrentDirectory)
721 if (DOSFS_GetFullName( lpCurrentDirectory, TRUE, &full_name ))
722 unixdir = full_name.long_name;
727 if (GetCurrentDirectoryA(sizeof(buf),buf))
729 if (DOSFS_GetFullName( buf, TRUE, &full_name ))
730 unixdir = full_name.long_name;
734 /* create the process on the server side */
738 size_t len = (hFile == -1) ? 0 : MAX_PATH;
739 struct new_process_request *req = server_alloc_req( sizeof(*req), len );
740 req->inherit_all = inherit;
741 req->create_flags = flags;
742 req->start_flags = startup->dwFlags;
743 req->exe_file = hFile;
744 if (startup->dwFlags & STARTF_USESTDHANDLES)
746 req->hstdin = startup->hStdInput;
747 req->hstdout = startup->hStdOutput;
748 req->hstderr = startup->hStdError;
752 req->hstdin = GetStdHandle( STD_INPUT_HANDLE );
753 req->hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
754 req->hstderr = GetStdHandle( STD_ERROR_HANDLE );
756 req->cmd_show = startup->wShowWindow;
759 if (hFile == -1) /* unix process */
761 unixfilename = filename;
762 if (DOSFS_GetFullName( filename, TRUE, &full_name ))
763 unixfilename = full_name.long_name;
765 else /* new wine process */
767 if (!GetLongPathNameA( filename, server_data_ptr(req), MAX_PATH ))
768 lstrcpynA( server_data_ptr(req), filename, MAX_PATH );
770 ret = !server_call( REQ_NEW_PROCESS );
773 if (!ret) return FALSE;
775 /* fork and execute */
777 pid = fork_and_exec( unixfilename, cmd_line, env, unixdir );
781 struct wait_process_request *req = server_alloc_req( sizeof(*req), 0 );
782 req->cancel = (pid == -1);
783 req->pinherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
784 req->tinherit = (tsa && (tsa->nLength >= sizeof(*tsa)) && tsa->bInheritHandle);
786 if ((ret = !server_call( REQ_WAIT_PROCESS )) && (pid != -1))
788 info->dwProcessId = (DWORD)req->pid;
789 info->dwThreadId = (DWORD)req->tid;
790 info->hProcess = req->phandle;
791 info->hThread = req->thandle;
792 load_done_evt = req->event;
796 if (!ret || (pid == -1)) goto error;
798 /* Wait until process is initialized (or initialization failed) */
799 if (load_done_evt != (HANDLE)-1)
804 handles[0] = info->hProcess;
805 handles[1] = load_done_evt;
806 res = WaitForMultipleObjects( 2, handles, FALSE, INFINITE );
807 CloseHandle( load_done_evt );
808 if (res == STATUS_WAIT_0) /* the process died */
811 if (GetExitCodeProcess( info->hProcess, &exitcode )) SetLastError( exitcode );
812 CloseHandle( info->hThread );
813 CloseHandle( info->hProcess );
820 if (load_done_evt != (HANDLE)-1) CloseHandle( load_done_evt );
821 if (info->hThread != INVALID_HANDLE_VALUE) CloseHandle( info->hThread );
822 if (info->hProcess != INVALID_HANDLE_VALUE) CloseHandle( info->hProcess );
827 /***********************************************************************
828 * ExitProcess (KERNEL32.100)
830 void WINAPI ExitProcess( DWORD status )
832 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
835 struct terminate_process_request *req = server_alloc_req( sizeof(*req), 0 );
836 /* send the exit code to the server */
837 req->handle = GetCurrentProcess();
838 req->exit_code = status;
839 server_call( REQ_TERMINATE_PROCESS );
845 /***********************************************************************
846 * ExitProcess16 (KERNEL.466)
848 void WINAPI ExitProcess16( WORD status )
850 SYSLEVEL_ReleaseWin16Lock();
851 ExitProcess( status );
854 /******************************************************************************
855 * TerminateProcess (KERNEL32.684)
857 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
859 NTSTATUS status = NtTerminateProcess( handle, exit_code );
860 if (status) SetLastError( RtlNtStatusToDosError(status) );
865 /***********************************************************************
866 * GetProcessDword (KERNEL32.18) (KERNEL.485)
867 * 'Of course you cannot directly access Windows internal structures'
869 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
874 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
876 if (dwProcessID && dwProcessID != GetCurrentProcessId())
878 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
884 case GPD_APP_COMPAT_FLAGS:
885 pTask = (TDB *)GlobalLock16( GetCurrentTask() );
886 return pTask? pTask->compat_flags : 0;
888 case GPD_LOAD_DONE_EVENT:
889 return current_process.load_done_evt;
891 case GPD_HINSTANCE16:
892 pTask = (TDB *)GlobalLock16( GetCurrentTask() );
893 return pTask? pTask->hInstance : 0;
895 case GPD_WINDOWS_VERSION:
896 pTask = (TDB *)GlobalLock16( GetCurrentTask() );
897 return pTask? pTask->version : 0;
900 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
903 return (DWORD)¤t_process;
905 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
906 return current_startupinfo.hStdOutput;
908 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
909 return current_startupinfo.hStdInput;
911 case GPD_STARTF_SHOWWINDOW:
912 return current_startupinfo.wShowWindow;
914 case GPD_STARTF_SIZE:
915 x = current_startupinfo.dwXSize;
916 if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
917 y = current_startupinfo.dwYSize;
918 if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
919 return MAKELONG( x, y );
921 case GPD_STARTF_POSITION:
922 x = current_startupinfo.dwX;
923 if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
924 y = current_startupinfo.dwY;
925 if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
926 return MAKELONG( x, y );
928 case GPD_STARTF_FLAGS:
929 return current_startupinfo.dwFlags;
935 return current_process.flags;
938 return current_process.process_dword;
941 ERR_(win32)("Unknown offset %d\n", offset );
946 /***********************************************************************
947 * SetProcessDword (KERNEL.484)
948 * 'Of course you cannot directly access Windows internal structures'
950 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
952 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
954 if (dwProcessID && dwProcessID != GetCurrentProcessId())
956 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
962 case GPD_APP_COMPAT_FLAGS:
963 case GPD_LOAD_DONE_EVENT:
964 case GPD_HINSTANCE16:
965 case GPD_WINDOWS_VERSION:
968 case GPD_STARTF_SHELLDATA:
969 case GPD_STARTF_HOTKEY:
970 case GPD_STARTF_SHOWWINDOW:
971 case GPD_STARTF_SIZE:
972 case GPD_STARTF_POSITION:
973 case GPD_STARTF_FLAGS:
976 ERR_(win32)("Not allowed to modify offset %d\n", offset );
980 current_process.process_dword = value;
984 ERR_(win32)("Unknown offset %d\n", offset );
990 /*********************************************************************
991 * OpenProcess (KERNEL32.543)
993 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
998 struct open_process_request *req = server_alloc_req( sizeof(*req), 0 );
1000 req->pid = (void *)id;
1001 req->access = access;
1002 req->inherit = inherit;
1003 if (!server_call( REQ_OPEN_PROCESS )) ret = req->handle;
1009 /*********************************************************************
1010 * MapProcessHandle (KERNEL.483)
1012 DWORD WINAPI MapProcessHandle( HANDLE handle )
1017 struct get_process_info_request *req = server_alloc_req( sizeof(*req), 0 );
1018 req->handle = handle;
1019 if (!server_call( REQ_GET_PROCESS_INFO )) ret = (DWORD)req->pid;
1025 /***********************************************************************
1026 * SetPriorityClass (KERNEL32.503)
1028 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
1033 struct set_process_info_request *req = server_alloc_req( sizeof(*req), 0 );
1034 req->handle = hprocess;
1035 req->priority = priorityclass;
1036 req->mask = SET_PROCESS_INFO_PRIORITY;
1037 ret = !server_call( REQ_SET_PROCESS_INFO );
1044 /***********************************************************************
1045 * GetPriorityClass (KERNEL32.250)
1047 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
1052 struct get_process_info_request *req = server_alloc_req( sizeof(*req), 0 );
1053 req->handle = hprocess;
1054 if (!server_call( REQ_GET_PROCESS_INFO )) ret = req->priority;
1061 /***********************************************************************
1062 * SetProcessAffinityMask (KERNEL32.662)
1064 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
1069 struct set_process_info_request *req = server_alloc_req( sizeof(*req), 0 );
1070 req->handle = hProcess;
1071 req->affinity = affmask;
1072 req->mask = SET_PROCESS_INFO_AFFINITY;
1073 ret = !server_call( REQ_SET_PROCESS_INFO );
1079 /**********************************************************************
1080 * GetProcessAffinityMask (KERNEL32.373)
1082 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
1083 LPDWORD lpProcessAffinityMask,
1084 LPDWORD lpSystemAffinityMask )
1089 struct get_process_info_request *req = server_alloc_req( sizeof(*req), 0 );
1090 req->handle = hProcess;
1091 if (!server_call( REQ_GET_PROCESS_INFO ))
1093 if (lpProcessAffinityMask) *lpProcessAffinityMask = req->process_affinity;
1094 if (lpSystemAffinityMask) *lpSystemAffinityMask = req->system_affinity;
1103 /***********************************************************************
1104 * GetProcessVersion (KERNEL32)
1106 DWORD WINAPI GetProcessVersion( DWORD processid )
1108 IMAGE_NT_HEADERS *nt;
1110 if (processid && processid != GetCurrentProcessId())
1111 return 0; /* FIXME: should use ReadProcessMemory */
1112 if ((nt = RtlImageNtHeader( current_process.module )))
1113 return ((nt->OptionalHeader.MajorSubsystemVersion << 16) |
1114 nt->OptionalHeader.MinorSubsystemVersion);
1118 /***********************************************************************
1119 * GetProcessFlags (KERNEL32)
1121 DWORD WINAPI GetProcessFlags( DWORD processid )
1123 if (processid && processid != GetCurrentProcessId()) return 0;
1124 return current_process.flags;
1128 /***********************************************************************
1129 * SetProcessWorkingSetSize [KERNEL32.662]
1130 * Sets the min/max working set sizes for a specified process.
1133 * hProcess [I] Handle to the process of interest
1134 * minset [I] Specifies minimum working set size
1135 * maxset [I] Specifies maximum working set size
1139 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess,DWORD minset,
1142 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
1143 if(( minset == (DWORD)-1) && (maxset == (DWORD)-1)) {
1144 /* Trim the working set to zero */
1145 /* Swap the process out of physical RAM */
1150 /***********************************************************************
1151 * GetProcessWorkingSetSize (KERNEL32)
1153 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess,LPDWORD minset,
1156 FIXME("(0x%08x,%p,%p): stub\n",hProcess,minset,maxset);
1157 /* 32 MB working set size */
1158 if (minset) *minset = 32*1024*1024;
1159 if (maxset) *maxset = 32*1024*1024;
1163 /***********************************************************************
1164 * SetProcessShutdownParameters (KERNEL32)
1166 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1167 * Now tracks changes made (but does not act on these changes)
1169 static DWORD shutdown_flags = 0;
1170 static DWORD shutdown_priority = 0x280;
1172 BOOL WINAPI SetProcessShutdownParameters(DWORD level, DWORD flags)
1174 FIXME("(%08lx, %08lx): partial stub.\n", level, flags);
1175 shutdown_flags = flags;
1176 shutdown_priority = level;
1181 /***********************************************************************
1182 * GetProcessShutdownParameters (KERNEL32)
1185 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel, LPDWORD lpdwFlags )
1187 *lpdwLevel = shutdown_priority;
1188 *lpdwFlags = shutdown_flags;
1193 /***********************************************************************
1194 * SetProcessPriorityBoost (KERNEL32)
1196 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
1198 FIXME("(%d,%d): stub\n",hprocess,disableboost);
1199 /* Say we can do it. I doubt the program will notice that we don't. */
1204 /***********************************************************************
1205 * ReadProcessMemory (KERNEL32)
1207 BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, DWORD size,
1208 LPDWORD bytes_read )
1210 unsigned int offset = (unsigned int)addr % sizeof(int);
1211 unsigned int pos = 0, len, max;
1214 if (bytes_read) *bytes_read = size;
1216 /* first time, read total length to check for permissions */
1217 len = (size + offset + sizeof(int) - 1) / sizeof(int);
1218 max = min( REQUEST_MAX_VAR_SIZE, len * sizeof(int) );
1224 struct read_process_memory_request *req = server_alloc_req( sizeof(*req), max );
1225 req->handle = process;
1226 req->addr = (char *)addr + pos - offset;
1228 if (!(res = server_call( REQ_READ_PROCESS_MEMORY )))
1230 size_t result = server_data_size( req );
1231 if (result > size + offset) result = size + offset;
1232 memcpy( (char *)buffer + pos, server_data_ptr(req) + offset, result - offset );
1233 size -= result - offset;
1234 pos += result - offset;
1240 if (bytes_read) *bytes_read = 0;
1243 if (!size) return TRUE;
1244 max = min( REQUEST_MAX_VAR_SIZE, size );
1245 len = (max + sizeof(int) - 1) / sizeof(int);
1251 /***********************************************************************
1252 * WriteProcessMemory (KERNEL32)
1254 BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPVOID buffer, DWORD size,
1255 LPDWORD bytes_written )
1257 unsigned int first_offset, last_offset;
1258 unsigned int pos = 0, len, max, first_mask, last_mask;
1263 SetLastError( ERROR_INVALID_PARAMETER );
1266 if (bytes_written) *bytes_written = size;
1268 /* compute the mask for the first int */
1270 first_offset = (unsigned int)addr % sizeof(int);
1271 memset( &first_mask, 0, first_offset );
1273 /* compute the mask for the last int */
1274 last_offset = (size + first_offset) % sizeof(int);
1276 memset( &last_mask, 0xff, last_offset ? last_offset : sizeof(int) );
1278 /* for the first request, use the total length */
1279 len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
1280 max = min( REQUEST_MAX_VAR_SIZE, len * sizeof(int) );
1286 struct write_process_memory_request *req = server_alloc_req( sizeof(*req), max );
1287 req->handle = process;
1288 req->addr = (char *)addr - first_offset + pos;
1290 req->first_mask = (!pos) ? first_mask : ~0;
1291 if (size + first_offset <= max) /* last round */
1293 req->last_mask = last_mask;
1294 max = size + first_offset;
1296 else req->last_mask = ~0;
1298 memcpy( (char *)server_data_ptr(req) + first_offset, (char *)buffer + pos,
1299 max - first_offset );
1300 if (!(res = server_call( REQ_WRITE_PROCESS_MEMORY )))
1302 pos += max - first_offset;
1303 size -= max - first_offset;
1309 if (bytes_written) *bytes_written = 0;
1312 if (!size) return TRUE;
1314 len = min( size + sizeof(int) - 1, REQUEST_MAX_VAR_SIZE ) / sizeof(int);
1315 max = len * sizeof(int);
1320 /***********************************************************************
1321 * RegisterServiceProcess (KERNEL, KERNEL32)
1323 * A service process calls this function to ensure that it continues to run
1324 * even after a user logged off.
1326 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
1328 /* I don't think that Wine needs to do anything in that function */
1329 return 1; /* success */
1332 /***********************************************************************
1333 * GetExitCodeProcess [KERNEL32.325]
1335 * Gets termination status of specified process
1341 BOOL WINAPI GetExitCodeProcess(
1342 HANDLE hProcess, /* [I] handle to the process */
1343 LPDWORD lpExitCode) /* [O] address to receive termination status */
1348 struct get_process_info_request *req = server_alloc_req( sizeof(*req), 0 );
1349 req->handle = hProcess;
1350 ret = !server_call( REQ_GET_PROCESS_INFO );
1351 if (ret && lpExitCode) *lpExitCode = req->exit_code;
1358 /***********************************************************************
1359 * SetErrorMode (KERNEL32.486)
1361 UINT WINAPI SetErrorMode( UINT mode )
1363 UINT old = current_process.error_mode;
1364 current_process.error_mode = mode;
1368 /***********************************************************************
1369 * GetCurrentProcess (KERNEL32.198)
1371 #undef GetCurrentProcess
1372 HANDLE WINAPI GetCurrentProcess(void)