1 /* $Id: create.c,v 1.71 2003/10/06 17:53:55 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
);
331 if (3 < ++_except_recursion_trap
)
333 DPRINT1("_except_handler(...) appears to be recursing.\n");
334 DPRINT1("Process HALTED.\n");
340 if (/* FIXME: */ TRUE
) /* Not a service */
342 DPRINT(" calling ExitProcess(0) no, lets try ExitThread . . .\n");
343 /* ExitProcess(0); */
348 DPRINT(" calling ExitThread(0) . . .\n");
352 DPRINT1(" We should not get to here !!!\n");
353 /* We should not get to here */
354 return ExceptionContinueSearch
;
358 BaseProcessStart(LPTHREAD_START_ROUTINE lpStartAddress
,
363 DPRINT("BaseProcessStart(..) - setting up exception frame.\n");
365 __try1(_except_handler
)
367 uExitCode
= (lpStartAddress
)((PVOID
)lpParameter
);
372 DPRINT("BaseProcessStart(..) - cleaned up exception frame.\n");
374 ExitThread(uExitCode
);
378 HANDLE STDCALL KlCreateFirstThread
380 HANDLE ProcessHandle
,
381 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
382 PSECTION_IMAGE_INFORMATION Sii
,
383 LPTHREAD_START_ROUTINE lpStartAddress
,
384 DWORD dwCreationFlags
,
388 OBJECT_ATTRIBUTES oaThreadAttribs
;
389 CLIENT_ID cidClientId
;
390 PVOID pTrueStartAddress
;
394 /* convert the thread attributes */
395 RtlRosR32AttribsToNativeAttribs(&oaThreadAttribs
, lpThreadAttributes
);
398 if(Sii
->Subsystem
!= IMAGE_SUBSYSTEM_NATIVE
)
399 pTrueStartAddress
= (PVOID
)BaseProcessStart
;
402 pTrueStartAddress
= (PVOID
)RtlBaseProcessStartRoutine
;
406 "RtlRosCreateUserThreadVa\n"
408 " ProcessHandle %p,\n"
409 " ObjectAttributes %p,\n"
410 " CreateSuspended %d,\n"
411 " StackZeroBits %d,\n"
412 " StackReserve %lu,\n"
413 " StackCommit %lu,\n"
414 " StartAddress %p,\n"
415 " ThreadHandle %p,\n"
417 " ParameterCount %u,\n"
418 " Parameters[0] %p,\n"
419 " Parameters[1] %p\n"
423 dwCreationFlags
& CREATE_SUSPENDED
,
435 /* create the first thread */
436 nErrCode
= RtlRosCreateUserThreadVa
440 dwCreationFlags
& CREATE_SUSPENDED
,
442 &(Sii
->StackReserve
),
448 (ULONG_PTR
)lpStartAddress
,
453 if(!NT_SUCCESS(nErrCode
))
455 SetLastErrorByStatus(nErrCode
);
464 "ClientId.UniqueThread %p\n",
468 cidClientId
.UniqueThread
472 if(lpThreadId
) *lpThreadId
= (DWORD
)cidClientId
.UniqueThread
;
476 HANDLE
KlMapFile(LPCWSTR lpApplicationName
)
479 IO_STATUS_BLOCK IoStatusBlock
;
480 UNICODE_STRING ApplicationNameString
;
481 OBJECT_ATTRIBUTES ObjectAttributes
;
482 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
489 * Find the application name
492 if (!RtlDosPathNameToNtPathName_U ((LPWSTR
)lpApplicationName
,
493 &ApplicationNameString
,
498 DPRINT("ApplicationName %S\n",ApplicationNameString
.Buffer
);
500 InitializeObjectAttributes(&ObjectAttributes
,
501 &ApplicationNameString
,
502 OBJ_CASE_INSENSITIVE
,
507 * Try to open the executable
510 Status
= NtOpenFile(&hFile
,
511 SYNCHRONIZE
|FILE_EXECUTE
|FILE_READ_DATA
,
514 FILE_SHARE_DELETE
|FILE_SHARE_READ
,
515 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
517 RtlFreeUnicodeString (&ApplicationNameString
);
519 if (!NT_SUCCESS(Status
))
521 DPRINT("Failed to open file\n");
522 SetLastErrorByStatus (Status
);
526 Status
= NtCreateSection(&hSection
,
535 if (!NT_SUCCESS(Status
))
537 DPRINT("Failed to create section\n");
538 SetLastErrorByStatus (Status
);
545 static NTSTATUS KlInitPeb
547 HANDLE ProcessHandle
,
548 PRTL_USER_PROCESS_PARAMETERS Ppb
,
549 PVOID
* ImageBaseAddress
557 PVOID ParentEnv
= NULL
;
560 ULONG EnvSize
= 0, EnvSize1
= 0;
562 /* create the Environment */
563 if (Ppb
->Environment
!= NULL
)
565 ParentEnv
= Ppb
->Environment
;
572 EnvSize
= (PVOID
)ptr
- ParentEnv
;
574 else if (NtCurrentPeb()->ProcessParameters
->Environment
!= NULL
)
576 MEMORY_BASIC_INFORMATION MemInfo
;
577 ParentEnv
= NtCurrentPeb()->ProcessParameters
->Environment
;
579 Status
= NtQueryVirtualMemory (NtCurrentProcess (),
581 MemoryBasicInformation
,
583 sizeof(MEMORY_BASIC_INFORMATION
),
585 if (!NT_SUCCESS(Status
))
589 EnvSize
= MemInfo
.RegionSize
;
591 DPRINT("EnvironmentSize %ld\n", EnvSize
);
593 /* allocate and initialize new environment block */
597 Status
= NtAllocateVirtualMemory(ProcessHandle
,
601 MEM_RESERVE
| MEM_COMMIT
,
603 if (!NT_SUCCESS(Status
))
608 NtWriteVirtualMemory(ProcessHandle
,
617 PpbSize
= Ppb
->AllocationSize
;
618 Status
= NtAllocateVirtualMemory(ProcessHandle
,
622 MEM_RESERVE
| MEM_COMMIT
,
624 if (!NT_SUCCESS(Status
))
629 //DPRINT("Ppb->MaximumLength %x\n", Ppb->MaximumLength);
630 NtWriteVirtualMemory(ProcessHandle
,
636 /* write pointer to environment */
637 Offset
= FIELD_OFFSET(RTL_USER_PROCESS_PARAMETERS
, Environment
);
638 NtWriteVirtualMemory(ProcessHandle
,
639 (PVOID
)(PpbBase
+ Offset
),
644 /* write pointer to process parameter block */
645 Offset
= FIELD_OFFSET(PEB
, ProcessParameters
);
646 NtWriteVirtualMemory(ProcessHandle
,
647 (PVOID
)(PEB_BASE
+ Offset
),
652 /* Read image base address. */
653 Offset
= FIELD_OFFSET(PEB
, ImageBaseAddress
);
654 NtReadVirtualMemory(ProcessHandle
,
655 (PVOID
)(PEB_BASE
+ Offset
),
660 return(STATUS_SUCCESS
);
670 LPCWSTR lpApplicationName
,
671 LPWSTR lpCommandLine
,
672 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
673 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
674 WINBOOL bInheritHandles
,
675 DWORD dwCreationFlags
,
676 LPVOID lpEnvironment
,
677 LPCWSTR lpCurrentDirectory
,
678 LPSTARTUPINFOW lpStartupInfo
,
679 LPPROCESS_INFORMATION lpProcessInformation
682 HANDLE hSection
, hProcess
, hThread
;
684 WCHAR ImagePathName
[256];
685 UNICODE_STRING ImagePathName_U
;
686 PROCESS_BASIC_INFORMATION ProcessBasicInfo
;
688 PRTL_USER_PROCESS_PARAMETERS Ppb
;
689 UNICODE_STRING CommandLine_U
;
690 CSRSS_API_REQUEST CsrRequest
;
691 CSRSS_API_REPLY CsrReply
;
692 CHAR ImageFileName
[8];
695 UNICODE_STRING CurrentDirectory_U
;
696 SECTION_IMAGE_INFORMATION Sii
;
697 WCHAR TempCurrentDirectoryW
[256];
698 WCHAR TempApplicationNameW
[256];
699 WCHAR TempCommandLineNameW
[256];
700 UNICODE_STRING RuntimeInfo_U
;
701 PVOID ImageBaseAddress
;
703 DPRINT("CreateProcessW(lpApplicationName '%S', lpCommandLine '%S')\n",
704 lpApplicationName
, lpCommandLine
);
706 if (lpApplicationName
!= NULL
&& lpApplicationName
[0] != 0)
708 wcscpy (TempApplicationNameW
, lpApplicationName
);
709 i
= wcslen(TempApplicationNameW
);
710 if (TempApplicationNameW
[i
- 1] == L
'.')
712 TempApplicationNameW
[i
- 1] = 0;
716 s
= max(wcsrchr(TempApplicationNameW
, L
'\\'), wcsrchr(TempApplicationNameW
, L
'/'));
719 s
= TempApplicationNameW
;
725 e
= wcsrchr(s
, L
'.');
729 e
= wcsrchr(s
, L
'.');
733 else if (lpCommandLine
!= NULL
&& lpCommandLine
[0] != 0)
735 if (lpCommandLine
[0] == L
'"')
737 wcscpy(TempApplicationNameW
, lpCommandLine
+ 1);
738 s
= wcschr(TempApplicationNameW
, L
'"');
747 wcscpy(TempApplicationNameW
, lpCommandLine
);
748 s
= wcschr(TempApplicationNameW
, L
' ');
754 s
= max(wcsrchr(TempApplicationNameW
, L
'\\'), wcsrchr(TempApplicationNameW
, L
'/'));
757 s
= TempApplicationNameW
;
759 s
= wcsrchr(s
, L
'.');
761 wcscat(TempApplicationNameW
, L
".exe");
768 DPRINT("CreateProcessW(lpApplicationName '%S', lpCommandLine '%S')\n",
769 lpApplicationName
, lpCommandLine
);
771 if (!SearchPathW(NULL
, TempApplicationNameW
, NULL
, sizeof(ImagePathName
)/sizeof(WCHAR
), ImagePathName
, &s
))
776 e
= wcsrchr(s
, L
'.');
777 if (e
!= NULL
&& (!_wcsicmp(e
, L
".bat") || !_wcsicmp(e
, L
".cmd")))
779 // the command is a batch file
780 if (lpApplicationName
!= NULL
&& lpApplicationName
[0])
782 // FIXME: use COMSPEC for the command interpreter
783 wcscpy(TempCommandLineNameW
, L
"cmd /c ");
784 wcscat(TempCommandLineNameW
, lpApplicationName
);
785 lpCommandLine
= TempCommandLineNameW
;
786 wcscpy(TempApplicationNameW
, L
"cmd.exe");
787 if (!SearchPathW(NULL
, TempApplicationNameW
, NULL
, sizeof(ImagePathName
)/sizeof(WCHAR
), ImagePathName
, &s
))
799 * Store the image file name for the process
806 for (i
= 0; i
< 8; i
++)
808 ImageFileName
[i
] = (CHAR
)(s
[i
]);
816 * Process the application name and command line
818 RtlInitUnicodeString(&ImagePathName_U
, ImagePathName
);
819 RtlInitUnicodeString(&CommandLine_U
, lpCommandLine
);
821 DPRINT("ImagePathName_U %S\n", ImagePathName_U
.Buffer
);
822 DPRINT("CommandLine_U %S\n", CommandLine_U
.Buffer
);
824 /* Initialize the current directory string */
825 if (lpCurrentDirectory
!= NULL
)
827 RtlInitUnicodeString(&CurrentDirectory_U
,
832 GetCurrentDirectoryW(256, TempCurrentDirectoryW
);
833 RtlInitUnicodeString(&CurrentDirectory_U
,
834 TempCurrentDirectoryW
);
838 * Create a section for the executable
841 hSection
= KlMapFile (ImagePathName
);
842 if (hSection
== NULL
)
844 /////////////////////////////////////////
846 * Inspect the image to determine executable flavour
848 IO_STATUS_BLOCK IoStatusBlock
;
849 UNICODE_STRING ApplicationNameString
;
850 OBJECT_ATTRIBUTES ObjectAttributes
;
851 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
852 IMAGE_DOS_HEADER DosHeader
;
853 IO_STATUS_BLOCK Iosb
;
854 LARGE_INTEGER Offset
;
857 DPRINT("Inspecting Image Header for image type id\n");
859 // Find the application name
860 if (!RtlDosPathNameToNtPathName_U((LPWSTR
)lpApplicationName
,
861 &ApplicationNameString
, NULL
, NULL
)) {
864 DPRINT("ApplicationName %S\n",ApplicationNameString
.Buffer
);
866 InitializeObjectAttributes(&ObjectAttributes
,
867 &ApplicationNameString
,
868 OBJ_CASE_INSENSITIVE
,
872 // Try to open the executable
873 Status
= NtOpenFile(&hFile
,
874 SYNCHRONIZE
|FILE_EXECUTE
|FILE_READ_DATA
,
877 FILE_SHARE_DELETE
|FILE_SHARE_READ
,
878 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
880 RtlFreeUnicodeString(&ApplicationNameString
);
882 if (!NT_SUCCESS(Status
)) {
883 DPRINT("Failed to open file\n");
884 SetLastErrorByStatus(Status
);
888 // Read the dos header
890 Status
= ZwReadFile(hFile
,
900 if (!NT_SUCCESS(Status
)) {
901 DPRINT("Failed to read from file\n");
902 SetLastErrorByStatus(Status
);
905 if (Iosb
.Information
!= sizeof(DosHeader
)) {
906 DPRINT("Failed to read dos header from file\n");
907 SetLastErrorByStatus(STATUS_INVALID_IMAGE_FORMAT
);
911 // Check the DOS signature
912 if (DosHeader
.e_magic
!= IMAGE_DOS_SIGNATURE
) {
913 DPRINT("Failed dos magic check\n");
914 SetLastErrorByStatus(STATUS_INVALID_IMAGE_FORMAT
);
919 DPRINT("Launching VDM...\n");
920 return CreateProcessW(L
"ntvdm.exe",
921 (LPWSTR
)lpApplicationName
,
929 lpProcessInformation
);
931 /////////////////////////////////////////
933 * Create a new process
935 Status
= NtCreateProcess(&hProcess
,
945 if (lpStartupInfo
->lpReserved2
)
948 * ROUND_UP(xxx,2) + 2 is a dirty hack. RtlCreateProcessParameters assumes that
949 * the runtimeinfo is a unicode string and use RtlCopyUnicodeString for duplication.
950 * If is possible that this function overwrite the last information in runtimeinfo
951 * with the null terminator for the unicode string.
953 RuntimeInfo_U
.Length
= RuntimeInfo_U
.MaximumLength
= ROUND_UP(lpStartupInfo
->cbReserved2
, 2) + 2;
954 RuntimeInfo_U
.Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, RuntimeInfo_U
.Length
);
955 memcpy(RuntimeInfo_U
.Buffer
, lpStartupInfo
->lpReserved2
, lpStartupInfo
->cbReserved2
);
962 RtlCreateProcessParameters(&Ppb
,
965 lpCurrentDirectory
? &CurrentDirectory_U
: NULL
,
971 lpStartupInfo
&& lpStartupInfo
->lpReserved2
? &RuntimeInfo_U
: NULL
);
973 if (lpStartupInfo
&& lpStartupInfo
->lpReserved2
)
974 RtlFreeHeap(GetProcessHeap(), 0, RuntimeInfo_U
.Buffer
);
978 * Translate some handles for the new process
980 if (Ppb
->CurrentDirectoryHandle
)
982 Status
= NtDuplicateObject (NtCurrentProcess(),
983 Ppb
->CurrentDirectoryHandle
,
985 &Ppb
->CurrentDirectoryHandle
,
988 DUPLICATE_SAME_ACCESS
);
993 Status
= NtDuplicateObject (NtCurrentProcess(),
999 DUPLICATE_SAME_ACCESS
);
1003 * Get some information about the executable
1005 Status
= ZwQuerySection(hSection
,
1006 SectionImageInformation
,
1016 * Get some information about the process
1018 NtQueryInformationProcess(hProcess
,
1019 ProcessBasicInformation
,
1021 sizeof(ProcessBasicInfo
),
1023 DPRINT("ProcessBasicInfo.UniqueProcessId %d\n",
1024 ProcessBasicInfo
.UniqueProcessId
);
1025 lpProcessInformation
->dwProcessId
= ProcessBasicInfo
.UniqueProcessId
;
1028 * Tell the csrss server we are creating a new process
1030 CsrRequest
.Type
= CSRSS_CREATE_PROCESS
;
1031 CsrRequest
.Data
.CreateProcessRequest
.NewProcessId
=
1032 ProcessBasicInfo
.UniqueProcessId
;
1033 if (Sii
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_GUI
)
1035 /* Do not create a console for GUI applications */
1036 dwCreationFlags
&= ~CREATE_NEW_CONSOLE
;
1037 dwCreationFlags
|= DETACHED_PROCESS
;
1039 CsrRequest
.Data
.CreateProcessRequest
.Flags
= dwCreationFlags
;
1040 CsrRequest
.Data
.CreateProcessRequest
.CtrlDispatcher
= ConsoleControlDispatcher
;
1041 Status
= CsrClientCallServer(&CsrRequest
,
1043 sizeof(CSRSS_API_REQUEST
),
1044 sizeof(CSRSS_API_REPLY
));
1045 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(CsrReply
.Status
))
1047 DbgPrint("Failed to tell csrss about new process. Expect trouble.\n");
1050 // Set the child console handles
1051 Ppb
->hStdInput
= NtCurrentPeb()->ProcessParameters
->hStdInput
;
1052 Ppb
->hStdOutput
= NtCurrentPeb()->ProcessParameters
->hStdOutput
;
1053 Ppb
->hStdError
= NtCurrentPeb()->ProcessParameters
->hStdError
;
1055 if (lpStartupInfo
&& (lpStartupInfo
->dwFlags
& STARTF_USESTDHANDLES
))
1057 if (lpStartupInfo
->hStdInput
)
1058 Ppb
->hStdInput
= lpStartupInfo
->hStdInput
;
1059 if (lpStartupInfo
->hStdOutput
)
1060 Ppb
->hStdOutput
= lpStartupInfo
->hStdOutput
;
1061 if (lpStartupInfo
->hStdError
)
1062 Ppb
->hStdError
= lpStartupInfo
->hStdError
;
1065 if (IsConsoleHandle(Ppb
->hStdInput
))
1067 Ppb
->hStdInput
= CsrReply
.Data
.CreateProcessReply
.InputHandle
;
1071 DPRINT("Duplicate input handle\n");
1072 Status
= NtDuplicateObject (NtCurrentProcess(),
1078 DUPLICATE_SAME_ACCESS
);
1079 if(!NT_SUCCESS(Status
))
1081 DPRINT("NtDuplicateObject failed, status %x\n", Status
);
1085 if (IsConsoleHandle(Ppb
->hStdOutput
))
1087 Ppb
->hStdOutput
= CsrReply
.Data
.CreateProcessReply
.OutputHandle
;
1091 DPRINT("Duplicate output handle\n");
1092 Status
= NtDuplicateObject (NtCurrentProcess(),
1098 DUPLICATE_SAME_ACCESS
);
1099 if(!NT_SUCCESS(Status
))
1101 DPRINT("NtDuplicateObject failed, status %x\n", Status
);
1104 if (IsConsoleHandle(Ppb
->hStdError
))
1106 CsrRequest
.Type
= CSRSS_DUPLICATE_HANDLE
;
1107 CsrRequest
.Data
.DuplicateHandleRequest
.ProcessId
= ProcessBasicInfo
.UniqueProcessId
;
1108 CsrRequest
.Data
.DuplicateHandleRequest
.Handle
= CsrReply
.Data
.CreateProcessReply
.OutputHandle
;
1109 Status
= CsrClientCallServer(&CsrRequest
,
1111 sizeof(CSRSS_API_REQUEST
),
1112 sizeof(CSRSS_API_REPLY
));
1113 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(CsrReply
.Status
))
1115 Ppb
->hStdError
= INVALID_HANDLE_VALUE
;
1119 Ppb
->hStdError
= CsrReply
.Data
.DuplicateHandleReply
.Handle
;
1124 DPRINT("Duplicate error handle\n");
1125 Status
= NtDuplicateObject (NtCurrentProcess(),
1131 DUPLICATE_SAME_ACCESS
);
1132 if(!NT_SUCCESS(Status
))
1134 DPRINT("NtDuplicateObject failed, status %x\n", Status
);
1139 * Initialize some other fields in the PPB
1143 Ppb
->dwFlags
= lpStartupInfo
->dwFlags
;
1144 if (Ppb
->dwFlags
& STARTF_USESHOWWINDOW
)
1146 Ppb
->wShowWindow
= lpStartupInfo
->wShowWindow
;
1150 Ppb
->wShowWindow
= SW_SHOWDEFAULT
;
1152 Ppb
->dwX
= lpStartupInfo
->dwX
;
1153 Ppb
->dwY
= lpStartupInfo
->dwY
;
1154 Ppb
->dwXSize
= lpStartupInfo
->dwXSize
;
1155 Ppb
->dwYSize
= lpStartupInfo
->dwYSize
;
1156 Ppb
->dwFillAttribute
= lpStartupInfo
->dwFillAttribute
;
1164 * Create Process Environment Block
1166 DPRINT("Creating peb\n");
1168 KlInitPeb(hProcess
, Ppb
, &ImageBaseAddress
);
1170 RtlDestroyProcessParameters (Ppb
);
1172 Status
= NtSetInformationProcess(hProcess
,
1173 ProcessImageFileName
,
1177 * Create the thread for the kernel
1179 DPRINT("Creating thread for process (EntryPoint = 0x%.08x)\n",
1180 ImageBaseAddress
+ (ULONG
)Sii
.EntryPoint
);
1181 hThread
= KlCreateFirstThread(hProcess
,
1184 ImageBaseAddress
+ (ULONG
)Sii
.EntryPoint
,
1186 &lpProcessInformation
->dwThreadId
);
1187 if (hThread
== INVALID_HANDLE_VALUE
)
1192 lpProcessInformation
->hProcess
= hProcess
;
1193 lpProcessInformation
->hThread
= hThread
;