- new interface declaration for IShellfolder2
[wine/dcerpc.git] / scheduler / process.c
blobb5f3b4e4641449e2724450a48527cc62f7cb2be8
1 /*
2 * Win32 processes
4 * Copyright 1996, 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include "wine/winbase16.h"
12 #include "process.h"
13 #include "module.h"
14 #include "neexe.h"
15 #include "file.h"
16 #include "global.h"
17 #include "heap.h"
18 #include "task.h"
19 #include "ldt.h"
20 #include "syslevel.h"
21 #include "thread.h"
22 #include "winerror.h"
23 #include "pe_image.h"
24 #include "task.h"
25 #include "server.h"
26 #include "options.h"
27 #include "callback.h"
28 #include "debugtools.h"
30 DECLARE_DEBUG_CHANNEL(process)
31 DECLARE_DEBUG_CHANNEL(relay)
32 DECLARE_DEBUG_CHANNEL(win32)
35 /* The initial process PDB */
36 static PDB initial_pdb;
38 static PDB *PROCESS_First = &initial_pdb;
40 /* Pointer to debugger callback routine */
41 void (*TASK_AddTaskEntryBreakpoint)( HTASK16 hTask ) = NULL;
44 /***********************************************************************
45 * PROCESS_WalkProcess
47 void PROCESS_WalkProcess(void)
49 PDB *pdb;
50 char *name;
52 pdb = PROCESS_First;
53 MESSAGE( " pid PDB #th modref module \n" );
54 while(pdb)
56 if (pdb == &initial_pdb)
57 name = "initial PDB";
58 else
59 name = (pdb->exe_modref) ? pdb->exe_modref->shortname : "";
61 MESSAGE( " %8p %8p %5d %8p %s\n", pdb->server_pid, pdb,
62 pdb->threads, pdb->exe_modref, name);
63 pdb = pdb->next;
65 return;
68 /***********************************************************************
69 * PROCESS_Initial
71 * FIXME: This works only while running all processes in the same
72 * address space (or, at least, the initial process is mapped
73 * into all address spaces as is KERNEL32 in Windows 95)
76 PDB *PROCESS_Initial(void)
78 return &initial_pdb;
81 /***********************************************************************
82 * PROCESS_IsCurrent
84 * Check if a handle is to the current process
86 BOOL PROCESS_IsCurrent( HANDLE handle )
88 struct get_process_info_request *req = get_req_buffer();
89 req->handle = handle;
90 return (!server_call( REQ_GET_PROCESS_INFO ) &&
91 (req->pid == PROCESS_Current()->server_pid));
95 /***********************************************************************
96 * PROCESS_IdToPDB
98 * Convert a process id to a PDB, making sure it is valid.
100 PDB *PROCESS_IdToPDB( DWORD id )
102 PDB *pdb;
104 if (!id) return PROCESS_Current();
105 pdb = PROCESS_First;
106 while (pdb)
108 if ((DWORD)pdb->server_pid == id) return pdb;
109 pdb = pdb->next;
111 SetLastError( ERROR_INVALID_PARAMETER );
112 return NULL;
116 /***********************************************************************
117 * PROCESS_CallUserSignalProc
119 * FIXME: Some of the signals aren't sent correctly!
121 * The exact meaning of the USER signals is undocumented, but this
122 * should cover the basic idea:
124 * USIG_DLL_UNLOAD_WIN16
125 * This is sent when a 16-bit module is unloaded.
127 * USIG_DLL_UNLOAD_WIN32
128 * This is sent when a 32-bit module is unloaded.
130 * USIG_DLL_UNLOAD_ORPHANS
131 * This is sent after the last Win3.1 module is unloaded,
132 * to allow removal of orphaned menus.
134 * USIG_FAULT_DIALOG_PUSH
135 * USIG_FAULT_DIALOG_POP
136 * These are called to allow USER to prepare for displaying a
137 * fault dialog, even though the fault might have happened while
138 * inside a USER critical section.
140 * USIG_THREAD_INIT
141 * This is called from the context of a new thread, as soon as it
142 * has started to run.
144 * USIG_THREAD_EXIT
145 * This is called, still in its context, just before a thread is
146 * about to terminate.
148 * USIG_PROCESS_CREATE
149 * This is called, in the parent process context, after a new process
150 * has been created.
152 * USIG_PROCESS_INIT
153 * This is called in the new process context, just after the main thread
154 * has started execution (after the main thread's USIG_THREAD_INIT has
155 * been sent).
157 * USIG_PROCESS_LOADED
158 * This is called after the executable file has been loaded into the
159 * new process context.
161 * USIG_PROCESS_RUNNING
162 * This is called immediately before the main entry point is called.
164 * USIG_PROCESS_EXIT
165 * This is called in the context of a process that is about to
166 * terminate (but before the last thread's USIG_THREAD_EXIT has
167 * been sent).
169 * USIG_PROCESS_DESTROY
170 * This is called after a process has terminated.
173 * The meaning of the dwFlags bits is as follows:
175 * USIG_FLAGS_WIN32
176 * Current process is 32-bit.
178 * USIG_FLAGS_GUI
179 * Current process is a (Win32) GUI process.
181 * USIG_FLAGS_FEEDBACK
182 * Current process needs 'feedback' (determined from the STARTUPINFO
183 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
185 * USIG_FLAGS_FAULT
186 * The signal is being sent due to a fault.
188 static void PROCESS_CallUserSignalProcHelper( UINT uCode, DWORD dwThreadOrProcessId,
189 HMODULE hModule, DWORD flags, DWORD startup_flags )
191 DWORD dwFlags = 0;
193 /* Determine dwFlags */
195 if ( !(flags & PDB32_WIN16_PROC) ) dwFlags |= USIG_FLAGS_WIN32;
197 if ( !(flags & PDB32_CONSOLE_PROC) ) dwFlags |= USIG_FLAGS_GUI;
199 if ( dwFlags & USIG_FLAGS_GUI )
201 /* Feedback defaults to ON */
202 if ( !(startup_flags & STARTF_FORCEOFFFEEDBACK) )
203 dwFlags |= USIG_FLAGS_FEEDBACK;
205 else
207 /* Feedback defaults to OFF */
208 if (startup_flags & STARTF_FORCEONFEEDBACK)
209 dwFlags |= USIG_FLAGS_FEEDBACK;
212 /* Convert module handle to 16-bit */
214 if ( HIWORD( hModule ) )
215 hModule = MapHModuleLS( hModule );
217 /* Call USER signal proc */
219 if ( Callout.UserSignalProc )
220 Callout.UserSignalProc( uCode, dwThreadOrProcessId, dwFlags, hModule );
223 /* Call USER signal proc for the current thread/process */
224 void PROCESS_CallUserSignalProc( UINT uCode, HMODULE hModule )
226 DWORD dwThreadOrProcessId;
228 /* Get thread or process ID */
229 if ( uCode == USIG_THREAD_INIT || uCode == USIG_THREAD_EXIT )
230 dwThreadOrProcessId = GetCurrentThreadId();
231 else
232 dwThreadOrProcessId = GetCurrentProcessId();
234 PROCESS_CallUserSignalProcHelper( uCode, dwThreadOrProcessId, hModule,
235 PROCESS_Current()->flags,
236 PROCESS_Current()->env_db->startup_info->dwFlags );
240 /***********************************************************************
241 * PROCESS_CreateEnvDB
243 * Create the env DB for a newly started process.
245 static BOOL PROCESS_CreateEnvDB(void)
247 struct init_process_request *req = get_req_buffer();
248 STARTUPINFOA *startup;
249 ENVDB *env_db;
250 char cmd_line[4096];
251 PDB *pdb = PROCESS_Current();
253 /* Allocate the env DB */
255 if (!(env_db = HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(ENVDB) )))
256 return FALSE;
257 pdb->env_db = env_db;
258 InitializeCriticalSection( &env_db->section );
260 /* Allocate and fill the startup info */
261 if (!(startup = HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(STARTUPINFOA) )))
262 return FALSE;
263 env_db->startup_info = startup;
265 /* Retrieve startup info from the server */
267 if (server_call( REQ_INIT_PROCESS )) return FALSE;
268 startup->dwFlags = req->start_flags;
269 startup->wShowWindow = req->cmd_show;
270 env_db->hStdin = startup->hStdInput = req->hstdin;
271 env_db->hStdout = startup->hStdOutput = req->hstdout;
272 env_db->hStderr = startup->hStdError = req->hstderr;
273 lstrcpynA( cmd_line, req->cmdline, sizeof(cmd_line) );
275 /* Copy the parent environment */
277 if (!ENV_InheritEnvironment( pdb, req->env_ptr )) return FALSE;
279 /* Copy the command line */
281 if (!(pdb->env_db->cmd_line = HEAP_strdupA( pdb->heap, 0, cmd_line )))
282 return FALSE;
284 return TRUE;
288 /***********************************************************************
289 * PROCESS_FreePDB
291 * Free a PDB and all associated storage.
293 void PROCESS_FreePDB( PDB *pdb )
295 PDB **pptr = &PROCESS_First;
297 ENV_FreeEnvironment( pdb );
298 while (*pptr && (*pptr != pdb)) pptr = &(*pptr)->next;
299 if (*pptr) *pptr = pdb->next;
300 if (pdb->heap && (pdb->heap != pdb->system_heap)) HeapDestroy( pdb->heap );
301 HeapFree( SystemHeap, 0, pdb );
305 /***********************************************************************
306 * PROCESS_CreatePDB
308 * Allocate and fill a PDB structure.
309 * Runs in the context of the parent process.
311 static PDB *PROCESS_CreatePDB( PDB *parent, BOOL inherit )
313 PDB *pdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(PDB) );
315 if (!pdb) return NULL;
316 pdb->exit_code = 0x103; /* STILL_ACTIVE */
317 pdb->threads = 1;
318 pdb->running_threads = 1;
319 pdb->ring0_threads = 1;
320 pdb->system_heap = SystemHeap;
321 pdb->parent = parent;
322 pdb->group = pdb;
323 pdb->priority = 8; /* Normal */
324 pdb->heap = pdb->system_heap; /* will be changed later on */
325 pdb->next = PROCESS_First;
326 pdb->winver = 0xffff; /* to be determined */
327 PROCESS_First = pdb;
328 return pdb;
332 /***********************************************************************
333 * PROCESS_Init
335 BOOL PROCESS_Init(void)
337 TEB *teb;
338 int server_fd;
340 /* Start the server */
341 server_fd = CLIENT_InitServer();
343 /* Fill the initial process structure */
344 initial_pdb.exit_code = 0x103; /* STILL_ACTIVE */
345 initial_pdb.threads = 1;
346 initial_pdb.running_threads = 1;
347 initial_pdb.ring0_threads = 1;
348 initial_pdb.group = &initial_pdb;
349 initial_pdb.priority = 8; /* Normal */
350 initial_pdb.flags = PDB32_WIN16_PROC;
351 initial_pdb.winver = 0xffff; /* to be determined */
353 /* Initialize virtual memory management */
354 if (!VIRTUAL_Init()) return FALSE;
356 /* Create the initial thread structure and socket pair */
357 if (!(teb = THREAD_CreateInitialThread( &initial_pdb, server_fd ))) return FALSE;
359 /* Remember TEB selector of initial process for emergency use */
360 SYSLEVEL_EmergencyTeb = teb->teb_sel;
362 /* Create the system heap */
363 if (!(SystemHeap = HeapCreate( HEAP_GROWABLE, 0x10000, 0 ))) return FALSE;
364 initial_pdb.system_heap = initial_pdb.heap = SystemHeap;
366 /* Initialize signal handling */
367 if (!SIGNAL_Init()) return FALSE;
369 /* Create the environment DB of the first process */
370 if (!PROCESS_CreateEnvDB()) return FALSE;
372 /* Create the SEGPTR heap */
373 if (!(SegptrHeap = HeapCreate( HEAP_WINE_SEGPTR, 0, 0 ))) return FALSE;
375 /* Initialize the first process critical section */
376 InitializeCriticalSection( &initial_pdb.crit_section );
378 return TRUE;
382 /***********************************************************************
383 * PROCESS_Start
385 * Startup routine of a new process. Called in the context of the new process.
387 void PROCESS_Start(void)
389 UINT cmdShow = 0;
390 LPTHREAD_START_ROUTINE entry = NULL;
391 PDB *pdb = PROCESS_Current();
392 NE_MODULE *pModule = NE_GetPtr( pdb->module );
393 OFSTRUCT *ofs = (OFSTRUCT *)((char*)(pModule) + (pModule)->fileinfo);
394 IMAGE_OPTIONAL_HEADER *header = !pModule->module32? NULL :
395 &PE_HEADER(pModule->module32)->OptionalHeader;
397 /* Get process type */
398 enum { PROC_DOS, PROC_WIN16, PROC_WIN32 } type;
399 if ( pdb->flags & PDB32_DOS_PROC )
400 type = PROC_DOS;
401 else if ( pdb->flags & PDB32_WIN16_PROC )
402 type = PROC_WIN16;
403 else
404 type = PROC_WIN32;
406 /* Initialize the critical section */
407 InitializeCriticalSection( &pdb->crit_section );
409 /* Create the heap */
410 if (!(pdb->heap = HeapCreate( HEAP_GROWABLE,
411 header? header->SizeOfHeapReserve : 0x10000,
412 header? header->SizeOfHeapCommit : 0 )))
413 goto error;
414 pdb->heap_list = pdb->heap;
416 /* Create the environment db */
417 if (!PROCESS_CreateEnvDB()) goto error;
419 /* Create a task for this process */
420 if (pdb->env_db->startup_info->dwFlags & STARTF_USESHOWWINDOW)
421 cmdShow = pdb->env_db->startup_info->wShowWindow;
422 if (!TASK_Create( pModule, cmdShow ))
423 goto error;
425 /* Perform Win16 specific process initialization */
426 if ( type == PROC_WIN16 )
427 if ( !NE_InitProcess( pModule ) )
428 goto error;
430 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
431 * context of the parent process. Actually, the USER signal proc
432 * doesn't really care about that, but it *does* require that the
433 * startup parameters are correctly set up, so that GetProcessDword
434 * works. Furthermore, before calling the USER signal proc the
435 * 16-bit stack must be set up, which it is only after TASK_Create
436 * in the case of a 16-bit process. Thus, we send the signal here.
439 /* Load USER32.DLL before calling UserSignalProc (relay debugging!) */
440 LoadLibraryA( "USER32.DLL" );
442 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE, 0 );
444 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 ); /* for initial thread */
446 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT, 0 );
448 /* Signal the parent process to continue */
449 SetEvent( pdb->load_done_evt );
450 CloseHandle( pdb->load_done_evt );
451 pdb->load_done_evt = INVALID_HANDLE_VALUE;
453 /* Perform Win32 specific process initialization */
454 if ( type == PROC_WIN32 )
456 /* Send the debug event to the debugger */
457 entry = (LPTHREAD_START_ROUTINE)RVA_PTR(pModule->module32,
458 OptionalHeader.AddressOfEntryPoint);
459 if (pdb->flags & PDB32_DEBUGGED)
460 DEBUG_SendCreateProcessEvent( -1 /*FIXME*/, pModule->module32, entry );
462 /* Create 32-bit MODREF */
463 if (!PE_CreateModule( pModule->module32, ofs, 0, FALSE )) goto error;
465 /* Increment EXE refcount */
466 assert( pdb->exe_modref );
467 pdb->exe_modref->refCount++;
469 /* Initialize thread-local storage */
470 PE_InitTls();
473 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED, 0 ); /* FIXME: correct location? */
475 if ( (pdb->flags & PDB32_CONSOLE_PROC) || (pdb->flags & PDB32_DOS_PROC) )
476 AllocConsole();
478 if ( type == PROC_WIN32 )
480 EnterCriticalSection( &pdb->crit_section );
481 MODULE_DllProcessAttach( pdb->exe_modref, (LPVOID)1 );
482 LeaveCriticalSection( &pdb->crit_section );
485 /* If requested, add entry point breakpoint */
486 if ( Options.debug && TASK_AddTaskEntryBreakpoint )
487 TASK_AddTaskEntryBreakpoint( pdb->task );
489 /* Now call the entry point */
490 PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING, 0 );
492 switch ( type )
494 case PROC_DOS:
495 TRACE_(relay)( "Starting DOS process\n" );
496 DOSVM_Enter( NULL );
497 ERR_(relay)( "DOSVM_Enter returned; should not happen!\n" );
498 ExitProcess( 0 );
500 case PROC_WIN16:
501 TRACE_(relay)( "Starting Win16 process\n" );
502 TASK_CallToStart();
503 ERR_(relay)( "TASK_CallToStart returned; should not happen!\n" );
504 ExitProcess( 0 );
506 case PROC_WIN32:
507 TRACE_(relay)( "Starting Win32 process (entryproc=%p)\n", entry );
508 ExitProcess( entry(NULL) );
511 error:
512 ExitProcess( GetLastError() );
516 /***********************************************************************
517 * PROCESS_Create
519 * Create a new process database and associated info.
521 PDB *PROCESS_Create( NE_MODULE *pModule, LPCSTR cmd_line, LPCSTR env,
522 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
523 BOOL inherit, DWORD flags, STARTUPINFOA *startup,
524 PROCESS_INFORMATION *info )
526 HANDLE handles[2], load_done_evt = INVALID_HANDLE_VALUE;
527 DWORD exitcode, size;
528 BOOL alloc_stack16;
529 int server_thandle;
530 struct new_process_request *req = get_req_buffer();
531 TEB *teb = NULL;
532 PDB *parent = PROCESS_Current();
533 PDB *pdb = PROCESS_CreatePDB( parent, inherit );
535 if (!pdb) return NULL;
536 info->hThread = info->hProcess = INVALID_HANDLE_VALUE;
538 /* Create the process on the server side */
540 req->inherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
541 req->inherit_all = inherit;
542 req->create_flags = flags;
543 req->start_flags = startup->dwFlags;
544 if (startup->dwFlags & STARTF_USESTDHANDLES)
546 req->hstdin = startup->hStdInput;
547 req->hstdout = startup->hStdOutput;
548 req->hstderr = startup->hStdError;
550 else
552 req->hstdin = GetStdHandle( STD_INPUT_HANDLE );
553 req->hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
554 req->hstderr = GetStdHandle( STD_ERROR_HANDLE );
556 req->cmd_show = startup->wShowWindow;
557 req->env_ptr = (void*)env; /* FIXME: hack */
558 lstrcpynA( req->cmdline, cmd_line, server_remaining(req->cmdline) );
559 if (server_call( REQ_NEW_PROCESS )) goto error;
560 pdb->server_pid = req->pid;
561 info->hProcess = req->handle;
562 info->dwProcessId = (DWORD)pdb->server_pid;
564 if ((flags & DEBUG_PROCESS) ||
565 ((parent->flags & PDB32_DEBUGGED) && !(flags & DEBUG_ONLY_THIS_PROCESS)))
566 pdb->flags |= PDB32_DEBUGGED;
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;
579 size = 0;
580 pdb->flags |= PDB32_WIN16_PROC;
582 else /* DOS process */
584 alloc_stack16 = FALSE;
585 size = 0;
586 pdb->flags |= PDB32_DOS_PROC;
589 /* Create the main thread */
591 if (!(teb = THREAD_Create( pdb, 0L, size, alloc_stack16, tsa, &server_thandle )))
592 goto error;
593 info->hThread = server_thandle;
594 info->dwThreadId = (DWORD)teb->tid;
595 teb->startup = PROCESS_Start;
597 /* Create the load-done event */
598 load_done_evt = CreateEventA( NULL, TRUE, FALSE, NULL );
599 DuplicateHandle( GetCurrentProcess(), load_done_evt,
600 info->hProcess, &pdb->load_done_evt, 0, TRUE, DUPLICATE_SAME_ACCESS );
602 /* Pass module to new process (FIXME: hack) */
603 pdb->module = pModule->self;
604 SYSDEPS_SpawnThread( teb );
606 /* Wait until process is initialized (or initialization failed) */
607 handles[0] = info->hProcess;
608 handles[1] = load_done_evt;
610 switch ( WaitForMultipleObjects( 2, handles, FALSE, INFINITE ) )
612 default:
613 ERR_(process)( "WaitForMultipleObjects failed\n" );
614 break;
616 case 0:
617 /* Child initialization code returns error condition as exitcode */
618 if ( GetExitCodeProcess( info->hProcess, &exitcode ) )
619 SetLastError( exitcode );
620 goto error;
622 case 1:
623 /* Get 16-bit task up and running */
624 if ( pdb->flags & PDB32_WIN16_PROC )
626 /* Post event to start the task */
627 PostEvent16( pdb->task );
629 /* If we ourselves are a 16-bit task, we Yield() directly. */
630 if ( parent->flags & PDB32_WIN16_PROC )
631 OldYield16();
633 break;
636 CloseHandle( load_done_evt );
637 load_done_evt = INVALID_HANDLE_VALUE;
639 return pdb;
641 error:
642 if (load_done_evt != INVALID_HANDLE_VALUE) CloseHandle( load_done_evt );
643 if (info->hThread != INVALID_HANDLE_VALUE) CloseHandle( info->hThread );
644 if (info->hProcess != INVALID_HANDLE_VALUE) CloseHandle( info->hProcess );
645 PROCESS_FreePDB( pdb );
646 return NULL;
650 /***********************************************************************
651 * ExitProcess (KERNEL32.100)
653 void WINAPI ExitProcess( DWORD status )
655 EnterCriticalSection( &PROCESS_Current()->crit_section );
656 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
657 LeaveCriticalSection( &PROCESS_Current()->crit_section );
659 TASK_KillTask( 0 );
660 TerminateProcess( GetCurrentProcess(), status );
663 /***********************************************************************
664 * ExitProcess16 (KERNEL.466)
666 void WINAPI ExitProcess16( WORD status )
668 SYSLEVEL_ReleaseWin16Lock();
669 ExitProcess( status );
672 /******************************************************************************
673 * TerminateProcess (KERNEL32.684)
675 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
677 struct terminate_process_request *req = get_req_buffer();
678 req->handle = handle;
679 req->exit_code = exit_code;
680 return !server_call( REQ_TERMINATE_PROCESS );
684 /***********************************************************************
685 * GetProcessDword (KERNEL32.18) (KERNEL.485)
686 * 'Of course you cannot directly access Windows internal structures'
688 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
690 PDB *process = PROCESS_IdToPDB( dwProcessID );
691 TDB *pTask;
692 DWORD x, y;
694 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
695 if ( !process ) return 0;
697 switch ( offset )
699 case GPD_APP_COMPAT_FLAGS:
700 pTask = (TDB *)GlobalLock16( process->task );
701 return pTask? pTask->compat_flags : 0;
703 case GPD_LOAD_DONE_EVENT:
704 return process->load_done_evt;
706 case GPD_HINSTANCE16:
707 pTask = (TDB *)GlobalLock16( process->task );
708 return pTask? pTask->hInstance : 0;
710 case GPD_WINDOWS_VERSION:
711 pTask = (TDB *)GlobalLock16( process->task );
712 return pTask? pTask->version : 0;
714 case GPD_THDB:
715 if ( process != PROCESS_Current() ) return 0;
716 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
718 case GPD_PDB:
719 return (DWORD)process;
721 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
722 return process->env_db->startup_info->hStdOutput;
724 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
725 return process->env_db->startup_info->hStdInput;
727 case GPD_STARTF_SHOWWINDOW:
728 return process->env_db->startup_info->wShowWindow;
730 case GPD_STARTF_SIZE:
731 x = process->env_db->startup_info->dwXSize;
732 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
733 y = process->env_db->startup_info->dwYSize;
734 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
735 return MAKELONG( x, y );
737 case GPD_STARTF_POSITION:
738 x = process->env_db->startup_info->dwX;
739 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
740 y = process->env_db->startup_info->dwY;
741 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
742 return MAKELONG( x, y );
744 case GPD_STARTF_FLAGS:
745 return process->env_db->startup_info->dwFlags;
747 case GPD_PARENT:
748 return (DWORD)process->parent->server_pid;
750 case GPD_FLAGS:
751 return process->flags;
753 case GPD_USERDATA:
754 return process->process_dword;
756 default:
757 ERR_(win32)("Unknown offset %d\n", offset );
758 return 0;
762 /***********************************************************************
763 * SetProcessDword (KERNEL.484)
764 * 'Of course you cannot directly access Windows internal structures'
766 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
768 PDB *process = PROCESS_IdToPDB( dwProcessID );
770 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
771 if ( !process ) return;
773 switch ( offset )
775 case GPD_APP_COMPAT_FLAGS:
776 case GPD_LOAD_DONE_EVENT:
777 case GPD_HINSTANCE16:
778 case GPD_WINDOWS_VERSION:
779 case GPD_THDB:
780 case GPD_PDB:
781 case GPD_STARTF_SHELLDATA:
782 case GPD_STARTF_HOTKEY:
783 case GPD_STARTF_SHOWWINDOW:
784 case GPD_STARTF_SIZE:
785 case GPD_STARTF_POSITION:
786 case GPD_STARTF_FLAGS:
787 case GPD_PARENT:
788 case GPD_FLAGS:
789 ERR_(win32)("Not allowed to modify offset %d\n", offset );
790 break;
792 case GPD_USERDATA:
793 process->process_dword = value;
794 break;
796 default:
797 ERR_(win32)("Unknown offset %d\n", offset );
798 break;
803 /***********************************************************************
804 * GetCurrentProcess (KERNEL32.198)
806 HANDLE WINAPI GetCurrentProcess(void)
808 return CURRENT_PROCESS_PSEUDOHANDLE;
812 /*********************************************************************
813 * OpenProcess (KERNEL32.543)
815 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
817 HANDLE ret = 0;
818 struct open_process_request *req = get_req_buffer();
820 req->pid = (void *)id;
821 req->access = access;
822 req->inherit = inherit;
823 if (!server_call( REQ_OPEN_PROCESS )) ret = req->handle;
824 return ret;
827 /*********************************************************************
828 * MapProcessHandle (KERNEL.483)
830 DWORD WINAPI MapProcessHandle( HANDLE handle )
832 DWORD ret = 0;
833 struct get_process_info_request *req = get_req_buffer();
834 req->handle = handle;
835 if (!server_call( REQ_GET_PROCESS_INFO )) ret = (DWORD)req->pid;
836 return ret;
839 /***********************************************************************
840 * GetCurrentProcessId (KERNEL32.199)
842 DWORD WINAPI GetCurrentProcessId(void)
844 return (DWORD)PROCESS_Current()->server_pid;
848 /***********************************************************************
849 * GetProcessHeap (KERNEL32.259)
851 HANDLE WINAPI GetProcessHeap(void)
853 PDB *pdb = PROCESS_Current();
854 return pdb->heap ? pdb->heap : SystemHeap;
858 /***********************************************************************
859 * GetThreadLocale (KERNEL32.295)
861 LCID WINAPI GetThreadLocale(void)
863 return PROCESS_Current()->locale;
867 /***********************************************************************
868 * SetPriorityClass (KERNEL32.503)
870 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
872 struct set_process_info_request *req = get_req_buffer();
873 req->handle = hprocess;
874 req->priority = priorityclass;
875 req->mask = SET_PROCESS_INFO_PRIORITY;
876 return !server_call( REQ_SET_PROCESS_INFO );
880 /***********************************************************************
881 * GetPriorityClass (KERNEL32.250)
883 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
885 DWORD ret = 0;
886 struct get_process_info_request *req = get_req_buffer();
887 req->handle = hprocess;
888 if (!server_call( REQ_GET_PROCESS_INFO )) ret = req->priority;
889 return ret;
893 /***********************************************************************
894 * SetProcessAffinityMask (KERNEL32.662)
896 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
898 struct set_process_info_request *req = get_req_buffer();
899 req->handle = hProcess;
900 req->affinity = affmask;
901 req->mask = SET_PROCESS_INFO_AFFINITY;
902 return !server_call( REQ_SET_PROCESS_INFO );
905 /**********************************************************************
906 * GetProcessAffinityMask (KERNEL32.373)
908 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
909 LPDWORD lpProcessAffinityMask,
910 LPDWORD lpSystemAffinityMask )
912 BOOL ret = FALSE;
913 struct get_process_info_request *req = get_req_buffer();
914 req->handle = hProcess;
915 if (!server_call( REQ_GET_PROCESS_INFO ))
917 if (lpProcessAffinityMask) *lpProcessAffinityMask = req->process_affinity;
918 if (lpSystemAffinityMask) *lpSystemAffinityMask = req->system_affinity;
919 ret = TRUE;
921 return ret;
925 /***********************************************************************
926 * GetStdHandle (KERNEL32.276)
928 HANDLE WINAPI GetStdHandle( DWORD std_handle )
930 PDB *pdb = PROCESS_Current();
932 switch(std_handle)
934 case STD_INPUT_HANDLE: return pdb->env_db->hStdin;
935 case STD_OUTPUT_HANDLE: return pdb->env_db->hStdout;
936 case STD_ERROR_HANDLE: return pdb->env_db->hStderr;
938 SetLastError( ERROR_INVALID_PARAMETER );
939 return INVALID_HANDLE_VALUE;
943 /***********************************************************************
944 * SetStdHandle (KERNEL32.506)
946 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
948 PDB *pdb = PROCESS_Current();
949 /* FIXME: should we close the previous handle? */
950 switch(std_handle)
952 case STD_INPUT_HANDLE:
953 pdb->env_db->hStdin = handle;
954 return TRUE;
955 case STD_OUTPUT_HANDLE:
956 pdb->env_db->hStdout = handle;
957 return TRUE;
958 case STD_ERROR_HANDLE:
959 pdb->env_db->hStderr = handle;
960 return TRUE;
962 SetLastError( ERROR_INVALID_PARAMETER );
963 return FALSE;
966 /***********************************************************************
967 * GetProcessVersion (KERNEL32)
969 DWORD WINAPI GetProcessVersion( DWORD processid )
971 TDB *pTask;
972 PDB *pdb = PROCESS_IdToPDB( processid );
974 if (!pdb) return 0;
975 if (!(pTask = (TDB *)GlobalLock16( pdb->task ))) return 0;
976 return (pTask->version&0xff) | (((pTask->version >>8) & 0xff)<<16);
979 /***********************************************************************
980 * GetProcessFlags (KERNEL32)
982 DWORD WINAPI GetProcessFlags( DWORD processid )
984 PDB *pdb = PROCESS_IdToPDB( processid );
985 if (!pdb) return 0;
986 return pdb->flags;
989 /***********************************************************************
990 * SetProcessWorkingSetSize [KERNEL32.662]
991 * Sets the min/max working set sizes for a specified process.
993 * PARAMS
994 * hProcess [I] Handle to the process of interest
995 * minset [I] Specifies minimum working set size
996 * maxset [I] Specifies maximum working set size
998 * RETURNS STD
1000 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess,DWORD minset,
1001 DWORD maxset)
1003 FIXME_(process)("(0x%08x,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
1004 if(( minset == -1) && (maxset == -1)) {
1005 /* Trim the working set to zero */
1006 /* Swap the process out of physical RAM */
1008 return TRUE;
1011 /***********************************************************************
1012 * GetProcessWorkingSetSize (KERNEL32)
1014 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess,LPDWORD minset,
1015 LPDWORD maxset)
1017 FIXME_(process)("(0x%08x,%p,%p): stub\n",hProcess,minset,maxset);
1018 /* 32 MB working set size */
1019 if (minset) *minset = 32*1024*1024;
1020 if (maxset) *maxset = 32*1024*1024;
1021 return TRUE;
1024 /***********************************************************************
1025 * SetProcessShutdownParameters (KERNEL32)
1027 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1028 * Now tracks changes made (but does not act on these changes)
1029 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
1030 * It really shouldn't be here, but I'll move it when it's been checked!
1032 #define SHUTDOWN_NORETRY 1
1033 static unsigned int shutdown_noretry = 0;
1034 static unsigned int shutdown_priority = 0x280L;
1035 BOOL WINAPI SetProcessShutdownParameters(DWORD level,DWORD flags)
1037 if (flags & SHUTDOWN_NORETRY)
1038 shutdown_noretry = 1;
1039 else
1040 shutdown_noretry = 0;
1041 if (level > 0x100L && level < 0x3FFL)
1042 shutdown_priority = level;
1043 else
1045 ERR_(process)("invalid priority level 0x%08lx\n", level);
1046 return FALSE;
1048 return TRUE;
1052 /***********************************************************************
1053 * GetProcessShutdownParameters (KERNEL32)
1056 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel,
1057 LPDWORD lpdwFlags )
1059 (*lpdwLevel) = shutdown_priority;
1060 (*lpdwFlags) = (shutdown_noretry * SHUTDOWN_NORETRY);
1061 return TRUE;
1063 /***********************************************************************
1064 * SetProcessPriorityBoost (KERNEL32)
1066 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
1068 FIXME_(process)("(%d,%d): stub\n",hprocess,disableboost);
1069 /* Say we can do it. I doubt the program will notice that we don't. */
1070 return TRUE;
1073 /***********************************************************************
1074 * ReadProcessMemory (KERNEL32)
1075 * FIXME: check this, if we ever run win32 binaries in different addressspaces
1076 * ... and add a sizecheck
1078 BOOL WINAPI ReadProcessMemory( HANDLE hProcess, LPCVOID lpBaseAddress,
1079 LPVOID lpBuffer, DWORD nSize,
1080 LPDWORD lpNumberOfBytesRead )
1082 memcpy(lpBuffer,lpBaseAddress,nSize);
1083 if (lpNumberOfBytesRead) *lpNumberOfBytesRead = nSize;
1084 return TRUE;
1087 /***********************************************************************
1088 * WriteProcessMemory (KERNEL32)
1089 * FIXME: check this, if we ever run win32 binaries in different addressspaces
1090 * ... and add a sizecheck
1092 BOOL WINAPI WriteProcessMemory(HANDLE hProcess, LPVOID lpBaseAddress,
1093 LPVOID lpBuffer, DWORD nSize,
1094 LPDWORD lpNumberOfBytesWritten )
1096 memcpy(lpBaseAddress,lpBuffer,nSize);
1097 if (lpNumberOfBytesWritten) *lpNumberOfBytesWritten = nSize;
1098 return TRUE;
1101 /***********************************************************************
1102 * RegisterServiceProcess (KERNEL, KERNEL32)
1104 * A service process calls this function to ensure that it continues to run
1105 * even after a user logged off.
1107 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
1109 /* I don't think that Wine needs to do anything in that function */
1110 return 1; /* success */
1113 /***********************************************************************
1114 * GetExitCodeProcess [KERNEL32.325]
1116 * Gets termination status of specified process
1118 * RETURNS
1119 * Success: TRUE
1120 * Failure: FALSE
1122 BOOL WINAPI GetExitCodeProcess(
1123 HANDLE hProcess, /* [I] handle to the process */
1124 LPDWORD lpExitCode) /* [O] address to receive termination status */
1126 BOOL ret = FALSE;
1127 struct get_process_info_request *req = get_req_buffer();
1128 req->handle = hProcess;
1129 if (!server_call( REQ_GET_PROCESS_INFO ))
1131 if (lpExitCode) *lpExitCode = req->exit_code;
1132 ret = TRUE;
1134 return ret;
1138 /***********************************************************************
1139 * GetProcessHeaps [KERNEL32.376]
1141 DWORD WINAPI GetProcessHeaps(DWORD nrofheaps,HANDLE *heaps) {
1142 FIXME_(win32)("(%ld,%p), incomplete implementation.\n",nrofheaps,heaps);
1144 if (nrofheaps) {
1145 heaps[0] = GetProcessHeap();
1146 /* ... probably SystemHeap too ? */
1147 return 1;
1149 /* number of available heaps */
1150 return 1;
1154 /***********************************************************************
1155 * SetErrorMode (KERNEL32.486)
1157 UINT WINAPI SetErrorMode( UINT mode )
1159 UINT old = PROCESS_Current()->error_mode;
1160 PROCESS_Current()->error_mode = mode;
1161 return old;