2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
5 * PURPOSE: Process parameters functions
6 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
9 /* INCLUDES ****************************************************************/
16 /* MACROS ****************************************************************/
18 #define NORMALIZE(x,addr) {if(x) x=(PVOID)((ULONG_PTR)(x)+(ULONG_PTR)(addr));}
19 #define DENORMALIZE(x,addr) {if(x) x=(PVOID)((ULONG_PTR)(x)-(ULONG_PTR)(addr));}
20 #define ALIGN(x,align) (((ULONG)(x)+(align)-1UL)&(~((align)-1UL)))
22 /* FUNCTIONS ****************************************************************/
26 RtlpCopyParameterString(PWCHAR
*Ptr
,
27 PUNICODE_STRING Destination
,
28 PUNICODE_STRING Source
,
31 Destination
->Length
= Source
->Length
;
32 Destination
->MaximumLength
= Size
? Size
: Source
->MaximumLength
;
33 Destination
->Buffer
= (PWCHAR
)(*Ptr
);
35 memmove (Destination
->Buffer
, Source
->Buffer
, Source
->Length
);
36 Destination
->Buffer
[Destination
->Length
/ sizeof(WCHAR
)] = 0;
37 *Ptr
+= Destination
->MaximumLength
/sizeof(WCHAR
);
45 RtlCreateProcessParameters(PRTL_USER_PROCESS_PARAMETERS
*ProcessParameters
,
46 PUNICODE_STRING ImagePathName
,
47 PUNICODE_STRING DllPath
,
48 PUNICODE_STRING CurrentDirectory
,
49 PUNICODE_STRING CommandLine
,
51 PUNICODE_STRING WindowTitle
,
52 PUNICODE_STRING DesktopInfo
,
53 PUNICODE_STRING ShellInfo
,
54 PUNICODE_STRING RuntimeData
)
56 NTSTATUS Status
= STATUS_SUCCESS
;
57 PRTL_USER_PROCESS_PARAMETERS Param
= NULL
;
58 SIZE_T RegionSize
= 0;
61 UNICODE_STRING EmptyString
;
62 HANDLE CurrentDirectoryHandle
;
66 DPRINT ("RtlCreateProcessParameters\n");
70 EmptyString
.Length
= 0;
71 EmptyString
.MaximumLength
= sizeof(WCHAR
);
72 EmptyString
.Buffer
= L
"";
74 if (RtlpGetMode() == UserMode
)
77 DllPath
= &NtCurrentPeb()->ProcessParameters
->DllPath
;
78 if (Environment
== NULL
)
79 Environment
= NtCurrentPeb()->ProcessParameters
->Environment
;
80 if (CurrentDirectory
== NULL
)
81 CurrentDirectory
= &NtCurrentPeb()->ProcessParameters
->CurrentDirectory
.DosPath
;
82 CurrentDirectoryHandle
= NtCurrentPeb()->ProcessParameters
->CurrentDirectory
.Handle
;
83 ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
84 ConsoleFlags
= NtCurrentPeb()->ProcessParameters
->ConsoleFlags
;
89 DllPath
= &EmptyString
;
90 if (CurrentDirectory
== NULL
)
91 CurrentDirectory
= &EmptyString
;
92 CurrentDirectoryHandle
= NULL
;
97 if (CommandLine
== NULL
)
98 CommandLine
= &EmptyString
;
99 if (WindowTitle
== NULL
)
100 WindowTitle
= &EmptyString
;
101 if (DesktopInfo
== NULL
)
102 DesktopInfo
= &EmptyString
;
103 if (ShellInfo
== NULL
)
104 ShellInfo
= &EmptyString
;
105 if (RuntimeData
== NULL
)
106 RuntimeData
= &EmptyString
;
108 /* size of process parameter block */
109 Length
= sizeof(RTL_USER_PROCESS_PARAMETERS
);
111 /* size of current directory buffer */
112 Length
+= (MAX_PATH
* sizeof(WCHAR
));
114 /* add string lengths */
115 Length
+= ALIGN(DllPath
->MaximumLength
, sizeof(ULONG
));
116 Length
+= ALIGN(ImagePathName
->Length
+ sizeof(WCHAR
), sizeof(ULONG
));
117 Length
+= ALIGN(CommandLine
->Length
+ sizeof(WCHAR
), sizeof(ULONG
));
118 Length
+= ALIGN(WindowTitle
->MaximumLength
, sizeof(ULONG
));
119 Length
+= ALIGN(DesktopInfo
->MaximumLength
, sizeof(ULONG
));
120 Length
+= ALIGN(ShellInfo
->MaximumLength
, sizeof(ULONG
));
121 Length
+= ALIGN(RuntimeData
->MaximumLength
, sizeof(ULONG
));
123 /* Calculate the required block size */
124 RegionSize
= ROUND_UP(Length
, PAGE_SIZE
);
126 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(),
130 MEM_RESERVE
| MEM_COMMIT
,
132 if (!NT_SUCCESS(Status
))
138 DPRINT ("Process parameters allocated\n");
140 Param
->MaximumLength
= RegionSize
;
141 Param
->Length
= Length
;
142 Param
->Flags
= RTL_USER_PROCESS_PARAMETERS_NORMALIZED
;
143 Param
->Environment
= Environment
;
144 Param
->CurrentDirectory
.Handle
= CurrentDirectoryHandle
;
145 Param
->ConsoleHandle
= ConsoleHandle
;
146 Param
->ConsoleFlags
= ConsoleFlags
;
148 Dest
= (PWCHAR
)(((PBYTE
)Param
) + sizeof(RTL_USER_PROCESS_PARAMETERS
));
150 /* copy current directory */
151 RtlpCopyParameterString(&Dest
,
152 &Param
->CurrentDirectory
.DosPath
,
154 MAX_PATH
* sizeof(WCHAR
));
156 /* make sure the current directory has a trailing backslash */
157 if (Param
->CurrentDirectory
.DosPath
.Length
> 0)
161 Length
= Param
->CurrentDirectory
.DosPath
.Length
/ sizeof(WCHAR
);
162 if (Param
->CurrentDirectory
.DosPath
.Buffer
[Length
-1] != L
'\\')
164 Param
->CurrentDirectory
.DosPath
.Buffer
[Length
] = L
'\\';
165 Param
->CurrentDirectory
.DosPath
.Buffer
[Length
+ 1] = 0;
166 Param
->CurrentDirectory
.DosPath
.Length
+= sizeof(WCHAR
);
171 RtlpCopyParameterString(&Dest
,
176 /* copy image path name */
177 RtlpCopyParameterString(&Dest
,
178 &Param
->ImagePathName
,
180 ImagePathName
->Length
+ sizeof(WCHAR
));
182 /* copy command line */
183 RtlpCopyParameterString(&Dest
,
186 CommandLine
->Length
+ sizeof(WCHAR
));
189 RtlpCopyParameterString(&Dest
,
195 RtlpCopyParameterString(&Dest
,
200 /* copy shell info */
201 RtlpCopyParameterString(&Dest
,
206 /* copy runtime info */
207 RtlpCopyParameterString(&Dest
,
212 RtlDeNormalizeProcessParams(Param
);
213 *ProcessParameters
= Param
;
216 return STATUS_SUCCESS
;
223 RtlDestroyProcessParameters(PRTL_USER_PROCESS_PARAMETERS ProcessParameters
)
225 SIZE_T RegionSize
= 0;
227 return ZwFreeVirtualMemory (NtCurrentProcess (),
228 (PVOID
)ProcessParameters
,
234 * denormalize process parameters (Pointer-->Offset)
238 PRTL_USER_PROCESS_PARAMETERS NTAPI
239 RtlDeNormalizeProcessParams(PRTL_USER_PROCESS_PARAMETERS Params
)
241 if (Params
&& (Params
->Flags
& RTL_USER_PROCESS_PARAMETERS_NORMALIZED
))
243 DENORMALIZE(Params
->CurrentDirectory
.DosPath
.Buffer
, Params
);
244 DENORMALIZE(Params
->DllPath
.Buffer
, Params
);
245 DENORMALIZE(Params
->ImagePathName
.Buffer
, Params
);
246 DENORMALIZE(Params
->CommandLine
.Buffer
, Params
);
247 DENORMALIZE(Params
->WindowTitle
.Buffer
, Params
);
248 DENORMALIZE(Params
->DesktopInfo
.Buffer
, Params
);
249 DENORMALIZE(Params
->ShellInfo
.Buffer
, Params
);
250 DENORMALIZE(Params
->RuntimeData
.Buffer
, Params
);
252 Params
->Flags
&= ~RTL_USER_PROCESS_PARAMETERS_NORMALIZED
;
259 * normalize process parameters (Offset-->Pointer)
263 PRTL_USER_PROCESS_PARAMETERS NTAPI
264 RtlNormalizeProcessParams(PRTL_USER_PROCESS_PARAMETERS Params
)
266 if (Params
&& !(Params
->Flags
& RTL_USER_PROCESS_PARAMETERS_NORMALIZED
))
268 NORMALIZE(Params
->CurrentDirectory
.DosPath
.Buffer
, Params
);
269 NORMALIZE(Params
->DllPath
.Buffer
, Params
);
270 NORMALIZE(Params
->ImagePathName
.Buffer
, Params
);
271 NORMALIZE(Params
->CommandLine
.Buffer
, Params
);
272 NORMALIZE(Params
->WindowTitle
.Buffer
, Params
);
273 NORMALIZE(Params
->DesktopInfo
.Buffer
, Params
);
274 NORMALIZE(Params
->ShellInfo
.Buffer
, Params
);
275 NORMALIZE(Params
->RuntimeData
.Buffer
, Params
);
277 Params
->Flags
|= RTL_USER_PROCESS_PARAMETERS_NORMALIZED
;