4 * Copyright 1996, 1998 Alexandre Julliard
26 #include "debugtools.h"
28 DECLARE_DEBUG_CHANNEL(process
)
29 DECLARE_DEBUG_CHANNEL(relay
)
30 DECLARE_DEBUG_CHANNEL(win32
)
33 /* The initial process PDB */
34 static PDB initial_pdb
;
36 static PDB
*PROCESS_First
= &initial_pdb
;
38 /***********************************************************************
41 PDB
*PROCESS_Current(void)
43 return THREAD_Current()->process
;
46 /***********************************************************************
49 * FIXME: This works only while running all processes in the same
50 * address space (or, at least, the initial process is mapped
51 * into all address spaces as is KERNEL32 in Windows 95)
54 PDB
*PROCESS_Initial(void)
59 /***********************************************************************
62 * Retrieve information about a process
64 static BOOL
PROCESS_QueryInfo( HANDLE handle
,
65 struct get_process_info_reply
*reply
)
67 struct get_process_info_request req
;
69 CLIENT_SendRequest( REQ_GET_PROCESS_INFO
, -1, 1, &req
, sizeof(req
) );
70 return !CLIENT_WaitSimpleReply( reply
, sizeof(*reply
), NULL
);
73 /***********************************************************************
76 * Check if a handle is to the current process
78 BOOL
PROCESS_IsCurrent( HANDLE handle
)
80 struct get_process_info_reply reply
;
81 return (PROCESS_QueryInfo( handle
, &reply
) &&
82 (reply
.pid
== PROCESS_Current()->server_pid
));
86 /***********************************************************************
89 * Convert a process id to a PDB, making sure it is valid.
91 PDB
*PROCESS_IdToPDB( DWORD id
)
95 if (!id
) return PROCESS_Current();
99 if ((DWORD
)pdb
->server_pid
== id
) return pdb
;
102 SetLastError( ERROR_INVALID_PARAMETER
);
107 /***********************************************************************
108 * PROCESS_CallUserSignalProc
110 * FIXME: Some of the signals aren't sent correctly!
112 * The exact meaning of the USER signals is undocumented, but this
113 * should cover the basic idea:
115 * USIG_DLL_UNLOAD_WIN16
116 * This is sent when a 16-bit module is unloaded.
118 * USIG_DLL_UNLOAD_WIN32
119 * This is sent when a 32-bit module is unloaded.
121 * USIG_DLL_UNLOAD_ORPHANS
122 * This is sent after the last Win3.1 module is unloaded,
123 * to allow removal of orphaned menus.
125 * USIG_FAULT_DIALOG_PUSH
126 * USIG_FAULT_DIALOG_POP
127 * These are called to allow USER to prepare for displaying a
128 * fault dialog, even though the fault might have happened while
129 * inside a USER critical section.
132 * This is called from the context of a new thread, as soon as it
133 * has started to run.
136 * This is called, still in its context, just before a thread is
137 * about to terminate.
139 * USIG_PROCESS_CREATE
140 * This is called, in the parent process context, after a new process
144 * This is called in the new process context, just after the main thread
145 * has started execution (after the main thread's USIG_THREAD_INIT has
148 * USIG_PROCESS_LOADED
149 * This is called after the executable file has been loaded into the
150 * new process context.
152 * USIG_PROCESS_RUNNING
153 * This is called immediately before the main entry point is called.
156 * This is called in the context of a process that is about to
157 * terminate (but before the last thread's USIG_THREAD_EXIT has
160 * USIG_PROCESS_DESTROY
161 * This is called after a process has terminated.
164 * The meaning of the dwFlags bits is as follows:
167 * Current process is 32-bit.
170 * Current process is a (Win32) GUI process.
172 * USIG_FLAGS_FEEDBACK
173 * Current process needs 'feedback' (determined from the STARTUPINFO
174 * flags STARTF_FORCEONFEEDBACK / STARTF_FORCEOFFFEEDBACK).
177 * The signal is being sent due to a fault.
179 void PROCESS_CallUserSignalProc( UINT uCode
, DWORD dwThreadOrProcessId
, HMODULE hModule
)
181 PDB
*pdb
= PROCESS_Current();
182 STARTUPINFOA
*startup
= pdb
->env_db
? pdb
->env_db
->startup_info
: NULL
;
185 /* Determine dwFlags */
187 if ( !(pdb
->flags
& PDB32_WIN16_PROC
) )
188 dwFlags
|= USIG_FLAGS_WIN32
;
190 if ( !(pdb
->flags
& PDB32_CONSOLE_PROC
) )
191 dwFlags
|= USIG_FLAGS_GUI
;
193 if ( dwFlags
& USIG_FLAGS_GUI
)
195 /* Feedback defaults to ON */
196 if ( !(startup
&& (startup
->dwFlags
& STARTF_FORCEOFFFEEDBACK
)) )
197 dwFlags
|= USIG_FLAGS_FEEDBACK
;
201 /* Feedback defaults to OFF */
202 if ( startup
&& (startup
->dwFlags
& STARTF_FORCEONFEEDBACK
) )
203 dwFlags
|= USIG_FLAGS_FEEDBACK
;
206 /* Get thread or process ID */
208 if ( dwThreadOrProcessId
== 0 )
210 if ( uCode
== USIG_THREAD_INIT
|| uCode
== USIG_THREAD_EXIT
)
211 dwThreadOrProcessId
= GetCurrentThreadId();
213 dwThreadOrProcessId
= GetCurrentProcessId();
216 /* Convert module handle to 16-bit */
218 if ( HIWORD( hModule
) )
219 hModule
= MapHModuleLS( hModule
);
221 /* Call USER signal proc */
223 if ( Callout
.UserSignalProc
)
224 Callout
.UserSignalProc( uCode
, dwThreadOrProcessId
, dwFlags
, hModule
);
228 /***********************************************************************
231 * Build the env DB for the initial process
233 static BOOL
PROCESS_BuildEnvDB( PDB
*pdb
)
235 /* Allocate the env DB (FIXME: should not be on the system heap) */
237 if (!(pdb
->env_db
= HeapAlloc(SystemHeap
,HEAP_ZERO_MEMORY
,sizeof(ENVDB
))))
239 InitializeCriticalSection( &pdb
->env_db
->section
);
241 /* Allocate startup info */
242 if (!(pdb
->env_db
->startup_info
=
243 HeapAlloc( SystemHeap
, HEAP_ZERO_MEMORY
, sizeof(STARTUPINFOA
) )))
246 /* Allocate the standard handles */
248 pdb
->env_db
->hStdin
= FILE_DupUnixHandle( 0, GENERIC_READ
);
249 pdb
->env_db
->hStdout
= FILE_DupUnixHandle( 1, GENERIC_WRITE
);
250 pdb
->env_db
->hStderr
= FILE_DupUnixHandle( 2, GENERIC_WRITE
);
252 /* Build the command-line */
254 pdb
->env_db
->cmd_line
= HEAP_strdupA( SystemHeap
, 0, "kernel32" );
256 /* Build the environment strings */
258 return ENV_BuildEnvironment( pdb
);
262 /***********************************************************************
263 * PROCESS_InheritEnvDB
265 static BOOL
PROCESS_InheritEnvDB( PDB
*pdb
, LPCSTR cmd_line
, LPCSTR env
,
266 BOOL inherit_handles
, STARTUPINFOA
*startup
)
268 if (!(pdb
->env_db
= HeapAlloc(pdb
->heap
, HEAP_ZERO_MEMORY
, sizeof(ENVDB
))))
270 InitializeCriticalSection( &pdb
->env_db
->section
);
272 /* Copy the parent environment */
274 if (!ENV_InheritEnvironment( pdb
, env
)) return FALSE
;
276 /* Copy the command line */
278 if (!(pdb
->env_db
->cmd_line
= HEAP_strdupA( pdb
->heap
, 0, cmd_line
)))
281 /* Remember startup info */
282 if (!(pdb
->env_db
->startup_info
=
283 HeapAlloc( pdb
->heap
, HEAP_ZERO_MEMORY
, sizeof(STARTUPINFOA
) )))
285 *pdb
->env_db
->startup_info
= *startup
;
287 /* Inherit the standard handles */
288 if (pdb
->env_db
->startup_info
->dwFlags
& STARTF_USESTDHANDLES
)
290 pdb
->env_db
->hStdin
= pdb
->env_db
->startup_info
->hStdInput
;
291 pdb
->env_db
->hStdout
= pdb
->env_db
->startup_info
->hStdOutput
;
292 pdb
->env_db
->hStderr
= pdb
->env_db
->startup_info
->hStdError
;
294 else if (inherit_handles
)
296 pdb
->env_db
->hStdin
= pdb
->parent
->env_db
->hStdin
;
297 pdb
->env_db
->hStdout
= pdb
->parent
->env_db
->hStdout
;
298 pdb
->env_db
->hStderr
= pdb
->parent
->env_db
->hStderr
;
300 /* else will be done later on in PROCESS_Create */
306 /***********************************************************************
307 * PROCESS_CreateEnvDB
309 * Create the env DB for a newly started process.
311 static BOOL
PROCESS_CreateEnvDB(void)
313 struct init_process_request req
;
314 struct init_process_reply reply
;
315 STARTUPINFOA
*startup
;
317 PDB
*pdb
= PROCESS_Current();
319 /* Retrieve startup info from the server */
322 CLIENT_SendRequest( REQ_INIT_PROCESS
, -1, 1, &req
, sizeof(req
) );
323 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) return FALSE
;
326 /* Allocate the env DB */
328 if (!(env_db
= HeapAlloc( pdb
->heap
, HEAP_ZERO_MEMORY
, sizeof(ENVDB
) )))
330 pdb
->env_db
= env_db
;
331 InitializeCriticalSection( &env_db
->section
);
333 /* Allocate and fill the startup info */
334 if (!(startup
= HeapAlloc( pdb
->heap
, HEAP_ZERO_MEMORY
, sizeof(STARTUPINFOA
) )))
336 pdb
->env_db
->startup_info
= startup
;
338 startup
= pdb
->env_db
->startup_info
;
340 startup
->dwFlags
= reply
.start_flags
;
341 pdb
->env_db
->hStdin
= startup
->hStdInput
= reply
.hstdin
;
342 pdb
->env_db
->hStdout
= startup
->hStdOutput
= reply
.hstdout
;
343 pdb
->env_db
->hStderr
= startup
->hStdError
= reply
.hstderr
;
346 /* Copy the parent environment */
348 if (!ENV_InheritEnvironment( pdb
, env
)) return FALSE
;
350 /* Copy the command line */
352 if (!(pdb
->env_db
->cmd_line
= HEAP_strdupA( pdb
->heap
, 0, cmd_line
)))
359 /***********************************************************************
362 * Free a PDB and all associated storage.
364 void PROCESS_FreePDB( PDB
*pdb
)
366 PDB
**pptr
= &PROCESS_First
;
368 ENV_FreeEnvironment( pdb
);
369 while (*pptr
&& (*pptr
!= pdb
)) pptr
= &(*pptr
)->next
;
370 if (*pptr
) *pptr
= pdb
->next
;
371 if (pdb
->heap
&& (pdb
->heap
!= pdb
->system_heap
)) HeapDestroy( pdb
->heap
);
372 HeapFree( SystemHeap
, 0, pdb
);
376 /***********************************************************************
379 * Allocate and fill a PDB structure.
380 * Runs in the context of the parent process.
382 static PDB
*PROCESS_CreatePDB( PDB
*parent
, BOOL inherit
)
384 PDB
*pdb
= HeapAlloc( SystemHeap
, HEAP_ZERO_MEMORY
, sizeof(PDB
) );
386 if (!pdb
) return NULL
;
387 pdb
->exit_code
= 0x103; /* STILL_ACTIVE */
389 pdb
->running_threads
= 1;
390 pdb
->ring0_threads
= 1;
391 pdb
->system_heap
= SystemHeap
;
392 pdb
->parent
= parent
;
394 pdb
->priority
= 8; /* Normal */
395 pdb
->heap
= pdb
->system_heap
; /* will be changed later on */
396 pdb
->next
= PROCESS_First
;
402 /***********************************************************************
405 BOOL
PROCESS_Init(void)
410 /* Start the server */
411 server_fd
= CLIENT_InitServer();
413 /* Fill the initial process structure */
414 initial_pdb
.exit_code
= 0x103; /* STILL_ACTIVE */
415 initial_pdb
.threads
= 1;
416 initial_pdb
.running_threads
= 1;
417 initial_pdb
.ring0_threads
= 1;
418 initial_pdb
.group
= &initial_pdb
;
419 initial_pdb
.priority
= 8; /* Normal */
420 initial_pdb
.flags
= PDB32_WIN16_PROC
;
422 /* Initialize virtual memory management */
423 if (!VIRTUAL_Init()) return FALSE
;
425 /* Create the initial thread structure and socket pair */
426 if (!(thdb
= THREAD_CreateInitialThread( &initial_pdb
, server_fd
))) return FALSE
;
428 /* Remember TEB selector of initial process for emergency use */
429 SYSLEVEL_EmergencyTeb
= thdb
->teb_sel
;
431 /* Create the system heap */
432 if (!(SystemHeap
= HeapCreate( HEAP_GROWABLE
, 0x10000, 0 ))) return FALSE
;
433 initial_pdb
.system_heap
= initial_pdb
.heap
= SystemHeap
;
435 /* Create the environment DB of the first process */
436 if (!PROCESS_BuildEnvDB( &initial_pdb
)) return FALSE
;
438 /* Create the SEGPTR heap */
439 if (!(SegptrHeap
= HeapCreate( HEAP_WINE_SEGPTR
, 0, 0 ))) return FALSE
;
441 /* Initialize the first process critical section */
442 InitializeCriticalSection( &initial_pdb
.crit_section
);
448 /***********************************************************************
451 * Startup routine of a new process. Called in the context of the new process.
453 void PROCESS_Start(void)
456 LPTHREAD_START_ROUTINE entry
;
457 THDB
*thdb
= THREAD_Current();
458 PDB
*pdb
= thdb
->process
;
459 NE_MODULE
*pModule
= NE_GetPtr( pdb
->module
);
460 OFSTRUCT
*ofs
= (OFSTRUCT
*)((char*)(pModule
) + (pModule
)->fileinfo
);
461 IMAGE_OPTIONAL_HEADER
*header
= &PE_HEADER(pModule
->module32
)->OptionalHeader
;
463 /* Setup process flags */
464 if (header
->Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
) pdb
->flags
|= PDB32_CONSOLE_PROC
;
466 /* Map system DLLs into this process (from initial process) */
467 /* FIXME: this is a hack */
468 pdb
->modref_list
= PROCESS_Initial()->modref_list
;
470 PROCESS_CallUserSignalProc( USIG_THREAD_INIT
, 0, 0 ); /* for initial thread */
472 /* Initialize the critical section */
473 InitializeCriticalSection( &pdb
->crit_section
);
476 /* Create the heap */
478 if (!(pdb
->heap
= HeapCreate( HEAP_GROWABLE
, header
->SizeOfHeapReserve
,
479 header
->SizeOfHeapCommit
))) goto error
;
480 pdb
->heap_list
= pdb
->heap
;
483 /* Create the environment db */
484 if (!PROCESS_CreateEnvDB()) goto error
;
486 /* Create a task for this process */
487 if (pdb
->env_db
->startup_info
->dwFlags
& STARTF_USESHOWWINDOW
)
488 cmdShow
= pdb
->env_db
->startup_info
->wShowWindow
;
489 if (!TASK_Create( thdb
, pModule
, 0, 0, cmdShow
)) goto error
;
491 /* Link the task in the task list */
492 TASK_StartTask( pdb
->task
);
494 PROCESS_CallUserSignalProc( USIG_PROCESS_INIT
, 0, 0 );
496 /* Create 32-bit MODREF */
497 if (!PE_CreateModule( pModule
->module32
, ofs
, 0, FALSE
)) goto error
;
499 /* Increment EXE refcount */
500 assert( pdb
->exe_modref
);
501 pdb
->exe_modref
->refCount
++;
503 PROCESS_CallUserSignalProc( USIG_PROCESS_LOADED
, 0, 0 ); /* FIXME: correct location? */
505 /* Initialize thread-local storage */
509 if ( pdb
->flags
& PDB32_CONSOLE_PROC
)
512 /* Now call the entry point */
514 EnterCriticalSection( &pdb
->crit_section
);
515 MODULE_DllProcessAttach( pdb
->exe_modref
, (LPVOID
)1 );
516 LeaveCriticalSection( &pdb
->crit_section
);
518 PROCESS_CallUserSignalProc( USIG_PROCESS_RUNNING
, 0, 0 );
520 entry
= (LPTHREAD_START_ROUTINE
)RVA_PTR(pModule
->module32
,
521 OptionalHeader
.AddressOfEntryPoint
);
522 TRACE_(relay
)("(entryproc=%p)\n", entry
);
523 ExitProcess( entry(NULL
) );
530 /***********************************************************************
533 * Create a new process database and associated info.
535 PDB
*PROCESS_Create( NE_MODULE
*pModule
, LPCSTR cmd_line
, LPCSTR env
,
536 HINSTANCE16 hInstance
, HINSTANCE16 hPrevInstance
,
537 LPSECURITY_ATTRIBUTES psa
, LPSECURITY_ATTRIBUTES tsa
,
538 BOOL inherit
, STARTUPINFOA
*startup
,
539 PROCESS_INFORMATION
*info
)
543 struct new_process_request req
;
544 struct new_process_reply reply
;
547 PDB
*parent
= PROCESS_Current();
548 PDB
*pdb
= PROCESS_CreatePDB( parent
, inherit
);
550 if (!pdb
) return NULL
;
551 info
->hThread
= info
->hProcess
= INVALID_HANDLE_VALUE
;
553 /* Create the process on the server side */
555 req
.inherit
= (psa
&& (psa
->nLength
>= sizeof(*psa
)) && psa
->bInheritHandle
);
556 req
.inherit_all
= inherit
;
557 req
.start_flags
= startup
->dwFlags
;
558 if (startup
->dwFlags
& STARTF_USESTDHANDLES
)
560 req
.hstdin
= startup
->hStdInput
;
561 req
.hstdout
= startup
->hStdOutput
;
562 req
.hstderr
= startup
->hStdError
;
566 req
.hstdin
= GetStdHandle( STD_INPUT_HANDLE
);
567 req
.hstdout
= GetStdHandle( STD_OUTPUT_HANDLE
);
568 req
.hstderr
= GetStdHandle( STD_ERROR_HANDLE
);
570 CLIENT_SendRequest( REQ_NEW_PROCESS
, -1, 2,
571 &req
, sizeof(req
), cmd_line
, strlen(cmd_line
) + 1 );
572 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) goto error
;
573 pdb
->server_pid
= reply
.pid
;
574 info
->hProcess
= reply
.handle
;
575 info
->dwProcessId
= (DWORD
)pdb
->server_pid
;
577 if (pModule
->module32
)
579 /* Create the main thread */
580 size
= PE_HEADER(pModule
->module32
)->OptionalHeader
.SizeOfStackReserve
;
581 if (!(thdb
= THREAD_Create( pdb
, 0L, size
, TRUE
, tsa
, &server_thandle
)))
583 info
->hThread
= server_thandle
;
584 info
->dwThreadId
= (DWORD
)thdb
->server_tid
;
585 thdb
->startup
= PROCESS_Start
;
587 /* Create the heap */
588 size
= PE_HEADER(pModule
->module32
)->OptionalHeader
.SizeOfHeapReserve
;
589 commit
= PE_HEADER(pModule
->module32
)->OptionalHeader
.SizeOfHeapCommit
;
590 if (!(pdb
->heap
= HeapCreate( HEAP_GROWABLE
, size
, commit
))) goto error
;
591 pdb
->heap_list
= pdb
->heap
;
593 /* Inherit the env DB from the parent */
594 if (!PROCESS_InheritEnvDB( pdb
, cmd_line
, env
, inherit
, startup
)) goto error
;
596 /* Call USER signal proc */
597 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE
, info
->dwProcessId
, 0 );
599 /* Set the process module (FIXME: hack) */
600 pdb
->module
= pModule
->self
;
601 SYSDEPS_SpawnThread( thdb
);
603 else /* Create a 16-bit process */
605 /* Setup process flags */
606 pdb
->flags
|= PDB32_WIN16_PROC
;
608 /* Create the heap */
611 if (!(pdb
->heap
= HeapCreate( HEAP_GROWABLE
, size
, commit
))) goto error
;
612 pdb
->heap_list
= pdb
->heap
;
614 /* Inherit the env DB from the parent */
615 if (!PROCESS_InheritEnvDB( pdb
, cmd_line
, env
, inherit
, startup
)) goto error
;
617 /* Create the main thread */
618 if (!(thdb
= THREAD_Create( pdb
, 0L, 0, hInstance
== 0, tsa
, &server_thandle
)))
620 info
->hThread
= server_thandle
;
621 info
->dwThreadId
= (DWORD
)thdb
->server_tid
;
622 thdb
->startup
= PROCESS_Start
;
624 /* Duplicate the standard handles */
625 if ((!(pdb
->env_db
->startup_info
->dwFlags
& STARTF_USESTDHANDLES
)) && !inherit
)
627 DuplicateHandle( GetCurrentProcess(), pdb
->parent
->env_db
->hStdin
,
628 info
->hProcess
, &pdb
->env_db
->hStdin
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
629 DuplicateHandle( GetCurrentProcess(), pdb
->parent
->env_db
->hStdout
,
630 info
->hProcess
, &pdb
->env_db
->hStdout
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
631 DuplicateHandle( GetCurrentProcess(), pdb
->parent
->env_db
->hStderr
,
632 info
->hProcess
, &pdb
->env_db
->hStderr
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
635 /* Call USER signal proc */
636 PROCESS_CallUserSignalProc( USIG_PROCESS_CREATE
, info
->dwProcessId
, 0 );
638 /* Create a Win16 task for this process */
639 if (startup
->dwFlags
& STARTF_USESHOWWINDOW
) cmdShow
= startup
->wShowWindow
;
640 if (!TASK_Create( thdb
, pModule
, hInstance
, hPrevInstance
, cmdShow
)) goto error
;
642 /* Map system DLLs into this process (from initial process) */
643 /* FIXME: this is a hack */
644 pdb
->modref_list
= PROCESS_Initial()->modref_list
;
647 TASK_StartTask( pdb
->task
);
653 if (info
->hThread
!= INVALID_HANDLE_VALUE
) CloseHandle( info
->hThread
);
654 if (info
->hProcess
!= INVALID_HANDLE_VALUE
) CloseHandle( info
->hProcess
);
655 PROCESS_FreePDB( pdb
);
660 /***********************************************************************
661 * ExitProcess (KERNEL32.100)
663 void WINAPI
ExitProcess( DWORD status
)
665 EnterCriticalSection( &PROCESS_Current()->crit_section
);
666 MODULE_DllProcessDetach( TRUE
, (LPVOID
)1 );
667 LeaveCriticalSection( &PROCESS_Current()->crit_section
);
669 if ( THREAD_IsWin16( THREAD_Current() ) )
670 TASK_KillCurrentTask( status
);
673 TerminateProcess( GetCurrentProcess(), status
);
676 /***********************************************************************
677 * ExitProcess16 (KERNEL.466)
679 void WINAPI
ExitProcess16( WORD status
)
681 SYSLEVEL_ReleaseWin16Lock();
682 ExitProcess( status
);
685 /******************************************************************************
686 * TerminateProcess (KERNEL32.684)
688 BOOL WINAPI
TerminateProcess( HANDLE handle
, DWORD exit_code
)
690 struct terminate_process_request req
;
692 req
.exit_code
= exit_code
;
693 CLIENT_SendRequest( REQ_TERMINATE_PROCESS
, -1, 1, &req
, sizeof(req
) );
694 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
698 /***********************************************************************
699 * GetProcessDword (KERNEL32.18) (KERNEL.485)
700 * 'Of course you cannot directly access Windows internal structures'
702 DWORD WINAPI
GetProcessDword( DWORD dwProcessID
, INT offset
)
704 PDB
*process
= PROCESS_IdToPDB( dwProcessID
);
708 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
709 if ( !process
) return 0;
713 case GPD_APP_COMPAT_FLAGS
:
714 pTask
= (TDB
*)GlobalLock16( process
->task
);
715 return pTask
? pTask
->compat_flags
: 0;
717 case GPD_LOAD_DONE_EVENT
:
718 return process
->load_done_evt
;
720 case GPD_HINSTANCE16
:
721 pTask
= (TDB
*)GlobalLock16( process
->task
);
722 return pTask
? pTask
->hInstance
: 0;
724 case GPD_WINDOWS_VERSION
:
725 pTask
= (TDB
*)GlobalLock16( process
->task
);
726 return pTask
? pTask
->version
: 0;
729 if ( process
!= PROCESS_Current() ) return 0;
730 return (DWORD
)THREAD_Current();
733 return (DWORD
)process
;
735 case GPD_STARTF_SHELLDATA
: /* return stdoutput handle from startupinfo ??? */
736 return process
->env_db
->startup_info
->hStdOutput
;
738 case GPD_STARTF_HOTKEY
: /* return stdinput handle from startupinfo ??? */
739 return process
->env_db
->startup_info
->hStdInput
;
741 case GPD_STARTF_SHOWWINDOW
:
742 return process
->env_db
->startup_info
->wShowWindow
;
744 case GPD_STARTF_SIZE
:
745 x
= process
->env_db
->startup_info
->dwXSize
;
746 if ( x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
747 y
= process
->env_db
->startup_info
->dwYSize
;
748 if ( y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
749 return MAKELONG( x
, y
);
751 case GPD_STARTF_POSITION
:
752 x
= process
->env_db
->startup_info
->dwX
;
753 if ( x
== CW_USEDEFAULT
) x
= CW_USEDEFAULT16
;
754 y
= process
->env_db
->startup_info
->dwY
;
755 if ( y
== CW_USEDEFAULT
) y
= CW_USEDEFAULT16
;
756 return MAKELONG( x
, y
);
758 case GPD_STARTF_FLAGS
:
759 return process
->env_db
->startup_info
->dwFlags
;
762 return (DWORD
)process
->parent
->server_pid
;
765 return process
->flags
;
768 return process
->process_dword
;
771 ERR_(win32
)("Unknown offset %d\n", offset
);
776 /***********************************************************************
777 * SetProcessDword (KERNEL.484)
778 * 'Of course you cannot directly access Windows internal structures'
780 void WINAPI
SetProcessDword( DWORD dwProcessID
, INT offset
, DWORD value
)
782 PDB
*process
= PROCESS_IdToPDB( dwProcessID
);
784 TRACE_(win32
)("(%ld, %d)\n", dwProcessID
, offset
);
785 if ( !process
) return;
789 case GPD_APP_COMPAT_FLAGS
:
790 case GPD_LOAD_DONE_EVENT
:
791 case GPD_HINSTANCE16
:
792 case GPD_WINDOWS_VERSION
:
795 case GPD_STARTF_SHELLDATA
:
796 case GPD_STARTF_HOTKEY
:
797 case GPD_STARTF_SHOWWINDOW
:
798 case GPD_STARTF_SIZE
:
799 case GPD_STARTF_POSITION
:
800 case GPD_STARTF_FLAGS
:
803 ERR_(win32
)("Not allowed to modify offset %d\n", offset
);
807 process
->process_dword
= value
;
811 ERR_(win32
)("Unknown offset %d\n", offset
);
817 /***********************************************************************
818 * GetCurrentProcess (KERNEL32.198)
820 HANDLE WINAPI
GetCurrentProcess(void)
822 return CURRENT_PROCESS_PSEUDOHANDLE
;
826 /*********************************************************************
827 * OpenProcess (KERNEL32.543)
829 HANDLE WINAPI
OpenProcess( DWORD access
, BOOL inherit
, DWORD id
)
831 struct open_process_request req
;
832 struct open_process_reply reply
;
834 req
.pid
= (void *)id
;
836 req
.inherit
= inherit
;
837 CLIENT_SendRequest( REQ_OPEN_PROCESS
, -1, 1, &req
, sizeof(req
) );
838 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) return 0;
842 /*********************************************************************
843 * MapProcessHandle (KERNEL.483)
845 DWORD WINAPI
MapProcessHandle( HANDLE handle
)
847 struct get_process_info_reply reply
;
848 if ( !PROCESS_QueryInfo( handle
, &reply
) ) return 0;
849 return (DWORD
)reply
.pid
;
852 /***********************************************************************
853 * GetCurrentProcessId (KERNEL32.199)
855 DWORD WINAPI
GetCurrentProcessId(void)
857 return (DWORD
)PROCESS_Current()->server_pid
;
861 /***********************************************************************
862 * GetProcessHeap (KERNEL32.259)
864 HANDLE WINAPI
GetProcessHeap(void)
866 PDB
*pdb
= PROCESS_Current();
867 return pdb
->heap
? pdb
->heap
: SystemHeap
;
871 /***********************************************************************
872 * GetThreadLocale (KERNEL32.295)
874 LCID WINAPI
GetThreadLocale(void)
876 return PROCESS_Current()->locale
;
880 /***********************************************************************
881 * SetPriorityClass (KERNEL32.503)
883 BOOL WINAPI
SetPriorityClass( HANDLE hprocess
, DWORD priorityclass
)
885 struct set_process_info_request req
;
886 req
.handle
= hprocess
;
887 req
.priority
= priorityclass
;
888 req
.mask
= SET_PROCESS_INFO_PRIORITY
;
889 CLIENT_SendRequest( REQ_SET_PROCESS_INFO
, -1, 1, &req
, sizeof(req
) );
890 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
894 /***********************************************************************
895 * GetPriorityClass (KERNEL32.250)
897 DWORD WINAPI
GetPriorityClass(HANDLE hprocess
)
899 struct get_process_info_reply reply
;
900 if (!PROCESS_QueryInfo( hprocess
, &reply
)) return 0;
901 return reply
.priority
;
905 /***********************************************************************
906 * SetProcessAffinityMask (KERNEL32.662)
908 BOOL WINAPI
SetProcessAffinityMask( HANDLE hProcess
, DWORD affmask
)
910 struct set_process_info_request req
;
911 req
.handle
= hProcess
;
912 req
.affinity
= affmask
;
913 req
.mask
= SET_PROCESS_INFO_AFFINITY
;
914 CLIENT_SendRequest( REQ_SET_PROCESS_INFO
, -1, 1, &req
, sizeof(req
) );
915 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
918 /**********************************************************************
919 * GetProcessAffinityMask (KERNEL32.373)
921 BOOL WINAPI
GetProcessAffinityMask( HANDLE hProcess
,
922 LPDWORD lpProcessAffinityMask
,
923 LPDWORD lpSystemAffinityMask
)
925 struct get_process_info_reply reply
;
926 if (!PROCESS_QueryInfo( hProcess
, &reply
)) return FALSE
;
927 if (lpProcessAffinityMask
) *lpProcessAffinityMask
= reply
.process_affinity
;
928 if (lpSystemAffinityMask
) *lpSystemAffinityMask
= reply
.system_affinity
;
933 /***********************************************************************
934 * GetStdHandle (KERNEL32.276)
936 HANDLE WINAPI
GetStdHandle( DWORD std_handle
)
938 PDB
*pdb
= PROCESS_Current();
942 case STD_INPUT_HANDLE
: return pdb
->env_db
->hStdin
;
943 case STD_OUTPUT_HANDLE
: return pdb
->env_db
->hStdout
;
944 case STD_ERROR_HANDLE
: return pdb
->env_db
->hStderr
;
946 SetLastError( ERROR_INVALID_PARAMETER
);
947 return INVALID_HANDLE_VALUE
;
951 /***********************************************************************
952 * SetStdHandle (KERNEL32.506)
954 BOOL WINAPI
SetStdHandle( DWORD std_handle
, HANDLE handle
)
956 PDB
*pdb
= PROCESS_Current();
957 /* FIXME: should we close the previous handle? */
960 case STD_INPUT_HANDLE
:
961 pdb
->env_db
->hStdin
= handle
;
963 case STD_OUTPUT_HANDLE
:
964 pdb
->env_db
->hStdout
= handle
;
966 case STD_ERROR_HANDLE
:
967 pdb
->env_db
->hStderr
= handle
;
970 SetLastError( ERROR_INVALID_PARAMETER
);
974 /***********************************************************************
975 * GetProcessVersion (KERNEL32)
977 DWORD WINAPI
GetProcessVersion( DWORD processid
)
980 PDB
*pdb
= PROCESS_IdToPDB( processid
);
983 if (!(pTask
= (TDB
*)GlobalLock16( pdb
->task
))) return 0;
984 return (pTask
->version
&0xff) | (((pTask
->version
>>8) & 0xff)<<16);
987 /***********************************************************************
988 * GetProcessFlags (KERNEL32)
990 DWORD WINAPI
GetProcessFlags( DWORD processid
)
992 PDB
*pdb
= PROCESS_IdToPDB( processid
);
997 /***********************************************************************
998 * SetProcessWorkingSetSize [KERNEL32.662]
999 * Sets the min/max working set sizes for a specified process.
1002 * hProcess [I] Handle to the process of interest
1003 * minset [I] Specifies minimum working set size
1004 * maxset [I] Specifies maximum working set size
1008 BOOL WINAPI
SetProcessWorkingSetSize(HANDLE hProcess
,DWORD minset
,
1011 FIXME_(process
)("(0x%08x,%ld,%ld): stub - harmless\n",hProcess
,minset
,maxset
);
1012 if(( minset
== -1) && (maxset
== -1)) {
1013 /* Trim the working set to zero */
1014 /* Swap the process out of physical RAM */
1019 /***********************************************************************
1020 * GetProcessWorkingSetSize (KERNEL32)
1022 BOOL WINAPI
GetProcessWorkingSetSize(HANDLE hProcess
,LPDWORD minset
,
1025 FIXME_(process
)("(0x%08x,%p,%p): stub\n",hProcess
,minset
,maxset
);
1026 /* 32 MB working set size */
1027 if (minset
) *minset
= 32*1024*1024;
1028 if (maxset
) *maxset
= 32*1024*1024;
1032 /***********************************************************************
1033 * SetProcessShutdownParameters (KERNEL32)
1035 * CHANGED - James Sutherland (JamesSutherland@gmx.de)
1036 * Now tracks changes made (but does not act on these changes)
1037 * NOTE: the definition for SHUTDOWN_NORETRY was done on guesswork.
1038 * It really shouldn't be here, but I'll move it when it's been checked!
1040 #define SHUTDOWN_NORETRY 1
1041 static unsigned int shutdown_noretry
= 0;
1042 static unsigned int shutdown_priority
= 0x280L
;
1043 BOOL WINAPI
SetProcessShutdownParameters(DWORD level
,DWORD flags
)
1045 if (flags
& SHUTDOWN_NORETRY
)
1046 shutdown_noretry
= 1;
1048 shutdown_noretry
= 0;
1049 if (level
> 0x100L
&& level
< 0x3FFL
)
1050 shutdown_priority
= level
;
1053 ERR_(process
)("invalid priority level 0x%08lx\n", level
);
1060 /***********************************************************************
1061 * GetProcessShutdownParameters (KERNEL32)
1064 BOOL WINAPI
GetProcessShutdownParameters( LPDWORD lpdwLevel
,
1067 (*lpdwLevel
) = shutdown_priority
;
1068 (*lpdwFlags
) = (shutdown_noretry
* SHUTDOWN_NORETRY
);
1071 /***********************************************************************
1072 * SetProcessPriorityBoost (KERNEL32)
1074 BOOL WINAPI
SetProcessPriorityBoost(HANDLE hprocess
,BOOL disableboost
)
1076 FIXME_(process
)("(%d,%d): stub\n",hprocess
,disableboost
);
1077 /* Say we can do it. I doubt the program will notice that we don't. */
1081 /***********************************************************************
1082 * ReadProcessMemory (KERNEL32)
1083 * FIXME: check this, if we ever run win32 binaries in different addressspaces
1084 * ... and add a sizecheck
1086 BOOL WINAPI
ReadProcessMemory( HANDLE hProcess
, LPCVOID lpBaseAddress
,
1087 LPVOID lpBuffer
, DWORD nSize
,
1088 LPDWORD lpNumberOfBytesRead
)
1090 memcpy(lpBuffer
,lpBaseAddress
,nSize
);
1091 if (lpNumberOfBytesRead
) *lpNumberOfBytesRead
= nSize
;
1095 /***********************************************************************
1096 * WriteProcessMemory (KERNEL32)
1097 * FIXME: check this, if we ever run win32 binaries in different addressspaces
1098 * ... and add a sizecheck
1100 BOOL WINAPI
WriteProcessMemory(HANDLE hProcess
, LPVOID lpBaseAddress
,
1101 LPVOID lpBuffer
, DWORD nSize
,
1102 LPDWORD lpNumberOfBytesWritten
)
1104 memcpy(lpBaseAddress
,lpBuffer
,nSize
);
1105 if (lpNumberOfBytesWritten
) *lpNumberOfBytesWritten
= nSize
;
1109 /***********************************************************************
1110 * RegisterServiceProcess (KERNEL, KERNEL32)
1112 * A service process calls this function to ensure that it continues to run
1113 * even after a user logged off.
1115 DWORD WINAPI
RegisterServiceProcess(DWORD dwProcessId
, DWORD dwType
)
1117 /* I don't think that Wine needs to do anything in that function */
1118 return 1; /* success */
1121 /***********************************************************************
1122 * GetExitCodeProcess [KERNEL32.325]
1124 * Gets termination status of specified process
1130 BOOL WINAPI
GetExitCodeProcess(
1131 HANDLE hProcess
, /* [I] handle to the process */
1132 LPDWORD lpExitCode
) /* [O] address to receive termination status */
1134 struct get_process_info_reply reply
;
1135 if (!PROCESS_QueryInfo( hProcess
, &reply
)) return FALSE
;
1136 if (lpExitCode
) *lpExitCode
= reply
.exit_code
;
1141 /***********************************************************************
1142 * GetProcessHeaps [KERNEL32.376]
1144 DWORD WINAPI
GetProcessHeaps(DWORD nrofheaps
,HANDLE
*heaps
) {
1145 FIXME_(win32
)("(%ld,%p), incomplete implementation.\n",nrofheaps
,heaps
);
1148 heaps
[0] = GetProcessHeap();
1149 /* ... probably SystemHeap too ? */
1152 /* number of available heaps */