process-win32.cpp revision c97989161fbe75bc14cea477a5443bbf474dd3ad
45e9809aff7304721fddb95654901b32195c9c7avboxsync * innotek Portable Runtime - Process, Win32.
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Copyright (C) 2006-2007 innotek GmbH
45e9809aff7304721fddb95654901b32195c9c7avboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
45e9809aff7304721fddb95654901b32195c9c7avboxsync * available from http://www.virtualbox.org. This file is free software;
45e9809aff7304721fddb95654901b32195c9c7avboxsync * you can redistribute it and/or modify it under the terms of the GNU
45e9809aff7304721fddb95654901b32195c9c7avboxsync * General Public License as published by the Free Software Foundation,
45e9809aff7304721fddb95654901b32195c9c7avboxsync * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
45e9809aff7304721fddb95654901b32195c9c7avboxsync * distribution. VirtualBox OSE is distributed in the hope that it will
45e9809aff7304721fddb95654901b32195c9c7avboxsync * be useful, but WITHOUT ANY WARRANTY of any kind.
45e9809aff7304721fddb95654901b32195c9c7avboxsync * If you received this file as part of a commercial VirtualBox
45e9809aff7304721fddb95654901b32195c9c7avboxsync * distribution, then only the terms of your commercial VirtualBox
45e9809aff7304721fddb95654901b32195c9c7avboxsync * license agreement apply instead of the previous paragraph.
45e9809aff7304721fddb95654901b32195c9c7avboxsync/*******************************************************************************
45e9809aff7304721fddb95654901b32195c9c7avboxsync* Header Files *
45e9809aff7304721fddb95654901b32195c9c7avboxsync*******************************************************************************/
45e9809aff7304721fddb95654901b32195c9c7avboxsync * This is from Winternl.h. It has been copied here
45e9809aff7304721fddb95654901b32195c9c7avboxsync * because the header does not define a calling convention for
45e9809aff7304721fddb95654901b32195c9c7avboxsync * its prototypes and just assumes that _stdcall is the standard
45e9809aff7304721fddb95654901b32195c9c7avboxsync * calling convention.
45e9809aff7304721fddb95654901b32195c9c7avboxsynctypedef struct _PEB {
45e9809aff7304721fddb95654901b32195c9c7avboxsync/** @todo r=michael This function currently does not work correctly if the arguments
45e9809aff7304721fddb95654901b32195c9c7avboxsync contain spaces. */
45e9809aff7304721fddb95654901b32195c9c7avboxsyncRTR3DECL(int) RTProcCreate(const char *pszExec, const char * const *papszArgs, const char * const *papszEnv, unsigned fFlags, PRTPROCESS pProcess)
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Validate input.
45e9809aff7304721fddb95654901b32195c9c7avboxsync /* later: path searching. */
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Spawn the child.
45e9809aff7304721fddb95654901b32195c9c7avboxsync /** @todo utf-8 considerations! */
45e9809aff7304721fddb95654901b32195c9c7avboxsync HANDLE hProcess = (HANDLE)_spawnve(_P_NOWAITO, pszExec, papszArgs, papszEnv);
45e9809aff7304721fddb95654901b32195c9c7avboxsync if (hProcess != 0 && hProcess != INVALID_HANDLE_VALUE)
45e9809aff7304721fddb95654901b32195c9c7avboxsync * GetProcessId requires XP SP1 or later
45e9809aff7304721fddb95654901b32195c9c7avboxsync#else /* !__WIN64__ */
45e9809aff7304721fddb95654901b32195c9c7avboxsync static bool fInitialized = false;
45e9809aff7304721fddb95654901b32195c9c7avboxsync static DWORD (WINAPI *pfnGetProcessId)(HANDLE Thread) = NULL;
45e9809aff7304721fddb95654901b32195c9c7avboxsync HMODULE hmodKernel32 = GetModuleHandle("KERNEL32.DLL");
45e9809aff7304721fddb95654901b32195c9c7avboxsync pfnGetProcessId = (DWORD (WINAPI*)(HANDLE))GetProcAddress(hmodKernel32, "GetProcessId");
45e9809aff7304721fddb95654901b32195c9c7avboxsync AssertMsgFailed(("failed to get pid from hProcess=%#x rc=%Vrc\n", hProcess, rc));
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Fall back to the NT api for older versions.
45e9809aff7304721fddb95654901b32195c9c7avboxsync ULONG Status = NtQueryInformationProcess(hProcess, ProcessBasicInformation,
45e9809aff7304721fddb95654901b32195c9c7avboxsync int rc = ERROR_INTERNAL_ERROR; /* (we don't have a valid conversion here, but this shouldn't happen anyway.) */
45e9809aff7304721fddb95654901b32195c9c7avboxsync AssertMsgFailed(("failed to get pid from hProcess=%#x rc=%Vrc Status=%#x\n", hProcess, rc, Status));
45e9809aff7304721fddb95654901b32195c9c7avboxsync#endif /* !__WIN64__ */
45e9809aff7304721fddb95654901b32195c9c7avboxsync AssertMsgFailed(("spawn/exec failed rc=%Vrc\n", rc)); /* this migth be annoying... */
45e9809aff7304721fddb95654901b32195c9c7avboxsyncRTR3DECL(int) RTProcWait(RTPROCESS Process, unsigned fFlags, PRTPROCSTATUS pProcStatus)
AssertReturn(!(fFlags & ~(RTPROCWAIT_FLAGS_BLOCK | RTPROCWAIT_FLAGS_NOBLOCK)), VERR_INVALID_PARAMETER);
switch (WaitRc)
case WAIT_OBJECT_0:
if (pProcStatus)
return VINF_SUCCESS;
case WAIT_TIMEOUT:
return VERR_PROCESS_RUNNING;
case WAIT_FAILED:
case WAIT_ABANDONED:
AssertFailed();
return VERR_GENERAL_FAILURE;
return VERR_GENERAL_FAILURE;
if (fRc)
return VINF_SUCCESS;
BOOL fRc = GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinityMask, &dwSystemAffinityMask);
return dwProcessAffinityMask;
return pszExecName;
return NULL;