Added COMPOBJ.DllEntryPoint (Acrobat3 16bit needs it).
[wine.git] / scheduler / process.c
blob1b7fa95a098eb67c2301e1e69d0e7056b0723764
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 "callback.h"
27 #include "debugtools.h"
29 DECLARE_DEBUG_CHANNEL(process)
30 DECLARE_DEBUG_CHANNEL(relay)
31 DECLARE_DEBUG_CHANNEL(win32)
34 /* The initial process PDB */
35 static PDB initial_pdb;
37 static PDB *PROCESS_First = &initial_pdb;
39 /***********************************************************************
40 * PROCESS_WalkProcess
42 void PROCESS_WalkProcess(void)
44 PDB *pdb;
45 char *name;
47 pdb = PROCESS_First;
48 MESSAGE( " pid PDB #th modref module \n" );
49 while(pdb)
51 if (pdb == &initial_pdb)
52 name = "initial PDB";
53 else
54 name = (pdb->exe_modref) ? pdb->exe_modref->shortname : "";
56 MESSAGE( " %8p %8p %5d %8p %s\n", pdb->server_pid, pdb,
57 pdb->threads, pdb->exe_modref, name);
58 pdb = pdb->next;
60 return;
63 /***********************************************************************
64 * PROCESS_Initial
66 * FIXME: This works only while running all processes in the same
67 * address space (or, at least, the initial process is mapped
68 * into all address spaces as is KERNEL32 in Windows 95)
71 PDB *PROCESS_Initial(void)
73 return &initial_pdb;
76 /***********************************************************************
77 * PROCESS_QueryInfo
79 * Retrieve information about a process
81 static BOOL PROCESS_QueryInfo( HANDLE handle,
82 struct get_process_info_reply *reply )
84 struct get_process_info_request req;
85 req.handle = handle;
86 CLIENT_SendRequest( REQ_GET_PROCESS_INFO, -1, 1, &req, sizeof(req) );
87 return !CLIENT_WaitSimpleReply( reply, sizeof(*reply), NULL );
90 /***********************************************************************
91 * PROCESS_IsCurrent
93 * Check if a handle is to the current process
95 BOOL PROCESS_IsCurrent( HANDLE handle )
97 struct get_process_info_reply reply;
98 return (PROCESS_QueryInfo( handle, &reply ) &&
99 (reply.pid == PROCESS_Current()->server_pid));
103 /***********************************************************************
104 * PROCESS_IdToPDB
106 * Convert a process id to a PDB, making sure it is valid.
108 PDB *PROCESS_IdToPDB( DWORD id )
110 PDB *pdb;
112 if (!id) return PROCESS_Current();
113 pdb = PROCESS_First;
114 while (pdb)
116 if ((DWORD)pdb->server_pid == id) return pdb;
117 pdb = pdb->next;
119 SetLastError( ERROR_INVALID_PARAMETER );
120 return NULL;
124 /***********************************************************************
125 * PROCESS_CallUserSignalProc
127 * FIXME: Some of the signals aren't sent correctly!
129 * The exact meaning of the USER signals is undocumented, but this
130 * should cover the basic idea:
132 * USIG_DLL_UNLOAD_WIN16
133 * This is sent when a 16-bit module is unloaded.
135 * USIG_DLL_UNLOAD_WIN32
136 * This is sent when a 32-bit module is unloaded.
138 * USIG_DLL_UNLOAD_ORPHANS
139 * This is sent after the last Win3.1 module is unloaded,
140 * to allow removal of orphaned menus.
142 * USIG_FAULT_DIALOG_PUSH
143 * USIG_FAULT_DIALOG_POP
144 * These are called to allow USER to prepare for displaying a
145 * fault dialog, even though the fault might have happened while
146 * inside a USER critical section.
148 * USIG_THREAD_INIT
149 * This is called from the context of a new thread, as soon as it
150 * has started to run.
152 * USIG_THREAD_EXIT
153 * This is called, still in its context, just before a thread is
154 * about to terminate.
156 * USIG_PROCESS_CREATE
157 * This is called, in the parent process context, after a new process
158 * has been created.
160 * USIG_PROCESS_INIT
161 * This is called in the new process context, just after the main thread
162 * has started execution (after the main thread's USIG_THREAD_INIT has
163 * been sent).
165 * USIG_PROCESS_LOADED
166 * This is called after the executable file has been loaded into the
167 * new process context.
169 * USIG_PROCESS_RUNNING
170 * This is called immediately before the main entry point is called.
172 * USIG_PROCESS_EXIT
173 * This is called in the context of a process that is about to
174 * terminate (but before the last thread's USIG_THREAD_EXIT has
175 * been sent).
177 * USIG_PROCESS_DESTROY
178 * This is called after a process has terminated.
181 * The meaning of the dwFlags bits is as follows:
183 * USIG_FLAGS_WIN32
184 * Current process is 32-bit.
186 * USIG_FLAGS_GUI
187 * Current process is a (Win32) GUI process.
189 * USIG_FLAGS_FEEDBACK
190 * Current process needs 'feedback' (determined from the STARTUPINFO
191 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
193 * USIG_FLAGS_FAULT
194 * The signal is being sent due to a fault.
196 static void PROCESS_CallUserSignalProcHelper( UINT uCode, DWORD dwThreadOrProcessId,
197 HMODULE hModule, DWORD flags, DWORD startup_flags )
199 DWORD dwFlags = 0;
201 /* Determine dwFlags */
203 if ( !(flags & PDB32_WIN16_PROC) ) dwFlags |= USIG_FLAGS_WIN32;
205 if ( !(flags & PDB32_CONSOLE_PROC) ) dwFlags |= USIG_FLAGS_GUI;
207 if ( dwFlags & USIG_FLAGS_GUI )
209 /* Feedback defaults to ON */
210 if ( !(startup_flags & STARTF_FORCEOFFFEEDBACK) )
211 dwFlags |= USIG_FLAGS_FEEDBACK;
213 else
215 /* Feedback defaults to OFF */
216 if (startup_flags & STARTF_FORCEONFEEDBACK)
217 dwFlags |= USIG_FLAGS_FEEDBACK;
220 /* Convert module handle to 16-bit */
222 if ( HIWORD( hModule ) )
223 hModule = MapHModuleLS( hModule );
225 /* Call USER signal proc */
227 if ( Callout.UserSignalProc )
228 Callout.UserSignalProc( uCode, dwThreadOrProcessId, dwFlags, hModule );
231 /* Call USER signal proc for the current thread/process */
232 void PROCESS_CallUserSignalProc( UINT uCode, HMODULE hModule )
234 DWORD dwThreadOrProcessId;
236 /* Get thread or process ID */
237 if ( uCode == USIG_THREAD_INIT || uCode == USIG_THREAD_EXIT )
238 dwThreadOrProcessId = GetCurrentThreadId();
239 else
240 dwThreadOrProcessId = GetCurrentProcessId();
242 PROCESS_CallUserSignalProcHelper( uCode, dwThreadOrProcessId, hModule,
243 PROCESS_Current()->flags,
244 PROCESS_Current()->env_db->startup_info->dwFlags );
248 /***********************************************************************
249 * PROCESS_CreateEnvDB
251 * Create the env DB for a newly started process.
253 static BOOL PROCESS_CreateEnvDB(void)
255 struct init_process_request req;
256 struct init_process_reply reply;
257 STARTUPINFOA *startup;
258 ENVDB *env_db;
259 char cmd_line[4096];
260 int len;
261 PDB *pdb = PROCESS_Current();
263 /* Retrieve startup info from the server */
265 req.dummy = 0;
266 CLIENT_SendRequest( REQ_INIT_PROCESS, -1, 1, &req, sizeof(req) );
267 if (CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply), cmd_line, sizeof(cmd_line) ))
268 return FALSE;
270 /* Allocate the env DB */
272 if (!(env_db = HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(ENVDB) )))
273 return FALSE;
274 pdb->env_db = env_db;
275 InitializeCriticalSection( &env_db->section );
277 /* Allocate and fill the startup info */
278 if (!(startup = HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(STARTUPINFOA) )))
279 return FALSE;
280 env_db->startup_info = startup;
281 startup->dwFlags = reply.start_flags;
282 startup->wShowWindow = reply.cmd_show;
283 env_db->hStdin = startup->hStdInput = reply.hstdin;
284 env_db->hStdout = startup->hStdOutput = reply.hstdout;
285 env_db->hStderr = startup->hStdError = reply.hstderr;
287 /* Copy the parent environment */
289 if (!ENV_InheritEnvironment( pdb, reply.env_ptr )) return FALSE;
291 /* Copy the command line */
293 if (!(pdb->env_db->cmd_line = HEAP_strdupA( pdb->heap, 0, cmd_line )))
294 return FALSE;
296 return TRUE;
300 /***********************************************************************
301 * PROCESS_FreePDB
303 * Free a PDB and all associated storage.
305 void PROCESS_FreePDB( PDB *pdb )
307 PDB **pptr = &PROCESS_First;
309 ENV_FreeEnvironment( pdb );
310 while (*pptr && (*pptr != pdb)) pptr = &(*pptr)->next;
311 if (*pptr) *pptr = pdb->next;
312 if (pdb->heap && (pdb->heap != pdb->system_heap)) HeapDestroy( pdb->heap );
313 HeapFree( SystemHeap, 0, pdb );
317 /***********************************************************************
318 * PROCESS_CreatePDB
320 * Allocate and fill a PDB structure.
321 * Runs in the context of the parent process.
323 static PDB *PROCESS_CreatePDB( PDB *parent, BOOL inherit )
325 PDB *pdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(PDB) );
327 if (!pdb) return NULL;
328 pdb->exit_code = 0x103; /* STILL_ACTIVE */
329 pdb->threads = 1;
330 pdb->running_threads = 1;
331 pdb->ring0_threads = 1;
332 pdb->system_heap = SystemHeap;
333 pdb->parent = parent;
334 pdb->group = pdb;
335 pdb->priority = 8; /* Normal */
336 pdb->heap = pdb->system_heap; /* will be changed later on */
337 pdb->next = PROCESS_First;
338 pdb->winver = 0xffff; /* to be determined */
339 PROCESS_First = pdb;
340 return pdb;
344 /***********************************************************************
345 * PROCESS_Init
347 BOOL PROCESS_Init(void)
349 TEB *teb;
350 int server_fd;
352 /* Start the server */
353 server_fd = CLIENT_InitServer();
355 /* Fill the initial process structure */
356 initial_pdb.exit_code = 0x103; /* STILL_ACTIVE */
357 initial_pdb.threads = 1;
358 initial_pdb.running_threads = 1;
359 initial_pdb.ring0_threads = 1;
360 initial_pdb.group = &initial_pdb;
361 initial_pdb.priority = 8; /* Normal */
362 initial_pdb.flags = PDB32_WIN16_PROC;
363 initial_pdb.winver = 0xffff; /* to be determined */
365 /* Initialize virtual memory management */
366 if (!VIRTUAL_Init()) return FALSE;
368 /* Create the initial thread structure and socket pair */
369 if (!(teb = THREAD_CreateInitialThread( &initial_pdb, server_fd ))) return FALSE;
371 /* Remember TEB selector of initial process for emergency use */
372 SYSLEVEL_EmergencyTeb = teb->teb_sel;
374 /* Create the system heap */
375 if (!(SystemHeap = HeapCreate( HEAP_GROWABLE, 0x10000, 0 ))) return FALSE;
376 initial_pdb.system_heap = initial_pdb.heap = SystemHeap;
378 /* Create the environment DB of the first process */
379 if (!PROCESS_CreateEnvDB()) return FALSE;
381 /* Create the SEGPTR heap */
382 if (!(SegptrHeap = HeapCreate( HEAP_WINE_SEGPTR, 0, 0 ))) return FALSE;
384 /* Initialize the first process critical section */
385 InitializeCriticalSection( &initial_pdb.crit_section );
387 return TRUE;
391 /***********************************************************************
392 * PROCESS_Start
394 * Startup routine of a new process. Called in the context of the new process.
396 void PROCESS_Start(void)
398 UINT cmdShow = 0;
399 LPTHREAD_START_ROUTINE entry = NULL;
400 PDB *pdb = PROCESS_Current();
401 NE_MODULE *pModule = NE_GetPtr( pdb->module );
402 OFSTRUCT *ofs = (OFSTRUCT *)((char*)(pModule) + (pModule)->fileinfo);
403 IMAGE_OPTIONAL_HEADER *header = !pModule->module32? NULL :
404 &PE_HEADER(pModule->module32)->OptionalHeader;
406 /* Get process type */
407 enum { PROC_DOS, PROC_WIN16, PROC_WIN32 } type;
408 if ( pdb->flags & PDB32_DOS_PROC )
409 type = PROC_DOS;
410 else if ( pdb->flags & PDB32_WIN16_PROC )
411 type = PROC_WIN16;
412 else
413 type = PROC_WIN32;
415 /* Initialize the critical section */
416 InitializeCriticalSection( &pdb->crit_section );
418 /* Create the heap */
419 if (!(pdb->heap = HeapCreate( HEAP_GROWABLE,
420 header? header->SizeOfHeapReserve : 0x10000,
421 header? header->SizeOfHeapCommit : 0 )))
422 goto error;
423 pdb->heap_list = pdb->heap;
425 /* Create the environment db */
426 if (!PROCESS_CreateEnvDB()) goto error;
428 /* Create a task for this process */
429 if (pdb->env_db->startup_info->dwFlags & STARTF_USESHOWWINDOW)
430 cmdShow = pdb->env_db->startup_info->wShowWindow;
431 if (!TASK_Create( pModule, pdb->hInstance, pdb->hPrevInstance, cmdShow ))
432 goto error;
434 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
435 * context of the parent process. Actually, the USER signal proc
436 * doesn't really care about that, but it *does* require that the
437 * startup parameters are correctly set up, so that GetProcessDword
438 * works. Furthermore, before calling the USER signal proc the
439 * 16-bit stack must be set up, which it is only after TASK_Create
440 * in the case of a 16-bit process. Thus, we send the signal here.
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 /* Now call the entry point */
486 PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING, 0 );
488 switch ( type )
490 case PROC_DOS:
491 TRACE_(relay)( "Starting DOS process\n" );
492 DOSVM_Enter( NULL );
493 ERR_(relay)( "DOSVM_Enter returned; should not happen!\n" );
494 ExitProcess( 0 );
496 case PROC_WIN16:
497 TRACE_(relay)( "Starting Win16 process\n" );
498 TASK_CallToStart();
499 ERR_(relay)( "TASK_CallToStart returned; should not happen!\n" );
500 ExitProcess( 0 );
502 case PROC_WIN32:
503 TRACE_(relay)( "Starting Win32 process (entryproc=%p)\n", entry );
504 ExitProcess( entry(NULL) );
507 error:
508 ExitProcess( GetLastError() );
512 /***********************************************************************
513 * PROCESS_Create
515 * Create a new process database and associated info.
517 PDB *PROCESS_Create( NE_MODULE *pModule, LPCSTR cmd_line, LPCSTR env,
518 HINSTANCE16 hInstance, HINSTANCE16 hPrevInstance,
519 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
520 BOOL inherit, DWORD flags, STARTUPINFOA *startup,
521 PROCESS_INFORMATION *info )
523 HANDLE handles[2], load_done_evt = INVALID_HANDLE_VALUE;
524 DWORD exitcode, size;
525 int server_thandle;
526 struct new_process_request req;
527 struct new_process_reply reply;
528 TEB *teb = NULL;
529 PDB *parent = PROCESS_Current();
530 PDB *pdb = PROCESS_CreatePDB( parent, inherit );
532 if (!pdb) return NULL;
533 info->hThread = info->hProcess = INVALID_HANDLE_VALUE;
535 /* Create the process on the server side */
537 req.inherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
538 req.inherit_all = inherit;
539 req.create_flags = flags;
540 req.start_flags = startup->dwFlags;
541 if (startup->dwFlags & STARTF_USESTDHANDLES)
543 req.hstdin = startup->hStdInput;
544 req.hstdout = startup->hStdOutput;
545 req.hstderr = startup->hStdError;
547 else
549 req.hstdin = GetStdHandle( STD_INPUT_HANDLE );
550 req.hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
551 req.hstderr = GetStdHandle( STD_ERROR_HANDLE );
553 req.cmd_show = startup->wShowWindow;
554 req.env_ptr = (void*)env; /* FIXME: hack */
555 CLIENT_SendRequest( REQ_NEW_PROCESS, -1, 2,
556 &req, sizeof(req), cmd_line, strlen(cmd_line) + 1 );
557 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) goto error;
558 pdb->server_pid = reply.pid;
559 info->hProcess = reply.handle;
560 info->dwProcessId = (DWORD)pdb->server_pid;
562 if ((flags & DEBUG_PROCESS) ||
563 ((parent->flags & PDB32_DEBUGGED) && !(flags & DEBUG_ONLY_THIS_PROCESS)))
564 pdb->flags |= PDB32_DEBUGGED;
566 if (pModule->module32) /* Win32 process */
568 IMAGE_OPTIONAL_HEADER *header = &PE_HEADER(pModule->module32)->OptionalHeader;
569 size = header->SizeOfStackReserve;
570 if (header->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
571 pdb->flags |= PDB32_CONSOLE_PROC;
573 else if (!pModule->dos_image) /* Win16 process */
575 size = 0;
576 pdb->flags |= PDB32_WIN16_PROC;
578 else /* DOS process */
580 size = 0;
581 pdb->flags |= PDB32_DOS_PROC;
584 /* Create the main thread */
586 if (!(teb = THREAD_Create( pdb, 0L, size, hInstance == 0, tsa, &server_thandle )))
587 goto error;
588 info->hThread = server_thandle;
589 info->dwThreadId = (DWORD)teb->tid;
590 teb->startup = PROCESS_Start;
592 /* Create the load-done event */
593 load_done_evt = CreateEventA( NULL, TRUE, FALSE, NULL );
594 DuplicateHandle( GetCurrentProcess(), load_done_evt,
595 info->hProcess, &pdb->load_done_evt, 0, TRUE, DUPLICATE_SAME_ACCESS );
597 /* Pass module/instance to new process (FIXME: hack) */
598 pdb->module = pModule->self;
599 pdb->hInstance = hInstance;
600 pdb->hPrevInstance = hPrevInstance;
601 SYSDEPS_SpawnThread( teb );
603 /* Wait until process is initialized (or initialization failed) */
604 handles[0] = info->hProcess;
605 handles[1] = load_done_evt;
607 switch ( WaitForMultipleObjects( 2, handles, FALSE, INFINITE ) )
609 default:
610 ERR_(process)( "WaitForMultipleObjects failed\n" );
611 break;
613 case 0:
614 /* Child initialization code returns error condition as exitcode */
615 if ( GetExitCodeProcess( info->hProcess, &exitcode ) )
616 SetLastError( exitcode );
617 goto error;
619 case 1:
620 /* Get 16-bit task up and running */
621 if ( pdb->flags & PDB32_WIN16_PROC )
623 /* Post event to start the task */
624 PostEvent16( pdb->task );
626 /* If we ourselves are a 16-bit task, we Yield() directly. */
627 if ( parent->flags & PDB32_WIN16_PROC )
628 OldYield16();
630 break;
633 CloseHandle( load_done_evt );
634 load_done_evt = INVALID_HANDLE_VALUE;
636 return pdb;
638 error:
639 if (load_done_evt != INVALID_HANDLE_VALUE) CloseHandle( load_done_evt );
640 if (info->hThread != INVALID_HANDLE_VALUE) CloseHandle( info->hThread );
641 if (info->hProcess != INVALID_HANDLE_VALUE) CloseHandle( info->hProcess );
642 PROCESS_FreePDB( pdb );
643 return NULL;
647 /***********************************************************************
648 * ExitProcess (KERNEL32.100)
650 void WINAPI ExitProcess( DWORD status )
652 EnterCriticalSection( &PROCESS_Current()->crit_section );
653 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
654 LeaveCriticalSection( &PROCESS_Current()->crit_section );
656 TASK_KillTask( 0 );
657 TerminateProcess( GetCurrentProcess(), status );
660 /***********************************************************************
661 * ExitProcess16 (KERNEL.466)
663 void WINAPI ExitProcess16( WORD status )
665 SYSLEVEL_ReleaseWin16Lock();
666 ExitProcess( status );
669 /******************************************************************************
670 * TerminateProcess (KERNEL32.684)
672 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
674 struct terminate_process_request req;
675 req.handle = handle;
676 req.exit_code = exit_code;
677 CLIENT_SendRequest( REQ_TERMINATE_PROCESS, -1, 1, &req, sizeof(req) );
678 return !CLIENT_WaitReply( NULL, NULL, 0 );
682 /***********************************************************************
683 * GetProcessDword (KERNEL32.18) (KERNEL.485)
684 * 'Of course you cannot directly access Windows internal structures'
686 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
688 PDB *process = PROCESS_IdToPDB( dwProcessID );
689 TDB *pTask;
690 DWORD x, y;
692 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
693 if ( !process ) return 0;
695 switch ( offset )
697 case GPD_APP_COMPAT_FLAGS:
698 pTask = (TDB *)GlobalLock16( process->task );
699 return pTask? pTask->compat_flags : 0;
701 case GPD_LOAD_DONE_EVENT:
702 return process->load_done_evt;
704 case GPD_HINSTANCE16:
705 pTask = (TDB *)GlobalLock16( process->task );
706 return pTask? pTask->hInstance : 0;
708 case GPD_WINDOWS_VERSION:
709 pTask = (TDB *)GlobalLock16( process->task );
710 return pTask? pTask->version : 0;
712 case GPD_THDB:
713 if ( process != PROCESS_Current() ) return 0;
714 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
716 case GPD_PDB:
717 return (DWORD)process;
719 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
720 return process->env_db->startup_info->hStdOutput;
722 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
723 return process->env_db->startup_info->hStdInput;
725 case GPD_STARTF_SHOWWINDOW:
726 return process->env_db->startup_info->wShowWindow;
728 case GPD_STARTF_SIZE:
729 x = process->env_db->startup_info->dwXSize;
730 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
731 y = process->env_db->startup_info->dwYSize;
732 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
733 return MAKELONG( x, y );
735 case GPD_STARTF_POSITION:
736 x = process->env_db->startup_info->dwX;
737 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
738 y = process->env_db->startup_info->dwY;
739 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
740 return MAKELONG( x, y );
742 case GPD_STARTF_FLAGS:
743 return process->env_db->startup_info->dwFlags;
745 case GPD_PARENT:
746 return (DWORD)process->parent->server_pid;
748 case GPD_FLAGS:
749 return process->flags;
751 case GPD_USERDATA:
752 return process->process_dword;
754 default:
755 ERR_(win32)("Unknown offset %d\n", offset );
756 return 0;
760 /***********************************************************************
761 * SetProcessDword (KERNEL.484)
762 * 'Of course you cannot directly access Windows internal structures'
764 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
766 PDB *process = PROCESS_IdToPDB( dwProcessID );
768 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
769 if ( !process ) return;
771 switch ( offset )
773 case GPD_APP_COMPAT_FLAGS:
774 case GPD_LOAD_DONE_EVENT:
775 case GPD_HINSTANCE16:
776 case GPD_WINDOWS_VERSION:
777 case GPD_THDB:
778 case GPD_PDB:
779 case GPD_STARTF_SHELLDATA:
780 case GPD_STARTF_HOTKEY:
781 case GPD_STARTF_SHOWWINDOW:
782 case GPD_STARTF_SIZE:
783 case GPD_STARTF_POSITION:
784 case GPD_STARTF_FLAGS:
785 case GPD_PARENT:
786 case GPD_FLAGS:
787 ERR_(win32)("Not allowed to modify offset %d\n", offset );
788 break;
790 case GPD_USERDATA:
791 process->process_dword = value;
792 break;
794 default:
795 ERR_(win32)("Unknown offset %d\n", offset );
796 break;
801 /***********************************************************************
802 * GetCurrentProcess (KERNEL32.198)
804 HANDLE WINAPI GetCurrentProcess(void)
806 return CURRENT_PROCESS_PSEUDOHANDLE;
810 /*********************************************************************
811 * OpenProcess (KERNEL32.543)
813 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
815 struct open_process_request req;
816 struct open_process_reply reply;
818 req.pid = (void *)id;
819 req.access = access;
820 req.inherit = inherit;
821 CLIENT_SendRequest( REQ_OPEN_PROCESS, -1, 1, &req, sizeof(req) );
822 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0;
823 return reply.handle;
826 /*********************************************************************
827 * MapProcessHandle (KERNEL.483)
829 DWORD WINAPI MapProcessHandle( HANDLE handle )
831 struct get_process_info_reply reply;
832 if ( !PROCESS_QueryInfo( handle, &reply ) ) return 0;
833 return (DWORD)reply.pid;
836 /***********************************************************************
837 * GetCurrentProcessId (KERNEL32.199)
839 DWORD WINAPI GetCurrentProcessId(void)
841 return (DWORD)PROCESS_Current()->server_pid;
845 /***********************************************************************
846 * GetProcessHeap (KERNEL32.259)
848 HANDLE WINAPI GetProcessHeap(void)
850 PDB *pdb = PROCESS_Current();
851 return pdb->heap ? pdb->heap : SystemHeap;
855 /***********************************************************************
856 * GetThreadLocale (KERNEL32.295)
858 LCID WINAPI GetThreadLocale(void)
860 return PROCESS_Current()->locale;
864 /***********************************************************************
865 * SetPriorityClass (KERNEL32.503)
867 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
869 struct set_process_info_request req;
870 req.handle = hprocess;
871 req.priority = priorityclass;
872 req.mask = SET_PROCESS_INFO_PRIORITY;
873 CLIENT_SendRequest( REQ_SET_PROCESS_INFO, -1, 1, &req, sizeof(req) );
874 return !CLIENT_WaitReply( NULL, NULL, 0 );
878 /***********************************************************************
879 * GetPriorityClass (KERNEL32.250)
881 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
883 struct get_process_info_reply reply;
884 if (!PROCESS_QueryInfo( hprocess, &reply )) return 0;
885 return reply.priority;
889 /***********************************************************************
890 * SetProcessAffinityMask (KERNEL32.662)
892 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
894 struct set_process_info_request req;
895 req.handle = hProcess;
896 req.affinity = affmask;
897 req.mask = SET_PROCESS_INFO_AFFINITY;
898 CLIENT_SendRequest( REQ_SET_PROCESS_INFO, -1, 1, &req, sizeof(req) );
899 return !CLIENT_WaitReply( NULL, NULL, 0 );
902 /**********************************************************************
903 * GetProcessAffinityMask (KERNEL32.373)
905 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
906 LPDWORD lpProcessAffinityMask,
907 LPDWORD lpSystemAffinityMask )
909 struct get_process_info_reply reply;
910 if (!PROCESS_QueryInfo( hProcess, &reply )) return FALSE;
911 if (lpProcessAffinityMask) *lpProcessAffinityMask = reply.process_affinity;
912 if (lpSystemAffinityMask) *lpSystemAffinityMask = reply.system_affinity;
913 return TRUE;
917 /***********************************************************************
918 * GetStdHandle (KERNEL32.276)
920 HANDLE WINAPI GetStdHandle( DWORD std_handle )
922 PDB *pdb = PROCESS_Current();
924 switch(std_handle)
926 case STD_INPUT_HANDLE: return pdb->env_db->hStdin;
927 case STD_OUTPUT_HANDLE: return pdb->env_db->hStdout;
928 case STD_ERROR_HANDLE: return pdb->env_db->hStderr;
930 SetLastError( ERROR_INVALID_PARAMETER );
931 return INVALID_HANDLE_VALUE;
935 /***********************************************************************
936 * SetStdHandle (KERNEL32.506)
938 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
940 PDB *pdb = PROCESS_Current();
941 /* FIXME: should we close the previous handle? */
942 switch(std_handle)
944 case STD_INPUT_HANDLE:
945 pdb->env_db->hStdin = handle;
946 return TRUE;
947 case STD_OUTPUT_HANDLE:
948 pdb->env_db->hStdout = handle;
949 return TRUE;
950 case STD_ERROR_HANDLE:
951 pdb->env_db->hStderr = handle;
952 return TRUE;
954 SetLastError( ERROR_INVALID_PARAMETER );
955 return FALSE;
958 /***********************************************************************
959 * GetProcessVersion (KERNEL32)
961 DWORD WINAPI GetProcessVersion( DWORD processid )
963 TDB *pTask;
964 PDB *pdb = PROCESS_IdToPDB( processid );
966 if (!pdb) return 0;
967 if (!(pTask = (TDB *)GlobalLock16( pdb->task ))) return 0;
968 return (pTask->version&0xff) | (((pTask->version >>8) & 0xff)<<16);
971 /***********************************************************************
972 * GetProcessFlags (KERNEL32)
974 DWORD WINAPI GetProcessFlags( DWORD processid )
976 PDB *pdb = PROCESS_IdToPDB( processid );
977 if (!pdb) return 0;
978 return pdb->flags;
981 /***********************************************************************
982 * SetProcessWorkingSetSize [KERNEL32.662]
983 * Sets the min/max working set sizes for a specified process.
985 * PARAMS
986 * hProcess [I] Handle to the process of interest
987 * minset [I] Specifies minimum working set size
988 * maxset [I] Specifies maximum working set size
990 * RETURNS STD
992 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess,DWORD minset,
993 DWORD maxset)
995 FIXME_(process)("(0x%08x,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
996 if(( minset == -1) && (maxset == -1)) {
997 /* Trim the working set to zero */
998 /* Swap the process out of physical RAM */
1000 return TRUE;
1003 /***********************************************************************
1004 * GetProcessWorkingSetSize (KERNEL32)
1006 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess,LPDWORD minset,
1007 LPDWORD maxset)
1009 FIXME_(process)("(0x%08x,%p,%p): stub\n",hProcess,minset,maxset);
1010 /* 32 MB working set size */
1011 if (minset) *minset = 32*1024*1024;
1012 if (maxset) *maxset = 32*1024*1024;
1013 return TRUE;
1016 /***********************************************************************
1017 * SetProcessShutdownParameters (KERNEL32)
1019 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1020 * Now tracks changes made (but does not act on these changes)
1021 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
1022 * It really shouldn't be here, but I'll move it when it's been checked!
1024 #define SHUTDOWN_NORETRY 1
1025 static unsigned int shutdown_noretry = 0;
1026 static unsigned int shutdown_priority = 0x280L;
1027 BOOL WINAPI SetProcessShutdownParameters(DWORD level,DWORD flags)
1029 if (flags & SHUTDOWN_NORETRY)
1030 shutdown_noretry = 1;
1031 else
1032 shutdown_noretry = 0;
1033 if (level > 0x100L && level < 0x3FFL)
1034 shutdown_priority = level;
1035 else
1037 ERR_(process)("invalid priority level 0x%08lx\n", level);
1038 return FALSE;
1040 return TRUE;
1044 /***********************************************************************
1045 * GetProcessShutdownParameters (KERNEL32)
1048 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel,
1049 LPDWORD lpdwFlags )
1051 (*lpdwLevel) = shutdown_priority;
1052 (*lpdwFlags) = (shutdown_noretry * SHUTDOWN_NORETRY);
1053 return TRUE;
1055 /***********************************************************************
1056 * SetProcessPriorityBoost (KERNEL32)
1058 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
1060 FIXME_(process)("(%d,%d): stub\n",hprocess,disableboost);
1061 /* Say we can do it. I doubt the program will notice that we don't. */
1062 return TRUE;
1065 /***********************************************************************
1066 * ReadProcessMemory (KERNEL32)
1067 * FIXME: check this, if we ever run win32 binaries in different addressspaces
1068 * ... and add a sizecheck
1070 BOOL WINAPI ReadProcessMemory( HANDLE hProcess, LPCVOID lpBaseAddress,
1071 LPVOID lpBuffer, DWORD nSize,
1072 LPDWORD lpNumberOfBytesRead )
1074 memcpy(lpBuffer,lpBaseAddress,nSize);
1075 if (lpNumberOfBytesRead) *lpNumberOfBytesRead = nSize;
1076 return TRUE;
1079 /***********************************************************************
1080 * WriteProcessMemory (KERNEL32)
1081 * FIXME: check this, if we ever run win32 binaries in different addressspaces
1082 * ... and add a sizecheck
1084 BOOL WINAPI WriteProcessMemory(HANDLE hProcess, LPVOID lpBaseAddress,
1085 LPVOID lpBuffer, DWORD nSize,
1086 LPDWORD lpNumberOfBytesWritten )
1088 memcpy(lpBaseAddress,lpBuffer,nSize);
1089 if (lpNumberOfBytesWritten) *lpNumberOfBytesWritten = nSize;
1090 return TRUE;
1093 /***********************************************************************
1094 * RegisterServiceProcess (KERNEL, KERNEL32)
1096 * A service process calls this function to ensure that it continues to run
1097 * even after a user logged off.
1099 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
1101 /* I don't think that Wine needs to do anything in that function */
1102 return 1; /* success */
1105 /***********************************************************************
1106 * GetExitCodeProcess [KERNEL32.325]
1108 * Gets termination status of specified process
1110 * RETURNS
1111 * Success: TRUE
1112 * Failure: FALSE
1114 BOOL WINAPI GetExitCodeProcess(
1115 HANDLE hProcess, /* [I] handle to the process */
1116 LPDWORD lpExitCode) /* [O] address to receive termination status */
1118 struct get_process_info_reply reply;
1119 if (!PROCESS_QueryInfo( hProcess, &reply )) return FALSE;
1120 if (lpExitCode) *lpExitCode = reply.exit_code;
1121 return TRUE;
1125 /***********************************************************************
1126 * GetProcessHeaps [KERNEL32.376]
1128 DWORD WINAPI GetProcessHeaps(DWORD nrofheaps,HANDLE *heaps) {
1129 FIXME_(win32)("(%ld,%p), incomplete implementation.\n",nrofheaps,heaps);
1131 if (nrofheaps) {
1132 heaps[0] = GetProcessHeap();
1133 /* ... probably SystemHeap too ? */
1134 return 1;
1136 /* number of available heaps */
1137 return 1;
1141 /***********************************************************************
1142 * SetErrorMode (KERNEL32.486)
1144 UINT WINAPI SetErrorMode( UINT mode )
1146 UINT old = PROCESS_Current()->error_mode;
1147 PROCESS_Current()->error_mode = mode;
1148 return old;