Changed the loader so ZwCreateProcess now maps ntdll into memory
[reactos.git] / reactos / lib / kernel32 / process / proc.c
1 /* $Id: proc.c,v 1.29 2000/01/27 08:56:47 dwelch Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/proc/proc.c
6 * PURPOSE: Process functions
7 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
8 * UPDATE HISTORY:
9 * Created 01/11/98
10 */
11
12 /* INCLUDES ****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <windows.h>
16 #include <kernel32/proc.h>
17 #include <kernel32/thread.h>
18 #include <wchar.h>
19 #include <string.h>
20 #include <internal/i386/segment.h>
21 #include <internal/teb.h>
22
23 #define NDEBUG
24 #include <kernel32/kernel32.h>
25
26 /* TYPES *********************************************************************/
27 /*
28 typedef struct _WSTARTUPINFO {
29 DWORD cb;
30 LPWSTR lpReserved;
31 LPWSTR lpDesktop;
32 LPWSTR lpTitle;
33 DWORD dwX;
34 DWORD dwY;
35 DWORD dwXSize;
36 DWORD dwYSize;
37 DWORD dwXCountChars;
38 DWORD dwYCountChars;
39 DWORD dwFillAttribute;
40 DWORD dwFlags;
41 WORD wShowWindow;
42 WORD cbReserved2;
43 LPBYTE lpReserved2;
44 HANDLE hStdInput;
45 HANDLE hStdOutput;
46 HANDLE hStdError;
47 } WSTARTUPINFO, *LPWSTARTUPINFO;
48 */
49
50 /* GLOBALS *******************************************************************/
51
52 WaitForInputIdleType lpfnGlobalRegisterWaitForInputIdle;
53
54 VOID
55 STDCALL
56 RegisterWaitForInputIdle (
57 WaitForInputIdleType lpfnRegisterWaitForInputIdle
58 );
59
60 /* FUNCTIONS ****************************************************************/
61
62 WINBOOL
63 STDCALL
64 GetProcessId (
65 HANDLE hProcess,
66 LPDWORD lpProcessId
67 );
68
69
70
71
72
73 WINBOOL
74 STDCALL
75 GetProcessTimes (
76 HANDLE hProcess,
77 LPFILETIME lpCreationTime,
78 LPFILETIME lpExitTime,
79 LPFILETIME lpKernelTime,
80 LPFILETIME lpUserTime
81 )
82 {
83 DPRINT("GetProcessTimes is unimplemented\n");
84 return FALSE;
85 }
86
87
88 HANDLE STDCALL GetCurrentProcess (VOID)
89 {
90 return((HANDLE)NtCurrentProcess());
91 }
92
93
94 HANDLE STDCALL GetCurrentThread (VOID)
95 {
96 return((HANDLE)NtCurrentThread());
97 }
98
99
100 DWORD STDCALL GetCurrentProcessId (VOID)
101 {
102 return((DWORD)GetTeb()->Cid.UniqueProcess);
103 }
104
105
106 WINBOOL
107 STDCALL
108 GetExitCodeProcess (
109 HANDLE hProcess,
110 LPDWORD lpExitCode
111 )
112 {
113 NTSTATUS errCode;
114 PROCESS_BASIC_INFORMATION ProcessBasic;
115 ULONG BytesWritten;
116
117 errCode = NtQueryInformationProcess(hProcess,
118 ProcessBasicInformation,
119 &ProcessBasic,
120 sizeof(PROCESS_BASIC_INFORMATION),
121 &BytesWritten);
122 if (!NT_SUCCESS(errCode))
123 {
124 SetLastError(RtlNtStatusToDosError(errCode));
125 return FALSE;
126 }
127 memcpy(lpExitCode, &ProcessBasic.ExitStatus, sizeof(DWORD));
128 return TRUE;
129 }
130
131
132 WINBOOL
133 STDCALL
134 GetProcessId (
135 HANDLE hProcess,
136 LPDWORD lpProcessId
137 )
138 {
139 NTSTATUS errCode;
140 PROCESS_BASIC_INFORMATION ProcessBasic;
141 ULONG BytesWritten;
142
143 errCode = NtQueryInformationProcess(hProcess,
144 ProcessBasicInformation,
145 &ProcessBasic,
146 sizeof(PROCESS_BASIC_INFORMATION),
147 &BytesWritten);
148 if (!NT_SUCCESS(errCode))
149 {
150 SetLastError(RtlNtStatusToDosError(errCode));
151 return FALSE;
152 }
153 memcpy( lpProcessId ,&ProcessBasic.UniqueProcessId,sizeof(DWORD));
154 return TRUE;
155 }
156
157
158 PWSTR
159 InternalAnsiToUnicode (
160 PWSTR Out,
161 LPCSTR In,
162 ULONG MaxLength
163 )
164 {
165 ULONG i;
166
167 if (In == NULL)
168 {
169 return(NULL);
170 }
171 else
172 {
173 i = 0;
174 while ((*In)!=0 && i < MaxLength)
175 {
176 Out[i] = *In;
177 In++;
178 i++;
179 }
180 Out[i] = 0;
181 return(Out);
182 }
183 }
184
185
186 HANDLE
187 STDCALL
188 OpenProcess (
189 DWORD dwDesiredAccess,
190 WINBOOL bInheritHandle,
191 DWORD dwProcessId
192 )
193 {
194 NTSTATUS errCode;
195 HANDLE ProcessHandle;
196 OBJECT_ATTRIBUTES ObjectAttributes;
197 CLIENT_ID ClientId ;
198
199 ClientId.UniqueProcess = (HANDLE)dwProcessId;
200 ClientId.UniqueThread = INVALID_HANDLE_VALUE;
201
202 ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
203 ObjectAttributes.RootDirectory = (HANDLE)NULL;
204 ObjectAttributes.SecurityDescriptor = NULL;
205 ObjectAttributes.SecurityQualityOfService = NULL;
206 ObjectAttributes.ObjectName = NULL;
207
208 if (bInheritHandle == TRUE)
209 ObjectAttributes.Attributes = OBJ_INHERIT;
210 else
211 ObjectAttributes.Attributes = 0;
212
213 errCode = NtOpenProcess(&ProcessHandle,
214 dwDesiredAccess,
215 &ObjectAttributes,
216 &ClientId);
217 if (!NT_SUCCESS(errCode))
218 {
219 SetLastError(RtlNtStatusToDosError(errCode));
220 return NULL;
221 }
222 return ProcessHandle;
223 }
224
225
226 UINT
227 STDCALL
228 WinExec (
229 LPCSTR lpCmdLine,
230 UINT uCmdShow
231 )
232 {
233 STARTUPINFOA StartupInfo;
234 PROCESS_INFORMATION ProcessInformation;
235 HINSTANCE hInst;
236 DWORD dosErr;
237
238 StartupInfo.cb = sizeof(STARTUPINFOA);
239 StartupInfo.wShowWindow = uCmdShow;
240 StartupInfo.dwFlags = 0;
241
242 hInst = (HINSTANCE)CreateProcessA(NULL,
243 (PVOID)lpCmdLine,
244 NULL,
245 NULL,
246 FALSE,
247 0,
248 NULL,
249 NULL,
250 &StartupInfo,
251 &ProcessInformation);
252 if ( hInst == NULL )
253 {
254 dosErr = GetLastError();
255 return dosErr;
256 }
257 if ( lpfnGlobalRegisterWaitForInputIdle != NULL )
258 lpfnGlobalRegisterWaitForInputIdle(ProcessInformation.hProcess,10000);
259 NtClose(ProcessInformation.hProcess);
260 NtClose(ProcessInformation.hThread);
261 return 0;
262 }
263
264
265 VOID
266 STDCALL
267 RegisterWaitForInputIdle (
268 WaitForInputIdleType lpfnRegisterWaitForInputIdle
269 )
270 {
271 lpfnGlobalRegisterWaitForInputIdle = lpfnRegisterWaitForInputIdle;
272 return;
273 }
274
275
276 DWORD
277 STDCALL
278 WaitForInputIdle (
279 HANDLE hProcess,
280 DWORD dwMilliseconds
281 )
282 {
283 return 0;
284 }
285
286
287 VOID STDCALL Sleep (DWORD dwMilliseconds)
288 {
289 SleepEx (dwMilliseconds, FALSE);
290 return;
291 }
292
293 DWORD STDCALL SleepEx(DWORD dwMilliseconds,
294 BOOL bAlertable)
295 {
296 TIME Interval;
297 NTSTATUS errCode;
298
299 Interval.QuadPart = dwMilliseconds * 1000;
300
301 errCode = NtDelayExecution(bAlertable,&Interval);
302 if (!NT_SUCCESS(errCode))
303 {
304 SetLastError(RtlNtStatusToDosError(errCode));
305 return -1;
306 }
307 return 0;
308 }
309
310
311 VOID
312 STDCALL
313 GetStartupInfoW (
314 LPSTARTUPINFOW lpStartupInfo
315 )
316 {
317 PPEB pPeb = NtCurrentPeb();
318
319 if (lpStartupInfo == NULL)
320 {
321 SetLastError(ERROR_INVALID_PARAMETER);
322 return;
323 }
324
325 lpStartupInfo->cb = sizeof(STARTUPINFOW);
326 // lstrcpyW(lpStartupInfo->lpDesktop, pPeb->Ppb->Desktop);
327 // lstrcpyW(lpStartupInfo->lpTitle, pPeb->Ppb->Title);
328 lpStartupInfo->dwX = pPeb->ProcessParameters->X;
329 lpStartupInfo->dwY = pPeb->ProcessParameters->Y;
330 lpStartupInfo->dwXSize = pPeb->ProcessParameters->XSize;
331 lpStartupInfo->dwYSize = pPeb->ProcessParameters->YSize;
332 lpStartupInfo->dwXCountChars = pPeb->ProcessParameters->XCountChars;
333 lpStartupInfo->dwYCountChars = pPeb->ProcessParameters->YCountChars;
334 lpStartupInfo->dwFillAttribute = pPeb->ProcessParameters->FillAttribute;
335 lpStartupInfo->dwFlags = pPeb->ProcessParameters->Flags;
336 lpStartupInfo->wShowWindow = pPeb->ProcessParameters->ShowWindow;
337 // lpStartupInfo->lpReserved = pPeb->ProcessParameters->lpReserved1;
338 // lpStartupInfo->cbReserved2 = pPeb->ProcessParameters->cbReserved;
339 // lpStartupInfo->lpReserved2 = pPeb->ProcessParameters->lpReserved2;
340
341 lpStartupInfo->hStdInput = pPeb->ProcessParameters->InputHandle;
342 lpStartupInfo->hStdOutput = pPeb->ProcessParameters->OutputHandle;
343 lpStartupInfo->hStdError = pPeb->ProcessParameters->ErrorHandle;
344 }
345
346
347 VOID
348 STDCALL
349 GetStartupInfoA (
350 LPSTARTUPINFOA lpStartupInfo
351 )
352 {
353 PPEB pPeb = NtCurrentPeb();
354 ULONG i = 0;
355
356 if (lpStartupInfo == NULL)
357 {
358 SetLastError(ERROR_INVALID_PARAMETER);
359 return;
360 }
361
362 lpStartupInfo->cb = sizeof(STARTUPINFOA);
363 #if 0
364 i = 0;
365 while ((pPeb->ProcessParameters->Desktop[i])!=0 && i < MAX_PATH)
366 {
367 lpStartupInfo->lpDesktop[i] = (unsigned char)
368 pPeb->ProcessParameters->Desktop[i];
369 i++;
370 }
371 lpStartupInfo->lpDesktop[i] = 0;
372
373 i = 0;
374 while ((pPeb->ProcessParameters->Title[i])!=0 && i < MAX_PATH)
375 {
376 lpStartupInfo->lpTitle[i] = (unsigned char)pPeb->ProcessParameters->Title[i];
377 i++;
378 }
379 lpStartupInfo->lpTitle[i] = 0;
380 #endif
381 lpStartupInfo->dwX = pPeb->ProcessParameters->X;
382 lpStartupInfo->dwY = pPeb->ProcessParameters->Y;
383 lpStartupInfo->dwXSize = pPeb->ProcessParameters->XSize;
384 lpStartupInfo->dwYSize = pPeb->ProcessParameters->YSize;
385 lpStartupInfo->dwXCountChars = pPeb->ProcessParameters->XCountChars;
386 lpStartupInfo->dwYCountChars = pPeb->ProcessParameters->YCountChars;
387 lpStartupInfo->dwFillAttribute = pPeb->ProcessParameters->FillAttribute;
388 lpStartupInfo->dwFlags = pPeb->ProcessParameters->Flags;
389 lpStartupInfo->wShowWindow = pPeb->ProcessParameters->ShowWindow;
390 // lpStartupInfo->cbReserved2 = pPeb->ProcessParameters->cbReserved;
391 // lpStartupInfo->lpReserved = pPeb->ProcessParameters->lpReserved1;
392 // lpStartupInfo->lpReserved2 = pPeb->ProcessParameters->lpReserved2;
393
394 lpStartupInfo->hStdInput = pPeb->ProcessParameters->InputHandle;
395 lpStartupInfo->hStdOutput = pPeb->ProcessParameters->OutputHandle;
396 lpStartupInfo->hStdError = pPeb->ProcessParameters->ErrorHandle;
397 }
398
399
400 BOOL
401 STDCALL
402 FlushInstructionCache (
403 HANDLE hProcess,
404 LPCVOID lpBaseAddress,
405 DWORD dwSize
406 )
407 {
408 NTSTATUS errCode;
409
410 errCode = NtFlushInstructionCache(
411 hProcess,
412 (PVOID) lpBaseAddress,
413 dwSize
414 );
415 if (!NT_SUCCESS(errCode))
416 {
417 SetLastError(RtlNtStatusToDosError(errCode));
418 return FALSE;
419 }
420 return TRUE;
421 }
422
423
424 VOID
425 STDCALL
426 ExitProcess (
427 UINT uExitCode
428 )
429 {
430 NtTerminateProcess(
431 NtCurrentProcess(),
432 uExitCode
433 );
434 }
435
436
437 WINBOOL
438 STDCALL
439 TerminateProcess (
440 HANDLE hProcess,
441 UINT uExitCode
442 )
443 {
444 NTSTATUS errCode;
445 errCode = NtTerminateProcess(hProcess, uExitCode);
446 if (!NT_SUCCESS(errCode))
447 {
448 SetLastError(RtlNtStatusToDosError(errCode));
449 return FALSE;
450 }
451 return TRUE;
452 }
453
454 VOID
455 STDCALL
456 FatalAppExitA (
457 UINT uAction,
458 LPCSTR lpMessageText
459 )
460 {
461 WCHAR MessageTextW[MAX_PATH];
462 UINT i;
463 i = 0;
464 while ((*lpMessageText)!=0 && i < 35)
465 {
466 MessageTextW[i] = *lpMessageText;
467 lpMessageText++;
468 i++;
469 }
470 MessageTextW[i] = 0;
471
472 return FatalAppExitW(uAction,MessageTextW);
473 }
474
475
476 VOID
477 STDCALL
478 FatalAppExitW (
479 UINT uAction,
480 LPCWSTR lpMessageText
481 )
482 {
483 return;
484 }
485
486
487 /* EOF */