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