4 * Copyright 1996, 1998 Alexandre Julliard
12 #include "wine/winbase16.h"
13 #include "wine/exception.h"
29 #include "debugtools.h"
31 DEFAULT_DEBUG_CHANNEL(process)
32 DECLARE_DEBUG_CHANNEL(relay)
33 DECLARE_DEBUG_CHANNEL(win32)
36 /* The initial process PDB */
37 static PDB initial_pdb;
39 static PDB *PROCESS_First = &initial_pdb;
42 /***********************************************************************
45 void PROCESS_WalkProcess(void)
51 MESSAGE( " pid PDB #th modref module \n" );
54 if (pdb == &initial_pdb)
57 name = (pdb->exe_modref) ? pdb->exe_modref->filename : "";
59 MESSAGE( " %8p %8p %5d %8p %s\n", pdb->server_pid, pdb,
60 pdb->threads, pdb->exe_modref, name);
66 /***********************************************************************
69 * Check if a handle is to the current process
71 BOOL PROCESS_IsCurrent( HANDLE handle )
73 struct get_process_info_request *req = get_req_buffer();
75 return (!server_call( REQ_GET_PROCESS_INFO ) &&
76 (req->pid == PROCESS_Current()->server_pid));
80 /***********************************************************************
83 * Convert a process id to a PDB, making sure it is valid.
85 PDB *PROCESS_IdToPDB( DWORD pid )
89 if (!pid) return PROCESS_Current();
93 if ((DWORD)pdb->server_pid == pid) return pdb;
96 SetLastError( ERROR_INVALID_PARAMETER );
101 /***********************************************************************
102 * PROCESS_CallUserSignalProc
104 * FIXME: Some of the signals aren't sent correctly!
106 * The exact meaning of the USER signals is undocumented, but this
107 * should cover the basic idea:
109 * USIG_DLL_UNLOAD_WIN16
110 * This is sent when a 16-bit module is unloaded.
112 * USIG_DLL_UNLOAD_WIN32
113 * This is sent when a 32-bit module is unloaded.
115 * USIG_DLL_UNLOAD_ORPHANS
116 * This is sent after the last Win3.1 module is unloaded,
117 * to allow removal of orphaned menus.
119 * USIG_FAULT_DIALOG_PUSH
120 * USIG_FAULT_DIALOG_POP
121 * These are called to allow USER to prepare for displaying a
122 * fault dialog, even though the fault might have happened while
123 * inside a USER critical section.
126 * This is called from the context of a new thread, as soon as it
127 * has started to run.
130 * This is called, still in its context, just before a thread is
131 * about to terminate.
133 * USIG_PROCESS_CREATE
134 * This is called, in the parent process context, after a new process
138 * This is called in the new process context, just after the main thread
139 * has started execution (after the main thread's USIG_THREAD_INIT has
142 * USIG_PROCESS_LOADED
143 * This is called after the executable file has been loaded into the
144 * new process context.
146 * USIG_PROCESS_RUNNING
147 * This is called immediately before the main entry point is called.
150 * This is called in the context of a process that is about to
151 * terminate (but before the last thread's USIG_THREAD_EXIT has
154 * USIG_PROCESS_DESTROY
155 * This is called after a process has terminated.
158 * The meaning of the dwFlags bits is as follows:
161 * Current process is 32-bit.
164 * Current process is a (Win32) GUI process.
166 * USIG_FLAGS_FEEDBACK
167 * Current process needs 'feedback' (determined from the STARTUPINFO
168 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
171 * The signal is being sent due to a fault.
173 void PROCESS_CallUserSignalProc( UINT uCode, DWORD dwThreadId, HMODULE hModule )
175 DWORD flags = PROCESS_Current()->flags;
176 DWORD startup_flags = PROCESS_Current()->env_db->startup_info->dwFlags;
179 /* Determine dwFlags */
181 if ( !(flags & PDB32_WIN16_PROC) ) dwFlags |= USIG_FLAGS_WIN32;
183 if ( !(flags & PDB32_CONSOLE_PROC) ) dwFlags |= USIG_FLAGS_GUI;
185 if ( dwFlags & USIG_FLAGS_GUI )
187 /* Feedback defaults to ON */
188 if ( !(startup_flags & STARTF_FORCEOFFFEEDBACK) )
189 dwFlags |= USIG_FLAGS_FEEDBACK;
193 /* Feedback defaults to OFF */
194 if (startup_flags & STARTF_FORCEONFEEDBACK)
195 dwFlags |= USIG_FLAGS_FEEDBACK;
198 /* Convert module handle to 16-bit */
200 if ( HIWORD( hModule ) )
201 hModule = MapHModuleLS( hModule );
203 /* Call USER signal proc */
205 if ( Callout.UserSignalProc )
207 if ( uCode == USIG_THREAD_INIT || uCode == USIG_THREAD_EXIT )
208 Callout.UserSignalProc( uCode, dwThreadId, dwFlags, hModule );
210 Callout.UserSignalProc( uCode, GetCurrentProcessId(), dwFlags, hModule );
214 /***********************************************************************
215 * PROCESS_CreateEnvDB
217 * Create the env DB for a newly started process.
219 static BOOL PROCESS_CreateEnvDB(void)
221 struct init_process_request *req = get_req_buffer();
222 STARTUPINFOA *startup;
225 PDB *pdb = PROCESS_Current();
227 /* Allocate the env DB */
229 if (!(env_db = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ENVDB) )))
231 pdb->env_db = env_db;
232 InitializeCriticalSection( &env_db->section );
234 /* Allocate and fill the startup info */
235 if (!(startup = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(STARTUPINFOA) )))
237 env_db->startup_info = startup;
239 /* Retrieve startup info from the server */
241 req->ldt_copy = ldt_copy;
242 req->ldt_flags = ldt_flags_copy;
243 if (server_call( REQ_INIT_PROCESS )) return FALSE;
244 pdb->exe_file = req->exe_file;
245 startup->dwFlags = req->start_flags;
246 startup->wShowWindow = req->cmd_show;
247 env_db->hStdin = startup->hStdInput = req->hstdin;
248 env_db->hStdout = startup->hStdOutput = req->hstdout;
249 env_db->hStderr = startup->hStdError = req->hstderr;
250 lstrcpynA( cmd_line, req->cmdline, sizeof(cmd_line) );
252 /* Copy the parent environment */
254 if (!ENV_InheritEnvironment( req->env_ptr )) return FALSE;
256 /* Copy the command line */
258 if (!(pdb->env_db->cmd_line = HEAP_strdupA( GetProcessHeap(), 0, cmd_line )))
265 /***********************************************************************
268 * Free a PDB and all associated storage.
270 void PROCESS_FreePDB( PDB *pdb )
272 PDB **pptr = &PROCESS_First;
274 ENV_FreeEnvironment( pdb );
275 while (*pptr && (*pptr != pdb)) pptr = &(*pptr)->next;
276 if (*pptr) *pptr = pdb->next;
277 HeapFree( SystemHeap, 0, pdb );
281 /***********************************************************************
284 * Allocate and fill a PDB structure.
285 * Runs in the context of the parent process.
287 static PDB *PROCESS_CreatePDB( PDB *parent, BOOL inherit )
289 PDB *pdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(PDB) );
291 if (!pdb) return NULL;
292 pdb->exit_code = STILL_ACTIVE;
294 pdb->running_threads = 1;
295 pdb->ring0_threads = 1;
296 pdb->parent = parent;
298 pdb->priority = 8; /* Normal */
299 pdb->next = PROCESS_First;
300 pdb->winver = 0xffff; /* to be determined */
301 pdb->main_queue = INVALID_HANDLE_VALUE16;
307 /***********************************************************************
310 BOOL PROCESS_Init( BOOL win32 )
315 /* Start the server */
316 server_fd = CLIENT_InitServer();
318 /* Fill the initial process structure */
319 initial_pdb.exit_code = STILL_ACTIVE;
320 initial_pdb.threads = 1;
321 initial_pdb.running_threads = 1;
322 initial_pdb.ring0_threads = 1;
323 initial_pdb.group = &initial_pdb;
324 initial_pdb.priority = 8; /* Normal */
325 initial_pdb.flags = win32? 0 : PDB32_WIN16_PROC;
326 initial_pdb.winver = 0xffff; /* to be determined */
327 initial_pdb.main_queue = INVALID_HANDLE_VALUE16;
329 /* Initialize virtual memory management */
330 if (!VIRTUAL_Init()) return FALSE;
332 /* Create the initial thread structure and socket pair */
333 if (!(teb = THREAD_CreateInitialThread( &initial_pdb, server_fd ))) return FALSE;
335 /* Remember TEB selector of initial process for emergency use */
336 SYSLEVEL_EmergencyTeb = teb->teb_sel;
338 /* Create the system and process heaps */
339 if (!HEAP_CreateSystemHeap()) return FALSE;
340 initial_pdb.heap = HeapCreate( HEAP_GROWABLE, 0, 0 );
342 /* Create the idle event for the initial process
343 FIXME 1: Shouldn't we call UserSignalProc for the initial process too?
344 FIXME 2: It seems to me that the initial pdb becomes never freed, so I don't now
345 where to release the idle event for the initial process.
347 initial_pdb.idle_event = CreateEventA ( NULL, TRUE, FALSE, NULL );
348 initial_pdb.idle_event = ConvertToGlobalHandle ( initial_pdb.idle_event );
350 /* Initialize signal handling */
351 if (!SIGNAL_Init()) return FALSE;
353 /* Create the environment DB of the first process */
354 if (!PROCESS_CreateEnvDB()) return FALSE;
356 /* Create the SEGPTR heap */
357 if (!(SegptrHeap = HeapCreate( HEAP_WINE_SEGPTR, 0, 0 ))) return FALSE;
359 /* Initialize the first process critical section */
360 InitializeCriticalSection( &initial_pdb.crit_section );
366 /***********************************************************************
369 * Startup routine of a new process. Called in the context of the new process.
371 void PROCESS_Start(void)
373 struct init_process_done_request *req = get_req_buffer();
375 UINT cmdShow = SW_SHOWNORMAL;
376 LPTHREAD_START_ROUTINE entry = NULL;
377 PDB *pdb = PROCESS_Current();
378 NE_MODULE *pModule = NE_GetPtr( pdb->module );
379 LPCSTR filename = ((OFSTRUCT *)((char*)(pModule) + (pModule)->fileinfo))->szPathName;
380 IMAGE_OPTIONAL_HEADER *header = !pModule->module32? NULL :
381 &PE_HEADER(pModule->module32)->OptionalHeader;
383 /* Get process type */
384 enum { PROC_DOS, PROC_WIN16, PROC_WIN32 } type;
385 if ( pdb->flags & PDB32_DOS_PROC )
387 else if ( pdb->flags & PDB32_WIN16_PROC )
392 /* Initialize the critical section */
393 InitializeCriticalSection( &pdb->crit_section );
395 /* Create the heap */
396 if (!(pdb->heap = GetProcessHeap()))
398 if (!(pdb->heap = HeapCreate( HEAP_GROWABLE,
399 header? header->SizeOfHeapReserve : 0x10000,
400 header? header->SizeOfHeapCommit : 0 )))
404 /* Create the environment db */
405 if (!PROCESS_CreateEnvDB()) goto error;
407 /* Create a task for this process */
408 if (pdb->env_db->startup_info->dwFlags & STARTF_USESHOWWINDOW)
409 cmdShow = pdb->env_db->startup_info->wShowWindow;
410 if (!TASK_Create( pModule, cmdShow ))
413 /* Load all process modules */
417 if ( !NE_InitProcess( pModule ) )
422 /* Create 32-bit MODREF */
423 if ( !PE_CreateModule( pModule->module32, filename, 0, FALSE ) )
426 /* Increment EXE refcount */
427 assert( pdb->exe_modref );
428 pdb->exe_modref->refCount++;
430 /* Retrieve entry point address */
431 entry = (LPTHREAD_START_ROUTINE)RVA_PTR(pModule->module32,
432 OptionalHeader.AddressOfEntryPoint);
436 /* FIXME: move DOS startup code here */
441 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
442 * context of the parent process. Actually, the USER signal proc
443 * doesn't really care about that, but it *does* require that the
444 * startup parameters are correctly set up, so that GetProcessDword
445 * works. Furthermore, before calling the USER signal proc the
446 * 16-bit stack must be set up, which it is only after TASK_Create
447 * in the case of a 16-bit process. Thus, we send the signal here.
450 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE, 0, 0 );
451 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, GetCurrentThreadId(), 0 );
452 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT, 0, 0 );
453 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED, 0, 0 );
455 /* Signal the parent process to continue */
456 req->module = (void *)pModule->module32;
458 server_call( REQ_INIT_PROCESS_DONE );
459 debugged = req->debugged;
461 /* Send all required start-up debugger events */
462 if (type == PROC_WIN32 && debugged)
464 EnterCriticalSection( &pdb->crit_section );
465 MODULE_SendLoadDLLEvents();
466 LeaveCriticalSection( &pdb->crit_section );
469 if ( (pdb->flags & PDB32_CONSOLE_PROC) || (pdb->flags & PDB32_DOS_PROC) )
472 /* Perform Win32 specific process initialization */
473 if ( type == PROC_WIN32 )
475 EnterCriticalSection( &pdb->crit_section );
478 MODULE_DllProcessAttach( pdb->exe_modref, (LPVOID)1 );
480 LeaveCriticalSection( &pdb->crit_section );
483 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
484 if ( type != PROC_WIN16 && (pdb->flags & PDB32_CONSOLE_PROC))
485 PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING, 0, 0 );
490 TRACE_(relay)( "Starting DOS process\n" );
492 ERR_(relay)( "DOSVM_Enter returned; should not happen!\n" );
496 TRACE_(relay)( "Starting Win16 process\n" );
498 ERR_(relay)( "TASK_CallToStart returned; should not happen!\n" );
502 TRACE_(relay)( "Starting Win32 process (entryproc=%p)\n", entry );
503 if (debugged) DbgBreakPoint();
504 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
505 * Dunno about other OSs */
506 ExitProcess( entry(NULL) );
510 ExitProcess( GetLastError() );
514 /***********************************************************************
517 * Create a new process database and associated info.
519 PDB *PROCESS_Create( NE_MODULE *pModule, HFILE hFile, LPCSTR cmd_line, LPCSTR env,
520 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
521 BOOL inherit, DWORD flags, STARTUPINFOA *startup,
522 PROCESS_INFORMATION *info )
524 HANDLE handles[2], load_done_evt = 0;
525 DWORD exitcode, size;
527 int server_thandle, fd = -1;
528 struct new_process_request *req = get_req_buffer();
530 PDB *parent = PROCESS_Current();
531 PDB *pdb = PROCESS_CreatePDB( parent, inherit );
533 if (!pdb) return NULL;
534 info->hThread = info->hProcess = INVALID_HANDLE_VALUE;
535 if (!(load_done_evt = CreateEventA( NULL, TRUE, FALSE, NULL ))) goto error;
537 /* Create the process on the server side */
539 req->inherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
540 req->inherit_all = inherit;
541 req->create_flags = flags;
542 req->start_flags = startup->dwFlags;
543 req->exe_file = hFile;
544 req->event = load_done_evt;
545 if (startup->dwFlags & STARTF_USESTDHANDLES)
547 req->hstdin = startup->hStdInput;
548 req->hstdout = startup->hStdOutput;
549 req->hstderr = startup->hStdError;
553 req->hstdin = GetStdHandle( STD_INPUT_HANDLE );
554 req->hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
555 req->hstderr = GetStdHandle( STD_ERROR_HANDLE );
557 req->cmd_show = startup->wShowWindow;
558 req->env_ptr = (void*)env; /* FIXME: hack */
559 lstrcpynA( req->cmdline, cmd_line, server_remaining(req->cmdline) );
560 if (server_call_fd( REQ_NEW_PROCESS, -1, &fd )) goto error;
561 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
562 pdb->server_pid = req->pid;
563 info->hProcess = req->phandle;
564 info->dwProcessId = (DWORD)req->pid;
565 info->hThread = req->thandle;
566 info->dwThreadId = (DWORD)req->tid;
568 if (pModule->module32) /* Win32 process */
570 IMAGE_OPTIONAL_HEADER *header = &PE_HEADER(pModule->module32)->OptionalHeader;
571 size = header->SizeOfStackReserve;
572 if (header->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
573 pdb->flags |= PDB32_CONSOLE_PROC;
574 alloc_stack16 = TRUE;
576 else if (!pModule->dos_image) /* Win16 process */
578 alloc_stack16 = FALSE;
580 pdb->flags |= PDB32_WIN16_PROC;
582 else /* DOS process */
584 alloc_stack16 = FALSE;
586 pdb->flags |= PDB32_DOS_PROC;
589 /* Create the main thread */
591 if (!(teb = THREAD_Create( pdb, fd, flags & CREATE_SUSPENDED, size,
592 alloc_stack16, tsa, &server_thandle ))) goto error;
593 teb->tid = (void *)info->dwThreadId;
594 teb->startup = PROCESS_Start;
595 fd = -1; /* don't close it */
597 /* Pass module to new process (FIXME: hack) */
598 pdb->module = pModule->self;
599 SYSDEPS_SpawnThread( teb );
601 /* Wait until process is initialized (or initialization failed) */
602 handles[0] = info->hProcess;
603 handles[1] = load_done_evt;
605 switch ( WaitForMultipleObjects( 2, handles, FALSE, INFINITE ) )
608 ERR( "WaitForMultipleObjects failed\n" );
612 /* Child initialization code returns error condition as exitcode */
613 if ( GetExitCodeProcess( info->hProcess, &exitcode ) )
614 SetLastError( exitcode );
618 /* Get 16-bit task up and running */
619 if ( pdb->flags & PDB32_WIN16_PROC )
621 /* Post event to start the task */
622 PostEvent16( pdb->task );
624 /* If we ourselves are a 16-bit task, we Yield() directly. */
625 if ( parent->flags & PDB32_WIN16_PROC )
631 CloseHandle( load_done_evt );
637 if (load_done_evt) CloseHandle( load_done_evt );
638 if (info->hThread != INVALID_HANDLE_VALUE) CloseHandle( info->hThread );
639 if (info->hProcess != INVALID_HANDLE_VALUE) CloseHandle( info->hProcess );
640 PROCESS_FreePDB( pdb );
641 if (fd != -1) close( fd );
646 /***********************************************************************
647 * ExitProcess (KERNEL32.100)
649 void WINAPI ExitProcess( DWORD status )
651 EnterCriticalSection( &PROCESS_Current()->crit_section );
652 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
653 LeaveCriticalSection( &PROCESS_Current()->crit_section );
656 TerminateProcess( GetCurrentProcess(), status );
659 /***********************************************************************
660 * ExitProcess16 (KERNEL.466)
662 void WINAPI ExitProcess16( WORD status )
664 SYSLEVEL_ReleaseWin16Lock();
665 ExitProcess( status );
668 /******************************************************************************
669 * TerminateProcess (KERNEL32.684)
671 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
673 struct terminate_process_request *req = get_req_buffer();
674 req->handle = handle;
675 req->exit_code = exit_code;
676 return !server_call( REQ_TERMINATE_PROCESS );
680 /***********************************************************************
681 * GetProcessDword (KERNEL32.18) (KERNEL.485)
682 * 'Of course you cannot directly access Windows internal structures'
684 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
686 PDB *process = PROCESS_IdToPDB( dwProcessID );
690 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
691 if ( !process ) return 0;
695 case GPD_APP_COMPAT_FLAGS:
696 pTask = (TDB *)GlobalLock16( process->task );
697 return pTask? pTask->compat_flags : 0;
699 case GPD_LOAD_DONE_EVENT:
700 return process->load_done_evt;
702 case GPD_HINSTANCE16:
703 pTask = (TDB *)GlobalLock16( process->task );
704 return pTask? pTask->hInstance : 0;
706 case GPD_WINDOWS_VERSION:
707 pTask = (TDB *)GlobalLock16( process->task );
708 return pTask? pTask->version : 0;
711 if ( process != PROCESS_Current() ) return 0;
712 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
715 return (DWORD)process;
717 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
718 return process->env_db->startup_info->hStdOutput;
720 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
721 return process->env_db->startup_info->hStdInput;
723 case GPD_STARTF_SHOWWINDOW:
724 return process->env_db->startup_info->wShowWindow;
726 case GPD_STARTF_SIZE:
727 x = process->env_db->startup_info->dwXSize;
728 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
729 y = process->env_db->startup_info->dwYSize;
730 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
731 return MAKELONG( x, y );
733 case GPD_STARTF_POSITION:
734 x = process->env_db->startup_info->dwX;
735 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
736 y = process->env_db->startup_info->dwY;
737 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
738 return MAKELONG( x, y );
740 case GPD_STARTF_FLAGS:
741 return process->env_db->startup_info->dwFlags;
744 return process->parent? (DWORD)process->parent->server_pid : 0;
747 return process->flags;
750 return process->process_dword;
753 ERR_(win32)("Unknown offset %d\n", offset );
758 /***********************************************************************
759 * SetProcessDword (KERNEL.484)
760 * 'Of course you cannot directly access Windows internal structures'
762 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
764 PDB *process = PROCESS_IdToPDB( dwProcessID );
766 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
767 if ( !process ) return;
771 case GPD_APP_COMPAT_FLAGS:
772 case GPD_LOAD_DONE_EVENT:
773 case GPD_HINSTANCE16:
774 case GPD_WINDOWS_VERSION:
777 case GPD_STARTF_SHELLDATA:
778 case GPD_STARTF_HOTKEY:
779 case GPD_STARTF_SHOWWINDOW:
780 case GPD_STARTF_SIZE:
781 case GPD_STARTF_POSITION:
782 case GPD_STARTF_FLAGS:
785 ERR_(win32)("Not allowed to modify offset %d\n", offset );
789 process->process_dword = value;
793 ERR_(win32)("Unknown offset %d\n", offset );
799 /***********************************************************************
800 * GetCurrentProcess (KERNEL32.198)
802 HANDLE WINAPI GetCurrentProcess(void)
804 return CURRENT_PROCESS_PSEUDOHANDLE;
808 /*********************************************************************
809 * OpenProcess (KERNEL32.543)
811 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
814 struct open_process_request *req = get_req_buffer();
816 req->pid = (void *)id;
817 req->access = access;
818 req->inherit = inherit;
819 if (!server_call( REQ_OPEN_PROCESS )) ret = req->handle;
823 /*********************************************************************
824 * MapProcessHandle (KERNEL.483)
826 DWORD WINAPI MapProcessHandle( HANDLE handle )
829 struct get_process_info_request *req = get_req_buffer();
830 req->handle = handle;
831 if (!server_call( REQ_GET_PROCESS_INFO )) ret = (DWORD)req->pid;
835 /***********************************************************************
836 * GetCurrentProcessId (KERNEL32.199)
838 DWORD WINAPI GetCurrentProcessId(void)
840 return (DWORD)PROCESS_Current()->server_pid;
844 /***********************************************************************
845 * GetThreadLocale (KERNEL32.295)
847 LCID WINAPI GetThreadLocale(void)
849 return PROCESS_Current()->locale;
853 /***********************************************************************
854 * SetPriorityClass (KERNEL32.503)
856 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
858 struct set_process_info_request *req = get_req_buffer();
859 req->handle = hprocess;
860 req->priority = priorityclass;
861 req->mask = SET_PROCESS_INFO_PRIORITY;
862 return !server_call( REQ_SET_PROCESS_INFO );
866 /***********************************************************************
867 * GetPriorityClass (KERNEL32.250)
869 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
872 struct get_process_info_request *req = get_req_buffer();
873 req->handle = hprocess;
874 if (!server_call( REQ_GET_PROCESS_INFO )) ret = req->priority;
879 /***********************************************************************
880 * SetProcessAffinityMask (KERNEL32.662)
882 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
884 struct set_process_info_request *req = get_req_buffer();
885 req->handle = hProcess;
886 req->affinity = affmask;
887 req->mask = SET_PROCESS_INFO_AFFINITY;
888 return !server_call( REQ_SET_PROCESS_INFO );
891 /**********************************************************************
892 * GetProcessAffinityMask (KERNEL32.373)
894 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
895 LPDWORD lpProcessAffinityMask,
896 LPDWORD lpSystemAffinityMask )
899 struct get_process_info_request *req = get_req_buffer();
900 req->handle = hProcess;
901 if (!server_call( REQ_GET_PROCESS_INFO ))
903 if (lpProcessAffinityMask) *lpProcessAffinityMask = req->process_affinity;
904 if (lpSystemAffinityMask) *lpSystemAffinityMask = req->system_affinity;
911 /***********************************************************************
912 * GetStdHandle (KERNEL32.276)
914 HANDLE WINAPI GetStdHandle( DWORD std_handle )
916 PDB *pdb = PROCESS_Current();
920 case STD_INPUT_HANDLE: return pdb->env_db->hStdin;
921 case STD_OUTPUT_HANDLE: return pdb->env_db->hStdout;
922 case STD_ERROR_HANDLE: return pdb->env_db->hStderr;
924 SetLastError( ERROR_INVALID_PARAMETER );
925 return INVALID_HANDLE_VALUE;
929 /***********************************************************************
930 * SetStdHandle (KERNEL32.506)
932 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
934 PDB *pdb = PROCESS_Current();
935 /* FIXME: should we close the previous handle? */
938 case STD_INPUT_HANDLE:
939 pdb->env_db->hStdin = handle;
941 case STD_OUTPUT_HANDLE:
942 pdb->env_db->hStdout = handle;
944 case STD_ERROR_HANDLE:
945 pdb->env_db->hStderr = handle;
948 SetLastError( ERROR_INVALID_PARAMETER );
952 /***********************************************************************
953 * GetProcessVersion (KERNEL32)
955 DWORD WINAPI GetProcessVersion( DWORD processid )
958 PDB *pdb = PROCESS_IdToPDB( processid );
961 if (!(pTask = (TDB *)GlobalLock16( pdb->task ))) return 0;
962 return (pTask->version&0xff) | (((pTask->version >>8) & 0xff)<<16);
965 /***********************************************************************
966 * GetProcessFlags (KERNEL32)
968 DWORD WINAPI GetProcessFlags( DWORD processid )
970 PDB *pdb = PROCESS_IdToPDB( processid );
975 /***********************************************************************
976 * SetProcessWorkingSetSize [KERNEL32.662]
977 * Sets the min/max working set sizes for a specified process.
980 * hProcess [I] Handle to the process of interest
981 * minset [I] Specifies minimum working set size
982 * maxset [I] Specifies maximum working set size
986 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess,DWORD minset,
989 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
990 if(( minset == -1) && (maxset == -1)) {
991 /* Trim the working set to zero */
992 /* Swap the process out of physical RAM */
997 /***********************************************************************
998 * GetProcessWorkingSetSize (KERNEL32)
1000 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess,LPDWORD minset,
1003 FIXME("(0x%08x,%p,%p): stub\n",hProcess,minset,maxset);
1004 /* 32 MB working set size */
1005 if (minset) *minset = 32*1024*1024;
1006 if (maxset) *maxset = 32*1024*1024;
1010 /***********************************************************************
1011 * SetProcessShutdownParameters (KERNEL32)
1013 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1014 * Now tracks changes made (but does not act on these changes)
1015 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
1016 * It really shouldn't be here, but I'll move it when it's been checked!
1018 #define SHUTDOWN_NORETRY 1
1019 static unsigned int shutdown_noretry = 0;
1020 static unsigned int shutdown_priority = 0x280L;
1021 BOOL WINAPI SetProcessShutdownParameters(DWORD level,DWORD flags)
1023 if (flags & SHUTDOWN_NORETRY)
1024 shutdown_noretry = 1;
1026 shutdown_noretry = 0;
1027 if (level > 0x100L && level < 0x3FFL)
1028 shutdown_priority = level;
1031 ERR("invalid priority level 0x%08lx\n", level);
1038 /***********************************************************************
1039 * GetProcessShutdownParameters (KERNEL32)
1042 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel,
1045 (*lpdwLevel) = shutdown_priority;
1046 (*lpdwFlags) = (shutdown_noretry * SHUTDOWN_NORETRY);
1049 /***********************************************************************
1050 * SetProcessPriorityBoost (KERNEL32)
1052 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
1054 FIXME("(%d,%d): stub\n",hprocess,disableboost);
1055 /* Say we can do it. I doubt the program will notice that we don't. */
1060 /***********************************************************************
1061 * ReadProcessMemory (KERNEL32)
1063 BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, DWORD size,
1064 LPDWORD bytes_read )
1066 struct read_process_memory_request *req = get_req_buffer();
1067 unsigned int offset = (unsigned int)addr % sizeof(int);
1068 unsigned int max = server_remaining( req->data ); /* max length in one request */
1071 if (bytes_read) *bytes_read = size;
1073 /* first time, read total length to check for permissions */
1074 req->handle = process;
1075 req->addr = (char *)addr - offset;
1076 req->len = (size + offset + sizeof(int) - 1) / sizeof(int);
1077 if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1079 if (size <= max - offset)
1081 memcpy( buffer, (char *)req->data + offset, size );
1085 /* now take care of the remaining data */
1086 memcpy( buffer, (char *)req->data + offset, max - offset );
1091 if (max > size) max = size;
1092 req->handle = process;
1093 req->addr = (char *)addr + pos;
1094 req->len = (max + sizeof(int) - 1) / sizeof(int);
1095 if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1096 memcpy( (char *)buffer + pos, (char *)req->data, max );
1103 if (bytes_read) *bytes_read = 0;
1108 /***********************************************************************
1109 * WriteProcessMemory (KERNEL32)
1111 BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPVOID buffer, DWORD size,
1112 LPDWORD bytes_written )
1114 unsigned int first_offset, last_offset;
1115 struct write_process_memory_request *req = get_req_buffer();
1116 unsigned int max = server_remaining( req->data ); /* max length in one request */
1117 unsigned int pos, last_mask;
1121 SetLastError( ERROR_INVALID_PARAMETER );
1124 if (bytes_written) *bytes_written = size;
1126 /* compute the mask for the first int */
1127 req->first_mask = ~0;
1128 first_offset = (unsigned int)addr % sizeof(int);
1129 memset( &req->first_mask, 0, first_offset );
1131 /* compute the mask for the last int */
1132 last_offset = (size + first_offset) % sizeof(int);
1134 memset( &last_mask, 0xff, last_offset ? last_offset : sizeof(int) );
1136 req->handle = process;
1137 req->addr = (char *)addr - first_offset;
1138 /* for the first request, use the total length */
1139 req->len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
1141 if (size + first_offset < max) /* we can do it in one round */
1143 memcpy( (char *)req->data + first_offset, buffer, size );
1144 req->last_mask = last_mask;
1145 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1149 /* needs multiple server calls */
1151 memcpy( (char *)req->data + first_offset, buffer, max - first_offset );
1152 req->last_mask = ~0;
1153 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1154 pos = max - first_offset;
1158 if (size <= max) /* last one */
1160 req->last_mask = last_mask;
1163 req->handle = process;
1164 req->addr = (char *)addr + pos;
1165 req->len = (max + sizeof(int) - 1) / sizeof(int);
1166 req->first_mask = ~0;
1167 memcpy( req->data, (char *) buffer + pos, max );
1168 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1175 if (bytes_written) *bytes_written = 0;
1181 /***********************************************************************
1182 * RegisterServiceProcess (KERNEL, KERNEL32)
1184 * A service process calls this function to ensure that it continues to run
1185 * even after a user logged off.
1187 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
1189 /* I don't think that Wine needs to do anything in that function */
1190 return 1; /* success */
1193 /***********************************************************************
1194 * GetExitCodeProcess [KERNEL32.325]
1196 * Gets termination status of specified process
1202 BOOL WINAPI GetExitCodeProcess(
1203 HANDLE hProcess, /* [I] handle to the process */
1204 LPDWORD lpExitCode) /* [O] address to receive termination status */
1207 struct get_process_info_request *req = get_req_buffer();
1208 req->handle = hProcess;
1209 if (!server_call( REQ_GET_PROCESS_INFO ))
1211 if (lpExitCode) *lpExitCode = req->exit_code;
1218 /***********************************************************************
1219 * SetErrorMode (KERNEL32.486)
1221 UINT WINAPI SetErrorMode( UINT mode )
1223 UINT old = PROCESS_Current()->error_mode;
1224 PROCESS_Current()->error_mode = mode;