ReactOS  0.4.14-dev-114-gc8cbd56
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 
25 static __inline VOID
29  USHORT Size)
30 {
31  Destination->Length = Source->Length;
32  Destination->MaximumLength = Size ? Size : Source->MaximumLength;
34  if (Source->Length)
35  memmove (Destination->Buffer, Source->Buffer, Source->Length);
36  Destination->Buffer[Destination->Length / sizeof(WCHAR)] = 0;
37  *Ptr += Destination->MaximumLength/sizeof(WCHAR);
38 }
39 
40 
41 /*
42  * @implemented
43  */
46  PUNICODE_STRING ImagePathName,
49  PUNICODE_STRING CommandLine,
51  PUNICODE_STRING WindowTitle,
52  PUNICODE_STRING DesktopInfo,
53  PUNICODE_STRING ShellInfo,
54  PUNICODE_STRING RuntimeData)
55 {
57  ULONG Length = 0;
58  PWCHAR Dest;
59  UNICODE_STRING EmptyString;
60  HANDLE CurrentDirectoryHandle;
61  HANDLE ConsoleHandle;
62  ULONG ConsoleFlags;
63 
64  DPRINT ("RtlCreateProcessParameters\n");
65 
67 
68  EmptyString.Length = 0;
69  EmptyString.MaximumLength = sizeof(WCHAR);
70  EmptyString.Buffer = L"";
71 
72  if (RtlpGetMode() == UserMode)
73  {
74  if (DllPath == NULL)
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;
83  }
84  else
85  {
86  if (DllPath == NULL)
87  DllPath = &EmptyString;
88  if (CurrentDirectory == NULL)
89  CurrentDirectory = &EmptyString;
90  CurrentDirectoryHandle = NULL;
91  ConsoleHandle = NULL;
92  ConsoleFlags = 0;
93  }
94 
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;
105 
106  /* size of process parameter block */
108 
109  /* size of current directory buffer */
110  Length += (MAX_PATH * sizeof(WCHAR));
111 
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));
120 
121  /* Calculate the required block size */
122  Param = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, Length);
123  if (!Param)
124  {
127  }
128 
129  DPRINT ("Process parameters allocated\n");
130 
131  Param->MaximumLength = Length;
132  Param->Length = Length;
134  Param->Environment = Environment;
135  Param->CurrentDirectory.Handle = CurrentDirectoryHandle;
136  Param->ConsoleHandle = ConsoleHandle;
137  Param->ConsoleFlags = ConsoleFlags;
138 
139  Dest = (PWCHAR)(((PBYTE)Param) + sizeof(RTL_USER_PROCESS_PARAMETERS));
140 
141  /* copy current directory */
143  &Param->CurrentDirectory.DosPath,
145  MAX_PATH * sizeof(WCHAR));
146 
147  /* make sure the current directory has a trailing backslash */
148  if (Param->CurrentDirectory.DosPath.Length > 0)
149  {
150  Length = Param->CurrentDirectory.DosPath.Length / sizeof(WCHAR);
151  if (Param->CurrentDirectory.DosPath.Buffer[Length-1] != L'\\')
152  {
153  Param->CurrentDirectory.DosPath.Buffer[Length] = L'\\';
154  Param->CurrentDirectory.DosPath.Buffer[Length + 1] = 0;
155  Param->CurrentDirectory.DosPath.Length += sizeof(WCHAR);
156  }
157  }
158 
159  /* copy dll path */
161  &Param->DllPath,
162  DllPath,
163  0);
164 
165  /* copy image path name */
167  &Param->ImagePathName,
168  ImagePathName,
169  ImagePathName->Length + sizeof(WCHAR));
170 
171  /* copy command line */
173  &Param->CommandLine,
174  CommandLine,
175  CommandLine->Length + sizeof(WCHAR));
176 
177  /* copy title */
179  &Param->WindowTitle,
180  WindowTitle,
181  0);
182 
183  /* copy desktop */
185  &Param->DesktopInfo,
186  DesktopInfo,
187  0);
188 
189  /* copy shell info */
191  &Param->ShellInfo,
192  ShellInfo,
193  0);
194 
195  /* copy runtime info */
197  &Param->RuntimeData,
198  RuntimeData,
199  0);
200 
202  *ProcessParameters = Param;
204 
205  return STATUS_SUCCESS;
206 }
207 
208 /*
209  * @implemented
210  */
211 NTSTATUS
212 NTAPI
214 {
215  RtlFreeHeap(RtlGetProcessHeap(), 0, ProcessParameters);
216  return STATUS_SUCCESS;
217 }
218 
219 /*
220  * denormalize process parameters (Pointer-->Offset)
221  *
222  * @implemented
223  */
226 {
227  if (Params && (Params->Flags & RTL_USER_PROCESS_PARAMETERS_NORMALIZED))
228  {
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);
237 
239  }
240 
241  return Params;
242 }
243 
244 /*
245  * normalize process parameters (Offset-->Pointer)
246  *
247  * @implemented
248  */
251 {
252  if (Params && !(Params->Flags & RTL_USER_PROCESS_PARAMETERS_NORMALIZED))
253  {
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);
262 
264  }
265 
266  return Params;
267 }
268 
269 /* EOF */
#define NORMALIZE(x, addr)
Definition: ppb.c:18
PVOID PVOID PWCHAR PVOID Environment
Definition: env.c:45
#define IN
Definition: typedefs.h:38
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
KPROCESSOR_MODE NTAPI RtlpGetMode(VOID)
Definition: libsupp.c:53
USHORT MaximumLength
Definition: env_spec_w32.h:370
WCHAR CurrentDirectory[1024]
Definition: chkdsk.c:74
UNICODE_STRING WindowTitle
Definition: rtltypes.h:1525
uint16_t * PWSTR
Definition: typedefs.h:54
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
PRTL_USER_PROCESS_PARAMETERS NTAPI RtlDeNormalizeProcessParams(PRTL_USER_PROCESS_PARAMETERS Params)
Definition: ppb.c:225
VOID NTAPI RtlReleasePebLock(VOID)
Definition: libsupp.c:82
UNICODE_STRING ShellInfo
Definition: rtltypes.h:1527
uint16_t * PWCHAR
Definition: typedefs.h:54
UNICODE_STRING DesktopInfo
Definition: rtltypes.h:1526
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
struct _RTL_USER_PROCESS_PARAMETERS RTL_USER_PROCESS_PARAMETERS
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:45
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define MAX_PATH
Definition: compat.h:26
#define ALIGN(x, align)
Definition: ppb.c:20
_In_ PUNICODE_STRING _Inout_ PUNICODE_STRING Destination
Definition: rtlfuncs.h:2891
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define DENORMALIZE(x, addr)
Definition: ppb.c:19
UNICODE_STRING ImagePathName
Definition: btrfs_drv.h:1805
static const WCHAR L[]
Definition: oid.c:1250
NTSTATUS NTAPI RtlDestroyProcessParameters(IN PRTL_USER_PROCESS_PARAMETERS ProcessParameters)
Definition: ppb.c:213
UNICODE_STRING CommandLine
Definition: btrfs_drv.h:1806
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
UNICODE_STRING RuntimeData
Definition: rtltypes.h:1528
#define NtCurrentPeb()
Definition: FLS.c:20
unsigned short USHORT
Definition: pedump.c:61
static const char const char * DllPath
Definition: image.c:34
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
VOID NTAPI RtlAcquirePebLock(VOID)
Definition: libsupp.c:72
UNICODE_STRING DosPath
Definition: rtltypes.h:1362
unsigned int ULONG
Definition: retypes.h:1
HANDLE Handle
Definition: rtltypes.h:1363
_In_ UINT _In_ UINT _In_ PNDIS_PACKET Source
Definition: ndis.h:3167
#define RTL_USER_PROCESS_PARAMETERS_NORMALIZED
Definition: rtltypes.h:41
return STATUS_SUCCESS
Definition: btrfs.c:2966
BYTE * PBYTE
Definition: pedump.c:66
static __inline VOID RtlpCopyParameterString(PWCHAR *Ptr, PUNICODE_STRING Destination, PUNICODE_STRING Source, USHORT Size)
Definition: ppb.c:26
PRTL_USER_PROCESS_PARAMETERS NTAPI RtlNormalizeProcessParams(PRTL_USER_PROCESS_PARAMETERS Params)
Definition: ppb.c:250