ReactOS 0.4.17-dev-243-g1369312
ppb.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/rtl/ppb.c
5 * PURPOSE: Process parameters functions
6 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
7 */
8
9/* INCLUDES ****************************************************************/
10
11#include <rtl.h>
12
13#define NDEBUG
14#include <debug.h>
15
16/* MACROS ****************************************************************/
17
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)))
21
22/* FUNCTIONS ****************************************************************/
23
24
25static __inline VOID
30{
31 Destination->Length = Source->Length;
32 Destination->MaximumLength = Size ? Size : Source->MaximumLength;
33 if (Destination->MaximumLength != 0)
34 {
36 if (Source->Length)
37 memmove (Destination->Buffer, Source->Buffer, Source->Length);
38 Destination->Buffer[Destination->Length / sizeof(WCHAR)] = 0;
39 }
41 *Ptr = ALIGN_UP_POINTER_BY(*Ptr, sizeof(PVOID));
42}
43
44
45/*
46 * @implemented
47 */
50 PUNICODE_STRING ImagePathName,
53 PUNICODE_STRING CommandLine,
55 PUNICODE_STRING WindowTitle,
56 PUNICODE_STRING DesktopInfo,
57 PUNICODE_STRING ShellInfo,
58 PUNICODE_STRING RuntimeData)
59{
61 ULONG Length = 0;
62 SIZE_T EnvironmentSize = 0;
64 PWCHAR Dest;
65 UNICODE_STRING EmptyString = { .Length = 0, .MaximumLength = sizeof(WCHAR), .Buffer = L"" }; ;
66 UNICODE_STRING NullString = { .Length = 0, .MaximumLength = 0, .Buffer = NULL }; ;
67 HANDLE CurrentDirectoryHandle;
68 HANDLE ConsoleHandle;
69 ULONG ConsoleFlags;
70
71 DPRINT ("RtlCreateProcessParameters\n");
72
74
75 if (RtlpGetMode() == UserMode)
76 {
77 if (DllPath == NULL)
78 DllPath = &NtCurrentPeb()->ProcessParameters->DllPath;
79 if (Environment == NULL)
80 Environment = NtCurrentPeb()->ProcessParameters->Environment;
82 CurrentDirectory = &NtCurrentPeb()->ProcessParameters->CurrentDirectory.DosPath;
83 CurrentDirectoryHandle = NtCurrentPeb()->ProcessParameters->CurrentDirectory.Handle;
84 ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle;
85 ConsoleFlags = NtCurrentPeb()->ProcessParameters->ConsoleFlags;
86 }
87 else
88 {
89 if (DllPath == NULL)
90 DllPath = &EmptyString;
92 CurrentDirectory = &EmptyString;
93 CurrentDirectoryHandle = NULL;
94 ConsoleHandle = NULL;
95 ConsoleFlags = 0;
96 }
97
98 if (CommandLine == NULL)
99 CommandLine = ImagePathName;
100 if (CommandLine == NULL)
101 CommandLine = &EmptyString;
102 if (WindowTitle == NULL)
103 WindowTitle = &EmptyString;
104 if (DesktopInfo == NULL)
105 DesktopInfo = &EmptyString;
106 if (ShellInfo == NULL)
107 ShellInfo = &EmptyString;
108 if (RuntimeData == NULL)
109 RuntimeData = &NullString;
110
111 /* size of process parameter block */
113
114 /* size of current directory buffer */
115 Length += (MAX_PATH * sizeof(WCHAR));
116
117 /* add string lengths */
118 Length += ALIGN(DllPath->MaximumLength, sizeof(PVOID));
119 Length += ALIGN(ImagePathName->Length + sizeof(WCHAR), sizeof(PVOID));
120 Length += ALIGN(CommandLine->Length + sizeof(WCHAR), sizeof(PVOID));
121 Length += ALIGN(WindowTitle->MaximumLength, sizeof(PVOID));
122 Length += ALIGN(DesktopInfo->MaximumLength, sizeof(PVOID));
123 Length += ALIGN(ShellInfo->MaximumLength, sizeof(PVOID));
124 Length += ALIGN(RuntimeData->MaximumLength, sizeof(PVOID));
125
126 /* Vista stores the EnvironmentSize in RTL_PROCESS_PARAMETERS */
127 if (Environment != NULL)
128 {
129 PWCHAR EnvEnd = Environment;
130 while (*EnvEnd)
131 EnvEnd += wcslen(EnvEnd) + 1;
132 EnvironmentSize = (EnvEnd - Environment + 1) * sizeof(WCHAR);
133 }
134
135 AllocationSize = Length + EnvironmentSize;
136
137 /* Calculate the required block size */
138 Param = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, AllocationSize);
139 if (!Param)
140 {
143 }
144
145 DPRINT ("Process parameters allocated\n");
146
147 Param->MaximumLength = Length;
148 Param->Length = Length;
150 Param->Environment = NULL;
151 Param->EnvironmentSize = 0;
152 Param->CurrentDirectory.Handle = CurrentDirectoryHandle;
153 Param->ConsoleHandle = ConsoleHandle;
154 Param->ConsoleFlags = ConsoleFlags;
155
156 Dest = (PWCHAR)(((PBYTE)Param) + sizeof(RTL_USER_PROCESS_PARAMETERS));
157
158 /* copy current directory */
162 MAX_PATH * sizeof(WCHAR));
163
164 /* make sure the current directory has a trailing backslash */
165 if (Param->CurrentDirectory.DosPath.Length > 0)
166 {
167 Length = Param->CurrentDirectory.DosPath.Length / sizeof(WCHAR);
168 if (Param->CurrentDirectory.DosPath.Buffer[Length-1] != L'\\')
169 {
170 Param->CurrentDirectory.DosPath.Buffer[Length] = L'\\';
171 Param->CurrentDirectory.DosPath.Buffer[Length + 1] = 0;
172 Param->CurrentDirectory.DosPath.Length += sizeof(WCHAR);
173 }
174 }
175
176 /* copy dll path */
178 &Param->DllPath,
179 DllPath,
180 0);
181
182 /* copy image path name */
184 &Param->ImagePathName,
185 ImagePathName,
186 ImagePathName->Length + sizeof(WCHAR));
187
188 /* copy command line */
190 &Param->CommandLine,
191 CommandLine,
192 CommandLine->Length + sizeof(WCHAR));
193
194 /* copy title */
196 &Param->WindowTitle,
197 WindowTitle,
198 0);
199
200 /* copy desktop */
202 &Param->DesktopInfo,
203 DesktopInfo,
204 0);
205
206 /* copy shell info */
208 &Param->ShellInfo,
209 ShellInfo,
210 0);
211
212 /* copy runtime info */
214 &Param->RuntimeData,
215 RuntimeData,
216 0);
217
218 /* Make sure we didn't go past the end of the buffer */
219 ASSERT(((PUCHAR)Dest - (PUCHAR)Param) <= Param->MaximumLength);
220
221 /* Copy the environment */
222 if (Environment != NULL)
223 {
224 RtlCopyMemory(Dest, Environment, EnvironmentSize);
225 Param->Environment = Dest;
226 Param->EnvironmentSize = EnvironmentSize;
227
228 /* Make sure we didn't go past the end of the buffer */
229 ASSERT((PUCHAR)Dest + EnvironmentSize - (PUCHAR)Param <= AllocationSize);
230 }
231
233 *ProcessParameters = Param;
235
236 return STATUS_SUCCESS;
237}
238
239/*
240 * @implemented
241 */
243NTAPI
245{
246 RtlFreeHeap(RtlGetProcessHeap(), 0, ProcessParameters);
247 return STATUS_SUCCESS;
248}
249
250/*
251 * denormalize process parameters (Pointer-->Offset)
252 *
253 * @implemented
254 */
257{
259 {
260 DENORMALIZE(Params->CurrentDirectory.DosPath.Buffer, Params);
261 DENORMALIZE(Params->DllPath.Buffer, Params);
262 DENORMALIZE(Params->ImagePathName.Buffer, Params);
263 DENORMALIZE(Params->CommandLine.Buffer, Params);
264 DENORMALIZE(Params->WindowTitle.Buffer, Params);
265 DENORMALIZE(Params->DesktopInfo.Buffer, Params);
266 DENORMALIZE(Params->ShellInfo.Buffer, Params);
267 DENORMALIZE(Params->RuntimeData.Buffer, Params);
268
269 Params->Flags &= ~RTL_USER_PROCESS_PARAMETERS_NORMALIZED;
270 }
271
272 return Params;
273}
274
275/*
276 * normalize process parameters (Offset-->Pointer)
277 *
278 * @implemented
279 */
282{
284 {
285 NORMALIZE(Params->CurrentDirectory.DosPath.Buffer, Params);
286 NORMALIZE(Params->DllPath.Buffer, Params);
287 NORMALIZE(Params->ImagePathName.Buffer, Params);
288 NORMALIZE(Params->CommandLine.Buffer, Params);
289 NORMALIZE(Params->WindowTitle.Buffer, Params);
290 NORMALIZE(Params->DesktopInfo.Buffer, Params);
291 NORMALIZE(Params->ShellInfo.Buffer, Params);
292 NORMALIZE(Params->RuntimeData.Buffer, Params);
293
295 }
296
297 return Params;
298}
299
300/* EOF */
#define NtCurrentPeb()
Definition: FLS.c:22
#define __inline
Definition: _wctype.cpp:15
LONG NTSTATUS
Definition: precomp.h:26
WCHAR CurrentDirectory[1024]
Definition: chkdsk.c:74
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:616
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:634
struct _RTL_USER_PROCESS_PARAMETERS RTL_USER_PROCESS_PARAMETERS
Definition: bufpool.h:45
#define NULL
Definition: types.h:112
KPROCESSOR_MODE NTAPI RtlpGetMode(VOID)
Definition: libsupp.c:55
#define MAX_PATH
Definition: compat.h:34
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
_ACRTIMP size_t __cdecl wcslen(const wchar_t *)
Definition: wcs.c:2983
#define L(x)
Definition: resources.c:13
IN PFCB IN PFILE_OBJECT FileObject IN ULONG AllocationSize
Definition: fatprocs.h:323
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
#define ASSERT(a)
Definition: mode.c:44
PVOID PVOID PWCHAR PVOID Environment
Definition: env.c:47
static const char const char * DllPath
Definition: image.c:34
_In_ UINT _In_ UINT _In_ PNDIS_PACKET Source
Definition: ndis.h:3169
#define UserMode
Definition: asm.h:39
_In_ PUNICODE_STRING _Inout_ PUNICODE_STRING Destination
Definition: rtlfuncs.h:3051
#define RTL_USER_PROCESS_PARAMETERS_NORMALIZED
Definition: rtltypes.h:41
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
BYTE * PBYTE
Definition: pedump.c:66
short WCHAR
Definition: pedump.c:58
unsigned short USHORT
Definition: pedump.c:61
static __inline VOID RtlpCopyParameterString(PWCHAR *Ptr, PUNICODE_STRING Destination, PUNICODE_STRING Source, USHORT Size)
Definition: ppb.c:26
#define ALIGN(x, align)
Definition: ppb.c:20
#define DENORMALIZE(x, addr)
Definition: ppb.c:19
#define NORMALIZE(x, addr)
Definition: ppb.c:18
PRTL_USER_PROCESS_PARAMETERS NTAPI RtlDeNormalizeProcessParams(PRTL_USER_PROCESS_PARAMETERS Params)
Definition: ppb.c:256
PRTL_USER_PROCESS_PARAMETERS NTAPI RtlNormalizeProcessParams(PRTL_USER_PROCESS_PARAMETERS Params)
Definition: ppb.c:281
NTSTATUS NTAPI RtlDestroyProcessParameters(IN PRTL_USER_PROCESS_PARAMETERS ProcessParameters)
Definition: ppb.c:244
NTSTATUS NTAPI RtlCreateProcessParameters(PRTL_USER_PROCESS_PARAMETERS *ProcessParameters, PUNICODE_STRING ImagePathName, PUNICODE_STRING DllPath, PUNICODE_STRING CurrentDirectory, PUNICODE_STRING CommandLine, PWSTR Environment, PUNICODE_STRING WindowTitle, PUNICODE_STRING DesktopInfo, PUNICODE_STRING ShellInfo, PUNICODE_STRING RuntimeData)
Definition: ppb.c:49
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:73
HANDLE Handle
Definition: rtltypes.h:1363
UNICODE_STRING DosPath
Definition: rtltypes.h:1362
UNICODE_STRING RuntimeData
Definition: rtltypes.h:1576
UNICODE_STRING CommandLine
Definition: btrfs_drv.h:1902
UNICODE_STRING ImagePathName
Definition: btrfs_drv.h:1901
UNICODE_STRING WindowTitle
Definition: rtltypes.h:1573
UNICODE_STRING ShellInfo
Definition: rtltypes.h:1575
UNICODE_STRING DesktopInfo
Definition: rtltypes.h:1574
USHORT MaximumLength
Definition: env_spec_w32.h:370
uint16_t * PWSTR
Definition: typedefs.h:56
#define NTAPI
Definition: typedefs.h:36
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define IN
Definition: typedefs.h:39
uint16_t * PWCHAR
Definition: typedefs.h:56
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define ALIGN_UP_POINTER_BY(ptr, align)
Definition: umtypes.h:85
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4539
_In_ WDFIOTARGET _In_ PWDF_REQUEST_COMPLETION_PARAMS Params
Definition: wdfrequest.h:308
NTSYSAPI void WINAPI RtlReleasePebLock(void)
Definition: libsupp.c:84
NTSYSAPI void WINAPI RtlAcquirePebLock(void)
Definition: libsupp.c:74