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