4 * Copyright 1996, 1998 Alexandre Julliard
12 #include "wine/winbase16.h"
13 #include "wine/exception.h"
30 #include "debugtools.h"
32 DEFAULT_DEBUG_CHANNEL(process
)
33 DECLARE_DEBUG_CHANNEL(relay
)
34 DECLARE_DEBUG_CHANNEL(win32
)
37 /* The initial process PDB */
38 static PDB initial_pdb
;
40 static PDB
*PROCESS_First
= &initial_pdb
;
43 /***********************************************************************
46 void PROCESS_WalkProcess(void)
52 MESSAGE( " pid PDB #th modref module \n" );
55 if (pdb
== &initial_pdb
)
58 name
= (pdb
->exe_modref
) ? pdb
->exe_modref
->filename
: "";
60 MESSAGE( " %8p %8p %5d %8p %s\n", pdb
->server_pid
, pdb
,
61 pdb
->threads
, pdb
->exe_modref
, name
);
67 /***********************************************************************
70 * Check if a handle is to the current process
72 BOOL
PROCESS_IsCurrent( HANDLE handle
)
74 struct get_process_info_request
*req
= get_req_buffer();
76 return (!server_call( REQ_GET_PROCESS_INFO
) &&
77 (req
->pid
== PROCESS_Current()->server_pid
));
81 /***********************************************************************
84 * Convert a process id to a PDB, making sure it is valid.
86 PDB
*PROCESS_IdToPDB( DWORD pid
)
90 if (!pid
) return PROCESS_Current();
94 if ((DWORD
)pdb
->server_pid
== pid
) return pdb
;
97 SetLastError( ERROR_INVALID_PARAMETER
);
102 /***********************************************************************
103 * PROCESS_CallUserSignalProc
105 * FIXME: Some of the signals aren't sent correctly!
107 * The exact meaning of the USER signals is undocumented, but this
108 * should cover the basic idea:
110 * USIG_DLL_UNLOAD_WIN16
111 * This is sent when a 16-bit module is unloaded.
113 * USIG_DLL_UNLOAD_WIN32
114 * This is sent when a 32-bit module is unloaded.
116 * USIG_DLL_UNLOAD_ORPHANS
117 * This is sent after the last Win3.1 module is unloaded,
118 * to allow removal of orphaned menus.
120 * USIG_FAULT_DIALOG_PUSH
121 * USIG_FAULT_DIALOG_POP
122 * These are called to allow USER to prepare for displaying a
123 * fault dialog, even though the fault might have happened while
124 * inside a USER critical section.
127 * This is called from the context of a new thread, as soon as it
128 * has started to run.
131 * This is called, still in its context, just before a thread is
132 * about to terminate.
134 * USIG_PROCESS_CREATE
135 * This is called, in the parent process context, after a new process
139 * This is called in the new process context, just after the main thread
140 * has started execution (after the main thread's USIG_THREAD_INIT has
143 * USIG_PROCESS_LOADED
144 * This is called after the executable file has been loaded into the
145 * new process context.
147 * USIG_PROCESS_RUNNING
148 * This is called immediately before the main entry point is called.
151 * This is called in the context of a process that is about to
152 * terminate (but before the last thread's USIG_THREAD_EXIT has
155 * USIG_PROCESS_DESTROY
156 * This is called after a process has terminated.
159 * The meaning of the dwFlags bits is as follows:
162 * Current process is 32-bit.
165 * Current process is a (Win32) GUI process.
167 * USIG_FLAGS_FEEDBACK
168 * Current process needs 'feedback' (determined from the STARTUPINFO
169 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
172 * The signal is being sent due to a fault.
174 void PROCESS_CallUserSignalProc( UINT uCode
, DWORD dwThreadId
, HMODULE hModule
)
176 DWORD flags
= PROCESS_Current()->flags
;
177 DWORD startup_flags
= PROCESS_Current()->env_db
->startup_info
->dwFlags
;
180 /* Determine dwFlags */
182 if ( !(flags
& PDB32_WIN16_PROC
) ) dwFlags
|= USIG_FLAGS_WIN32
;
184 if ( !(flags
& PDB32_CONSOLE_PROC
) ) dwFlags
|= USIG_FLAGS_GUI
;
186 if ( dwFlags
& USIG_FLAGS_GUI
)
188 /* Feedback defaults to ON */
189 if ( !(startup_flags
& STARTF_FORCEOFFFEEDBACK
) )
190 dwFlags
|= USIG_FLAGS_FEEDBACK
;
194 /* Feedback defaults to OFF */
195 if (startup_flags
& STARTF_FORCEONFEEDBACK
)
196 dwFlags
|= USIG_FLAGS_FEEDBACK
;
199 /* Convert module handle to 16-bit */
201 if ( HIWORD( hModule
) )
202 hModule
= MapHModuleLS( hModule
);
204 /* Call USER signal proc */
206 if ( Callout
.UserSignalProc
)
208 if ( uCode
== USIG_THREAD_INIT
|| uCode
== USIG_THREAD_EXIT
)
209 Callout
.UserSignalProc( uCode
, dwThreadId
, dwFlags
, hModule
);
211 Callout
.UserSignalProc( uCode
, GetCurrentProcessId(), dwFlags
, hModule
);
215 /***********************************************************************
216 * PROCESS_CreateEnvDB
218 * Create the env DB for a newly started process.
220 static BOOL
PROCESS_CreateEnvDB(void)
222 struct init_process_request
*req
= get_req_buffer();
223 STARTUPINFOA
*startup
;
226 PDB
*pdb
= PROCESS_Current();
228 /* Allocate the env DB */
230 if (!(env_db
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ENVDB
) )))
232 pdb
->env_db
= env_db
;
233 InitializeCriticalSection( &env_db
->section
);
235 /* Allocate and fill the startup info */
236 if (!(startup
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(STARTUPINFOA
) )))
238 env_db
->startup_info
= startup
;
240 /* Retrieve startup info from the server */
242 req
->ldt_copy
= ldt_copy
;
243 req
->ldt_flags
= ldt_flags_copy
;
244 if (server_call( REQ_INIT_PROCESS
)) return FALSE
;
245 pdb
->exe_file
= req
->exe_file
;
246 startup
->dwFlags
= req
->start_flags
;
247 startup
->wShowWindow
= req
->cmd_show
;
248 env_db
->hStdin
= startup
->hStdInput
= req
->hstdin
;
249 env_db
->hStdout
= startup
->hStdOutput
= req
->hstdout
;
250 env_db
->hStderr
= startup
->hStdError
= req
->hstderr
;
251 lstrcpynA( cmd_line
, req
->cmdline
, sizeof(cmd_line
) );
253 /* Copy the parent environment */
255 if (!ENV_InheritEnvironment( req
->env_ptr
)) return FALSE
;
257 /* Copy the command line */
259 if (!(pdb
->env_db
->cmd_line
= HEAP_strdupA( GetProcessHeap(), 0, cmd_line
)))
266 /***********************************************************************
269 * Free a PDB and all associated storage.
271 void PROCESS_FreePDB( PDB
*pdb
)
273 PDB
**pptr
= &PROCESS_First
;
275 ENV_FreeEnvironment( pdb
);
276 while (*pptr
&& (*pptr
!= pdb
)) pptr
= &(*pptr
)->next
;
277 if (*pptr
) *pptr
= pdb
->next
;
278 HeapFree( SystemHeap
, 0, pdb
);
282 /***********************************************************************
285 * Allocate and fill a PDB structure.
286 * Runs in the context of the parent process.
288 static PDB
*PROCESS_CreatePDB( PDB
*parent
, BOOL inherit
)
290 PDB
*pdb
= HeapAlloc( SystemHeap
, HEAP_ZERO_MEMORY
, sizeof(PDB
) );
292 if (!pdb
) return NULL
;
293 pdb
->exit_code
= STILL_ACTIVE
;
295 pdb
->running_threads
= 1;
296 pdb
->ring0_threads
= 1;
297 pdb
->parent
= parent
;
299 pdb
->priority
= 8; /* Normal */
300 pdb
->next
= PROCESS_First
;
301 pdb
->winver
= 0xffff; /* to be determined */
302 pdb
->main_queue
= INVALID_HANDLE_VALUE16
;
308 /***********************************************************************
311 BOOL
PROCESS_Init( BOOL win32
)
316 /* Start the server */
317 server_fd
= CLIENT_InitServer();
319 /* Fill the initial process structure */
320 initial_pdb
.exit_code
= STILL_ACTIVE
;
321 initial_pdb
.threads
= 1;
322 initial_pdb
.running_threads
= 1;
323 initial_pdb
.ring0_threads
= 1;
324 initial_pdb
.group
= &initial_pdb
;
325 initial_pdb
.priority
= 8; /* Normal */
326 initial_pdb
.flags
= win32
? 0 : PDB32_WIN16_PROC
;
327 initial_pdb
.winver
= 0xffff; /* to be determined */
328 initial_pdb
.main_queue
= INVALID_HANDLE_VALUE16
;
330 /* Initialize virtual memory management */
331 if (!VIRTUAL_Init()) return FALSE
;
333 /* Create the initial thread structure and socket pair */
334 if (!(teb
= THREAD_CreateInitialThread( &initial_pdb
, server_fd
))) return FALSE
;
336 /* Remember TEB selector of initial process for emergency use */
337 SYSLEVEL_EmergencyTeb
= teb
->teb_sel
;
339 /* Create the system and process heaps */
340 if (!HEAP_CreateSystemHeap()) return FALSE
;
341 initial_pdb
.heap
= HeapCreate( HEAP_GROWABLE
, 0, 0 );
343 /* Create the idle event for the initial process
344 FIXME 1: Shouldn't we call UserSignalProc for the initial process too?
345 FIXME 2: It seems to me that the initial pdb becomes never freed, so I don't now
346 where to release the idle event for the initial process.
348 initial_pdb
.idle_event
= CreateEventA ( NULL
, TRUE
, FALSE
, NULL
);
349 initial_pdb
.idle_event
= ConvertToGlobalHandle ( initial_pdb
.idle_event
);
351 /* Initialize signal handling */
352 if (!SIGNAL_Init()) return FALSE
;
354 /* Create the environment DB of the first process */
355 if (!PROCESS_CreateEnvDB()) return FALSE
;
357 /* Create the SEGPTR heap */
358 if (!(SegptrHeap
= HeapCreate( HEAP_WINE_SEGPTR
, 0, 0 ))) return FALSE
;
360 /* Initialize the first process critical section */
361 InitializeCriticalSection( &initial_pdb
.crit_section
);
367 /***********************************************************************
370 * Startup routine of a new process. Called in the context of the new process.
372 void PROCESS_Start(void)
374 UINT cmdShow
= SW_SHOWNORMAL
;
375 LPTHREAD_START_ROUTINE entry
= NULL
;
376 PDB
*pdb
= PROCESS_Current();
377 NE_MODULE
*pModule
= NE_GetPtr( pdb
->module
);
378 LPCSTR filename
= ((OFSTRUCT
*)((char*)(pModule
) + (pModule
)->fileinfo
))->szPathName
;
379 IMAGE_OPTIONAL_HEADER
*header
= !pModule
->module32
? NULL
:
380 &PE_HEADER(pModule
->module32
)->OptionalHeader
;
382 /* Get process type */
383 enum { PROC_DOS
, PROC_WIN16
, PROC_WIN32
} type
;
384 if ( pdb
->flags
& PDB32_DOS_PROC
)
386 else if ( pdb
->flags
& PDB32_WIN16_PROC
)
391 /* Initialize the critical section */
392 InitializeCriticalSection( &pdb
->crit_section
);
394 /* Create the heap */
395 if (!(pdb
->heap
= GetProcessHeap()))
397 if (!(pdb
->heap
= HeapCreate( HEAP_GROWABLE
,
398 header
? header
->SizeOfHeapReserve
: 0x10000,
399 header
? header
->SizeOfHeapCommit
: 0 )))
403 /* Create the environment db */
404 if (!PROCESS_CreateEnvDB()) goto error
;
406 /* Create a task for this process */
407 if (pdb
->env_db
->startup_info
->dwFlags
& STARTF_USESHOWWINDOW
)
408 cmdShow
= pdb
->env_db
->startup_info
->wShowWindow
;
409 if (!TASK_Create( pModule
, cmdShow
))
412 /* Load all process modules */
416 if ( !NE_InitProcess( pModule
) )
421 /* Create 32-bit MODREF */
422 if ( !PE_CreateModule( pModule
->module32
, filename
, 0, FALSE
) )
425 /* Increment EXE refcount */
426 assert( pdb
->exe_modref
);
427 pdb
->exe_modref
->refCount
++;
429 /* Retrieve entry point address */
430 entry
= (LPTHREAD_START_ROUTINE
)RVA_PTR(pModule
->module32
,
431 OptionalHeader
.AddressOfEntryPoint
);
435 /* FIXME: move DOS startup code here */
440 /* Note: The USIG_PROCESS_CREATE signal is supposed to be sent in the
441 * context of the parent process. Actually, the USER signal proc
442 * doesn't really care about that, but it *does* require that the
443 * startup parameters are correctly set up, so that GetProcessDword
444 * works. Furthermore, before calling the USER signal proc the
445 * 16-bit stack must be set up, which it is only after TASK_Create
446 * in the case of a 16-bit process. Thus, we send the signal here.
449 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE
, 0, 0 );
450 PROCESS_CallUserSignalProc( USIG_THREAD_INIT
, GetCurrentThreadId(), 0 );
451 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT
, 0, 0 );
452 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED
, 0, 0 );
454 /* Signal the parent process to continue */
455 server_call( REQ_INIT_PROCESS_DONE
);
457 /* Send all required start-up debugger events */
458 if ( type
== PROC_WIN32
&& (pdb
->flags
& PDB32_DEBUGGED
) )
460 EnterCriticalSection( &pdb
->crit_section
);
462 DEBUG_SendCreateProcessEvent( -1 /*FIXME*/, pModule
->module32
, entry
);
463 MODULE_SendLoadDLLEvents();
465 LeaveCriticalSection( &pdb
->crit_section
);
468 if ( (pdb
->flags
& PDB32_CONSOLE_PROC
) || (pdb
->flags
& PDB32_DOS_PROC
) )
471 /* Perform Win32 specific process initialization */
472 if ( type
== PROC_WIN32
)
474 EnterCriticalSection( &pdb
->crit_section
);
477 MODULE_DllProcessAttach( pdb
->exe_modref
, (LPVOID
)1 );
479 LeaveCriticalSection( &pdb
->crit_section
);
482 /* If requested, add entry point breakpoint */
483 if ( Options
.debug
|| (pdb
->flags
& PDB32_DEBUGGED
) )
484 DEBUG_AddTaskEntryBreakpoint( pdb
->task
);
486 /* Call UserSignalProc ( USIG_PROCESS_RUNNING ... ) only for non-GUI win32 apps */
487 if ( type
!= PROC_WIN16
&& (pdb
->flags
& PDB32_CONSOLE_PROC
))
488 PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING
, 0, 0 );
493 TRACE_(relay
)( "Starting DOS process\n" );
495 ERR_(relay
)( "DOSVM_Enter returned; should not happen!\n" );
499 TRACE_(relay
)( "Starting Win16 process\n" );
501 ERR_(relay
)( "TASK_CallToStart returned; should not happen!\n" );
505 TRACE_(relay
)( "Starting Win32 process (entryproc=%p)\n", entry
);
506 if (pdb
->flags
& PDB32_DEBUGGED
) DebugBreak();
507 /* FIXME: should use _PEB as parameter for NT 3.5 programs !
508 * Dunno about other OSs */
509 ExitProcess( entry(NULL
) );
513 ExitProcess( GetLastError() );
517 /***********************************************************************
520 * Create a new process database and associated info.
522 PDB
*PROCESS_Create( NE_MODULE
*pModule
, HFILE hFile
, LPCSTR cmd_line
, LPCSTR env
,
523 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
524 BOOL inherit
, DWORD flags
, STARTUPINFOA
*startup
,
525 PROCESS_INFORMATION
*info
)
527 HANDLE handles
[2], load_done_evt
= 0;
528 DWORD exitcode
, size
;
530 int server_thandle
, fd
= -1;
531 struct new_process_request
*req
= get_req_buffer();
533 PDB
*parent
= PROCESS_Current();
534 PDB
*pdb
= PROCESS_CreatePDB( parent
, inherit
);
536 if (!pdb
) return NULL
;
537 info
->hThread
= info
->hProcess
= INVALID_HANDLE_VALUE
;
538 if (!(load_done_evt
= CreateEventA( NULL
, TRUE
, FALSE
, NULL
))) goto error
;
540 /* Create the process on the server side */
542 req
->inherit
= (psa
&& (psa
->nLength
>= sizeof(*psa
)) && psa
->bInheritHandle
);
543 req
->inherit_all
= inherit
;
544 req
->create_flags
= flags
;
545 req
->start_flags
= startup
->dwFlags
;
546 req
->exe_file
= hFile
;
547 req
->event
= load_done_evt
;
548 if (startup
->dwFlags
& STARTF_USESTDHANDLES
)
550 req
->hstdin
= startup
->hStdInput
;
551 req
->hstdout
= startup
->hStdOutput
;
552 req
->hstderr
= startup
->hStdError
;
556 req
->hstdin
= GetStdHandle( STD_INPUT_HANDLE
);
557 req
->hstdout
= GetStdHandle( STD_OUTPUT_HANDLE
);
558 req
->hstderr
= GetStdHandle( STD_ERROR_HANDLE
);
560 req
->cmd_show
= startup
->wShowWindow
;
561 req
->env_ptr
= (void*)env
; /* FIXME: hack */
562 lstrcpynA( req
->cmdline
, cmd_line
, server_remaining(req
->cmdline
) );
563 if (server_call_fd( REQ_NEW_PROCESS
, -1, &fd
)) goto error
;
564 fcntl( fd
, F_SETFD
, 1 ); /* set close on exec flag */
565 pdb
->server_pid
= req
->pid
;
566 info
->hProcess
= req
->phandle
;
567 info
->dwProcessId
= (DWORD
)req
->pid
;
568 info
->hThread
= req
->thandle
;
569 info
->dwThreadId
= (DWORD
)req
->tid
;
571 if ((flags
& (DEBUG_PROCESS
|DEBUG_ONLY_THIS_PROCESS
)) ||
572 ((parent
->flags
& PDB32_DEBUGGED
) && !(flags
& DEBUG_ONLY_THIS_PROCESS
)))
573 pdb
->flags
|= PDB32_DEBUGGED
;
575 if (pModule
->module32
) /* Win32 process */
577 IMAGE_OPTIONAL_HEADER
*header
= &PE_HEADER(pModule
->module32
)->OptionalHeader
;
578 size
= header
->SizeOfStackReserve
;
579 if (header
->Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
)
580 pdb
->flags
|= PDB32_CONSOLE_PROC
;
581 alloc_stack16
= TRUE
;
583 else if (!pModule
->dos_image
) /* Win16 process */
585 alloc_stack16
= FALSE
;
587 pdb
->flags
|= PDB32_WIN16_PROC
;
589 else /* DOS process */
591 alloc_stack16
= FALSE
;
593 pdb
->flags
|= PDB32_DOS_PROC
;
596 /* Create the main thread */
598 if (!(teb
= THREAD_Create( pdb
, fd
, flags
& CREATE_SUSPENDED
, size
,
599 alloc_stack16
, tsa
, &server_thandle
))) goto error
;
600 teb
->tid
= (void *)info
->dwThreadId
;
601 teb
->startup
= PROCESS_Start
;
602 fd
= -1; /* don't close it */
604 /* Pass module to new process (FIXME: hack) */
605 pdb
->module
= pModule
->self
;
606 SYSDEPS_SpawnThread( teb
);
608 /* Wait until process is initialized (or initialization failed) */
609 handles
[0] = info
->hProcess
;
610 handles
[1] = load_done_evt
;
612 switch ( WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
) )
615 ERR( "WaitForMultipleObjects failed\n" );
619 /* Child initialization code returns error condition as exitcode */
620 if ( GetExitCodeProcess( info
->hProcess
, &exitcode
) )
621 SetLastError( exitcode
);
625 /* Get 16-bit task up and running */
626 if ( pdb
->flags
& PDB32_WIN16_PROC
)
628 /* Post event to start the task */
629 PostEvent16( pdb
->task
);
631 /* If we ourselves are a 16-bit task, we Yield() directly. */
632 if ( parent
->flags
& PDB32_WIN16_PROC
)
638 CloseHandle( load_done_evt
);
644 if (load_done_evt
) CloseHandle( load_done_evt
);
645 if (info
->hThread
!= INVALID_HANDLE_VALUE
) CloseHandle( info
->hThread
);
646 if (info
->hProcess
!= INVALID_HANDLE_VALUE
) CloseHandle( info
->hProcess
);
647 PROCESS_FreePDB( pdb
);
648 if (fd
!= -1) close( fd
);
653 /***********************************************************************
654 * ExitProcess (KERNEL32.100)
656 void WINAPI
ExitProcess( DWORD status
)
658 EnterCriticalSection( &PROCESS_Current()->crit_section
);
659 MODULE_DllProcessDetach( TRUE
, (LPVOID
)1 );
660 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
663 TerminateProcess( GetCurrentProcess(), status
);
666 /***********************************************************************
667 * ExitProcess16 (KERNEL.466)
669 void WINAPI
ExitProcess16( WORD status
)
671 SYSLEVEL_ReleaseWin16Lock();
672 ExitProcess( status
);
675 /******************************************************************************
676 * TerminateProcess (KERNEL32.684)
678 BOOL WINAPI
TerminateProcess( HANDLE handle
, DWORD exit_code
)
680 struct terminate_process_request
*req
= get_req_buffer();
681 req
->handle
= handle
;
682 req
->exit_code
= exit_code
;
683 return !server_call( REQ_TERMINATE_PROCESS
);
687 /***********************************************************************
688 * GetProcessDword (KERNEL32.18) (KERNEL.485)
689 * 'Of course you cannot directly access Windows internal structures'
691 DWORD WINAPI
GetProcessDword( DWORD dwProcessID
, INT offset
)
693 PDB
*process
= PROCESS_IdToPDB( dwProcessID
);
697 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
698 if ( !process
) return 0;
702 case GPD_APP_COMPAT_FLAGS
:
703 pTask
= (TDB
*)GlobalLock16( process
->task
);
704 return pTask
? pTask
->compat_flags
: 0;
706 case GPD_LOAD_DONE_EVENT
:
707 return process
->load_done_evt
;
709 case GPD_HINSTANCE16
:
710 pTask
= (TDB
*)GlobalLock16( process
->task
);
711 return pTask
? pTask
->hInstance
: 0;
713 case GPD_WINDOWS_VERSION
:
714 pTask
= (TDB
*)GlobalLock16( process
->task
);
715 return pTask
? pTask
->version
: 0;
718 if ( process
!= PROCESS_Current() ) return 0;
719 return (DWORD
)NtCurrentTeb() - 0x10 /* FIXME */;
722 return (DWORD
)process
;
724 case GPD_STARTF_SHELLDATA
: /* return stdoutput handle from startupinfo ??? */
725 return process
->env_db
->startup_info
->hStdOutput
;
727 case GPD_STARTF_HOTKEY
: /* return stdinput handle from startupinfo ??? */
728 return process
->env_db
->startup_info
->hStdInput
;
730 case GPD_STARTF_SHOWWINDOW
:
731 return process
->env_db
->startup_info
->wShowWindow
;
733 case GPD_STARTF_SIZE
:
734 x
= process
->env_db
->startup_info
->dwXSize
;
735 if ( x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
736 y
= process
->env_db
->startup_info
->dwYSize
;
737 if ( y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
738 return MAKELONG( x
, y
);
740 case GPD_STARTF_POSITION
:
741 x
= process
->env_db
->startup_info
->dwX
;
742 if ( x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
743 y
= process
->env_db
->startup_info
->dwY
;
744 if ( y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
745 return MAKELONG( x
, y
);
747 case GPD_STARTF_FLAGS
:
748 return process
->env_db
->startup_info
->dwFlags
;
751 return process
->parent
? (DWORD
)process
->parent
->server_pid
: 0;
754 return process
->flags
;
757 return process
->process_dword
;
760 ERR_(win32
)("Unknown offset %d\n", offset
);
765 /***********************************************************************
766 * SetProcessDword (KERNEL.484)
767 * 'Of course you cannot directly access Windows internal structures'
769 void WINAPI
SetProcessDword( DWORD dwProcessID
, INT offset
, DWORD value
)
771 PDB
*process
= PROCESS_IdToPDB( dwProcessID
);
773 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
774 if ( !process
) return;
778 case GPD_APP_COMPAT_FLAGS
:
779 case GPD_LOAD_DONE_EVENT
:
780 case GPD_HINSTANCE16
:
781 case GPD_WINDOWS_VERSION
:
784 case GPD_STARTF_SHELLDATA
:
785 case GPD_STARTF_HOTKEY
:
786 case GPD_STARTF_SHOWWINDOW
:
787 case GPD_STARTF_SIZE
:
788 case GPD_STARTF_POSITION
:
789 case GPD_STARTF_FLAGS
:
792 ERR_(win32
)("Not allowed to modify offset %d\n", offset
);
796 process
->process_dword
= value
;
800 ERR_(win32
)("Unknown offset %d\n", offset
);
806 /***********************************************************************
807 * GetCurrentProcess (KERNEL32.198)
809 HANDLE WINAPI
GetCurrentProcess(void)
811 return CURRENT_PROCESS_PSEUDOHANDLE
;
815 /*********************************************************************
816 * OpenProcess (KERNEL32.543)
818 HANDLE WINAPI
OpenProcess( DWORD access
, BOOL inherit
, DWORD id
)
821 struct open_process_request
*req
= get_req_buffer();
823 req
->pid
= (void *)id
;
824 req
->access
= access
;
825 req
->inherit
= inherit
;
826 if (!server_call( REQ_OPEN_PROCESS
)) ret
= req
->handle
;
830 /*********************************************************************
831 * MapProcessHandle (KERNEL.483)
833 DWORD WINAPI
MapProcessHandle( HANDLE handle
)
836 struct get_process_info_request
*req
= get_req_buffer();
837 req
->handle
= handle
;
838 if (!server_call( REQ_GET_PROCESS_INFO
)) ret
= (DWORD
)req
->pid
;
842 /***********************************************************************
843 * GetCurrentProcessId (KERNEL32.199)
845 DWORD WINAPI
GetCurrentProcessId(void)
847 return (DWORD
)PROCESS_Current()->server_pid
;
851 /***********************************************************************
852 * GetThreadLocale (KERNEL32.295)
854 LCID WINAPI
GetThreadLocale(void)
856 return PROCESS_Current()->locale
;
860 /***********************************************************************
861 * SetPriorityClass (KERNEL32.503)
863 BOOL WINAPI
SetPriorityClass( HANDLE hprocess
, DWORD priorityclass
)
865 struct set_process_info_request
*req
= get_req_buffer();
866 req
->handle
= hprocess
;
867 req
->priority
= priorityclass
;
868 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
869 return !server_call( REQ_SET_PROCESS_INFO
);
873 /***********************************************************************
874 * GetPriorityClass (KERNEL32.250)
876 DWORD WINAPI
GetPriorityClass(HANDLE hprocess
)
879 struct get_process_info_request
*req
= get_req_buffer();
880 req
->handle
= hprocess
;
881 if (!server_call( REQ_GET_PROCESS_INFO
)) ret
= req
->priority
;
886 /***********************************************************************
887 * SetProcessAffinityMask (KERNEL32.662)
889 BOOL WINAPI
SetProcessAffinityMask( HANDLE hProcess
, DWORD affmask
)
891 struct set_process_info_request
*req
= get_req_buffer();
892 req
->handle
= hProcess
;
893 req
->affinity
= affmask
;
894 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
895 return !server_call( REQ_SET_PROCESS_INFO
);
898 /**********************************************************************
899 * GetProcessAffinityMask (KERNEL32.373)
901 BOOL WINAPI
GetProcessAffinityMask( HANDLE hProcess
,
902 LPDWORD lpProcessAffinityMask
,
903 LPDWORD lpSystemAffinityMask
)
906 struct get_process_info_request
*req
= get_req_buffer();
907 req
->handle
= hProcess
;
908 if (!server_call( REQ_GET_PROCESS_INFO
))
910 if (lpProcessAffinityMask
) *lpProcessAffinityMask
= req
->process_affinity
;
911 if (lpSystemAffinityMask
) *lpSystemAffinityMask
= req
->system_affinity
;
918 /***********************************************************************
919 * GetStdHandle (KERNEL32.276)
921 HANDLE WINAPI
GetStdHandle( DWORD std_handle
)
923 PDB
*pdb
= PROCESS_Current();
927 case STD_INPUT_HANDLE
: return pdb
->env_db
->hStdin
;
928 case STD_OUTPUT_HANDLE
: return pdb
->env_db
->hStdout
;
929 case STD_ERROR_HANDLE
: return pdb
->env_db
->hStderr
;
931 SetLastError( ERROR_INVALID_PARAMETER
);
932 return INVALID_HANDLE_VALUE
;
936 /***********************************************************************
937 * SetStdHandle (KERNEL32.506)
939 BOOL WINAPI
SetStdHandle( DWORD std_handle
, HANDLE handle
)
941 PDB
*pdb
= PROCESS_Current();
942 /* FIXME: should we close the previous handle? */
945 case STD_INPUT_HANDLE
:
946 pdb
->env_db
->hStdin
= handle
;
948 case STD_OUTPUT_HANDLE
:
949 pdb
->env_db
->hStdout
= handle
;
951 case STD_ERROR_HANDLE
:
952 pdb
->env_db
->hStderr
= handle
;
955 SetLastError( ERROR_INVALID_PARAMETER
);
959 /***********************************************************************
960 * GetProcessVersion (KERNEL32)
962 DWORD WINAPI
GetProcessVersion( DWORD processid
)
965 PDB
*pdb
= PROCESS_IdToPDB( processid
);
968 if (!(pTask
= (TDB
*)GlobalLock16( pdb
->task
))) return 0;
969 return (pTask
->version
&0xff) | (((pTask
->version
>>8) & 0xff)<<16);
972 /***********************************************************************
973 * GetProcessFlags (KERNEL32)
975 DWORD WINAPI
GetProcessFlags( DWORD processid
)
977 PDB
*pdb
= PROCESS_IdToPDB( processid
);
982 /***********************************************************************
983 * SetProcessWorkingSetSize [KERNEL32.662]
984 * Sets the min/max working set sizes for a specified process.
987 * hProcess [I] Handle to the process of interest
988 * minset [I] Specifies minimum working set size
989 * maxset [I] Specifies maximum working set size
993 BOOL WINAPI
SetProcessWorkingSetSize(HANDLE hProcess
,DWORD minset
,
996 FIXME("(0x%08x,%ld,%ld): stub - harmless\n",hProcess
,minset
,maxset
);
997 if(( minset
== -1) && (maxset
== -1)) {
998 /* Trim the working set to zero */
999 /* Swap the process out of physical RAM */
1004 /***********************************************************************
1005 * GetProcessWorkingSetSize (KERNEL32)
1007 BOOL WINAPI
GetProcessWorkingSetSize(HANDLE hProcess
,LPDWORD minset
,
1010 FIXME("(0x%08x,%p,%p): stub\n",hProcess
,minset
,maxset
);
1011 /* 32 MB working set size */
1012 if (minset
) *minset
= 32*1024*1024;
1013 if (maxset
) *maxset
= 32*1024*1024;
1017 /***********************************************************************
1018 * SetProcessShutdownParameters (KERNEL32)
1020 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1021 * Now tracks changes made (but does not act on these changes)
1022 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
1023 * It really shouldn't be here, but I'll move it when it's been checked!
1025 #define SHUTDOWN_NORETRY 1
1026 static unsigned int shutdown_noretry
= 0;
1027 static unsigned int shutdown_priority
= 0x280L
;
1028 BOOL WINAPI
SetProcessShutdownParameters(DWORD level
,DWORD flags
)
1030 if (flags
& SHUTDOWN_NORETRY
)
1031 shutdown_noretry
= 1;
1033 shutdown_noretry
= 0;
1034 if (level
> 0x100L
&& level
< 0x3FFL
)
1035 shutdown_priority
= level
;
1038 ERR("invalid priority level 0x%08lx\n", level
);
1045 /***********************************************************************
1046 * GetProcessShutdownParameters (KERNEL32)
1049 BOOL WINAPI
GetProcessShutdownParameters( LPDWORD lpdwLevel
,
1052 (*lpdwLevel
) = shutdown_priority
;
1053 (*lpdwFlags
) = (shutdown_noretry
* SHUTDOWN_NORETRY
);
1056 /***********************************************************************
1057 * SetProcessPriorityBoost (KERNEL32)
1059 BOOL WINAPI
SetProcessPriorityBoost(HANDLE hprocess
,BOOL disableboost
)
1061 FIXME("(%d,%d): stub\n",hprocess
,disableboost
);
1062 /* Say we can do it. I doubt the program will notice that we don't. */
1067 /***********************************************************************
1068 * ReadProcessMemory (KERNEL32)
1070 BOOL WINAPI
ReadProcessMemory( HANDLE process
, LPCVOID addr
, LPVOID buffer
, DWORD size
,
1071 LPDWORD bytes_read
)
1073 struct read_process_memory_request
*req
= get_req_buffer();
1074 unsigned int offset
= (unsigned int)addr
% sizeof(int);
1075 unsigned int max
= server_remaining( req
->data
); /* max length in one request */
1078 if (bytes_read
) *bytes_read
= size
;
1080 /* first time, read total length to check for permissions */
1081 req
->handle
= process
;
1082 req
->addr
= (char *)addr
- offset
;
1083 req
->len
= (size
+ offset
+ sizeof(int) - 1) / sizeof(int);
1084 if (server_call( REQ_READ_PROCESS_MEMORY
)) goto error
;
1086 if (size
<= max
- offset
)
1088 memcpy( buffer
, (char *)req
->data
+ offset
, size
);
1092 /* now take care of the remaining data */
1093 memcpy( buffer
, (char *)req
->data
+ offset
, max
- offset
);
1098 if (max
> size
) max
= size
;
1099 req
->handle
= process
;
1100 req
->addr
= (char *)addr
+ pos
;
1101 req
->len
= (max
+ sizeof(int) - 1) / sizeof(int);
1102 if (server_call( REQ_READ_PROCESS_MEMORY
)) goto error
;
1103 memcpy( (char *)buffer
+ pos
, (char *)req
->data
, max
);
1110 if (bytes_read
) *bytes_read
= 0;
1115 /***********************************************************************
1116 * WriteProcessMemory (KERNEL32)
1118 BOOL WINAPI
WriteProcessMemory( HANDLE process
, LPVOID addr
, LPVOID buffer
, DWORD size
,
1119 LPDWORD bytes_written
)
1121 unsigned int first_offset
, last_offset
;
1122 struct write_process_memory_request
*req
= get_req_buffer();
1123 unsigned int max
= server_remaining( req
->data
); /* max length in one request */
1124 unsigned int pos
, last_mask
;
1128 SetLastError( ERROR_INVALID_PARAMETER
);
1131 if (bytes_written
) *bytes_written
= size
;
1133 /* compute the mask for the first int */
1134 req
->first_mask
= ~0;
1135 first_offset
= (unsigned int)addr
% sizeof(int);
1136 memset( &req
->first_mask
, 0, first_offset
);
1138 /* compute the mask for the last int */
1139 last_offset
= (size
+ first_offset
) % sizeof(int);
1141 memset( &last_mask
, 0xff, last_offset
? last_offset
: sizeof(int) );
1143 req
->handle
= process
;
1144 req
->addr
= (char *)addr
- first_offset
;
1145 /* for the first request, use the total length */
1146 req
->len
= (size
+ first_offset
+ sizeof(int) - 1) / sizeof(int);
1148 if (size
+ first_offset
< max
) /* we can do it in one round */
1150 memcpy( (char *)req
->data
+ first_offset
, buffer
, size
);
1151 req
->last_mask
= last_mask
;
1152 if (server_call( REQ_WRITE_PROCESS_MEMORY
)) goto error
;
1156 /* needs multiple server calls */
1158 memcpy( (char *)req
->data
+ first_offset
, buffer
, max
- first_offset
);
1159 req
->last_mask
= ~0;
1160 if (server_call( REQ_WRITE_PROCESS_MEMORY
)) goto error
;
1161 pos
= max
- first_offset
;
1165 if (size
<= max
) /* last one */
1167 req
->last_mask
= last_mask
;
1170 req
->handle
= process
;
1171 req
->addr
= (char *)addr
+ pos
;
1172 req
->len
= (max
+ sizeof(int) - 1) / sizeof(int);
1173 req
->first_mask
= ~0;
1174 memcpy( req
->data
, (char *) buffer
+ pos
, max
);
1175 if (server_call( REQ_WRITE_PROCESS_MEMORY
)) goto error
;
1182 if (bytes_written
) *bytes_written
= 0;
1188 /***********************************************************************
1189 * RegisterServiceProcess (KERNEL, KERNEL32)
1191 * A service process calls this function to ensure that it continues to run
1192 * even after a user logged off.
1194 DWORD WINAPI
RegisterServiceProcess(DWORD dwProcessId
, DWORD dwType
)
1196 /* I don't think that Wine needs to do anything in that function */
1197 return 1; /* success */
1200 /***********************************************************************
1201 * GetExitCodeProcess [KERNEL32.325]
1203 * Gets termination status of specified process
1209 BOOL WINAPI
GetExitCodeProcess(
1210 HANDLE hProcess
, /* [I] handle to the process */
1211 LPDWORD lpExitCode
) /* [O] address to receive termination status */
1214 struct get_process_info_request
*req
= get_req_buffer();
1215 req
->handle
= hProcess
;
1216 if (!server_call( REQ_GET_PROCESS_INFO
))
1218 if (lpExitCode
) *lpExitCode
= req
->exit_code
;
1225 /***********************************************************************
1226 * SetErrorMode (KERNEL32.486)
1228 UINT WINAPI
SetErrorMode( UINT mode
)
1230 UINT old
= PROCESS_Current()->error_mode
;
1231 PROCESS_Current()->error_mode
= mode
;