1 /* $Id: create.c,v 1.67 2003/04/29 02:16:59 hyperion 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 ****************************************************************/
22 PRTL_BASE_PROCESS_START_ROUTINE RtlBaseProcessStartRoutine
;
24 typedef NTSTATUS
STDCALL (K32_MBSTR_TO_WCSTR
)
31 NTSTATUS STDCALL K32MbStrToWcStr
33 IN K32_MBSTR_TO_WCSTR
* True
,
34 UNICODE_STRING
* DestStr
,
35 ANSI_STRING
* SourceStr
,
39 if(SourceStr
->Buffer
== NULL
)
41 DestStr
->Length
= DestStr
->MaximumLength
= 0;
42 DestStr
->Buffer
= NULL
;
43 return STATUS_SUCCESS
;
46 return True(DestStr
, SourceStr
, Allocate
);
49 VOID STDCALL RtlRosR32AttribsToNativeAttribs
51 OUT OBJECT_ATTRIBUTES
* NativeAttribs
,
52 IN SECURITY_ATTRIBUTES
* Ros32Attribs OPTIONAL
55 NativeAttribs
->Length
= sizeof(*NativeAttribs
);
56 NativeAttribs
->ObjectName
= NULL
;
57 NativeAttribs
->RootDirectory
= NULL
;
58 NativeAttribs
->Attributes
= 0;
59 NativeAttribs
->SecurityQualityOfService
= NULL
;
62 if(Ros32Attribs
!= NULL
&& Ros32Attribs
->nLength
>= sizeof(*Ros32Attribs
))
64 NativeAttribs
->SecurityDescriptor
= Ros32Attribs
->lpSecurityDescriptor
;
66 if(Ros32Attribs
->bInheritHandle
)
67 NativeAttribs
->Attributes
|= OBJ_INHERIT
;
70 NativeAttribs
->SecurityDescriptor
= NULL
;
73 VOID STDCALL RtlRosR32AttribsToNativeAttribsNamed
75 OUT OBJECT_ATTRIBUTES
* NativeAttribs
,
76 IN SECURITY_ATTRIBUTES
* Ros32Attribs OPTIONAL
,
77 OUT UNICODE_STRING
* NativeName OPTIONAL
,
78 IN WCHAR
* Ros32Name OPTIONAL
,
79 IN HANDLE Ros32NameRoot OPTIONAL
82 if(!NativeAttribs
) return;
84 RtlRosR32AttribsToNativeAttribs(NativeAttribs
, Ros32Attribs
);
86 if(Ros32Name
!= NULL
&& NativeName
!= NULL
)
88 RtlInitUnicodeString(NativeName
, Ros32Name
);
90 NativeAttribs
->ObjectName
= NativeName
;
91 NativeAttribs
->RootDirectory
= Ros32NameRoot
;
92 NativeAttribs
->Attributes
|= OBJ_CASE_INSENSITIVE
;
96 BOOL STDCALL CreateProcessA
98 LPCSTR lpApplicationName
,
100 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
101 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
102 BOOL bInheritHandles
,
103 DWORD dwCreationFlags
,
104 LPVOID lpEnvironment
,
105 LPCSTR lpCurrentDirectory
,
106 LPSTARTUPINFOA lpStartupInfo
,
107 LPPROCESS_INFORMATION lpProcessInformation
110 * FUNCTION: The CreateProcess function creates a new process and its
111 * primary thread. The new process executes the specified executable file
114 * lpApplicationName = Pointer to name of executable module
115 * lpCommandLine = Pointer to command line string
116 * lpProcessAttributes = Process security attributes
117 * lpThreadAttributes = Thread security attributes
118 * bInheritHandles = Handle inheritance flag
119 * dwCreationFlags = Creation flags
120 * lpEnvironment = Pointer to new environment block
121 * lpCurrentDirectory = Pointer to current directory name
122 * lpStartupInfo = Pointer to startup info
123 * lpProcessInformation = Pointer to process information
126 PWCHAR pwcEnv
= NULL
;
127 UNICODE_STRING wstrApplicationName
;
128 UNICODE_STRING wstrCurrentDirectory
;
129 UNICODE_STRING wstrCommandLine
;
130 UNICODE_STRING wstrReserved
;
131 UNICODE_STRING wstrDesktop
;
132 UNICODE_STRING wstrTitle
;
133 ANSI_STRING strApplicationName
;
134 ANSI_STRING strCurrentDirectory
;
135 ANSI_STRING strCommandLine
;
136 ANSI_STRING strReserved
;
137 ANSI_STRING strDesktop
;
138 ANSI_STRING strTitle
;
140 STARTUPINFOW wsiStartupInfo
;
142 NTSTATUS
STDCALL (*pTrue
)
149 ULONG
STDCALL (*pRtlMbStringToUnicodeSize
)(ANSI_STRING
*);
151 DPRINT("CreateProcessA(%s)\n", lpApplicationName
);
155 "dwCreationFlags %x, lpEnvironment %x, lpCurrentDirectory %x, "
156 "lpStartupInfo %x, lpProcessInformation %x\n",
164 /* invalid parameter */
165 if(lpStartupInfo
== NULL
)
167 SetLastError(ERROR_INVALID_PARAMETER
);
171 /* multibyte strings are ANSI */
174 pTrue
= RtlAnsiStringToUnicodeString
;
175 pRtlMbStringToUnicodeSize
= RtlAnsiStringToUnicodeSize
;
177 /* multibyte strings are OEM */
180 pTrue
= RtlOemStringToUnicodeString
;
181 pRtlMbStringToUnicodeSize
= RtlOemStringToUnicodeSize
;
184 /* convert the environment */
185 if(lpEnvironment
&& !(dwCreationFlags
& CREATE_UNICODE_ENVIRONMENT
))
189 UNICODE_STRING wstrEnvVar
;
190 ANSI_STRING strEnvVar
;
192 /* scan the environment to calculate its Unicode size */
193 for(pcScan
= lpEnvironment
; *pcScan
; pcScan
+= strEnvVar
.MaximumLength
)
195 /* add the size of the current variable */
196 RtlInitAnsiString(&strEnvVar
, pcScan
);
197 nEnvLen
+= pRtlMbStringToUnicodeSize(&strEnvVar
) + sizeof(WCHAR
);
200 /* add the size of the final NUL character */
201 nEnvLen
+= sizeof(WCHAR
);
203 /* environment too large */
204 if(nEnvLen
> ~((USHORT
)0))
206 SetLastError(ERROR_OUTOFMEMORY
);
210 /* allocate the Unicode environment */
211 pwcEnv
= (PWCHAR
)RtlAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY
, nEnvLen
);
216 SetLastError(ERROR_OUTOFMEMORY
);
220 wstrEnvVar
.Buffer
= pwcEnv
;
221 wstrEnvVar
.Length
= 0;
222 wstrEnvVar
.MaximumLength
= nEnvLen
;
224 /* scan the environment to convert it */
225 for(pcScan
= lpEnvironment
; *pcScan
; pcScan
+= strEnvVar
.MaximumLength
)
227 /* convert the current variable */
228 RtlInitAnsiString(&strEnvVar
, pcScan
);
229 K32MbStrToWcStr(pTrue
, &wstrEnvVar
, &strEnvVar
, FALSE
);
231 /* advance the buffer to the next variable */
232 wstrEnvVar
.Buffer
+= (wstrEnvVar
.Length
/ sizeof(WCHAR
) + 1);
233 wstrEnvVar
.MaximumLength
-= (wstrEnvVar
.Length
+ sizeof(WCHAR
));
234 wstrEnvVar
.Length
= 0;
237 /* final NUL character */
238 wstrEnvVar
.Buffer
[0] = 0;
241 /* convert the strings */
242 RtlInitAnsiString(&strCommandLine
, lpCommandLine
);
243 RtlInitAnsiString(&strApplicationName
, (LPSTR
)lpApplicationName
);
244 RtlInitAnsiString(&strCurrentDirectory
, (LPSTR
)lpCurrentDirectory
);
245 RtlInitAnsiString(&strReserved
, (LPSTR
)lpStartupInfo
->lpReserved
);
246 RtlInitAnsiString(&strDesktop
, (LPSTR
)lpStartupInfo
->lpDesktop
);
247 RtlInitAnsiString(&strTitle
, (LPSTR
)lpStartupInfo
->lpTitle
);
249 K32MbStrToWcStr(pTrue
, &wstrCommandLine
, &strCommandLine
, TRUE
);
250 K32MbStrToWcStr(pTrue
, &wstrApplicationName
, &strApplicationName
, TRUE
);
251 K32MbStrToWcStr(pTrue
, &wstrCurrentDirectory
, &strCurrentDirectory
, TRUE
);
252 K32MbStrToWcStr(pTrue
, &wstrReserved
, &strReserved
, TRUE
);
253 K32MbStrToWcStr(pTrue
, &wstrDesktop
, &strDesktop
, TRUE
);
254 K32MbStrToWcStr(pTrue
, &wstrTitle
, &strTitle
, TRUE
);
256 /* convert the startup information */
257 memcpy(&wsiStartupInfo
, lpStartupInfo
, sizeof(wsiStartupInfo
));
259 wsiStartupInfo
.lpReserved
= wstrReserved
.Buffer
;
260 wsiStartupInfo
.lpDesktop
= wstrDesktop
.Buffer
;
261 wsiStartupInfo
.lpTitle
= wstrTitle
.Buffer
;
263 DPRINT("wstrApplicationName %wZ\n", &wstrApplicationName
);
264 DPRINT("wstrCommandLine %wZ\n", &wstrCommandLine
);
265 DPRINT("wstrCurrentDirectory %wZ\n", &wstrCurrentDirectory
);
266 DPRINT("wstrReserved %wZ\n", &wstrReserved
);
267 DPRINT("wstrDesktop %wZ\n", &wstrDesktop
);
268 DPRINT("wstrTitle %wZ\n", &wstrTitle
);
270 DPRINT("wstrApplicationName.Buffer %p\n", wstrApplicationName
.Buffer
);
271 DPRINT("wstrCommandLine.Buffer %p\n", wstrCommandLine
.Buffer
);
272 DPRINT("wstrCurrentDirectory.Buffer %p\n", wstrCurrentDirectory
.Buffer
);
273 DPRINT("wstrReserved.Buffer %p\n", wstrReserved
.Buffer
);
274 DPRINT("wstrDesktop.Buffer %p\n", wstrDesktop
.Buffer
);
275 DPRINT("wstrTitle.Buffer %p\n", wstrTitle
.Buffer
);
277 DPRINT("sizeof(STARTUPINFOA) %lu\n", sizeof(STARTUPINFOA
));
278 DPRINT("sizeof(STARTUPINFOW) %lu\n", sizeof(STARTUPINFOW
));
280 /* call the Unicode function */
281 bRetVal
= CreateProcessW
283 wstrApplicationName
.Buffer
,
284 wstrCommandLine
.Buffer
,
289 dwCreationFlags
& CREATE_UNICODE_ENVIRONMENT
? lpEnvironment
: pwcEnv
,
290 wstrCurrentDirectory
.Buffer
,
295 RtlFreeUnicodeString(&wstrApplicationName
);
296 RtlFreeUnicodeString(&wstrCommandLine
);
297 RtlFreeUnicodeString(&wstrCurrentDirectory
);
298 RtlFreeUnicodeString(&wstrReserved
);
299 RtlFreeUnicodeString(&wstrDesktop
);
300 RtlFreeUnicodeString(&wstrTitle
);
302 RtlFreeHeap(GetProcessHeap(), 0, pwcEnv
);
307 static int _except_recursion_trap
= 0;
310 struct __EXCEPTION_RECORD
;
313 EXCEPTION_DISPOSITION
316 struct _EXCEPTION_RECORD
*ExceptionRecord
,
317 void * EstablisherFrame
,
318 struct _CONTEXT
*ContextRecord
,
319 void * DispatcherContext
)
321 DPRINT1("Process terminated abnormally due to unhandled exception\n");
323 if (3 < ++_except_recursion_trap
)
325 DPRINT1("_except_handler(...) appears to be recursing.\n");
326 DPRINT1("Process HALTED.\n");
332 if (/* FIXME: */ TRUE
) /* Not a service */
334 DPRINT(" calling ExitProcess(0) no, lets try ExitThread . . .\n");
335 /* ExitProcess(0); */
340 DPRINT(" calling ExitThread(0) . . .\n");
344 DPRINT1(" We should not get to here !!!\n");
345 /* We should not get to here */
346 return ExceptionContinueSearch
;
350 BaseProcessStart(LPTHREAD_START_ROUTINE lpStartAddress
,
355 DPRINT("BaseProcessStart(..) - setting up exception frame.\n");
357 __try1(_except_handler
)
359 uExitCode
= (lpStartAddress
)((PVOID
)lpParameter
);
364 DPRINT("BaseProcessStart(..) - cleaned up exception frame.\n");
366 ExitThread(uExitCode
);
370 HANDLE STDCALL KlCreateFirstThread
372 HANDLE ProcessHandle
,
373 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
374 PSECTION_IMAGE_INFORMATION Sii
,
375 LPTHREAD_START_ROUTINE lpStartAddress
,
376 DWORD dwCreationFlags
,
380 OBJECT_ATTRIBUTES oaThreadAttribs
;
381 CLIENT_ID cidClientId
;
382 PVOID pTrueStartAddress
;
386 /* convert the thread attributes */
387 RtlRosR32AttribsToNativeAttribs(&oaThreadAttribs
, lpThreadAttributes
);
390 if(Sii
->Subsystem
!= IMAGE_SUBSYSTEM_NATIVE
)
391 pTrueStartAddress
= (PVOID
)BaseProcessStart
;
394 pTrueStartAddress
= (PVOID
)RtlBaseProcessStartRoutine
;
398 "RtlRosCreateUserThreadVa\n"
400 " ProcessHandle %p,\n"
401 " ObjectAttributes %p,\n"
402 " CreateSuspended %d,\n"
403 " StackZeroBits %d,\n"
404 " StackReserve %lu,\n"
405 " StackCommit %lu,\n"
406 " StartAddress %p,\n"
407 " ThreadHandle %p,\n"
409 " ParameterCount %u,\n"
410 " Parameters[0] %p,\n"
411 " Parameters[1] %p\n"
415 dwCreationFlags
& CREATE_SUSPENDED
,
427 /* create the first thread */
428 nErrCode
= RtlRosCreateUserThreadVa
432 dwCreationFlags
& CREATE_SUSPENDED
,
434 &(Sii
->StackReserve
),
440 (ULONG_PTR
)lpStartAddress
,
445 if(!NT_SUCCESS(nErrCode
))
447 SetLastErrorByStatus(nErrCode
);
456 "ClientId.UniqueThread %p\n",
460 cidClientId
.UniqueThread
464 if(lpThreadId
) *lpThreadId
= (DWORD
)cidClientId
.UniqueThread
;
468 HANDLE
KlMapFile(LPCWSTR lpApplicationName
)
471 IO_STATUS_BLOCK IoStatusBlock
;
472 UNICODE_STRING ApplicationNameString
;
473 OBJECT_ATTRIBUTES ObjectAttributes
;
474 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
481 * Find the application name
484 if (!RtlDosPathNameToNtPathName_U ((LPWSTR
)lpApplicationName
,
485 &ApplicationNameString
,
490 DPRINT("ApplicationName %S\n",ApplicationNameString
.Buffer
);
492 InitializeObjectAttributes(&ObjectAttributes
,
493 &ApplicationNameString
,
494 OBJ_CASE_INSENSITIVE
,
499 * Try to open the executable
502 Status
= NtOpenFile(&hFile
,
503 SYNCHRONIZE
|FILE_EXECUTE
|FILE_READ_DATA
,
506 FILE_SHARE_DELETE
|FILE_SHARE_READ
,
507 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
509 RtlFreeUnicodeString (&ApplicationNameString
);
511 if (!NT_SUCCESS(Status
))
513 DPRINT("Failed to open file\n");
514 SetLastErrorByStatus (Status
);
518 Status
= NtCreateSection(&hSection
,
527 if (!NT_SUCCESS(Status
))
529 DPRINT("Failed to create section\n");
530 SetLastErrorByStatus (Status
);
537 static NTSTATUS KlInitPeb
539 HANDLE ProcessHandle
,
540 PRTL_USER_PROCESS_PARAMETERS Ppb
,
541 PVOID
* ImageBaseAddress
549 PVOID ParentEnv
= NULL
;
552 ULONG EnvSize
= 0, EnvSize1
= 0;
554 /* create the Environment */
555 if (Ppb
->Environment
!= NULL
)
557 ParentEnv
= Ppb
->Environment
;
564 EnvSize
= (PVOID
)ptr
- ParentEnv
;
566 else if (NtCurrentPeb()->ProcessParameters
->Environment
!= NULL
)
568 MEMORY_BASIC_INFORMATION MemInfo
;
569 ParentEnv
= NtCurrentPeb()->ProcessParameters
->Environment
;
571 Status
= NtQueryVirtualMemory (NtCurrentProcess (),
573 MemoryBasicInformation
,
575 sizeof(MEMORY_BASIC_INFORMATION
),
577 if (!NT_SUCCESS(Status
))
581 EnvSize
= MemInfo
.RegionSize
;
583 DPRINT("EnvironmentSize %ld\n", EnvSize
);
585 /* allocate and initialize new environment block */
589 Status
= NtAllocateVirtualMemory(ProcessHandle
,
593 MEM_RESERVE
| MEM_COMMIT
,
595 if (!NT_SUCCESS(Status
))
600 NtWriteVirtualMemory(ProcessHandle
,
609 PpbSize
= Ppb
->AllocationSize
;
610 Status
= NtAllocateVirtualMemory(ProcessHandle
,
614 MEM_RESERVE
| MEM_COMMIT
,
616 if (!NT_SUCCESS(Status
))
621 //DPRINT("Ppb->MaximumLength %x\n", Ppb->MaximumLength);
622 NtWriteVirtualMemory(ProcessHandle
,
628 /* write pointer to environment */
629 Offset
= FIELD_OFFSET(RTL_USER_PROCESS_PARAMETERS
, Environment
);
630 NtWriteVirtualMemory(ProcessHandle
,
631 (PVOID
)(PpbBase
+ Offset
),
636 /* write pointer to process parameter block */
637 Offset
= FIELD_OFFSET(PEB
, ProcessParameters
);
638 NtWriteVirtualMemory(ProcessHandle
,
639 (PVOID
)(PEB_BASE
+ Offset
),
644 /* Read image base address. */
645 Offset
= FIELD_OFFSET(PEB
, ImageBaseAddress
);
646 NtReadVirtualMemory(ProcessHandle
,
647 (PVOID
)(PEB_BASE
+ Offset
),
652 return(STATUS_SUCCESS
);
659 LPCWSTR lpApplicationName
,
660 LPWSTR lpCommandLine
,
661 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
662 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
663 WINBOOL bInheritHandles
,
664 DWORD dwCreationFlags
,
665 LPVOID lpEnvironment
,
666 LPCWSTR lpCurrentDirectory
,
667 LPSTARTUPINFOW lpStartupInfo
,
668 LPPROCESS_INFORMATION lpProcessInformation
671 HANDLE hSection
, hProcess
, hThread
;
673 LPTHREAD_START_ROUTINE lpStartAddress
= NULL
;
674 WCHAR ImagePathName
[256];
675 UNICODE_STRING ImagePathName_U
;
676 PROCESS_BASIC_INFORMATION ProcessBasicInfo
;
678 PRTL_USER_PROCESS_PARAMETERS Ppb
;
679 UNICODE_STRING CommandLine_U
;
680 CSRSS_API_REQUEST CsrRequest
;
681 CSRSS_API_REPLY CsrReply
;
682 CHAR ImageFileName
[8];
685 ANSI_STRING ProcedureName
;
686 UNICODE_STRING CurrentDirectory_U
;
687 SECTION_IMAGE_INFORMATION Sii
;
688 WCHAR TempCurrentDirectoryW
[256];
689 WCHAR TempApplicationNameW
[256];
690 WCHAR TempCommandLineNameW
[256];
691 UNICODE_STRING RuntimeInfo_U
;
692 PVOID ImageBaseAddress
;
694 DPRINT("CreateProcessW(lpApplicationName '%S', lpCommandLine '%S')\n",
695 lpApplicationName
, lpCommandLine
);
697 if (lpApplicationName
!= NULL
&& lpApplicationName
[0] != 0)
699 wcscpy (TempApplicationNameW
, lpApplicationName
);
700 i
= wcslen(TempApplicationNameW
);
701 if (TempApplicationNameW
[i
- 1] == L
'.')
703 TempApplicationNameW
[i
- 1] = 0;
707 s
= max(wcsrchr(TempApplicationNameW
, L
'\\'), wcsrchr(TempApplicationNameW
, L
'/'));
710 s
= TempApplicationNameW
;
716 e
= wcsrchr(s
, L
'.');
720 e
= wcsrchr(s
, L
'.');
724 else if (lpCommandLine
!= NULL
&& lpCommandLine
[0] != 0)
726 if (lpCommandLine
[0] == L
'"')
728 wcscpy(TempApplicationNameW
, lpCommandLine
+ 1);
729 s
= wcschr(TempApplicationNameW
, L
'"');
738 wcscpy(TempApplicationNameW
, lpCommandLine
);
739 s
= wcschr(TempApplicationNameW
, L
' ');
745 s
= max(wcsrchr(TempApplicationNameW
, L
'\\'), wcsrchr(TempApplicationNameW
, L
'/'));
748 s
= TempApplicationNameW
;
750 s
= wcsrchr(s
, L
'.');
752 wcscat(TempApplicationNameW
, L
".exe");
759 DPRINT("CreateProcessW(lpApplicationName '%S', lpCommandLine '%S')\n",
760 lpApplicationName
, lpCommandLine
);
762 if (!SearchPathW(NULL
, TempApplicationNameW
, NULL
, sizeof(ImagePathName
)/sizeof(WCHAR
), ImagePathName
, &s
))
767 e
= wcsrchr(s
, L
'.');
768 if (e
!= NULL
&& (!_wcsicmp(e
, L
".bat") || !_wcsicmp(e
, L
".cmd")))
770 // the command is a batch file
771 if (lpApplicationName
!= NULL
&& lpApplicationName
[0])
773 // FIXME: use COMSPEC for the command interpreter
774 wcscpy(TempCommandLineNameW
, L
"cmd /c ");
775 wcscat(TempCommandLineNameW
, lpApplicationName
);
776 lpCommandLine
= TempCommandLineNameW
;
777 wcscpy(TempApplicationNameW
, L
"cmd.exe");
778 if (!SearchPathW(NULL
, TempApplicationNameW
, NULL
, sizeof(ImagePathName
)/sizeof(WCHAR
), ImagePathName
, &s
))
790 * Store the image file name for the process
797 for (i
= 0; i
< 8; i
++)
799 ImageFileName
[i
] = (CHAR
)(s
[i
]);
807 * Process the application name and command line
809 RtlInitUnicodeString(&ImagePathName_U
, ImagePathName
);
810 RtlInitUnicodeString(&CommandLine_U
, lpCommandLine
);
812 DPRINT("ImagePathName_U %S\n", ImagePathName_U
.Buffer
);
813 DPRINT("CommandLine_U %S\n", CommandLine_U
.Buffer
);
815 /* Initialize the current directory string */
816 if (lpCurrentDirectory
!= NULL
)
818 RtlInitUnicodeString(&CurrentDirectory_U
,
823 GetCurrentDirectoryW(256, TempCurrentDirectoryW
);
824 RtlInitUnicodeString(&CurrentDirectory_U
,
825 TempCurrentDirectoryW
);
829 * Create a section for the executable
832 hSection
= KlMapFile (ImagePathName
);
833 if (hSection
== NULL
)
835 /////////////////////////////////////////
837 * Inspect the image to determine executable flavour
839 IO_STATUS_BLOCK IoStatusBlock
;
840 UNICODE_STRING ApplicationNameString
;
841 OBJECT_ATTRIBUTES ObjectAttributes
;
842 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
843 IMAGE_DOS_HEADER DosHeader
;
844 IO_STATUS_BLOCK Iosb
;
845 LARGE_INTEGER Offset
;
848 DPRINT("Inspecting Image Header for image type id\n");
850 // Find the application name
851 if (!RtlDosPathNameToNtPathName_U((LPWSTR
)lpApplicationName
,
852 &ApplicationNameString
, NULL
, NULL
)) {
855 DPRINT("ApplicationName %S\n",ApplicationNameString
.Buffer
);
857 InitializeObjectAttributes(&ObjectAttributes
,
858 &ApplicationNameString
,
859 OBJ_CASE_INSENSITIVE
,
863 // Try to open the executable
864 Status
= NtOpenFile(&hFile
,
865 SYNCHRONIZE
|FILE_EXECUTE
|FILE_READ_DATA
,
868 FILE_SHARE_DELETE
|FILE_SHARE_READ
,
869 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
871 RtlFreeUnicodeString(&ApplicationNameString
);
873 if (!NT_SUCCESS(Status
)) {
874 DPRINT("Failed to open file\n");
875 SetLastErrorByStatus(Status
);
879 // Read the dos header
881 Status
= ZwReadFile(hFile
,
891 if (!NT_SUCCESS(Status
)) {
892 DPRINT("Failed to read from file\n");
893 SetLastErrorByStatus(Status
);
896 if (Iosb
.Information
!= sizeof(DosHeader
)) {
897 DPRINT("Failed to read dos header from file\n");
898 SetLastErrorByStatus(STATUS_INVALID_IMAGE_FORMAT
);
902 // Check the DOS signature
903 if (DosHeader
.e_magic
!= IMAGE_DOS_SIGNATURE
) {
904 DPRINT("Failed dos magic check\n");
905 SetLastErrorByStatus(STATUS_INVALID_IMAGE_FORMAT
);
910 DPRINT("Launching VDM...\n");
911 return CreateProcessW(L
"ntvdm.exe",
912 (LPWSTR
)lpApplicationName
,
920 lpProcessInformation
);
922 /////////////////////////////////////////
924 * Create a new process
926 Status
= NtCreateProcess(&hProcess
,
936 if (lpStartupInfo
->lpReserved2
)
938 ULONG i
, Count
= *(ULONG
*)lpStartupInfo
->lpReserved2
;
941 PRTL_USER_PROCESS_PARAMETERS CurrPpb
= NtCurrentPeb()->ProcessParameters
;
945 * ROUND_UP(xxx,2) + 2 is a dirty hack. RtlCreateProcessParameters assumes that
946 * the runtimeinfo is a unicode string and use RtlCopyUnicodeString for duplication.
947 * If is possible that this function overwrite the last information in runtimeinfo
948 * with the null terminator for the unicode string.
950 RuntimeInfo_U
.Length
= RuntimeInfo_U
.MaximumLength
= ROUND_UP(lpStartupInfo
->cbReserved2
, 2) + 2;
951 RuntimeInfo_U
.Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, RuntimeInfo_U
.Length
);
952 memcpy(RuntimeInfo_U
.Buffer
, lpStartupInfo
->lpReserved2
, lpStartupInfo
->cbReserved2
);
959 RtlCreateProcessParameters(&Ppb
,
962 lpCurrentDirectory
? &CurrentDirectory_U
: NULL
,
968 lpStartupInfo
&& lpStartupInfo
->lpReserved2
? &RuntimeInfo_U
: NULL
);
970 if (lpStartupInfo
&& lpStartupInfo
->lpReserved2
)
971 RtlFreeHeap(GetProcessHeap(), 0, RuntimeInfo_U
.Buffer
);
975 * Translate some handles for the new process
977 if (Ppb
->CurrentDirectoryHandle
)
979 Status
= NtDuplicateObject (NtCurrentProcess(),
980 Ppb
->CurrentDirectoryHandle
,
982 &Ppb
->CurrentDirectoryHandle
,
985 DUPLICATE_SAME_ACCESS
);
990 Status
= NtDuplicateObject (NtCurrentProcess(),
996 DUPLICATE_SAME_ACCESS
);
1000 * Get some information about the executable
1002 Status
= ZwQuerySection(hSection
,
1003 SectionImageInformation
,
1013 * Get some information about the process
1015 NtQueryInformationProcess(hProcess
,
1016 ProcessBasicInformation
,
1018 sizeof(ProcessBasicInfo
),
1020 DPRINT("ProcessBasicInfo.UniqueProcessId %d\n",
1021 ProcessBasicInfo
.UniqueProcessId
);
1022 lpProcessInformation
->dwProcessId
= ProcessBasicInfo
.UniqueProcessId
;
1025 * Tell the csrss server we are creating a new process
1027 CsrRequest
.Type
= CSRSS_CREATE_PROCESS
;
1028 CsrRequest
.Data
.CreateProcessRequest
.NewProcessId
=
1029 ProcessBasicInfo
.UniqueProcessId
;
1030 if (Sii
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_GUI
)
1032 /* Do not create a console for GUI applications */
1033 dwCreationFlags
&= ~CREATE_NEW_CONSOLE
;
1034 dwCreationFlags
|= DETACHED_PROCESS
;
1036 CsrRequest
.Data
.CreateProcessRequest
.Flags
= dwCreationFlags
;
1037 Status
= CsrClientCallServer(&CsrRequest
,
1039 sizeof(CSRSS_API_REQUEST
),
1040 sizeof(CSRSS_API_REPLY
));
1041 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(CsrReply
.Status
))
1043 DbgPrint("Failed to tell csrss about new process. Expect trouble.\n");
1046 // Set the child console handles
1047 Ppb
->hStdInput
= NtCurrentPeb()->ProcessParameters
->hStdInput
;
1048 Ppb
->hStdOutput
= NtCurrentPeb()->ProcessParameters
->hStdOutput
;
1049 Ppb
->hStdError
= NtCurrentPeb()->ProcessParameters
->hStdError
;
1051 if (lpStartupInfo
&& (lpStartupInfo
->dwFlags
& STARTF_USESTDHANDLES
))
1053 if (lpStartupInfo
->hStdInput
)
1054 Ppb
->hStdInput
= lpStartupInfo
->hStdInput
;
1055 if (lpStartupInfo
->hStdOutput
)
1056 Ppb
->hStdOutput
= lpStartupInfo
->hStdOutput
;
1057 if (lpStartupInfo
->hStdError
)
1058 Ppb
->hStdError
= lpStartupInfo
->hStdError
;
1061 if (IsConsoleHandle(Ppb
->hStdInput
))
1063 Ppb
->hStdInput
= CsrReply
.Data
.CreateProcessReply
.InputHandle
;
1067 DPRINT("Duplicate input handle\n");
1068 Status
= NtDuplicateObject (NtCurrentProcess(),
1074 DUPLICATE_SAME_ACCESS
);
1075 if(!NT_SUCCESS(Status
))
1077 DPRINT("NtDuplicateObject failed, status %x\n", Status
);
1081 if (IsConsoleHandle(Ppb
->hStdOutput
))
1083 Ppb
->hStdOutput
= CsrReply
.Data
.CreateProcessReply
.OutputHandle
;
1087 DPRINT("Duplicate output handle\n");
1088 Status
= NtDuplicateObject (NtCurrentProcess(),
1094 DUPLICATE_SAME_ACCESS
);
1095 if(!NT_SUCCESS(Status
))
1097 DPRINT("NtDuplicateObject failed, status %x\n", Status
);
1100 if (IsConsoleHandle(Ppb
->hStdError
))
1102 CsrRequest
.Type
= CSRSS_DUPLICATE_HANDLE
;
1103 CsrRequest
.Data
.DuplicateHandleRequest
.ProcessId
= ProcessBasicInfo
.UniqueProcessId
;
1104 CsrRequest
.Data
.DuplicateHandleRequest
.Handle
= CsrReply
.Data
.CreateProcessReply
.OutputHandle
;
1105 Status
= CsrClientCallServer(&CsrRequest
,
1107 sizeof(CSRSS_API_REQUEST
),
1108 sizeof(CSRSS_API_REPLY
));
1109 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(CsrReply
.Status
))
1111 Ppb
->hStdError
= INVALID_HANDLE_VALUE
;
1115 Ppb
->hStdError
= CsrReply
.Data
.DuplicateHandleReply
.Handle
;
1120 DPRINT("Duplicate error handle\n");
1121 Status
= NtDuplicateObject (NtCurrentProcess(),
1127 DUPLICATE_SAME_ACCESS
);
1128 if(!NT_SUCCESS(Status
))
1130 DPRINT("NtDuplicateObject failed, status %x\n", Status
);
1135 * Initialize some other fields in the PPB
1139 Ppb
->dwFlags
= lpStartupInfo
->dwFlags
;
1140 if (Ppb
->dwFlags
& STARTF_USESHOWWINDOW
)
1142 Ppb
->wShowWindow
= lpStartupInfo
->wShowWindow
;
1146 Ppb
->wShowWindow
= SW_SHOWDEFAULT
;
1148 Ppb
->dwX
= lpStartupInfo
->dwX
;
1149 Ppb
->dwY
= lpStartupInfo
->dwY
;
1150 Ppb
->dwXSize
= lpStartupInfo
->dwXSize
;
1151 Ppb
->dwYSize
= lpStartupInfo
->dwYSize
;
1152 Ppb
->dwFillAttribute
= lpStartupInfo
->dwFillAttribute
;
1160 * Create Process Environment Block
1162 DPRINT("Creating peb\n");
1164 KlInitPeb(hProcess
, Ppb
, &ImageBaseAddress
);
1166 RtlDestroyProcessParameters (Ppb
);
1168 Status
= NtSetInformationProcess(hProcess
,
1169 ProcessImageFileName
,
1173 * Create the thread for the kernel
1175 DPRINT("Creating thread for process (EntryPoint = 0x%.08x)\n",
1176 ImageBaseAddress
+ (ULONG
)Sii
.EntryPoint
);
1177 hThread
= KlCreateFirstThread(hProcess
,
1180 ImageBaseAddress
+ (ULONG
)Sii
.EntryPoint
,
1182 &lpProcessInformation
->dwThreadId
);
1183 if (hThread
== INVALID_HANDLE_VALUE
)
1188 lpProcessInformation
->hProcess
= hProcess
;
1189 lpProcessInformation
->hThread
= hThread
;