3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/ntdll/rtl/ppb.c
6 * PURPOSE: Process parameters functions
7 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
12 /* INCLUDES ****************************************************************/
19 /* MACROS ****************************************************************/
21 #define NORMALIZE(x,addr) {if(x) x=(PVOID)((ULONG_PTR)(x)+(ULONG_PTR)(addr));}
22 #define DENORMALIZE(x,addr) {if(x) x=(PVOID)((ULONG_PTR)(x)-(ULONG_PTR)(addr));}
23 #define ALIGN(x,align) (((ULONG)(x)+(align)-1UL)&(~((align)-1UL)))
30 /* FUNCTIONS ****************************************************************/
34 RtlpCopyParameterString(PWCHAR
*Ptr
,
35 PUNICODE_STRING Destination
,
36 PUNICODE_STRING Source
,
39 Destination
->Length
= Source
->Length
;
40 Destination
->MaximumLength
= Size
? Size
: Source
->MaximumLength
;
41 Destination
->Buffer
= (PWCHAR
)(*Ptr
);
43 memmove (Destination
->Buffer
, Source
->Buffer
, Source
->Length
);
44 Destination
->Buffer
[Destination
->Length
/ sizeof(WCHAR
)] = 0;
45 *Ptr
+= Destination
->MaximumLength
/sizeof(WCHAR
);
53 RtlCreateProcessParameters(PRTL_USER_PROCESS_PARAMETERS
*ProcessParameters
,
54 PUNICODE_STRING ImagePathName
,
55 PUNICODE_STRING DllPath
,
56 PUNICODE_STRING CurrentDirectory
,
57 PUNICODE_STRING CommandLine
,
59 PUNICODE_STRING WindowTitle
,
60 PUNICODE_STRING DesktopInfo
,
61 PUNICODE_STRING ShellInfo
,
62 PUNICODE_STRING RuntimeInfo
)
64 NTSTATUS Status
= STATUS_SUCCESS
;
65 PRTL_USER_PROCESS_PARAMETERS Param
= NULL
;
69 UNICODE_STRING EmptyString
;
70 HANDLE CurrentDirectoryHandle
;
74 DPRINT ("RtlCreateProcessParameters\n");
78 EmptyString
.Length
= 0;
79 EmptyString
.MaximumLength
= sizeof(WCHAR
);
80 EmptyString
.Buffer
= L
"";
82 if (RtlpGetMode() == UserMode
)
85 DllPath
= &NtCurrentPeb()->ProcessParameters
->DllPath
;
86 if (Environment
== NULL
)
87 Environment
= NtCurrentPeb()->ProcessParameters
->Environment
;
88 if (CurrentDirectory
== NULL
)
89 CurrentDirectory
= &NtCurrentPeb()->ProcessParameters
->CurrentDirectoryName
;
90 CurrentDirectoryHandle
= NtCurrentPeb()->ProcessParameters
->CurrentDirectoryHandle
;
91 ConsoleHandle
= NtCurrentPeb()->ProcessParameters
->hConsole
;
92 ConsoleFlags
= NtCurrentPeb()->ProcessParameters
->ProcessGroup
;
97 DllPath
= &EmptyString
;
98 if (CurrentDirectory
== NULL
)
99 CurrentDirectory
= &EmptyString
;
100 CurrentDirectoryHandle
= NULL
;
101 ConsoleHandle
= NULL
;
105 if (CommandLine
== NULL
)
106 CommandLine
= &EmptyString
;
107 if (WindowTitle
== NULL
)
108 WindowTitle
= &EmptyString
;
109 if (DesktopInfo
== NULL
)
110 DesktopInfo
= &EmptyString
;
111 if (ShellInfo
== NULL
)
112 ShellInfo
= &EmptyString
;
113 if (RuntimeInfo
== NULL
)
114 RuntimeInfo
= &EmptyString
;
116 /* size of process parameter block */
117 Length
= sizeof(RTL_USER_PROCESS_PARAMETERS
);
119 /* size of current directory buffer */
120 Length
+= (MAX_PATH
* sizeof(WCHAR
));
122 /* add string lengths */
123 Length
+= ALIGN(DllPath
->MaximumLength
, sizeof(ULONG
));
124 Length
+= ALIGN(ImagePathName
->Length
+ sizeof(WCHAR
), sizeof(ULONG
));
125 Length
+= ALIGN(CommandLine
->Length
+ sizeof(WCHAR
), sizeof(ULONG
));
126 Length
+= ALIGN(WindowTitle
->MaximumLength
, sizeof(ULONG
));
127 Length
+= ALIGN(DesktopInfo
->MaximumLength
, sizeof(ULONG
));
128 Length
+= ALIGN(ShellInfo
->MaximumLength
, sizeof(ULONG
));
129 Length
+= ALIGN(RuntimeInfo
->MaximumLength
, sizeof(ULONG
));
131 /* Calculate the required block size */
132 RegionSize
= ROUNDUP(Length
, PAGE_SIZE
);
134 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(),
138 MEM_RESERVE
| MEM_COMMIT
,
140 if (!NT_SUCCESS(Status
))
146 DPRINT ("Process parameters allocated\n");
148 Param
->AllocationSize
= RegionSize
;
149 Param
->Size
= Length
;
150 Param
->Flags
= PPF_NORMALIZED
;
151 Param
->Environment
= Environment
;
152 Param
->CurrentDirectoryHandle
= CurrentDirectoryHandle
;
153 Param
->hConsole
= ConsoleHandle
;
154 Param
->ProcessGroup
= ConsoleFlags
;
156 Dest
= (PWCHAR
)(((PBYTE
)Param
) + sizeof(RTL_USER_PROCESS_PARAMETERS
));
158 /* copy current directory */
159 RtlpCopyParameterString(&Dest
,
160 &Param
->CurrentDirectoryName
,
162 MAX_PATH
* sizeof(WCHAR
));
164 /* make sure the current directory has a trailing backslash */
165 if (Param
->CurrentDirectoryName
.Length
> 0)
169 Length
= Param
->CurrentDirectoryName
.Length
/ sizeof(WCHAR
);
170 if (Param
->CurrentDirectoryName
.Buffer
[Length
-1] != L
'\\')
172 Param
->CurrentDirectoryName
.Buffer
[Length
] = L
'\\';
173 Param
->CurrentDirectoryName
.Buffer
[Length
+ 1] = 0;
174 Param
->CurrentDirectoryName
.Length
+= sizeof(WCHAR
);
179 RtlpCopyParameterString(&Dest
,
184 /* copy image path name */
185 RtlpCopyParameterString(&Dest
,
186 &Param
->ImagePathName
,
188 ImagePathName
->Length
+ sizeof(WCHAR
));
190 /* copy command line */
191 RtlpCopyParameterString(&Dest
,
194 CommandLine
->Length
+ sizeof(WCHAR
));
197 RtlpCopyParameterString(&Dest
,
203 RtlpCopyParameterString(&Dest
,
208 /* copy shell info */
209 RtlpCopyParameterString(&Dest
,
214 /* copy runtime info */
215 RtlpCopyParameterString(&Dest
,
220 RtlDeNormalizeProcessParams(Param
);
221 *ProcessParameters
= Param
;
224 return STATUS_SUCCESS
;
231 RtlDestroyProcessParameters(PRTL_USER_PROCESS_PARAMETERS ProcessParameters
)
233 ULONG RegionSize
= 0;
235 return ZwFreeVirtualMemory (NtCurrentProcess (),
236 (PVOID
)ProcessParameters
,
242 * denormalize process parameters (Pointer-->Offset)
246 PRTL_USER_PROCESS_PARAMETERS STDCALL
247 RtlDeNormalizeProcessParams(PRTL_USER_PROCESS_PARAMETERS Params
)
249 if (Params
&& (Params
->Flags
& PPF_NORMALIZED
))
251 DENORMALIZE(Params
->CurrentDirectoryName
.Buffer
, Params
);
252 DENORMALIZE(Params
->DllPath
.Buffer
, Params
);
253 DENORMALIZE(Params
->ImagePathName
.Buffer
, Params
);
254 DENORMALIZE(Params
->CommandLine
.Buffer
, Params
);
255 DENORMALIZE(Params
->WindowTitle
.Buffer
, Params
);
256 DENORMALIZE(Params
->DesktopInfo
.Buffer
, Params
);
257 DENORMALIZE(Params
->ShellInfo
.Buffer
, Params
);
258 DENORMALIZE(Params
->RuntimeInfo
.Buffer
, Params
);
260 Params
->Flags
&= ~PPF_NORMALIZED
;
267 * normalize process parameters (Offset-->Pointer)
271 PRTL_USER_PROCESS_PARAMETERS STDCALL
272 RtlNormalizeProcessParams(PRTL_USER_PROCESS_PARAMETERS Params
)
274 if (Params
&& !(Params
->Flags
& PPF_NORMALIZED
))
276 NORMALIZE(Params
->CurrentDirectoryName
.Buffer
, Params
);
277 NORMALIZE(Params
->DllPath
.Buffer
, Params
);
278 NORMALIZE(Params
->ImagePathName
.Buffer
, Params
);
279 NORMALIZE(Params
->CommandLine
.Buffer
, Params
);
280 NORMALIZE(Params
->WindowTitle
.Buffer
, Params
);
281 NORMALIZE(Params
->DesktopInfo
.Buffer
, Params
);
282 NORMALIZE(Params
->ShellInfo
.Buffer
, Params
);
283 NORMALIZE(Params
->RuntimeInfo
.Buffer
, Params
);
285 Params
->Flags
|= PPF_NORMALIZED
;