1 /* $Id: create.c,v 1.76 2003/12/27 23:55:02 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 ((PVOID
)Frame
[1] != NULL
&& (PULONG
)Frame
[1] != (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
);
677 /*************************************************************************
680 * Helper for CreateProcessW: retrieve the file name to load from the
681 * app name and command line. Store the file name in buffer, and
682 * return a possibly modified command line.
684 static LPWSTR FASTCALL
685 GetFileName(LPCWSTR AppName
, LPWSTR CmdLine
, LPWSTR Buffer
,
688 WCHAR
*Name
, *Pos
, *Ret
= NULL
;
691 /* if we have an app name, everything is easy */
695 /* use the unmodified app name as file name */
696 wcsncpy(Buffer
, AppName
, BufLen
);
698 if (NULL
== Ret
|| L
'\0' == CmdLine
[0])
700 /* no command-line, create one */
701 Ret
= RtlAllocateHeap(GetProcessHeap(), 0, (wcslen(AppName
) + 3) * sizeof(WCHAR
));
705 wcscpy(Ret
+ 1, AppName
);
714 SetLastError(ERROR_INVALID_PARAMETER
);
718 /* first check for a quoted file name */
719 if (L
'"' == CmdLine
[0] && NULL
!= (p
= wcschr(CmdLine
+ 1, L
'"')))
721 int Len
= p
- CmdLine
- 1;
722 /* extract the quoted portion as file name */
723 Name
= RtlAllocateHeap(GetProcessHeap(), 0, (Len
+ 1) * sizeof(WCHAR
));
728 memcpy(Name
, CmdLine
+ 1, Len
* sizeof(WCHAR
));
731 if (SearchPathW(NULL
, Name
, L
".exe", BufLen
, Buffer
, NULL
))
733 Ret
= CmdLine
; /* no change necessary */
736 RtlFreeHeap(GetProcessHeap(), 0, Name
);
740 /* now try the command-line word by word */
741 Name
= RtlAllocateHeap(GetProcessHeap(), 0, (wcslen(CmdLine
) + 1) * sizeof(WCHAR
));
755 while (L
'\0' != *p
&& L
' ' != *p
);
757 if (SearchPathW(NULL
, Name
, L
".exe", BufLen
, Buffer
, NULL
))
764 if (NULL
== Ret
|| NULL
== wcschr(Name
, L
' '))
766 RtlFreeHeap(GetProcessHeap(), 0, Name
); /* no change necessary */
770 /* now build a new command-line with quotes */
771 Ret
= RtlAllocateHeap(GetProcessHeap(), 0, (wcslen(CmdLine
) + 3) * sizeof(WCHAR
));
774 RtlFreeHeap(GetProcessHeap(), 0, Name
); /* no change necessary */
778 wcscpy(Ret
+ 1, Name
);
782 RtlFreeHeap(GetProcessHeap(), 0, Name
);
793 LPCWSTR lpApplicationName
,
794 LPWSTR lpCommandLine
,
795 LPSECURITY_ATTRIBUTES lpProcessAttributes
,
796 LPSECURITY_ATTRIBUTES lpThreadAttributes
,
797 WINBOOL bInheritHandles
,
798 DWORD dwCreationFlags
,
799 LPVOID lpEnvironment
,
800 LPCWSTR lpCurrentDirectory
,
801 LPSTARTUPINFOW lpStartupInfo
,
802 LPPROCESS_INFORMATION lpProcessInformation
805 HANDLE hSection
, hProcess
, hThread
;
807 WCHAR ImagePathName
[256];
808 UNICODE_STRING ImagePathName_U
;
809 PROCESS_BASIC_INFORMATION ProcessBasicInfo
;
811 PRTL_USER_PROCESS_PARAMETERS Ppb
;
812 UNICODE_STRING CommandLine_U
;
813 CSRSS_API_REQUEST CsrRequest
;
814 CSRSS_API_REPLY CsrReply
;
815 CHAR ImageFileName
[8];
818 UNICODE_STRING CurrentDirectory_U
;
819 SECTION_IMAGE_INFORMATION Sii
;
820 WCHAR TempCurrentDirectoryW
[256];
821 WCHAR TempApplicationNameW
[256];
822 WCHAR TempCommandLineNameW
[256];
823 UNICODE_STRING RuntimeInfo_U
;
824 PVOID ImageBaseAddress
;
825 BOOL InputSet
, OutputSet
, ErrorSet
;
826 BOOL InputDup
, OutputDup
, ErrorDup
;
827 WCHAR Name
[MAX_PATH
];
830 DPRINT("CreateProcessW(lpApplicationName '%S', lpCommandLine '%S')\n",
831 lpApplicationName
, lpCommandLine
);
833 TidyCmdLine
= GetFileName(lpApplicationName
, lpCommandLine
, Name
,
834 sizeof(Name
) / sizeof(WCHAR
));
835 if (NULL
== TidyCmdLine
)
840 if (lpApplicationName
!= NULL
&& lpApplicationName
[0] != 0)
842 wcscpy (TempApplicationNameW
, lpApplicationName
);
843 i
= wcslen(TempApplicationNameW
);
844 if (TempApplicationNameW
[i
- 1] == L
'.')
846 TempApplicationNameW
[i
- 1] = 0;
850 s
= max(wcsrchr(TempApplicationNameW
, L
'\\'), wcsrchr(TempApplicationNameW
, L
'/'));
853 s
= TempApplicationNameW
;
859 e
= wcsrchr(s
, L
'.');
863 e
= wcsrchr(s
, L
'.');
867 else if (L
'"' == TidyCmdLine
[0])
869 wcscpy(TempApplicationNameW
, TidyCmdLine
+ 1);
870 s
= wcschr(TempApplicationNameW
, L
'"');
879 wcscpy(TempApplicationNameW
, TidyCmdLine
);
880 s
= wcschr(TempApplicationNameW
, L
' ');
886 s
= max(wcsrchr(TempApplicationNameW
, L
'\\'), wcsrchr(TempApplicationNameW
, L
'/'));
889 s
= TempApplicationNameW
;
891 s
= wcsrchr(s
, L
'.');
894 wcscat(TempApplicationNameW
, L
".exe");
897 if (!SearchPathW(NULL
, TempApplicationNameW
, NULL
, sizeof(ImagePathName
)/sizeof(WCHAR
), ImagePathName
, &s
))
902 e
= wcsrchr(s
, L
'.');
903 if (e
!= NULL
&& (!_wcsicmp(e
, L
".bat") || !_wcsicmp(e
, L
".cmd")))
905 // the command is a batch file
906 if (lpApplicationName
!= NULL
&& lpApplicationName
[0])
908 // FIXME: use COMSPEC for the command interpreter
909 wcscpy(TempCommandLineNameW
, L
"cmd /c ");
910 wcscat(TempCommandLineNameW
, lpApplicationName
);
911 lpCommandLine
= TempCommandLineNameW
;
912 wcscpy(TempApplicationNameW
, L
"cmd.exe");
913 if (!SearchPathW(NULL
, TempApplicationNameW
, NULL
, sizeof(ImagePathName
)/sizeof(WCHAR
), ImagePathName
, &s
))
925 * Store the image file name for the process
932 for (i
= 0; i
< 8; i
++)
934 ImageFileName
[i
] = (CHAR
)(s
[i
]);
942 * Process the application name and command line
944 RtlInitUnicodeString(&ImagePathName_U
, ImagePathName
);
945 RtlInitUnicodeString(&CommandLine_U
, lpCommandLine
);
947 DPRINT("ImagePathName_U %S\n", ImagePathName_U
.Buffer
);
948 DPRINT("CommandLine_U %S\n", CommandLine_U
.Buffer
);
950 /* Initialize the current directory string */
951 if (lpCurrentDirectory
!= NULL
)
953 RtlInitUnicodeString(&CurrentDirectory_U
,
958 GetCurrentDirectoryW(256, TempCurrentDirectoryW
);
959 RtlInitUnicodeString(&CurrentDirectory_U
,
960 TempCurrentDirectoryW
);
964 * Create a section for the executable
967 hSection
= KlMapFile (ImagePathName
);
968 if (hSection
== NULL
)
970 /////////////////////////////////////////
972 * Inspect the image to determine executable flavour
974 IO_STATUS_BLOCK IoStatusBlock
;
975 UNICODE_STRING ApplicationNameString
;
976 OBJECT_ATTRIBUTES ObjectAttributes
;
977 PSECURITY_DESCRIPTOR SecurityDescriptor
= NULL
;
978 IMAGE_DOS_HEADER DosHeader
;
979 IO_STATUS_BLOCK Iosb
;
980 LARGE_INTEGER Offset
;
983 DPRINT("Inspecting Image Header for image type id\n");
985 // Find the application name
986 if (!RtlDosPathNameToNtPathName_U((LPWSTR
)lpApplicationName
,
987 &ApplicationNameString
, NULL
, NULL
)) {
990 DPRINT("ApplicationName %S\n",ApplicationNameString
.Buffer
);
992 InitializeObjectAttributes(&ObjectAttributes
,
993 &ApplicationNameString
,
994 OBJ_CASE_INSENSITIVE
,
998 // Try to open the executable
999 Status
= NtOpenFile(&hFile
,
1000 SYNCHRONIZE
|FILE_EXECUTE
|FILE_READ_DATA
,
1003 FILE_SHARE_DELETE
|FILE_SHARE_READ
,
1004 FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
);
1006 RtlFreeUnicodeString(&ApplicationNameString
);
1008 if (!NT_SUCCESS(Status
)) {
1009 DPRINT("Failed to open file\n");
1010 SetLastErrorByStatus(Status
);
1014 // Read the dos header
1015 Offset
.QuadPart
= 0;
1016 Status
= ZwReadFile(hFile
,
1026 if (!NT_SUCCESS(Status
)) {
1027 DPRINT("Failed to read from file\n");
1028 SetLastErrorByStatus(Status
);
1031 if (Iosb
.Information
!= sizeof(DosHeader
)) {
1032 DPRINT("Failed to read dos header from file\n");
1033 SetLastErrorByStatus(STATUS_INVALID_IMAGE_FORMAT
);
1037 // Check the DOS signature
1038 if (DosHeader
.e_magic
!= IMAGE_DOS_SIGNATURE
) {
1039 DPRINT("Failed dos magic check\n");
1040 SetLastErrorByStatus(STATUS_INVALID_IMAGE_FORMAT
);
1045 DPRINT("Launching VDM...\n");
1046 return CreateProcessW(L
"ntvdm.exe",
1047 (LPWSTR
)lpApplicationName
,
1048 lpProcessAttributes
,
1055 lpProcessInformation
);
1057 /////////////////////////////////////////
1059 * Create a new process
1061 Status
= NtCreateProcess(&hProcess
,
1071 if (lpStartupInfo
->lpReserved2
)
1074 * ROUND_UP(xxx,2) + 2 is a dirty hack. RtlCreateProcessParameters assumes that
1075 * the runtimeinfo is a unicode string and use RtlCopyUnicodeString for duplication.
1076 * If is possible that this function overwrite the last information in runtimeinfo
1077 * with the null terminator for the unicode string.
1079 RuntimeInfo_U
.Length
= RuntimeInfo_U
.MaximumLength
= ROUND_UP(lpStartupInfo
->cbReserved2
, 2) + 2;
1080 RuntimeInfo_U
.Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, RuntimeInfo_U
.Length
);
1081 memcpy(RuntimeInfo_U
.Buffer
, lpStartupInfo
->lpReserved2
, lpStartupInfo
->cbReserved2
);
1088 RtlCreateProcessParameters(&Ppb
,
1091 lpCurrentDirectory
? &CurrentDirectory_U
: NULL
,
1097 lpStartupInfo
&& lpStartupInfo
->lpReserved2
? &RuntimeInfo_U
: NULL
);
1099 if (lpStartupInfo
&& lpStartupInfo
->lpReserved2
)
1100 RtlFreeHeap(GetProcessHeap(), 0, RuntimeInfo_U
.Buffer
);
1104 * Translate some handles for the new process
1106 if (Ppb
->CurrentDirectoryHandle
)
1108 Status
= NtDuplicateObject (NtCurrentProcess(),
1109 Ppb
->CurrentDirectoryHandle
,
1111 &Ppb
->CurrentDirectoryHandle
,
1114 DUPLICATE_SAME_ACCESS
);
1119 Status
= NtDuplicateObject (NtCurrentProcess(),
1125 DUPLICATE_SAME_ACCESS
);
1129 * Get some information about the executable
1131 Status
= ZwQuerySection(hSection
,
1132 SectionImageInformation
,
1142 * Get some information about the process
1144 NtQueryInformationProcess(hProcess
,
1145 ProcessBasicInformation
,
1147 sizeof(ProcessBasicInfo
),
1149 DPRINT("ProcessBasicInfo.UniqueProcessId %d\n",
1150 ProcessBasicInfo
.UniqueProcessId
);
1151 lpProcessInformation
->dwProcessId
= ProcessBasicInfo
.UniqueProcessId
;
1154 * Tell the csrss server we are creating a new process
1156 CsrRequest
.Type
= CSRSS_CREATE_PROCESS
;
1157 CsrRequest
.Data
.CreateProcessRequest
.NewProcessId
=
1158 ProcessBasicInfo
.UniqueProcessId
;
1159 if (Sii
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_GUI
)
1161 /* Do not create a console for GUI applications */
1162 dwCreationFlags
&= ~CREATE_NEW_CONSOLE
;
1163 dwCreationFlags
|= DETACHED_PROCESS
;
1165 else if (Sii
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
)
1167 if (NULL
== Ppb
->hConsole
)
1169 dwCreationFlags
|= CREATE_NEW_CONSOLE
;
1172 CsrRequest
.Data
.CreateProcessRequest
.Flags
= dwCreationFlags
;
1173 CsrRequest
.Data
.CreateProcessRequest
.CtrlDispatcher
= ConsoleControlDispatcher
;
1174 Status
= CsrClientCallServer(&CsrRequest
,
1176 sizeof(CSRSS_API_REQUEST
),
1177 sizeof(CSRSS_API_REPLY
));
1178 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(CsrReply
.Status
))
1180 DbgPrint("Failed to tell csrss about new process. Expect trouble.\n");
1182 else if (Sii
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_CUI
)
1184 Ppb
->hConsole
= CsrReply
.Data
.CreateProcessReply
.Console
;
1191 /* Set the child console handles */
1193 /* First check if handles were passed in startup info */
1194 if (lpStartupInfo
&& (lpStartupInfo
->dwFlags
& STARTF_USESTDHANDLES
))
1196 if (lpStartupInfo
->hStdInput
)
1198 Ppb
->hStdInput
= lpStartupInfo
->hStdInput
;
1202 if (lpStartupInfo
->hStdOutput
)
1204 Ppb
->hStdOutput
= lpStartupInfo
->hStdOutput
;
1208 if (lpStartupInfo
->hStdError
)
1210 Ppb
->hStdError
= lpStartupInfo
->hStdError
;
1216 /* Check if new console was created, use it for input and output if
1218 if (0 != (dwCreationFlags
& CREATE_NEW_CONSOLE
)
1219 && NT_SUCCESS(Status
) && NT_SUCCESS(CsrReply
.Status
))
1223 Ppb
->hStdInput
= CsrReply
.Data
.CreateProcessReply
.InputHandle
;
1229 Ppb
->hStdOutput
= CsrReply
.Data
.CreateProcessReply
.OutputHandle
;
1235 Ppb
->hStdError
= CsrReply
.Data
.CreateProcessReply
.OutputHandle
;
1241 /* Use existing handles otherwise */
1244 Ppb
->hStdInput
= NtCurrentPeb()->ProcessParameters
->hStdInput
;
1249 Ppb
->hStdOutput
= NtCurrentPeb()->ProcessParameters
->hStdOutput
;
1254 Ppb
->hStdError
= NtCurrentPeb()->ProcessParameters
->hStdError
;
1258 /* Now duplicate handles if required */
1261 if (IsConsoleHandle(Ppb
->hStdInput
))
1263 Ppb
->hStdInput
= CsrReply
.Data
.CreateProcessReply
.InputHandle
;
1267 DPRINT("Duplicate input handle\n");
1268 Status
= NtDuplicateObject (NtCurrentProcess(),
1274 DUPLICATE_SAME_ACCESS
);
1275 if(!NT_SUCCESS(Status
))
1277 DPRINT("NtDuplicateObject failed, status %x\n", Status
);
1284 if (IsConsoleHandle(Ppb
->hStdOutput
))
1286 Ppb
->hStdOutput
= CsrReply
.Data
.CreateProcessReply
.OutputHandle
;
1290 DPRINT("Duplicate output handle\n");
1291 Status
= NtDuplicateObject (NtCurrentProcess(),
1297 DUPLICATE_SAME_ACCESS
);
1298 if(!NT_SUCCESS(Status
))
1300 DPRINT("NtDuplicateObject failed, status %x\n", Status
);
1307 if (IsConsoleHandle(Ppb
->hStdError
))
1309 CsrRequest
.Type
= CSRSS_DUPLICATE_HANDLE
;
1310 CsrRequest
.Data
.DuplicateHandleRequest
.ProcessId
= ProcessBasicInfo
.UniqueProcessId
;
1311 CsrRequest
.Data
.DuplicateHandleRequest
.Handle
= CsrReply
.Data
.CreateProcessReply
.OutputHandle
;
1312 Status
= CsrClientCallServer(&CsrRequest
,
1314 sizeof(CSRSS_API_REQUEST
),
1315 sizeof(CSRSS_API_REPLY
));
1316 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(CsrReply
.Status
))
1318 Ppb
->hStdError
= INVALID_HANDLE_VALUE
;
1322 Ppb
->hStdError
= CsrReply
.Data
.DuplicateHandleReply
.Handle
;
1327 DPRINT("Duplicate error handle\n");
1328 Status
= NtDuplicateObject (NtCurrentProcess(),
1334 DUPLICATE_SAME_ACCESS
);
1335 if(!NT_SUCCESS(Status
))
1337 DPRINT("NtDuplicateObject failed, status %x\n", Status
);
1343 * Initialize some other fields in the PPB
1347 Ppb
->dwFlags
= lpStartupInfo
->dwFlags
;
1348 if (Ppb
->dwFlags
& STARTF_USESHOWWINDOW
)
1350 Ppb
->wShowWindow
= lpStartupInfo
->wShowWindow
;
1354 Ppb
->wShowWindow
= SW_SHOWDEFAULT
;
1356 Ppb
->dwX
= lpStartupInfo
->dwX
;
1357 Ppb
->dwY
= lpStartupInfo
->dwY
;
1358 Ppb
->dwXSize
= lpStartupInfo
->dwXSize
;
1359 Ppb
->dwYSize
= lpStartupInfo
->dwYSize
;
1360 Ppb
->dwFillAttribute
= lpStartupInfo
->dwFillAttribute
;
1368 * Create Process Environment Block
1370 DPRINT("Creating peb\n");
1372 KlInitPeb(hProcess
, Ppb
, &ImageBaseAddress
);
1374 RtlDestroyProcessParameters (Ppb
);
1376 Status
= NtSetInformationProcess(hProcess
,
1377 ProcessImageFileName
,
1381 * Create the thread for the kernel
1383 DPRINT("Creating thread for process (EntryPoint = 0x%.08x)\n",
1384 ImageBaseAddress
+ (ULONG
)Sii
.EntryPoint
);
1385 hThread
= KlCreateFirstThread(hProcess
,
1388 ImageBaseAddress
+ (ULONG
)Sii
.EntryPoint
,
1390 &lpProcessInformation
->dwThreadId
);
1391 if (hThread
== INVALID_HANDLE_VALUE
)
1396 lpProcessInformation
->hProcess
= hProcess
;
1397 lpProcessInformation
->hThread
= hThread
;