Added proper handling TVIS_OVERLAYMASK flag and allows to display
[wine.git] / scheduler / process.c
blobba2ed7b4e38cd2feb2d8de6b09a8c050486aa485
1 /*
2 * Win32 processes
4 * Copyright 1996, 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include "wine/winbase16.h"
13 #include "wine/exception.h"
14 #include "process.h"
15 #include "module.h"
16 #include "neexe.h"
17 #include "file.h"
18 #include "global.h"
19 #include "heap.h"
20 #include "task.h"
21 #include "ldt.h"
22 #include "syslevel.h"
23 #include "thread.h"
24 #include "winerror.h"
25 #include "pe_image.h"
26 #include "server.h"
27 #include "options.h"
28 #include "callback.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 /***********************************************************************
43 * PROCESS_WalkProcess
45 void PROCESS_WalkProcess(void)
47 PDB *pdb;
48 char *name;
50 pdb = PROCESS_First;
51 MESSAGE( " pid PDB #th modref module \n" );
52 while(pdb)
54 if (pdb == &initial_pdb)
55 name = "initial PDB";
56 else
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);
61 pdb = pdb->next;
63 return;
67 /***********************************************************************
68 * PROCESS_IdToPDB
70 * Convert a process id to a PDB, making sure it is valid.
72 PDB *PROCESS_IdToPDB( DWORD pid )
74 PDB *pdb;
76 if (!pid) return PROCESS_Current();
77 pdb = PROCESS_First;
78 while (pdb)
80 if ((DWORD)pdb->server_pid == pid) return pdb;
81 pdb = pdb->next;
83 SetLastError( ERROR_INVALID_PARAMETER );
84 return NULL;
88 /***********************************************************************
89 * PROCESS_CallUserSignalProc
91 * FIXME: Some of the signals aren't sent correctly!
93 * The exact meaning of the USER signals is undocumented, but this
94 * should cover the basic idea:
96 * USIG_DLL_UNLOAD_WIN16
97 * This is sent when a 16-bit module is unloaded.
99 * USIG_DLL_UNLOAD_WIN32
100 * This is sent when a 32-bit module is unloaded.
102 * USIG_DLL_UNLOAD_ORPHANS
103 * This is sent after the last Win3.1 module is unloaded,
104 * to allow removal of orphaned menus.
106 * USIG_FAULT_DIALOG_PUSH
107 * USIG_FAULT_DIALOG_POP
108 * These are called to allow USER to prepare for displaying a
109 * fault dialog, even though the fault might have happened while
110 * inside a USER critical section.
112 * USIG_THREAD_INIT
113 * This is called from the context of a new thread, as soon as it
114 * has started to run.
116 * USIG_THREAD_EXIT
117 * This is called, still in its context, just before a thread is
118 * about to terminate.
120 * USIG_PROCESS_CREATE
121 * This is called, in the parent process context, after a new process
122 * has been created.
124 * USIG_PROCESS_INIT
125 * This is called in the new process context, just after the main thread
126 * has started execution (after the main thread's USIG_THREAD_INIT has
127 * been sent).
129 * USIG_PROCESS_LOADED
130 * This is called after the executable file has been loaded into the
131 * new process context.
133 * USIG_PROCESS_RUNNING
134 * This is called immediately before the main entry point is called.
136 * USIG_PROCESS_EXIT
137 * This is called in the context of a process that is about to
138 * terminate (but before the last thread's USIG_THREAD_EXIT has
139 * been sent).
141 * USIG_PROCESS_DESTROY
142 * This is called after a process has terminated.
145 * The meaning of the dwFlags bits is as follows:
147 * USIG_FLAGS_WIN32
148 * Current process is 32-bit.
150 * USIG_FLAGS_GUI
151 * Current process is a (Win32) GUI process.
153 * USIG_FLAGS_FEEDBACK
154 * Current process needs 'feedback' (determined from the STARTUPINFO
155 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
157 * USIG_FLAGS_FAULT
158 * The signal is being sent due to a fault.
160 void PROCESS_CallUserSignalProc( UINT uCode, HMODULE hModule )
162 DWORD flags = PROCESS_Current()->flags;
163 DWORD startup_flags = PROCESS_Current()->env_db->startup_info->dwFlags;
164 DWORD dwFlags = 0;
166 /* Determine dwFlags */
168 if ( !(flags & PDB32_WIN16_PROC) ) dwFlags |= USIG_FLAGS_WIN32;
170 if ( !(flags & PDB32_CONSOLE_PROC) ) dwFlags |= USIG_FLAGS_GUI;
172 if ( dwFlags & USIG_FLAGS_GUI )
174 /* Feedback defaults to ON */
175 if ( !(startup_flags & STARTF_FORCEOFFFEEDBACK) )
176 dwFlags |= USIG_FLAGS_FEEDBACK;
178 else
180 /* Feedback defaults to OFF */
181 if (startup_flags & STARTF_FORCEONFEEDBACK)
182 dwFlags |= USIG_FLAGS_FEEDBACK;
185 /* Convert module handle to 16-bit */
187 if ( HIWORD( hModule ) )
188 hModule = MapHModuleLS( hModule );
190 /* Call USER signal proc */
192 if ( Callout.UserSignalProc )
194 if ( uCode == USIG_THREAD_INIT || uCode == USIG_THREAD_EXIT )
195 Callout.UserSignalProc( uCode, GetCurrentThreadId(), dwFlags, hModule );
196 else
197 Callout.UserSignalProc( uCode, GetCurrentProcessId(), dwFlags, hModule );
201 /***********************************************************************
202 * PROCESS_CreateEnvDB
204 * Create the env DB for a newly started process.
206 static BOOL PROCESS_CreateEnvDB(void)
208 struct init_process_request *req = get_req_buffer();
209 STARTUPINFOA *startup;
210 ENVDB *env_db;
211 char cmd_line[4096];
212 PDB *pdb = PROCESS_Current();
214 /* Allocate the env DB */
216 if (!(env_db = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ENVDB) )))
217 return FALSE;
218 pdb->env_db = env_db;
219 InitializeCriticalSection( &env_db->section );
221 /* Allocate and fill the startup info */
222 if (!(startup = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(STARTUPINFOA) )))
223 return FALSE;
224 env_db->startup_info = startup;
226 /* Retrieve startup info from the server */
228 req->ldt_copy = ldt_copy;
229 req->ldt_flags = ldt_flags_copy;
230 if (server_call( REQ_INIT_PROCESS )) return FALSE;
231 pdb->exe_file = req->exe_file;
232 startup->dwFlags = req->start_flags;
233 startup->wShowWindow = req->cmd_show;
234 env_db->hStdin = startup->hStdInput = req->hstdin;
235 env_db->hStdout = startup->hStdOutput = req->hstdout;
236 env_db->hStderr = startup->hStdError = req->hstderr;
237 lstrcpynA( cmd_line, req->cmdline, sizeof(cmd_line) );
239 /* Copy the parent environment */
241 if (!ENV_InheritEnvironment( req->env_ptr )) return FALSE;
243 /* Copy the command line */
245 if (!(pdb->env_db->cmd_line = HEAP_strdupA( GetProcessHeap(), 0, cmd_line )))
246 return FALSE;
248 return TRUE;
252 /***********************************************************************
253 * PROCESS_FreePDB
255 * Free a PDB and all associated storage.
257 void PROCESS_FreePDB( PDB *pdb )
259 PDB **pptr = &PROCESS_First;
261 ENV_FreeEnvironment( pdb );
262 while (*pptr && (*pptr != pdb)) pptr = &(*pptr)->next;
263 if (*pptr) *pptr = pdb->next;
264 HeapFree( GetProcessHeap(), 0, pdb );
268 /***********************************************************************
269 * PROCESS_CreatePDB
271 * Allocate and fill a PDB structure.
272 * Runs in the context of the parent process.
274 static PDB *PROCESS_CreatePDB( PDB *parent, BOOL inherit )
276 PDB *pdb = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(PDB) );
278 if (!pdb) return NULL;
279 pdb->exit_code = STILL_ACTIVE;
280 pdb->heap = GetProcessHeap();
281 pdb->threads = 1;
282 pdb->running_threads = 1;
283 pdb->ring0_threads = 1;
284 pdb->parent = parent;
285 pdb->group = pdb;
286 pdb->priority = 8; /* Normal */
287 pdb->next = PROCESS_First;
288 pdb->winver = 0xffff; /* to be determined */
289 pdb->main_queue = INVALID_HANDLE_VALUE16;
290 PROCESS_First = pdb;
291 return pdb;
295 /***********************************************************************
296 * PROCESS_Init
298 BOOL PROCESS_Init( BOOL win32 )
300 TEB *teb;
301 int server_fd;
303 /* Start the server */
304 server_fd = CLIENT_InitServer();
306 /* Fill the initial process structure */
307 initial_pdb.exit_code = STILL_ACTIVE;
308 initial_pdb.threads = 1;
309 initial_pdb.running_threads = 1;
310 initial_pdb.ring0_threads = 1;
311 initial_pdb.group = &initial_pdb;
312 initial_pdb.priority = 8; /* Normal */
313 initial_pdb.flags = win32? 0 : PDB32_WIN16_PROC;
314 initial_pdb.winver = 0xffff; /* to be determined */
315 initial_pdb.main_queue = INVALID_HANDLE_VALUE16;
317 /* Initialize virtual memory management */
318 if (!VIRTUAL_Init()) return FALSE;
320 /* Create the initial thread structure and socket pair */
321 if (!(teb = THREAD_CreateInitialThread( &initial_pdb, server_fd ))) return FALSE;
323 /* Remember TEB selector of initial process for emergency use */
324 SYSLEVEL_EmergencyTeb = teb->teb_sel;
326 /* Create the system and process heaps */
327 if (!HEAP_CreateSystemHeap()) return FALSE;
328 initial_pdb.heap = HeapCreate( HEAP_GROWABLE, 0, 0 );
330 /* Create the idle event for the initial process
331 FIXME 1: Shouldn't we call UserSignalProc for the initial process too?
332 FIXME 2: It seems to me that the initial pdb becomes never freed, so I don't now
333 where to release the idle event for the initial process.
335 initial_pdb.idle_event = CreateEventA ( NULL, TRUE, FALSE, NULL );
336 initial_pdb.idle_event = ConvertToGlobalHandle ( initial_pdb.idle_event );
338 /* Initialize signal handling */
339 if (!SIGNAL_Init()) return FALSE;
341 /* Create the environment DB of the first process */
342 if (!PROCESS_CreateEnvDB()) return FALSE;
344 /* Create the SEGPTR heap */
345 if (!(SegptrHeap = HeapCreate( HEAP_WINE_SEGPTR, 0, 0 ))) return FALSE;
347 /* Initialize the first process critical section */
348 InitializeCriticalSection( &initial_pdb.crit_section );
350 return TRUE;
354 /***********************************************************************
355 * PROCESS_Start
357 * Startup routine of a new process. Called in the context of the new process.
359 void PROCESS_Start(void)
361 struct init_process_done_request *req = get_req_buffer();
362 int debugged;
363 UINT cmdShow = SW_SHOWNORMAL;
364 LPTHREAD_START_ROUTINE entry = NULL;
365 PDB *pdb = PROCESS_Current();
366 NE_MODULE *pModule = NE_GetPtr( pdb->module );
367 LPCSTR filename = ((OFSTRUCT *)((char*)(pModule) + (pModule)->fileinfo))->szPathName;
369 /* Get process type */
370 enum { PROC_DOS, PROC_WIN16, PROC_WIN32 } type;
371 if ( pdb->flags & PDB32_DOS_PROC )
372 type = PROC_DOS;
373 else if ( pdb->flags & PDB32_WIN16_PROC )
374 type = PROC_WIN16;
375 else
376 type = PROC_WIN32;
378 /* Initialize the critical section */
379 InitializeCriticalSection( &pdb->crit_section );
381 /* Create the environment db */
382 if (!PROCESS_CreateEnvDB()) goto error;
384 /* Create a task for this process */
385 if (pdb->env_db->startup_info->dwFlags & STARTF_USESHOWWINDOW)
386 cmdShow = pdb->env_db->startup_info->wShowWindow;
387 if (!TASK_Create( pModule, cmdShow ))
388 goto error;
390 /* Load all process modules */
391 switch ( type )
393 case PROC_WIN16:
394 if ( !NE_InitProcess( pModule ) )
395 goto error;
396 break;
398 case PROC_WIN32:
399 /* Create 32-bit MODREF */
400 if ( !PE_CreateModule( pModule->module32, filename, 0, FALSE ) )
401 goto error;
403 /* Increment EXE refcount */
404 assert( pdb->exe_modref );
405 pdb->exe_modref->refCount++;
407 /* Retrieve entry point address */
408 entry = (LPTHREAD_START_ROUTINE)RVA_PTR(pModule->module32,
409 OptionalHeader.AddressOfEntryPoint);
410 break;
412 case PROC_DOS:
413 /* FIXME: move DOS startup code here */
414 break;
418 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
419 * context of the parent process. Actually, the USER signal proc
420 * doesn't really care about that, but it *does* require that the
421 * startup parameters are correctly set up, so that GetProcessDword
422 * works. Furthermore, before calling the USER signal proc the
423 * 16-bit stack must be set up, which it is only after TASK_Create
424 * in the case of a 16-bit process. Thus, we send the signal here.
427 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE, 0 );
428 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
429 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT, 0 );
430 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED, 0 );
432 /* Signal the parent process to continue */
433 req->module = (void *)pModule->module32;
434 req->entry = entry;
435 server_call( REQ_INIT_PROCESS_DONE );
436 debugged = req->debugged;
438 if ( (pdb->flags & PDB32_CONSOLE_PROC) || (pdb->flags & PDB32_DOS_PROC) )
439 AllocConsole();
441 /* Perform Win32 specific process initialization */
442 if ( type == PROC_WIN32 )
444 EnterCriticalSection( &pdb->crit_section );
446 PE_InitTls();
447 MODULE_DllProcessAttach( pdb->exe_modref, (LPVOID)1 );
449 LeaveCriticalSection( &pdb->crit_section );
452 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
453 if ( type != PROC_WIN16 && (pdb->flags & PDB32_CONSOLE_PROC))
454 PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING, 0 );
456 switch ( type )
458 case PROC_DOS:
459 TRACE_(relay)( "Starting DOS process\n" );
460 DOSVM_Enter( NULL );
461 ERR_(relay)( "DOSVM_Enter returned; should not happen!\n" );
462 ExitProcess( 0 );
464 case PROC_WIN16:
465 TRACE_(relay)( "Starting Win16 process\n" );
466 TASK_CallToStart();
467 ERR_(relay)( "TASK_CallToStart returned; should not happen!\n" );
468 ExitProcess( 0 );
470 case PROC_WIN32:
471 TRACE_(relay)( "Starting Win32 process (entryproc=%p)\n", entry );
472 if (debugged) DbgBreakPoint();
473 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
474 * Dunno about other OSs */
475 ExitProcess( entry(NULL) );
478 error:
479 ExitProcess( GetLastError() );
483 /***********************************************************************
484 * PROCESS_Create
486 * Create a new process database and associated info.
488 PDB *PROCESS_Create( NE_MODULE *pModule, HFILE hFile, LPCSTR cmd_line, LPCSTR env,
489 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
490 BOOL inherit, DWORD flags, STARTUPINFOA *startup,
491 PROCESS_INFORMATION *info )
493 HANDLE handles[2], load_done_evt = -1;
494 DWORD exitcode, size;
495 BOOL alloc_stack16;
496 int fd = -1;
497 struct new_process_request *req = get_req_buffer();
498 TEB *teb = NULL;
499 PDB *parent = PROCESS_Current();
500 PDB *pdb = PROCESS_CreatePDB( parent, inherit );
502 if (!pdb) return NULL;
503 info->hThread = info->hProcess = INVALID_HANDLE_VALUE;
505 /* Create the process on the server side */
507 req->pinherit = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle);
508 req->tinherit = (tsa && (tsa->nLength >= sizeof(*tsa)) && tsa->bInheritHandle);
509 req->inherit_all = 2 /*inherit*/; /* HACK! */
510 req->create_flags = flags;
511 req->start_flags = startup->dwFlags;
512 req->exe_file = hFile;
513 if (startup->dwFlags & STARTF_USESTDHANDLES)
515 req->hstdin = startup->hStdInput;
516 req->hstdout = startup->hStdOutput;
517 req->hstderr = startup->hStdError;
519 else
521 req->hstdin = GetStdHandle( STD_INPUT_HANDLE );
522 req->hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
523 req->hstderr = GetStdHandle( STD_ERROR_HANDLE );
525 req->cmd_show = startup->wShowWindow;
526 req->env_ptr = (void*)env; /* FIXME: hack */
527 lstrcpynA( req->cmdline, cmd_line, server_remaining(req->cmdline) );
528 if (server_call_fd( REQ_NEW_PROCESS, -1, &fd )) goto error;
529 pdb->server_pid = req->pid;
530 info->hProcess = req->phandle;
531 info->dwProcessId = (DWORD)req->pid;
532 info->hThread = req->thandle;
533 info->dwThreadId = (DWORD)req->tid;
534 load_done_evt = req->event;
536 if (pModule->module32) /* Win32 process */
538 IMAGE_OPTIONAL_HEADER *header = &PE_HEADER(pModule->module32)->OptionalHeader;
539 size = header->SizeOfStackReserve;
540 if (header->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
541 pdb->flags |= PDB32_CONSOLE_PROC;
542 alloc_stack16 = TRUE;
544 else if (!pModule->dos_image) /* Win16 process */
546 alloc_stack16 = FALSE;
547 size = 0;
548 pdb->flags |= PDB32_WIN16_PROC;
550 else /* DOS process */
552 alloc_stack16 = FALSE;
553 size = 0;
554 pdb->flags |= PDB32_DOS_PROC;
557 /* Create the main thread */
559 if (!(teb = THREAD_Create( pdb, req->pid, req->tid, fd, size, alloc_stack16 ))) goto error;
560 teb->startup = PROCESS_Start;
561 fd = -1; /* don't close it */
563 /* Pass module to new process (FIXME: hack) */
564 pdb->module = pModule->self;
565 SYSDEPS_SpawnThread( teb );
567 /* Wait until process is initialized (or initialization failed) */
568 handles[0] = info->hProcess;
569 handles[1] = load_done_evt;
571 switch ( WaitForMultipleObjects( 2, handles, FALSE, INFINITE ) )
573 default:
574 ERR( "WaitForMultipleObjects failed\n" );
575 break;
577 case 0:
578 /* Child initialization code returns error condition as exitcode */
579 if ( GetExitCodeProcess( info->hProcess, &exitcode ) )
580 SetLastError( exitcode );
581 goto error;
583 case 1:
584 /* Get 16-bit task up and running */
585 if ( pdb->flags & PDB32_WIN16_PROC )
587 /* Post event to start the task */
588 PostEvent16( pdb->task );
590 /* If we ourselves are a 16-bit task, we Yield() directly. */
591 if ( parent->flags & PDB32_WIN16_PROC )
592 OldYield16();
594 break;
597 CloseHandle( load_done_evt );
598 return pdb;
600 error:
601 if (load_done_evt != -1) CloseHandle( load_done_evt );
602 if (info->hThread != INVALID_HANDLE_VALUE) CloseHandle( info->hThread );
603 if (info->hProcess != INVALID_HANDLE_VALUE) CloseHandle( info->hProcess );
604 PROCESS_FreePDB( pdb );
605 if (fd != -1) close( fd );
606 return NULL;
610 /***********************************************************************
611 * ExitProcess (KERNEL32.100)
613 void WINAPI ExitProcess( DWORD status )
615 struct terminate_process_request *req = get_req_buffer();
617 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
618 TASK_KillTask( 0 );
620 /* send the exit code to the server */
621 req->handle = GetCurrentProcess();
622 req->exit_code = status;
623 server_call( REQ_TERMINATE_PROCESS );
624 /* FIXME: need separate address spaces for that */
625 /* exit( status ); */
626 SYSDEPS_ExitThread( status );
629 /***********************************************************************
630 * ExitProcess16 (KERNEL.466)
632 void WINAPI ExitProcess16( WORD status )
634 SYSLEVEL_ReleaseWin16Lock();
635 ExitProcess( status );
638 /******************************************************************************
639 * TerminateProcess (KERNEL32.684)
641 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
643 BOOL ret;
644 struct terminate_process_request *req = get_req_buffer();
645 req->handle = handle;
646 req->exit_code = exit_code;
647 if ((ret = !server_call( REQ_TERMINATE_PROCESS )) && req->self) exit( exit_code );
648 return ret;
652 /***********************************************************************
653 * GetProcessDword (KERNEL32.18) (KERNEL.485)
654 * 'Of course you cannot directly access Windows internal structures'
656 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
658 PDB *process = PROCESS_IdToPDB( dwProcessID );
659 TDB *pTask;
660 DWORD x, y;
662 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
663 if ( !process ) return 0;
665 switch ( offset )
667 case GPD_APP_COMPAT_FLAGS:
668 pTask = (TDB *)GlobalLock16( process->task );
669 return pTask? pTask->compat_flags : 0;
671 case GPD_LOAD_DONE_EVENT:
672 return process->load_done_evt;
674 case GPD_HINSTANCE16:
675 pTask = (TDB *)GlobalLock16( process->task );
676 return pTask? pTask->hInstance : 0;
678 case GPD_WINDOWS_VERSION:
679 pTask = (TDB *)GlobalLock16( process->task );
680 return pTask? pTask->version : 0;
682 case GPD_THDB:
683 if ( process != PROCESS_Current() ) return 0;
684 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
686 case GPD_PDB:
687 return (DWORD)process;
689 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
690 return process->env_db->startup_info->hStdOutput;
692 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
693 return process->env_db->startup_info->hStdInput;
695 case GPD_STARTF_SHOWWINDOW:
696 return process->env_db->startup_info->wShowWindow;
698 case GPD_STARTF_SIZE:
699 x = process->env_db->startup_info->dwXSize;
700 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
701 y = process->env_db->startup_info->dwYSize;
702 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
703 return MAKELONG( x, y );
705 case GPD_STARTF_POSITION:
706 x = process->env_db->startup_info->dwX;
707 if ( x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
708 y = process->env_db->startup_info->dwY;
709 if ( y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
710 return MAKELONG( x, y );
712 case GPD_STARTF_FLAGS:
713 return process->env_db->startup_info->dwFlags;
715 case GPD_PARENT:
716 return process->parent? (DWORD)process->parent->server_pid : 0;
718 case GPD_FLAGS:
719 return process->flags;
721 case GPD_USERDATA:
722 return process->process_dword;
724 default:
725 ERR_(win32)("Unknown offset %d\n", offset );
726 return 0;
730 /***********************************************************************
731 * SetProcessDword (KERNEL.484)
732 * 'Of course you cannot directly access Windows internal structures'
734 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
736 PDB *process = PROCESS_IdToPDB( dwProcessID );
738 TRACE_(win32)("(%ld, %d)\n", dwProcessID, offset );
739 if ( !process ) return;
741 switch ( offset )
743 case GPD_APP_COMPAT_FLAGS:
744 case GPD_LOAD_DONE_EVENT:
745 case GPD_HINSTANCE16:
746 case GPD_WINDOWS_VERSION:
747 case GPD_THDB:
748 case GPD_PDB:
749 case GPD_STARTF_SHELLDATA:
750 case GPD_STARTF_HOTKEY:
751 case GPD_STARTF_SHOWWINDOW:
752 case GPD_STARTF_SIZE:
753 case GPD_STARTF_POSITION:
754 case GPD_STARTF_FLAGS:
755 case GPD_PARENT:
756 case GPD_FLAGS:
757 ERR_(win32)("Not allowed to modify offset %d\n", offset );
758 break;
760 case GPD_USERDATA:
761 process->process_dword = value;
762 break;
764 default:
765 ERR_(win32)("Unknown offset %d\n", offset );
766 break;
771 /*********************************************************************
772 * OpenProcess (KERNEL32.543)
774 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
776 HANDLE ret = 0;
777 struct open_process_request *req = get_req_buffer();
779 req->pid = (void *)id;
780 req->access = access;
781 req->inherit = inherit;
782 if (!server_call( REQ_OPEN_PROCESS )) ret = req->handle;
783 return ret;
786 /*********************************************************************
787 * MapProcessHandle (KERNEL.483)
789 DWORD WINAPI MapProcessHandle( HANDLE handle )
791 DWORD ret = 0;
792 struct get_process_info_request *req = get_req_buffer();
793 req->handle = handle;
794 if (!server_call( REQ_GET_PROCESS_INFO )) ret = (DWORD)req->pid;
795 return ret;
798 /***********************************************************************
799 * GetThreadLocale (KERNEL32.295)
801 LCID WINAPI GetThreadLocale(void)
803 return PROCESS_Current()->locale;
807 /***********************************************************************
808 * SetPriorityClass (KERNEL32.503)
810 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
812 struct set_process_info_request *req = get_req_buffer();
813 req->handle = hprocess;
814 req->priority = priorityclass;
815 req->mask = SET_PROCESS_INFO_PRIORITY;
816 return !server_call( REQ_SET_PROCESS_INFO );
820 /***********************************************************************
821 * GetPriorityClass (KERNEL32.250)
823 DWORD WINAPI GetPriorityClass(HANDLE hprocess)
825 DWORD ret = 0;
826 struct get_process_info_request *req = get_req_buffer();
827 req->handle = hprocess;
828 if (!server_call( REQ_GET_PROCESS_INFO )) ret = req->priority;
829 return ret;
833 /***********************************************************************
834 * SetProcessAffinityMask (KERNEL32.662)
836 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD affmask )
838 struct set_process_info_request *req = get_req_buffer();
839 req->handle = hProcess;
840 req->affinity = affmask;
841 req->mask = SET_PROCESS_INFO_AFFINITY;
842 return !server_call( REQ_SET_PROCESS_INFO );
845 /**********************************************************************
846 * GetProcessAffinityMask (KERNEL32.373)
848 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
849 LPDWORD lpProcessAffinityMask,
850 LPDWORD lpSystemAffinityMask )
852 BOOL ret = FALSE;
853 struct get_process_info_request *req = get_req_buffer();
854 req->handle = hProcess;
855 if (!server_call( REQ_GET_PROCESS_INFO ))
857 if (lpProcessAffinityMask) *lpProcessAffinityMask = req->process_affinity;
858 if (lpSystemAffinityMask) *lpSystemAffinityMask = req->system_affinity;
859 ret = TRUE;
861 return ret;
865 /***********************************************************************
866 * GetStdHandle (KERNEL32.276)
868 HANDLE WINAPI GetStdHandle( DWORD std_handle )
870 PDB *pdb = PROCESS_Current();
872 switch(std_handle)
874 case STD_INPUT_HANDLE: return pdb->env_db->hStdin;
875 case STD_OUTPUT_HANDLE: return pdb->env_db->hStdout;
876 case STD_ERROR_HANDLE: return pdb->env_db->hStderr;
878 SetLastError( ERROR_INVALID_PARAMETER );
879 return INVALID_HANDLE_VALUE;
883 /***********************************************************************
884 * SetStdHandle (KERNEL32.506)
886 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
888 PDB *pdb = PROCESS_Current();
889 /* FIXME: should we close the previous handle? */
890 switch(std_handle)
892 case STD_INPUT_HANDLE:
893 pdb->env_db->hStdin = handle;
894 return TRUE;
895 case STD_OUTPUT_HANDLE:
896 pdb->env_db->hStdout = handle;
897 return TRUE;
898 case STD_ERROR_HANDLE:
899 pdb->env_db->hStderr = handle;
900 return TRUE;
902 SetLastError( ERROR_INVALID_PARAMETER );
903 return FALSE;
906 /***********************************************************************
907 * GetProcessVersion (KERNEL32)
909 DWORD WINAPI GetProcessVersion( DWORD processid )
911 TDB *pTask;
912 PDB *pdb = PROCESS_IdToPDB( processid );
914 if (!pdb) return 0;
915 if (!(pTask = (TDB *)GlobalLock16( pdb->task ))) return 0;
916 return (pTask->version&0xff) | (((pTask->version >>8) & 0xff)<<16);
919 /***********************************************************************
920 * GetProcessFlags (KERNEL32)
922 DWORD WINAPI GetProcessFlags( DWORD processid )
924 PDB *pdb = PROCESS_IdToPDB( processid );
925 if (!pdb) return 0;
926 return pdb->flags;
929 /***********************************************************************
930 * SetProcessWorkingSetSize [KERNEL32.662]
931 * Sets the min/max working set sizes for a specified process.
933 * PARAMS
934 * hProcess [I] Handle to the process of interest
935 * minset [I] Specifies minimum working set size
936 * maxset [I] Specifies maximum working set size
938 * RETURNS STD
940 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess,DWORD minset,
941 DWORD maxset)
943 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
944 if(( minset == -1) && (maxset == -1)) {
945 /* Trim the working set to zero */
946 /* Swap the process out of physical RAM */
948 return TRUE;
951 /***********************************************************************
952 * GetProcessWorkingSetSize (KERNEL32)
954 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess,LPDWORD minset,
955 LPDWORD maxset)
957 FIXME("(0x%08x,%p,%p): stub\n",hProcess,minset,maxset);
958 /* 32 MB working set size */
959 if (minset) *minset = 32*1024*1024;
960 if (maxset) *maxset = 32*1024*1024;
961 return TRUE;
964 /***********************************************************************
965 * SetProcessShutdownParameters (KERNEL32)
967 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
968 * Now tracks changes made (but does not act on these changes)
969 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
970 * It really shouldn't be here, but I'll move it when it's been checked!
972 #define SHUTDOWN_NORETRY 1
973 static unsigned int shutdown_noretry = 0;
974 static unsigned int shutdown_priority = 0x280L;
975 BOOL WINAPI SetProcessShutdownParameters(DWORD level,DWORD flags)
977 if (flags & SHUTDOWN_NORETRY)
978 shutdown_noretry = 1;
979 else
980 shutdown_noretry = 0;
981 if (level > 0x100L && level < 0x3FFL)
982 shutdown_priority = level;
983 else
985 ERR("invalid priority level 0x%08lx\n", level);
986 return FALSE;
988 return TRUE;
992 /***********************************************************************
993 * GetProcessShutdownParameters (KERNEL32)
996 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel,
997 LPDWORD lpdwFlags )
999 (*lpdwLevel) = shutdown_priority;
1000 (*lpdwFlags) = (shutdown_noretry * SHUTDOWN_NORETRY);
1001 return TRUE;
1003 /***********************************************************************
1004 * SetProcessPriorityBoost (KERNEL32)
1006 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
1008 FIXME("(%d,%d): stub\n",hprocess,disableboost);
1009 /* Say we can do it. I doubt the program will notice that we don't. */
1010 return TRUE;
1014 /***********************************************************************
1015 * ReadProcessMemory (KERNEL32)
1017 BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, DWORD size,
1018 LPDWORD bytes_read )
1020 struct read_process_memory_request *req = get_req_buffer();
1021 unsigned int offset = (unsigned int)addr % sizeof(int);
1022 unsigned int max = server_remaining( req->data ); /* max length in one request */
1023 unsigned int pos;
1025 if (bytes_read) *bytes_read = size;
1027 /* first time, read total length to check for permissions */
1028 req->handle = process;
1029 req->addr = (char *)addr - offset;
1030 req->len = (size + offset + sizeof(int) - 1) / sizeof(int);
1031 if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1033 if (size <= max - offset)
1035 memcpy( buffer, (char *)req->data + offset, size );
1036 return TRUE;
1039 /* now take care of the remaining data */
1040 memcpy( buffer, (char *)req->data + offset, max - offset );
1041 pos = max - offset;
1042 size -= pos;
1043 while (size)
1045 if (max > size) max = size;
1046 req->handle = process;
1047 req->addr = (char *)addr + pos;
1048 req->len = (max + sizeof(int) - 1) / sizeof(int);
1049 if (server_call( REQ_READ_PROCESS_MEMORY )) goto error;
1050 memcpy( (char *)buffer + pos, (char *)req->data, max );
1051 size -= max;
1052 pos += max;
1054 return TRUE;
1056 error:
1057 if (bytes_read) *bytes_read = 0;
1058 return FALSE;
1062 /***********************************************************************
1063 * WriteProcessMemory (KERNEL32)
1065 BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPVOID buffer, DWORD size,
1066 LPDWORD bytes_written )
1068 unsigned int first_offset, last_offset;
1069 struct write_process_memory_request *req = get_req_buffer();
1070 unsigned int max = server_remaining( req->data ); /* max length in one request */
1071 unsigned int pos, last_mask;
1073 if (!size)
1075 SetLastError( ERROR_INVALID_PARAMETER );
1076 return FALSE;
1078 if (bytes_written) *bytes_written = size;
1080 /* compute the mask for the first int */
1081 req->first_mask = ~0;
1082 first_offset = (unsigned int)addr % sizeof(int);
1083 memset( &req->first_mask, 0, first_offset );
1085 /* compute the mask for the last int */
1086 last_offset = (size + first_offset) % sizeof(int);
1087 last_mask = 0;
1088 memset( &last_mask, 0xff, last_offset ? last_offset : sizeof(int) );
1090 req->handle = process;
1091 req->addr = (char *)addr - first_offset;
1092 /* for the first request, use the total length */
1093 req->len = (size + first_offset + sizeof(int) - 1) / sizeof(int);
1095 if (size + first_offset < max) /* we can do it in one round */
1097 memcpy( (char *)req->data + first_offset, buffer, size );
1098 req->last_mask = last_mask;
1099 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1100 return TRUE;
1103 /* needs multiple server calls */
1105 memcpy( (char *)req->data + first_offset, buffer, max - first_offset );
1106 req->last_mask = ~0;
1107 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1108 pos = max - first_offset;
1109 size -= pos;
1110 while (size)
1112 if (size <= max) /* last one */
1114 req->last_mask = last_mask;
1115 max = size;
1117 req->handle = process;
1118 req->addr = (char *)addr + pos;
1119 req->len = (max + sizeof(int) - 1) / sizeof(int);
1120 req->first_mask = ~0;
1121 memcpy( req->data, (char *) buffer + pos, max );
1122 if (server_call( REQ_WRITE_PROCESS_MEMORY )) goto error;
1123 pos += max;
1124 size -= max;
1126 return TRUE;
1128 error:
1129 if (bytes_written) *bytes_written = 0;
1130 return FALSE;
1135 /***********************************************************************
1136 * RegisterServiceProcess (KERNEL, KERNEL32)
1138 * A service process calls this function to ensure that it continues to run
1139 * even after a user logged off.
1141 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
1143 /* I don't think that Wine needs to do anything in that function */
1144 return 1; /* success */
1147 /***********************************************************************
1148 * GetExitCodeProcess [KERNEL32.325]
1150 * Gets termination status of specified process
1152 * RETURNS
1153 * Success: TRUE
1154 * Failure: FALSE
1156 BOOL WINAPI GetExitCodeProcess(
1157 HANDLE hProcess, /* [I] handle to the process */
1158 LPDWORD lpExitCode) /* [O] address to receive termination status */
1160 BOOL ret = FALSE;
1161 struct get_process_info_request *req = get_req_buffer();
1162 req->handle = hProcess;
1163 if (!server_call( REQ_GET_PROCESS_INFO ))
1165 if (lpExitCode) *lpExitCode = req->exit_code;
1166 ret = TRUE;
1168 return ret;
1172 /***********************************************************************
1173 * SetErrorMode (KERNEL32.486)
1175 UINT WINAPI SetErrorMode( UINT mode )
1177 UINT old = PROCESS_Current()->error_mode;
1178 PROCESS_Current()->error_mode = mode;
1179 return old;
1182 /***********************************************************************
1183 * GetCurrentProcess (KERNEL32.198)
1185 #undef GetCurrentProcess
1186 HANDLE WINAPI GetCurrentProcess(void)
1188 return 0xffffffff;