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 PRTL_USER_PROCESS_PARAMETERS Param
= NULL
;
59 UNICODE_STRING EmptyString
;
60 HANDLE CurrentDirectoryHandle
;
64 DPRINT ("RtlCreateProcessParameters\n");
68 EmptyString
.Length
= 0;
69 EmptyString
.MaximumLength
= sizeof(WCHAR
);
70 EmptyString
.Buffer
= L
"";
72 if (RtlpGetMode() == UserMode
)
75 DllPath
= &NtCurrentPeb()->ProcessParameters
->DllPath
;
76 if (Environment
== NULL
)
77 Environment
= NtCurrentPeb()->ProcessParameters
->Environment
;
78 if (CurrentDirectory
== NULL
)
79 CurrentDirectory
= &NtCurrentPeb()->ProcessParameters
->CurrentDirectory
.DosPath
;
80 CurrentDirectoryHandle
= NtCurrentPeb()->ProcessParameters
->CurrentDirectory
.Handle
;
81 ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->ConsoleHandle
;
82 ConsoleFlags
= NtCurrentPeb()->ProcessParameters
->ConsoleFlags
;
87 DllPath
= &EmptyString
;
88 if (CurrentDirectory
== NULL
)
89 CurrentDirectory
= &EmptyString
;
90 CurrentDirectoryHandle
= NULL
;
95 if (CommandLine
== NULL
)
96 CommandLine
= &EmptyString
;
97 if (WindowTitle
== NULL
)
98 WindowTitle
= &EmptyString
;
99 if (DesktopInfo
== NULL
)
100 DesktopInfo
= &EmptyString
;
101 if (ShellInfo
== NULL
)
102 ShellInfo
= &EmptyString
;
103 if (RuntimeData
== NULL
)
104 RuntimeData
= &EmptyString
;
106 /* size of process parameter block */
107 Length
= sizeof(RTL_USER_PROCESS_PARAMETERS
);
109 /* size of current directory buffer */
110 Length
+= (MAX_PATH
* sizeof(WCHAR
));
112 /* add string lengths */
113 Length
+= ALIGN(DllPath
->MaximumLength
, sizeof(ULONG
));
114 Length
+= ALIGN(ImagePathName
->Length
+ sizeof(WCHAR
), sizeof(ULONG
));
115 Length
+= ALIGN(CommandLine
->Length
+ sizeof(WCHAR
), sizeof(ULONG
));
116 Length
+= ALIGN(WindowTitle
->MaximumLength
, sizeof(ULONG
));
117 Length
+= ALIGN(DesktopInfo
->MaximumLength
, sizeof(ULONG
));
118 Length
+= ALIGN(ShellInfo
->MaximumLength
, sizeof(ULONG
));
119 Length
+= ALIGN(RuntimeData
->MaximumLength
, sizeof(ULONG
));
121 /* Calculate the required block size */
122 Param
= RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY
, Length
);
126 return STATUS_INSUFFICIENT_RESOURCES
;
129 DPRINT ("Process parameters allocated\n");
131 Param
->MaximumLength
= Length
;
132 Param
->Length
= Length
;
133 Param
->Flags
= RTL_USER_PROCESS_PARAMETERS_NORMALIZED
;
134 Param
->Environment
= Environment
;
135 Param
->CurrentDirectory
.Handle
= CurrentDirectoryHandle
;
136 Param
->ConsoleHandle
= ConsoleHandle
;
137 Param
->ConsoleFlags
= ConsoleFlags
;
139 Dest
= (PWCHAR
)(((PBYTE
)Param
) + sizeof(RTL_USER_PROCESS_PARAMETERS
));
141 /* copy current directory */
142 RtlpCopyParameterString(&Dest
,
143 &Param
->CurrentDirectory
.DosPath
,
145 MAX_PATH
* sizeof(WCHAR
));
147 /* make sure the current directory has a trailing backslash */
148 if (Param
->CurrentDirectory
.DosPath
.Length
> 0)
150 Length
= Param
->CurrentDirectory
.DosPath
.Length
/ sizeof(WCHAR
);
151 if (Param
->CurrentDirectory
.DosPath
.Buffer
[Length
-1] != L
'\\')
153 Param
->CurrentDirectory
.DosPath
.Buffer
[Length
] = L
'\\';
154 Param
->CurrentDirectory
.DosPath
.Buffer
[Length
+ 1] = 0;
155 Param
->CurrentDirectory
.DosPath
.Length
+= sizeof(WCHAR
);
160 RtlpCopyParameterString(&Dest
,
165 /* copy image path name */
166 RtlpCopyParameterString(&Dest
,
167 &Param
->ImagePathName
,
169 ImagePathName
->Length
+ sizeof(WCHAR
));
171 /* copy command line */
172 RtlpCopyParameterString(&Dest
,
175 CommandLine
->Length
+ sizeof(WCHAR
));
178 RtlpCopyParameterString(&Dest
,
184 RtlpCopyParameterString(&Dest
,
189 /* copy shell info */
190 RtlpCopyParameterString(&Dest
,
195 /* copy runtime info */
196 RtlpCopyParameterString(&Dest
,
201 RtlDeNormalizeProcessParams(Param
);
202 *ProcessParameters
= Param
;
205 return STATUS_SUCCESS
;
213 RtlDestroyProcessParameters(IN PRTL_USER_PROCESS_PARAMETERS ProcessParameters
)
215 RtlFreeHeap(RtlGetProcessHeap(), 0, ProcessParameters
);
216 return STATUS_SUCCESS
;
220 * denormalize process parameters (Pointer-->Offset)
224 PRTL_USER_PROCESS_PARAMETERS NTAPI
225 RtlDeNormalizeProcessParams(PRTL_USER_PROCESS_PARAMETERS Params
)
227 if (Params
&& (Params
->Flags
& RTL_USER_PROCESS_PARAMETERS_NORMALIZED
))
229 DENORMALIZE(Params
->CurrentDirectory
.DosPath
.Buffer
, Params
);
230 DENORMALIZE(Params
->DllPath
.Buffer
, Params
);
231 DENORMALIZE(Params
->ImagePathName
.Buffer
, Params
);
232 DENORMALIZE(Params
->CommandLine
.Buffer
, Params
);
233 DENORMALIZE(Params
->WindowTitle
.Buffer
, Params
);
234 DENORMALIZE(Params
->DesktopInfo
.Buffer
, Params
);
235 DENORMALIZE(Params
->ShellInfo
.Buffer
, Params
);
236 DENORMALIZE(Params
->RuntimeData
.Buffer
, Params
);
238 Params
->Flags
&= ~RTL_USER_PROCESS_PARAMETERS_NORMALIZED
;
245 * normalize process parameters (Offset-->Pointer)
249 PRTL_USER_PROCESS_PARAMETERS NTAPI
250 RtlNormalizeProcessParams(PRTL_USER_PROCESS_PARAMETERS Params
)
252 if (Params
&& !(Params
->Flags
& RTL_USER_PROCESS_PARAMETERS_NORMALIZED
))
254 NORMALIZE(Params
->CurrentDirectory
.DosPath
.Buffer
, Params
);
255 NORMALIZE(Params
->DllPath
.Buffer
, Params
);
256 NORMALIZE(Params
->ImagePathName
.Buffer
, Params
);
257 NORMALIZE(Params
->CommandLine
.Buffer
, Params
);
258 NORMALIZE(Params
->WindowTitle
.Buffer
, Params
);
259 NORMALIZE(Params
->DesktopInfo
.Buffer
, Params
);
260 NORMALIZE(Params
->ShellInfo
.Buffer
, Params
);
261 NORMALIZE(Params
->RuntimeData
.Buffer
, Params
);
263 Params
->Flags
|= RTL_USER_PROCESS_PARAMETERS_NORMALIZED
;