1 /* $Id: create.c,v 1.72 2003/10/31 16:27:01 navaraf Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/process/create.c
6 * PURPOSE: Process functions
7 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
12 /* INCLUDES ****************************************************************/
17 #include <kernel32/kernel32.h>
19 /* FUNCTIONS ****************************************************************/
21 extern __declspec(noreturn
)
22 VOID CALLBACK
ConsoleControlDispatcher(DWORD CodeAndFlag
);
25 PRTL_BASE_PROCESS_START_ROUTINE RtlBaseProcessStartRoutine
;
27 typedef NTSTATUS
STDCALL (K32_MBSTR_TO_WCSTR
)
34 NTSTATUS STDCALL K32MbStrToWcStr
36 IN K32_MBSTR_TO_WCSTR
* True
,
37 UNICODE_STRING
* DestStr
,
38 ANSI_STRING
* SourceStr
,
42 if(SourceStr
->Buffer
== NULL
)
44 DestStr
->Length
= DestStr
->MaximumLength
= 0;
45 DestStr
->Buffer
= NULL
;
46 return STATUS_SUCCESS
;
49 return True(DestStr
, SourceStr
, Allocate
);
52 VOID STDCALL RtlRosR32AttribsToNativeAttribs
54 OUT OBJECT_ATTRIBUTES
* NativeAttribs
,
55 IN SECURITY_ATTRIBUTES
* Ros32Attribs OPTIONAL
58 NativeAttribs
->Length
= sizeof(*NativeAttribs
);
59 NativeAttribs
->ObjectName
= NULL
;
60 NativeAttribs
->RootDirectory
= NULL
;
61 NativeAttribs
->Attributes
= 0;
62 NativeAttribs
->SecurityQualityOfService
= NULL
;
65 if(Ros32Attribs
!= NULL
&& Ros32Attribs
->nLength
>= sizeof(*Ros32Attribs
))
67 NativeAttribs
->SecurityDescriptor
= Ros32Attribs
->lpSecurityDescriptor
;
69 if(Ros32Attribs
->bInheritHandle
)
70 NativeAttribs
->Attributes
|= OBJ_INHERIT
;
73 NativeAttribs
->SecurityDescriptor
= NULL
;
76 VOID STDCALL RtlRosR32AttribsToNativeAttribsNamed
78 OUT OBJECT_ATTRIBUTES
* NativeAttribs
,
79 IN SECURITY_ATTRIBUTES
* Ros32Attribs OPTIONAL
,
80 OUT UNICODE_STRING
* NativeName OPTIONAL
,
81 IN WCHAR
* Ros32Name OPTIONAL
,
82 IN HANDLE Ros32NameRoot OPTIONAL
85 if(!NativeAttribs
) return;
87 RtlRosR32AttribsToNativeAttribs(NativeAttribs
, Ros32Attribs
);
89 if(Ros32Name
!= NULL
&& NativeName
!= NULL
)
91 RtlInitUnicodeString(NativeName
, Ros32Name
);
93 NativeAttribs
->ObjectName
= NativeName
;
94 NativeAttribs
->RootDirectory
= Ros32NameRoot
;
95 NativeAttribs
->Attributes
|= OBJ_CASE_INSENSITIVE
;
103 BOOL STDCALL CreateProcessA
105 LPCSTR lpApplicationName
,
107 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
108 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
109 BOOL bInheritHandles
,
110 DWORD dwCreationFlags
,
111 LPVOID lpEnvironment
,
112 LPCSTR lpCurrentDirectory
,
113 LPSTARTUPINFOA lpStartupInfo
,
114 LPPROCESS_INFORMATION lpProcessInformation
117 * FUNCTION: The CreateProcess function creates a new process and its
118 * primary thread. The new process executes the specified executable file
121 * lpApplicationName = Pointer to name of executable module
122 * lpCommandLine = Pointer to command line string
123 * lpProcessAttributes = Process security attributes
124 * lpThreadAttributes = Thread security attributes
125 * bInheritHandles = Handle inheritance flag
126 * dwCreationFlags = Creation flags
127 * lpEnvironment = Pointer to new environment block
128 * lpCurrentDirectory = Pointer to current directory name
129 * lpStartupInfo = Pointer to startup info
130 * lpProcessInformation = Pointer to process information
133 PWCHAR pwcEnv
= NULL
;
134 UNICODE_STRING wstrApplicationName
;
135 UNICODE_STRING wstrCurrentDirectory
;
136 UNICODE_STRING wstrCommandLine
;
137 UNICODE_STRING wstrReserved
;
138 UNICODE_STRING wstrDesktop
;
139 UNICODE_STRING wstrTitle
;
140 ANSI_STRING strApplicationName
;
141 ANSI_STRING strCurrentDirectory
;
142 ANSI_STRING strCommandLine
;
143 ANSI_STRING strReserved
;
144 ANSI_STRING strDesktop
;
145 ANSI_STRING strTitle
;
147 STARTUPINFOW wsiStartupInfo
;
149 NTSTATUS
STDCALL (*pTrue
)
156 ULONG
STDCALL (*pRtlMbStringToUnicodeSize
)(ANSI_STRING
*);
158 DPRINT("CreateProcessA(%s)\n", lpApplicationName
);
162 "dwCreationFlags %x, lpEnvironment %x, lpCurrentDirectory %x, "
163 "lpStartupInfo %x, lpProcessInformation %x\n",
171 /* invalid parameter */
172 if(lpStartupInfo
== NULL
)
174 SetLastError(ERROR_INVALID_PARAMETER
);
178 /* multibyte strings are ANSI */
181 pTrue
= RtlAnsiStringToUnicodeString
;
182 pRtlMbStringToUnicodeSize
= RtlAnsiStringToUnicodeSize
;
184 /* multibyte strings are OEM */
187 pTrue
= RtlOemStringToUnicodeString
;
188 pRtlMbStringToUnicodeSize
= RtlOemStringToUnicodeSize
;
191 /* convert the environment */
192 if(lpEnvironment
&& !(dwCreationFlags
& CREATE_UNICODE_ENVIRONMENT
))
196 UNICODE_STRING wstrEnvVar
;
197 ANSI_STRING strEnvVar
;
199 /* scan the environment to calculate its Unicode size */
200 for(pcScan
= lpEnvironment
; *pcScan
; pcScan
+= strEnvVar
.Length
+ sizeof(char))
202 /* add the size of the current variable */
203 RtlInitAnsiString(&strEnvVar
, pcScan
);
204 nEnvLen
+= pRtlMbStringToUnicodeSize(&strEnvVar
) + sizeof(WCHAR
);
207 /* add the size of the final NUL character */
208 nEnvLen
+= sizeof(WCHAR
);
210 /* environment too large */
211 if(nEnvLen
> ~((USHORT
)0))
213 SetLastError(ERROR_OUTOFMEMORY
);
217 /* allocate the Unicode environment */
218 pwcEnv
= (PWCHAR
)RtlAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY
, nEnvLen
);
223 SetLastError(ERROR_OUTOFMEMORY
);
227 wstrEnvVar
.Buffer
= pwcEnv
;
228 wstrEnvVar
.Length
= 0;
229 wstrEnvVar
.MaximumLength
= nEnvLen
;
231 /* scan the environment to convert it */
232 for(pcScan
= lpEnvironment
; *pcScan
; pcScan
+= strEnvVar
.Length
+ sizeof(char))
234 /* convert the current variable */
235 RtlInitAnsiString(&strEnvVar
, pcScan
);
236 K32MbStrToWcStr(pTrue
, &wstrEnvVar
, &strEnvVar
, FALSE
);
238 /* advance the buffer to the next variable */
239 wstrEnvVar
.Buffer
+= (wstrEnvVar
.Length
/ sizeof(WCHAR
) + 1);
240 wstrEnvVar
.MaximumLength
-= (wstrEnvVar
.Length
+ sizeof(WCHAR
));
241 wstrEnvVar
.Length
= 0;
244 /* final NUL character */
245 wstrEnvVar
.Buffer
[0] = 0;
248 /* convert the strings */
249 RtlInitAnsiString(&strCommandLine
, lpCommandLine
);
250 RtlInitAnsiString(&strApplicationName
, (LPSTR
)lpApplicationName
);
251 RtlInitAnsiString(&strCurrentDirectory
, (LPSTR
)lpCurrentDirectory
);
252 RtlInitAnsiString(&strReserved
, (LPSTR
)lpStartupInfo
->lpReserved
);
253 RtlInitAnsiString(&strDesktop
, (LPSTR
)lpStartupInfo
->lpDesktop
);
254 RtlInitAnsiString(&strTitle
, (LPSTR
)lpStartupInfo
->lpTitle
);
256 K32MbStrToWcStr(pTrue
, &wstrCommandLine
, &strCommandLine
, TRUE
);
257 K32MbStrToWcStr(pTrue
, &wstrApplicationName
, &strApplicationName
, TRUE
);
258 K32MbStrToWcStr(pTrue
, &wstrCurrentDirectory
, &strCurrentDirectory
, TRUE
);
259 K32MbStrToWcStr(pTrue
, &wstrReserved
, &strReserved
, TRUE
);
260 K32MbStrToWcStr(pTrue
, &wstrDesktop
, &strDesktop
, TRUE
);
261 K32MbStrToWcStr(pTrue
, &wstrTitle
, &strTitle
, TRUE
);
263 /* convert the startup information */
264 memcpy(&wsiStartupInfo
, lpStartupInfo
, sizeof(wsiStartupInfo
));
266 wsiStartupInfo
.lpReserved
= wstrReserved
.Buffer
;
267 wsiStartupInfo
.lpDesktop
= wstrDesktop
.Buffer
;
268 wsiStartupInfo
.lpTitle
= wstrTitle
.Buffer
;
270 DPRINT("wstrApplicationName %wZ\n", &wstrApplicationName
);
271 DPRINT("wstrCommandLine %wZ\n", &wstrCommandLine
);
272 DPRINT("wstrCurrentDirectory %wZ\n", &wstrCurrentDirectory
);
273 DPRINT("wstrReserved %wZ\n", &wstrReserved
);
274 DPRINT("wstrDesktop %wZ\n", &wstrDesktop
);
275 DPRINT("wstrTitle %wZ\n", &wstrTitle
);
277 DPRINT("wstrApplicationName.Buffer %p\n", wstrApplicationName
.Buffer
);
278 DPRINT("wstrCommandLine.Buffer %p\n", wstrCommandLine
.Buffer
);
279 DPRINT("wstrCurrentDirectory.Buffer %p\n", wstrCurrentDirectory
.Buffer
);
280 DPRINT("wstrReserved.Buffer %p\n", wstrReserved
.Buffer
);
281 DPRINT("wstrDesktop.Buffer %p\n", wstrDesktop
.Buffer
);
282 DPRINT("wstrTitle.Buffer %p\n", wstrTitle
.Buffer
);
284 DPRINT("sizeof(STARTUPINFOA) %lu\n", sizeof(STARTUPINFOA
));
285 DPRINT("sizeof(STARTUPINFOW) %lu\n", sizeof(STARTUPINFOW
));
287 /* call the Unicode function */
288 bRetVal
= CreateProcessW
290 wstrApplicationName
.Buffer
,
291 wstrCommandLine
.Buffer
,
296 dwCreationFlags
& CREATE_UNICODE_ENVIRONMENT
? lpEnvironment
: pwcEnv
,
297 wstrCurrentDirectory
.Buffer
,
302 RtlFreeUnicodeString(&wstrApplicationName
);
303 RtlFreeUnicodeString(&wstrCommandLine
);
304 RtlFreeUnicodeString(&wstrCurrentDirectory
);
305 RtlFreeUnicodeString(&wstrReserved
);
306 RtlFreeUnicodeString(&wstrDesktop
);
307 RtlFreeUnicodeString(&wstrTitle
);
309 RtlFreeHeap(GetProcessHeap(), 0, pwcEnv
);
314 static int _except_recursion_trap
= 0;
317 struct __EXCEPTION_RECORD
;
320 EXCEPTION_DISPOSITION
323 struct _EXCEPTION_RECORD
*ExceptionRecord
,
324 void * EstablisherFrame
,
325 struct _CONTEXT
*ContextRecord
,
326 void * DispatcherContext
)
328 DPRINT1("Process terminated abnormally due to unhandled exception\n");
329 DPRINT1("Address: %x\n", ExceptionRecord
->ExceptionAddress
);
334 DPRINT1("Frames:\n");
335 Frame
= (PULONG
)((CONTEXT
*)ContextRecord
)->Ebp
;
336 while (Frame
!= NULL
&& Frame
!= (PULONG
)0xdeadbeef)
338 DPRINT1("%x\n", (PVOID
)Frame
[1]);
339 Frame
= (PULONG
)Frame
[0];
344 if (3 < ++_except_recursion_trap
)
346 DPRINT1("_except_handler(...) appears to be recursing.\n");
347 DPRINT1("Process HALTED.\n");
353 if (/* FIXME: */ TRUE
) /* Not a service */
355 DPRINT(" calling ExitProcess(0) no, lets try ExitThread . . .\n");
356 /* ExitProcess(0); */
361 DPRINT(" calling ExitThread(0) . . .\n");
365 DPRINT1(" We should not get to here !!!\n");
366 /* We should not get to here */
367 return ExceptionContinueSearch
;
371 BaseProcessStart(LPTHREAD_START_ROUTINE lpStartAddress
,
376 DPRINT("BaseProcessStart(..) - setting up exception frame.\n");
378 __try1(_except_handler
)
380 uExitCode
= (lpStartAddress
)((PVOID
)lpParameter
);
385 DPRINT("BaseProcessStart(..) - cleaned up exception frame.\n");
387 ExitThread(uExitCode
);
391 HANDLE STDCALL KlCreateFirstThread
393 HANDLE ProcessHandle
,
394 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
395 PSECTION_IMAGE_INFORMATION Sii
,
396 LPTHREAD_START_ROUTINE lpStartAddress
,
397 DWORD dwCreationFlags
,
401 OBJECT_ATTRIBUTES oaThreadAttribs
;
402 CLIENT_ID cidClientId
;
403 PVOID pTrueStartAddress
;
407 /* convert the thread attributes */
408 RtlRosR32AttribsToNativeAttribs(&oaThreadAttribs
, lpThreadAttributes
);
411 if(Sii
->Subsystem
!= IMAGE_SUBSYSTEM_NATIVE
)
412 pTrueStartAddress
= (PVOID
)BaseProcessStart
;
415 pTrueStartAddress
= (PVOID
)RtlBaseProcessStartRoutine
;
419 "RtlRosCreateUserThreadVa\n"
421 " ProcessHandle %p,\n"
422 " ObjectAttributes %p,\n"
423 " CreateSuspended %d,\n"
424 " StackZeroBits %d,\n"
425 " StackReserve %lu,\n"
426 " StackCommit %lu,\n"
427 " StartAddress %p,\n"
428 " ThreadHandle %p,\n"
430 " ParameterCount %u,\n"
431 " Parameters[0] %p,\n"
432 " Parameters[1] %p\n"
436 dwCreationFlags
& CREATE_SUSPENDED
,
448 /* create the first thread */
449 nErrCode
= RtlRosCreateUserThreadVa
453 dwCreationFlags
& CREATE_SUSPENDED
,
455 &(Sii
->StackReserve
),
461 (ULONG_PTR
)lpStartAddress
,
466 if(!NT_SUCCESS(nErrCode
))
468 SetLastErrorByStatus(nErrCode
);
477 "ClientId.UniqueThread %p\n",
481 cidClientId
.UniqueThread
485 if(lpThreadId
) *lpThreadId
= (DWORD
)cidClientId
.UniqueThread
;
489 HANDLE
KlMapFile(LPCWSTR lpApplicationName
)
492 IO_STATUS_BLOCK IoStatusBlock
;
493 UNICODE_STRING ApplicationNameString
;
494 OBJECT_ATTRIBUTES ObjectAttributes
;
495 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
502 * Find the application name
505 if (!RtlDosPathNameToNtPathName_U ((LPWSTR
)lpApplicationName
,
506 &ApplicationNameString
,
511 DPRINT("ApplicationName %S\n",ApplicationNameString
.Buffer
);
513 InitializeObjectAttributes(&ObjectAttributes
,
514 &ApplicationNameString
,
515 OBJ_CASE_INSENSITIVE
,
520 * Try to open the executable
523 Status
= NtOpenFile(&hFile
,
524 SYNCHRONIZE
|FILE_EXECUTE
|FILE_READ_DATA
,
527 FILE_SHARE_DELETE
|FILE_SHARE_READ
,
528 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
530 RtlFreeUnicodeString (&ApplicationNameString
);
532 if (!NT_SUCCESS(Status
))
534 DPRINT("Failed to open file\n");
535 SetLastErrorByStatus (Status
);
539 Status
= NtCreateSection(&hSection
,
548 if (!NT_SUCCESS(Status
))
550 DPRINT("Failed to create section\n");
551 SetLastErrorByStatus (Status
);
558 static NTSTATUS KlInitPeb
560 HANDLE ProcessHandle
,
561 PRTL_USER_PROCESS_PARAMETERS Ppb
,
562 PVOID
* ImageBaseAddress
570 PVOID ParentEnv
= NULL
;
573 ULONG EnvSize
= 0, EnvSize1
= 0;
575 /* create the Environment */
576 if (Ppb
->Environment
!= NULL
)
578 ParentEnv
= Ppb
->Environment
;
585 EnvSize
= (PVOID
)ptr
- ParentEnv
;
587 else if (NtCurrentPeb()->ProcessParameters
->Environment
!= NULL
)
589 MEMORY_BASIC_INFORMATION MemInfo
;
590 ParentEnv
= NtCurrentPeb()->ProcessParameters
->Environment
;
592 Status
= NtQueryVirtualMemory (NtCurrentProcess (),
594 MemoryBasicInformation
,
596 sizeof(MEMORY_BASIC_INFORMATION
),
598 if (!NT_SUCCESS(Status
))
602 EnvSize
= MemInfo
.RegionSize
;
604 DPRINT("EnvironmentSize %ld\n", EnvSize
);
606 /* allocate and initialize new environment block */
610 Status
= NtAllocateVirtualMemory(ProcessHandle
,
614 MEM_RESERVE
| MEM_COMMIT
,
616 if (!NT_SUCCESS(Status
))
621 NtWriteVirtualMemory(ProcessHandle
,
630 PpbSize
= Ppb
->AllocationSize
;
631 Status
= NtAllocateVirtualMemory(ProcessHandle
,
635 MEM_RESERVE
| MEM_COMMIT
,
637 if (!NT_SUCCESS(Status
))
642 //DPRINT("Ppb->MaximumLength %x\n", Ppb->MaximumLength);
643 NtWriteVirtualMemory(ProcessHandle
,
649 /* write pointer to environment */
650 Offset
= FIELD_OFFSET(RTL_USER_PROCESS_PARAMETERS
, Environment
);
651 NtWriteVirtualMemory(ProcessHandle
,
652 (PVOID
)(PpbBase
+ Offset
),
657 /* write pointer to process parameter block */
658 Offset
= FIELD_OFFSET(PEB
, ProcessParameters
);
659 NtWriteVirtualMemory(ProcessHandle
,
660 (PVOID
)(PEB_BASE
+ Offset
),
665 /* Read image base address. */
666 Offset
= FIELD_OFFSET(PEB
, ImageBaseAddress
);
667 NtReadVirtualMemory(ProcessHandle
,
668 (PVOID
)(PEB_BASE
+ Offset
),
673 return(STATUS_SUCCESS
);
683 LPCWSTR lpApplicationName
,
684 LPWSTR lpCommandLine
,
685 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
686 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
687 WINBOOL bInheritHandles
,
688 DWORD dwCreationFlags
,
689 LPVOID lpEnvironment
,
690 LPCWSTR lpCurrentDirectory
,
691 LPSTARTUPINFOW lpStartupInfo
,
692 LPPROCESS_INFORMATION lpProcessInformation
695 HANDLE hSection
, hProcess
, hThread
;
697 WCHAR ImagePathName
[256];
698 UNICODE_STRING ImagePathName_U
;
699 PROCESS_BASIC_INFORMATION ProcessBasicInfo
;
701 PRTL_USER_PROCESS_PARAMETERS Ppb
;
702 UNICODE_STRING CommandLine_U
;
703 CSRSS_API_REQUEST CsrRequest
;
704 CSRSS_API_REPLY CsrReply
;
705 CHAR ImageFileName
[8];
708 UNICODE_STRING CurrentDirectory_U
;
709 SECTION_IMAGE_INFORMATION Sii
;
710 WCHAR TempCurrentDirectoryW
[256];
711 WCHAR TempApplicationNameW
[256];
712 WCHAR TempCommandLineNameW
[256];
713 UNICODE_STRING RuntimeInfo_U
;
714 PVOID ImageBaseAddress
;
716 DPRINT("CreateProcessW(lpApplicationName '%S', lpCommandLine '%S')\n",
717 lpApplicationName
, lpCommandLine
);
719 if (lpApplicationName
!= NULL
&& lpApplicationName
[0] != 0)
721 wcscpy (TempApplicationNameW
, lpApplicationName
);
722 i
= wcslen(TempApplicationNameW
);
723 if (TempApplicationNameW
[i
- 1] == L
'.')
725 TempApplicationNameW
[i
- 1] = 0;
729 s
= max(wcsrchr(TempApplicationNameW
, L
'\\'), wcsrchr(TempApplicationNameW
, L
'/'));
732 s
= TempApplicationNameW
;
738 e
= wcsrchr(s
, L
'.');
742 e
= wcsrchr(s
, L
'.');
746 else if (lpCommandLine
!= NULL
&& lpCommandLine
[0] != 0)
748 if (lpCommandLine
[0] == L
'"')
750 wcscpy(TempApplicationNameW
, lpCommandLine
+ 1);
751 s
= wcschr(TempApplicationNameW
, L
'"');
760 wcscpy(TempApplicationNameW
, lpCommandLine
);
761 s
= wcschr(TempApplicationNameW
, L
' ');
767 s
= max(wcsrchr(TempApplicationNameW
, L
'\\'), wcsrchr(TempApplicationNameW
, L
'/'));
770 s
= TempApplicationNameW
;
772 s
= wcsrchr(s
, L
'.');
774 wcscat(TempApplicationNameW
, L
".exe");
781 DPRINT("CreateProcessW(lpApplicationName '%S', lpCommandLine '%S')\n",
782 lpApplicationName
, lpCommandLine
);
784 if (!SearchPathW(NULL
, TempApplicationNameW
, NULL
, sizeof(ImagePathName
)/sizeof(WCHAR
), ImagePathName
, &s
))
789 e
= wcsrchr(s
, L
'.');
790 if (e
!= NULL
&& (!_wcsicmp(e
, L
".bat") || !_wcsicmp(e
, L
".cmd")))
792 // the command is a batch file
793 if (lpApplicationName
!= NULL
&& lpApplicationName
[0])
795 // FIXME: use COMSPEC for the command interpreter
796 wcscpy(TempCommandLineNameW
, L
"cmd /c ");
797 wcscat(TempCommandLineNameW
, lpApplicationName
);
798 lpCommandLine
= TempCommandLineNameW
;
799 wcscpy(TempApplicationNameW
, L
"cmd.exe");
800 if (!SearchPathW(NULL
, TempApplicationNameW
, NULL
, sizeof(ImagePathName
)/sizeof(WCHAR
), ImagePathName
, &s
))
812 * Store the image file name for the process
819 for (i
= 0; i
< 8; i
++)
821 ImageFileName
[i
] = (CHAR
)(s
[i
]);
829 * Process the application name and command line
831 RtlInitUnicodeString(&ImagePathName_U
, ImagePathName
);
832 RtlInitUnicodeString(&CommandLine_U
, lpCommandLine
);
834 DPRINT("ImagePathName_U %S\n", ImagePathName_U
.Buffer
);
835 DPRINT("CommandLine_U %S\n", CommandLine_U
.Buffer
);
837 /* Initialize the current directory string */
838 if (lpCurrentDirectory
!= NULL
)
840 RtlInitUnicodeString(&CurrentDirectory_U
,
845 GetCurrentDirectoryW(256, TempCurrentDirectoryW
);
846 RtlInitUnicodeString(&CurrentDirectory_U
,
847 TempCurrentDirectoryW
);
851 * Create a section for the executable
854 hSection
= KlMapFile (ImagePathName
);
855 if (hSection
== NULL
)
857 /////////////////////////////////////////
859 * Inspect the image to determine executable flavour
861 IO_STATUS_BLOCK IoStatusBlock
;
862 UNICODE_STRING ApplicationNameString
;
863 OBJECT_ATTRIBUTES ObjectAttributes
;
864 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
865 IMAGE_DOS_HEADER DosHeader
;
866 IO_STATUS_BLOCK Iosb
;
867 LARGE_INTEGER Offset
;
870 DPRINT("Inspecting Image Header for image type id\n");
872 // Find the application name
873 if (!RtlDosPathNameToNtPathName_U((LPWSTR
)lpApplicationName
,
874 &ApplicationNameString
, NULL
, NULL
)) {
877 DPRINT("ApplicationName %S\n",ApplicationNameString
.Buffer
);
879 InitializeObjectAttributes(&ObjectAttributes
,
880 &ApplicationNameString
,
881 OBJ_CASE_INSENSITIVE
,
885 // Try to open the executable
886 Status
= NtOpenFile(&hFile
,
887 SYNCHRONIZE
|FILE_EXECUTE
|FILE_READ_DATA
,
890 FILE_SHARE_DELETE
|FILE_SHARE_READ
,
891 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
893 RtlFreeUnicodeString(&ApplicationNameString
);
895 if (!NT_SUCCESS(Status
)) {
896 DPRINT("Failed to open file\n");
897 SetLastErrorByStatus(Status
);
901 // Read the dos header
903 Status
= ZwReadFile(hFile
,
913 if (!NT_SUCCESS(Status
)) {
914 DPRINT("Failed to read from file\n");
915 SetLastErrorByStatus(Status
);
918 if (Iosb
.Information
!= sizeof(DosHeader
)) {
919 DPRINT("Failed to read dos header from file\n");
920 SetLastErrorByStatus(STATUS_INVALID_IMAGE_FORMAT
);
924 // Check the DOS signature
925 if (DosHeader
.e_magic
!= IMAGE_DOS_SIGNATURE
) {
926 DPRINT("Failed dos magic check\n");
927 SetLastErrorByStatus(STATUS_INVALID_IMAGE_FORMAT
);
932 DPRINT("Launching VDM...\n");
933 return CreateProcessW(L
"ntvdm.exe",
934 (LPWSTR
)lpApplicationName
,
942 lpProcessInformation
);
944 /////////////////////////////////////////
946 * Create a new process
948 Status
= NtCreateProcess(&hProcess
,
958 if (lpStartupInfo
->lpReserved2
)
961 * ROUND_UP(xxx,2) + 2 is a dirty hack. RtlCreateProcessParameters assumes that
962 * the runtimeinfo is a unicode string and use RtlCopyUnicodeString for duplication.
963 * If is possible that this function overwrite the last information in runtimeinfo
964 * with the null terminator for the unicode string.
966 RuntimeInfo_U
.Length
= RuntimeInfo_U
.MaximumLength
= ROUND_UP(lpStartupInfo
->cbReserved2
, 2) + 2;
967 RuntimeInfo_U
.Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, RuntimeInfo_U
.Length
);
968 memcpy(RuntimeInfo_U
.Buffer
, lpStartupInfo
->lpReserved2
, lpStartupInfo
->cbReserved2
);
975 RtlCreateProcessParameters(&Ppb
,
978 lpCurrentDirectory
? &CurrentDirectory_U
: NULL
,
984 lpStartupInfo
&& lpStartupInfo
->lpReserved2
? &RuntimeInfo_U
: NULL
);
986 if (lpStartupInfo
&& lpStartupInfo
->lpReserved2
)
987 RtlFreeHeap(GetProcessHeap(), 0, RuntimeInfo_U
.Buffer
);
991 * Translate some handles for the new process
993 if (Ppb
->CurrentDirectoryHandle
)
995 Status
= NtDuplicateObject (NtCurrentProcess(),
996 Ppb
->CurrentDirectoryHandle
,
998 &Ppb
->CurrentDirectoryHandle
,
1001 DUPLICATE_SAME_ACCESS
);
1006 Status
= NtDuplicateObject (NtCurrentProcess(),
1012 DUPLICATE_SAME_ACCESS
);
1016 * Get some information about the executable
1018 Status
= ZwQuerySection(hSection
,
1019 SectionImageInformation
,
1029 * Get some information about the process
1031 NtQueryInformationProcess(hProcess
,
1032 ProcessBasicInformation
,
1034 sizeof(ProcessBasicInfo
),
1036 DPRINT("ProcessBasicInfo.UniqueProcessId %d\n",
1037 ProcessBasicInfo
.UniqueProcessId
);
1038 lpProcessInformation
->dwProcessId
= ProcessBasicInfo
.UniqueProcessId
;
1041 * Tell the csrss server we are creating a new process
1043 CsrRequest
.Type
= CSRSS_CREATE_PROCESS
;
1044 CsrRequest
.Data
.CreateProcessRequest
.NewProcessId
=
1045 ProcessBasicInfo
.UniqueProcessId
;
1046 if (Sii
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_GUI
)
1048 /* Do not create a console for GUI applications */
1049 dwCreationFlags
&= ~CREATE_NEW_CONSOLE
;
1050 dwCreationFlags
|= DETACHED_PROCESS
;
1052 CsrRequest
.Data
.CreateProcessRequest
.Flags
= dwCreationFlags
;
1053 CsrRequest
.Data
.CreateProcessRequest
.CtrlDispatcher
= ConsoleControlDispatcher
;
1054 Status
= CsrClientCallServer(&CsrRequest
,
1056 sizeof(CSRSS_API_REQUEST
),
1057 sizeof(CSRSS_API_REPLY
));
1058 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(CsrReply
.Status
))
1060 DbgPrint("Failed to tell csrss about new process. Expect trouble.\n");
1063 // Set the child console handles
1064 Ppb
->hStdInput
= NtCurrentPeb()->ProcessParameters
->hStdInput
;
1065 Ppb
->hStdOutput
= NtCurrentPeb()->ProcessParameters
->hStdOutput
;
1066 Ppb
->hStdError
= NtCurrentPeb()->ProcessParameters
->hStdError
;
1068 if (lpStartupInfo
&& (lpStartupInfo
->dwFlags
& STARTF_USESTDHANDLES
))
1070 if (lpStartupInfo
->hStdInput
)
1071 Ppb
->hStdInput
= lpStartupInfo
->hStdInput
;
1072 if (lpStartupInfo
->hStdOutput
)
1073 Ppb
->hStdOutput
= lpStartupInfo
->hStdOutput
;
1074 if (lpStartupInfo
->hStdError
)
1075 Ppb
->hStdError
= lpStartupInfo
->hStdError
;
1078 if (IsConsoleHandle(Ppb
->hStdInput
))
1080 Ppb
->hStdInput
= CsrReply
.Data
.CreateProcessReply
.InputHandle
;
1084 DPRINT("Duplicate input handle\n");
1085 Status
= NtDuplicateObject (NtCurrentProcess(),
1091 DUPLICATE_SAME_ACCESS
);
1092 if(!NT_SUCCESS(Status
))
1094 DPRINT("NtDuplicateObject failed, status %x\n", Status
);
1098 if (IsConsoleHandle(Ppb
->hStdOutput
))
1100 Ppb
->hStdOutput
= CsrReply
.Data
.CreateProcessReply
.OutputHandle
;
1104 DPRINT("Duplicate output handle\n");
1105 Status
= NtDuplicateObject (NtCurrentProcess(),
1111 DUPLICATE_SAME_ACCESS
);
1112 if(!NT_SUCCESS(Status
))
1114 DPRINT("NtDuplicateObject failed, status %x\n", Status
);
1117 if (IsConsoleHandle(Ppb
->hStdError
))
1119 CsrRequest
.Type
= CSRSS_DUPLICATE_HANDLE
;
1120 CsrRequest
.Data
.DuplicateHandleRequest
.ProcessId
= ProcessBasicInfo
.UniqueProcessId
;
1121 CsrRequest
.Data
.DuplicateHandleRequest
.Handle
= CsrReply
.Data
.CreateProcessReply
.OutputHandle
;
1122 Status
= CsrClientCallServer(&CsrRequest
,
1124 sizeof(CSRSS_API_REQUEST
),
1125 sizeof(CSRSS_API_REPLY
));
1126 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(CsrReply
.Status
))
1128 Ppb
->hStdError
= INVALID_HANDLE_VALUE
;
1132 Ppb
->hStdError
= CsrReply
.Data
.DuplicateHandleReply
.Handle
;
1137 DPRINT("Duplicate error handle\n");
1138 Status
= NtDuplicateObject (NtCurrentProcess(),
1144 DUPLICATE_SAME_ACCESS
);
1145 if(!NT_SUCCESS(Status
))
1147 DPRINT("NtDuplicateObject failed, status %x\n", Status
);
1152 * Initialize some other fields in the PPB
1156 Ppb
->dwFlags
= lpStartupInfo
->dwFlags
;
1157 if (Ppb
->dwFlags
& STARTF_USESHOWWINDOW
)
1159 Ppb
->wShowWindow
= lpStartupInfo
->wShowWindow
;
1163 Ppb
->wShowWindow
= SW_SHOWDEFAULT
;
1165 Ppb
->dwX
= lpStartupInfo
->dwX
;
1166 Ppb
->dwY
= lpStartupInfo
->dwY
;
1167 Ppb
->dwXSize
= lpStartupInfo
->dwXSize
;
1168 Ppb
->dwYSize
= lpStartupInfo
->dwYSize
;
1169 Ppb
->dwFillAttribute
= lpStartupInfo
->dwFillAttribute
;
1177 * Create Process Environment Block
1179 DPRINT("Creating peb\n");
1181 KlInitPeb(hProcess
, Ppb
, &ImageBaseAddress
);
1183 RtlDestroyProcessParameters (Ppb
);
1185 Status
= NtSetInformationProcess(hProcess
,
1186 ProcessImageFileName
,
1190 * Create the thread for the kernel
1192 DPRINT("Creating thread for process (EntryPoint = 0x%.08x)\n",
1193 ImageBaseAddress
+ (ULONG
)Sii
.EntryPoint
);
1194 hThread
= KlCreateFirstThread(hProcess
,
1197 ImageBaseAddress
+ (ULONG
)Sii
.EntryPoint
,
1199 &lpProcessInformation
->dwThreadId
);
1200 if (hThread
== INVALID_HANDLE_VALUE
)
1205 lpProcessInformation
->hProcess
= hProcess
;
1206 lpProcessInformation
->hThread
= hThread
;