VBoxInstallHelper.cpp revision 1cede4c702f5b7f8734214a2a5d5bc516b47b330
/* $Id$ */
/** @file
* VBoxInstallHelper - Various helper routines for Windows host installer.
*/
/*
* Copyright (C) 2008-2011 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#ifdef VBOX_WITH_NETFLT
# include "VBox/VBoxNetCfg-win.h"
# include "VBox/VBoxDrvCfg-win.h"
#endif /* VBOX_WITH_NETFLT */
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <msi.h>
#include <msiquery.h>
#define _WIN32_DCOM
#include <windows.h>
#include <assert.h>
#include <shellapi.h>
#define INITGUID
#include <guiddef.h>
#include <devguid.h>
#include <objbase.h>
#include <setupapi.h>
#include <shlobj.h>
#include <cfgmgr32.h>
#include "VBoxCommon.h"
#ifndef VBOX_OSE
# include "internal/VBoxSerial.h"
#endif
#ifdef DEBUG
#else
# define NonStandardAssert(_expr) do{ }while(0)
#endif
{
return TRUE;
}
{
char szBuffer[1024] = {0};
#ifdef DEBUG
#endif
}
{
}
{
#ifndef VBOX_OSE
#endif
return ERROR_SUCCESS;
}
{
#ifndef VBOX_OSE
#endif
return ERROR_SUCCESS;
}
{
#ifdef UNICODE
#endif
SetLastError(0);
pszCmdLine, /* Command line. */
NULL, /* Process handle not inheritable. */
NULL, /* Thread handle not inheritable. */
FALSE, /* Set handle inheritance to FALSE .*/
0, /* No creation flags. */
NULL, /* Use parent's environment block. */
pszWorkDir, /* Use parent's starting directory. */
&si, /* Pointer to STARTUPINFO structure. */
&pi)) /* Pointer to PROCESS_INFORMATION structure. */
{
rc = GetLastError();
return rc;
}
/* Wait until child process exits. */
{
rc = GetLastError();
}
else
{
{
rc = GetLastError();
}
}
/* Close process and thread handles. */
return rc;
}
{
LONG rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Python\\PythonCore", 0, KEY_READ, &hkPythonCore);
if (rc != ERROR_SUCCESS)
{
return ERROR_SUCCESS;
}
for (int i = 0;; ++i)
{
break;
if (rc != ERROR_SUCCESS)
continue;
if (rc == ERROR_SUCCESS)
}
/* Python path found? */
DWORD dwExitCode = 0;
{
/* Cool, check for installed Win32 extensions. */
_stprintf_s(szCmdLine, sizeof(szCmdLine) / sizeof(TCHAR), L"%s\\python.exe -c \"import win32api\"", szVal);
if ( (ERROR_SUCCESS == dwRetExec)
&& ( 0 == dwExitCode))
{
/* Did we get the correct error level (=0)? */
}
else
}
if (bFound) /* Is Python and all required stuff installed? */
{
/* Get the VBoxAPI setup string. */
if (_tcslen(szPathTargetDir))
{
/* Set final path. */
/* Install our API module. */
_stprintf_s(szCmdLine, sizeof(szCmdLine) / sizeof(TCHAR), L"%s\\python.exe vboxapisetup.py install", szVal);
/* Set required environment variables. */
{
}
else
{
if ( (ERROR_SUCCESS == dwRetExec)
&& ( 0 == dwExitCode))
{
/* All done! */
bInstalled = TRUE;
}
else
{
if (dwRetExec)
LogStringW(hModule, TEXT("InstallPythonAPI: Error while executing installation of VBox API: %ld"), dwRetExec);
else
LogStringW(hModule, TEXT("InstallPythonAPI: Python reported an error while installing VBox API: %ld"), dwExitCode);
}
}
}
else
}
if (!bInstalled)
return ERROR_SUCCESS; /* Do not fail here. */
}
const TCHAR* pszFileName,
const TCHAR* pszSection,
{
{
_stprintf_s(szKey, sizeof(szKey) / sizeof(TCHAR), L"SOFTWARE\\%s\\VirtualBox\\Branding\\", VBOX_VENDOR_SHORT, pszSection);
else
_stprintf_s(szKey, sizeof(szKey) / sizeof(TCHAR), L"SOFTWARE\\%s\\VirtualBox\\Branding", VBOX_VENDOR_SHORT);
0, KEY_WRITE, &hkBranding);
if (rc == ERROR_SUCCESS)
{
NULL,
if (rc != ERROR_SUCCESS)
}
}
else
return rc;
}
{
SHFILEOPSTRUCT s = {0};
s.fFlags = FOF_SILENT |
int r = SHFileOperation(&s);
if (r != 0)
{
}
else
rc = ERROR_SUCCESS;
return rc;
}
{
SHFILEOPSTRUCT s = {0};
s.fFlags = FOF_SILENT |
int r = SHFileOperation(&s);
if (r != 0)
{
}
else
rc = ERROR_SUCCESS;
return rc;
}
{
SHFILEOPSTRUCT s = {0};
s.fFlags = FOF_SILENT |
int r = SHFileOperation(&s);
if (r != 0)
{
}
else
rc = ERROR_SUCCESS;
return rc;
}
{
if (rc == ERROR_SUCCESS)
{
/** @todo Check trailing slash after %s. */
if (rc != ERROR_SUCCESS)
{
/* Check for hidden .custom directory and remove it. */
}
}
return ERROR_SUCCESS; /* Do not fail here. */
}
{
if (rc == ERROR_SUCCESS)
{
if (rc == ERROR_SUCCESS)
{
/** @todo Check for trailing slash after %s. */
if (rc == ERROR_SUCCESS)
{
}
}
}
return ERROR_SUCCESS; /* Do not fail here. */
}
#ifdef VBOX_WITH_NETFLT
/** @todo should use some real VBox app name */
#define VBOX_NETCFG_APP_NAME L"VirtualBox Installer"
#define VBOX_NETCFG_MAX_RETRIES 10
#define NETFLT_PT_INF_REL_PATH L"drivers\\network\\netflt\\VBoxNetFlt.inf"
#define NETFLT_MP_INF_REL_PATH L"drivers\\network\\netflt\\VBoxNetFltM.inf"
{
if (g_hCurrentModule)
}
static VOID netCfgLoggerDisable()
{
if (g_hCurrentModule)
{
}
}
{
if (g_hCurrentModule)
}
{
switch (hr)
{
case S_OK:
break;
case NETCFG_S_REBOOT:
{
if (hr2 != ERROR_SUCCESS)
break;
}
default:
}
return uRet;
}
{
if (hRecord)
{
if (uErr != ERROR_SUCCESS)
{
LogStringW(hModule, TEXT("createNetCfgLockedMsgRecord: MsiRecordSetInteger failed, error = 0x%x"), uErr);
}
}
else
return hRecord;
}
{
int MsgResult;
int cRetries = 0;
do
{
if (hr != NETCFG_E_NO_WRITE_LOCK)
{
break;
}
/* hr == NETCFG_E_NO_WRITE_LOCK */
if (!lpszLockedBy)
{
break;
}
/* on vista the 6to4svc.dll periodically maintains the lock for some reason,
* if this is the case, increase the wait period by retrying multiple times
* NOTE: we could alternatively increase the wait timeout,
* however it seems unneeded for most cases, e.g. in case some network connection property
* dialog is opened, it would be better to post a notification to the user as soon as possible
* rather than waiting for a longer period of time before displaying it */
if ( cRetries < VBOX_NETCFG_MAX_RETRIES
{
cRetries++;
LogStringW(hModule, TEXT("doNetCfgInit: lpszLockedBy is 6to4svc.dll, retrying %d out of %d"), cRetries, VBOX_NETCFG_MAX_RETRIES);
}
else
{
if (!hMsg)
{
if (!hMsg)
{
break;
}
}
if (rTmp != ERROR_SUCCESS)
{
break;
}
MsgResult = MsiProcessMessage(hModule, (INSTALLMESSAGE)(INSTALLMESSAGE_USER | MB_RETRYCANCEL), hMsg);
}
if (hMsg)
return uErr;
}
static UINT vboxNetFltQueryInfArray(MSIHANDLE hModule, OUT LPWSTR *apInfFullPaths, PUINT pcInfs, DWORD dwSize)
{
if (*pcInfs >= 2)
{
*pcInfs = 2;
if ( uErr == ERROR_SUCCESS
&& dwBuf)
{
/** @todo r=andy Avoid wcscpy and wcsncat, can cause buffer overruns! */
}
else
{
if (uErr != ERROR_SUCCESS)
else
}
}
else
{
}
return uErr;
}
#endif /*VBOX_WITH_NETFLT*/
{
#ifdef VBOX_WITH_NETFLT
{
if (uErr == ERROR_SUCCESS)
{
LogStringW(hModule, TEXT("UninstallNetFlt: VBoxNetCfgWinUninstallComponent failed, error = 0x%x"), hr);
/* Never fail on uninstall. */
}
else
}
{
if (bOldIntMode)
{
/* The prev mode != FALSE, i.e. non-interactive. */
}
}
#endif /* VBOX_WITH_NETFLT */
/* Never fail the install even if we did not succeed. */
return ERROR_SUCCESS;
}
{
#ifdef VBOX_WITH_NETFLT
{
if (uErr == ERROR_SUCCESS)
{
if (uErr == ERROR_SUCCESS)
{
}
else
}
else
}
{
if (bOldIntMode)
{
/* The prev mode != FALSE, i.e. non-interactive. */
}
}
#endif /* VBOX_WITH_NETFLT */
/* Never fail the install even if we did not succeed. */
return ERROR_SUCCESS;
}
#if 0
static BOOL RenameHostOnlyConnectionsCallback(HDEVINFO hDevInfo, PSP_DEVINFO_DATA pDev, PVOID pContext)
{
SPDRP_FRIENDLYNAME , /* IN DWORD Property,*/
NULL, /*OUT PDWORD PropertyRegDataType, OPTIONAL*/
sizeof(DevName), /* IN DWORD PropertyBufferSize,*/
NULL /*OUT PDWORD RequiredSize OPTIONAL*/
))
{
DICS_FLAG_GLOBAL, /* IN DWORD Scope,*/
0, /*IN DWORD HwProfile, */
DIREG_DRV, /* IN DWORD KeyType, */
KEY_READ /*IN REGSAM samDesired*/
);
if (hKey != INVALID_HANDLE_VALUE)
{
L"NetCfgInstanceId", /*__in_opt LPCTSTR lpValueName,*/
NULL, /*__reserved LPDWORD lpReserved,*/
NULL, /*__out_opt LPDWORD lpType,*/
&cbGuid /*guid__inout_opt LPDWORD lpcbData*/
);
if (winEr == ERROR_SUCCESS)
{
{
}
}
}
}
else
{
}
return TRUE;
}
#endif
{
#ifdef VBOX_WITH_NETFLT
bool bSetStaticIp = true;
{
LogStringW(hModule, TEXT("CreateHostOnlyInterface: VBoxNetCfgWinRemoveAllNetDevicesOfId failed, error = 0x%x"), hr);
bSetStaticIp = false;
}
bool bIsFile = false;
if (uErr == ERROR_SUCCESS)
{
if (cSize)
{
{
++cSize;
}
/** @todo r=andy Avoid wcscat, can cause buffer overruns! */
bIsFile = true;
}
else
}
else
LogStringW(hModule, TEXT("CreateHostOnlyInterface: Failed to get NetAdpDir property, error = 0x%x"), uErr);
/* Make sure the inf file is installed. */
{
}
{
{
LogStringW(hModule, TEXT("CreateHostOnlyInterface: VBoxNetCfgWinEnableStaticIpConfig failed, error = 0x%x"), hr);
}
else
LogStringW(hModule, TEXT("CreateHostOnlyInterface: VBoxNetCfgWinCreateHostOnlyNetworkInterface failed, error = 0x%x"), hr);
}
/* Restore original setup mode. */
#endif /* VBOX_WITH_NETFLT */
/* Never fail the install even if we did not succeed. */
return ERROR_SUCCESS;
}
{
#ifdef VBOX_WITH_NETFLT
{
hr = VBoxDrvCfgInfUninstallAllSetupDi(&GUID_DEVCLASS_NET, NETADP_ID, L"Net", 0/* could be SUOI_FORCEDELETE */);
{
LogStringW(hModule, TEXT("RemoveHostOnlyInterfaces: NetAdp uninstalled successfully, but failed to remove infs\n"));
}
}
else
/* Restore original setup mode. */
#endif /* VBOX_WITH_NETFLT */
/* Never fail the install even if we did not succeed. */
return ERROR_SUCCESS;
}
{
bool bIsTapDevice = false;
TEXT("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"),
if (lStatus != ERROR_SUCCESS)
return false;
int i = 0;
while (true)
{
if (lStatus != ERROR_SUCCESS)
break;
if (lStatus == ERROR_SUCCESS)
{
dwLen = sizeof(szNetCfgInstanceId);
lStatus = RegQueryValueEx(hNetCardGUID, TEXT("NetCfgInstanceId"), NULL, &dwKeyType, (LPBYTE)szNetCfgInstanceId, &dwLen);
if ( lStatus == ERROR_SUCCESS
{
szNetProductName[0] = 0;
dwLen = sizeof(szNetProductName);
lStatus = RegQueryValueEx(hNetCardGUID, TEXT("ProductName"), NULL, &dwKeyType, (LPBYTE)szNetProductName, &dwLen);
szNetProviderName[0] = 0;
dwLen = sizeof(szNetProviderName);
lStatus = RegQueryValueEx(hNetCardGUID, TEXT("ProviderName"), NULL, &dwKeyType, (LPBYTE)szNetProviderName, &dwLen);
)
)
{
bIsTapDevice = true;
break;
}
}
}
++i;
}
return bIsTapDevice;
}
#define SetErrBreak(strAndArgs) \
if (1) { \
rc = 0; \
break; \
} else do {} while (0)
{
int rc = 1;
do
{
/* We have to find the device instance ID through a registry search */
HKEY hkeyNetwork = 0;
HKEY hkeyConnection = 0;
do
{
TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\")
TEXT("{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s"),
pcGUID);
KEY_READ, &hkeyNetwork);
SetErrBreak((TEXT("VBox HostInterfaces: Host interface network was not found in registry (%s)! [1]"), strRegLocation));
SetErrBreak((TEXT("VBox HostInterfaces: Host interface network was not found in registry (%s)! [2]"), strRegLocation));
SetErrBreak((TEXT("VBox HostInterfaces: Host interface network was not found in registry (%s)! [3]"), strRegLocation));
}
while (0);
if (hkeyConnection)
if (hkeyNetwork)
/*
* Now we are going to enumerate all network devices and
* wait until we encounter the right device instance ID
*/
do
{
/* initialize the structure size */
/* copy the net class GUID */
/* return a device info set contains all installed devices of the Net class */
if (hDeviceInfo == INVALID_HANDLE_VALUE)
{
LogStringW(hModule, TEXT("VBox HostInterfaces: SetupDiGetClassDevs failed (0x%08X)!"), GetLastError());
}
/* enumerate the driver info list */
while (TRUE)
{
if (!fResult)
{
if (GetLastError() == ERROR_NO_MORE_ITEMS)
break;
else
{
index++;
continue;
}
}
/* try to get the hardware ID registry property */
NULL,
NULL,
0,
&size);
if (!fResult)
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
index++;
continue;
}
if (pszDeviceHwid)
{
NULL,
size,
NULL);
if (!fResult)
{
index++;
continue;
}
}
}
else
{
/* something is wrong. This shouldn't have worked with a NULL buffer */
index++;
continue;
}
for (TCHAR *t = pszDeviceHwid;
t += _tcslen (t) + 1)
{
if (!_tcsicmp(VBOX_TAP_HWID, t))
{
/* get the device instance ID */
{
/* compare to what we determined before */
{
fFoundDevice = TRUE;
break;
}
}
}
}
if (pszDeviceHwid)
{
}
if (fFoundDevice)
break;
index++;
}
if (fFoundDevice)
{
if (!fResult)
{
LogStringW(hModule, TEXT("VBox HostInterfaces: SetupDiSetSelectedDevice failed (0x%08X)!"), GetLastError());
}
if (!fResult)
{
LogStringW(hModule, TEXT("VBox HostInterfaces: SetupDiCallClassInstaller (DIF_REMOVE) failed (0x%08X)!"), GetLastError());
}
}
else
}
while (0);
/* clean up the device info set */
if (hDeviceInfo != INVALID_HANDLE_VALUE)
}
while (0);
return rc;
}
{
TEXT("{4D36E972-E325-11CE-BFC1-08002BE10318}");
if (lStatus == ERROR_SUCCESS)
{
for (int i = 0;; ++ i)
{
if (lStatus != ERROR_SUCCESS)
{
switch (lStatus)
{
case ERROR_NO_MORE_ITEMS:
break;
default:
break;
};
break;
}
if (IsTAPDevice(szNetworkGUID))
{
}
}
}
return ERROR_SUCCESS;
}