static int VBoxNetAdpInstall(void) { VBoxNetCfgWinSetLogging(winNetCfgLogger); HRESULT hr = CoInitialize(NULL); if (SUCCEEDED(hr)) { printf("adding host-only interface..\n"); DWORD dwErr = ERROR_SUCCESS; WCHAR MpInf[MAX_PATH]; if (!GetFullPathNameW(VBOX_NETADP_INF, sizeof(MpInf)/sizeof(MpInf[0]), MpInf, NULL)) dwErr = GetLastError(); if (dwErr == ERROR_SUCCESS) { hr = VBoxDrvCfgInfInstall(MpInf); if (FAILED(hr)) printf("VBoxDrvCfgInfInstall failed %#x\n", hr); GUID guid; BSTR name, errMsg; hr = VBoxNetCfgWinCreateHostOnlyNetworkInterface (MpInf, true, &guid, &name, &errMsg); if (SUCCEEDED(hr)) { ULONG ip, mask; hr = VBoxNetCfgWinGenHostOnlyNetworkNetworkIp(&ip, &mask); if (SUCCEEDED(hr)) { /* ip returned by VBoxNetCfgWinGenHostOnlyNetworkNetworkIp is a network ip, * i.e. 192.168.xxx.0, assign 192.168.xxx.1 for the hostonly adapter */ ip = ip | (1 << 24); hr = VBoxNetCfgWinEnableStaticIpConfig(&guid, ip, mask); if (SUCCEEDED(hr)) { printf("installation successful\n"); } else printf("VBoxNetCfgWinEnableStaticIpConfig failed: hr = 0x%x\n", hr); } else printf("VBoxNetCfgWinGenHostOnlyNetworkNetworkIp failed: hr = 0x%x\n", hr); } else printf("VBoxNetCfgWinCreateHostOnlyNetworkInterface failed: hr = 0x%x\n", hr); } else { printf("GetFullPathNameW failed: winEr = %d\n", dwErr); hr = HRESULT_FROM_WIN32(dwErr); } CoUninitialize(); } else printf("Error initializing COM (0x%x)\n", hr); VBoxNetCfgWinSetLogging(NULL); return SUCCEEDED(hr) ? 0 : 1; }
UINT __stdcall CreateHostOnlyInterface(MSIHANDLE hModule) { #ifdef VBOX_WITH_NETFLT netCfgLoggerEnable(hModule); BOOL bSetupModeInteractive = SetupSetNonInteractiveMode(FALSE); bool bSetStaticIp = true; logStringW(hModule, L"CreateHostOnlyInterface: Creating host-only interface"); HRESULT hr; GUID guid; WCHAR wszMpInf[MAX_PATH]; DWORD cchMpInf = RT_ELEMENTS(wszMpInf) - sizeof("VBoxNetAdp.inf") - 1; LPCWSTR pwszInfPath = NULL; bool bIsFile = false; UINT uErr = MsiGetPropertyW(hModule, L"CustomActionData", wszMpInf, &cchMpInf); if (uErr == ERROR_SUCCESS) { if (cchMpInf) { logStringW(hModule, L"CreateHostOnlyInterface: NetAdpDir property = %s", wszMpInf); if (wszMpInf[cchMpInf - 1] != L'\\') { wszMpInf[cchMpInf++] = L'\\'; wszMpInf[cchMpInf] = L'\0'; } wcscat(wszMpInf, L"VBoxNetAdp.inf"); pwszInfPath = wszMpInf; bIsFile = true; logStringW(hModule, L"CreateHostOnlyInterface: Resulting INF path = %s", pwszInfPath); } else logStringW(hModule, L"CreateHostOnlyInterface: VBox installation path is empty"); } else logStringW(hModule, L"CreateHostOnlyInterface: Unable to retrieve VBox installation path, error = 0x%x", uErr); /* Make sure the inf file is installed. */ if (pwszInfPath != NULL && bIsFile) { logStringW(hModule, L"CreateHostOnlyInterface: Calling VBoxDrvCfgInfInstall(%s)", pwszInfPath); hr = VBoxDrvCfgInfInstall(pwszInfPath); logStringW(hModule, L"CreateHostOnlyInterface: VBoxDrvCfgInfInstall returns 0x%x", hr); if (FAILED(hr)) logStringW(hModule, L"CreateHostOnlyInterface: Failed to install INF file, error = 0x%x", hr); } if (SUCCEEDED(hr)) { //first, try to update Host Only Network Interface BOOL fRebootRequired = FALSE; hr = VBoxNetCfgWinUpdateHostOnlyNetworkInterface(pwszInfPath, &fRebootRequired); if (SUCCEEDED(hr)) { if (fRebootRequired) { logStringW(hModule, L"UpdateHostOnlyInterfaces: Reboot required, setting REBOOT property to force"); HRESULT hr2 = MsiSetPropertyW(hModule, L"REBOOT", L"Force"); if (hr2 != ERROR_SUCCESS) logStringW(hModule, L"UpdateHostOnlyInterfaces: Failed to set REBOOT property, error = 0x%x", hr2); } } else logStringW(hModule, L"UpdateHostOnlyInterfaces: VBoxNetCfgWinUpdateHostOnlyNetworkInterface failed, hr = 0x%x", hr); //in fail case call CreateHostOnlyInterface if (FAILED(hr)) { logStringW(hModule, L"CreateHostOnlyInterface: calling VBoxNetCfgWinCreateHostOnlyNetworkInterface"); hr = VBoxNetCfgWinCreateHostOnlyNetworkInterface(pwszInfPath, bIsFile, &guid, NULL, NULL); logStringW(hModule, L"CreateHostOnlyInterface: VBoxNetCfgWinCreateHostOnlyNetworkInterface returns 0x%x", hr); if (SUCCEEDED(hr)) { ULONG ip = inet_addr("192.168.56.1"); ULONG mask = inet_addr("255.255.255.0"); logStringW(hModule, L"CreateHostOnlyInterface: calling VBoxNetCfgWinEnableStaticIpConfig"); hr = VBoxNetCfgWinEnableStaticIpConfig(&guid, ip, mask); logStringW(hModule, L"CreateHostOnlyInterface: VBoxNetCfgWinEnableStaticIpConfig returns 0x%x", hr); if (FAILED(hr)) logStringW(hModule, L"CreateHostOnlyInterface: VBoxNetCfgWinEnableStaticIpConfig failed, error = 0x%x", hr); } else logStringW(hModule, L"CreateHostOnlyInterface: VBoxNetCfgWinCreateHostOnlyNetworkInterface failed, error = 0x%x", hr); } } if (SUCCEEDED(hr)) logStringW(hModule, L"CreateHostOnlyInterface: Creating host-only interface done"); /* Restore original setup mode. */ logStringW(hModule, L"CreateHostOnlyInterface: Almost done..."); if (bSetupModeInteractive) SetupSetNonInteractiveMode(bSetupModeInteractive); netCfgLoggerDisable(); #endif /* VBOX_WITH_NETFLT */ logStringW(hModule, L"CreateHostOnlyInterface: Returns success (ignoring all failures)"); /* Never fail the install even if we did not succeed. */ return ERROR_SUCCESS; }
static int VBoxNetAdpInstall() { int r = 1; VBoxNetCfgWinSetLogging(winNetCfgLogger); HRESULT hr = CoInitialize(NULL); if(hr == S_OK) { #if 0 //ndef DEBUG_misha printf("not implemented yet, please use device manager for Host-Only net interface installation.. sorry :( \n"); #else GUID guid; BSTR name, errMsg; printf("adding host-only interface..\n"); DWORD WinEr; WCHAR MpInf[MAX_PATH]; GetFullPathNameW(VBOX_NETADP_INF, sizeof(MpInf)/sizeof(MpInf[0]), MpInf, NULL); WinEr = GetLastError(); if(WinEr == ERROR_SUCCESS) { hr = VBoxNetCfgWinCreateHostOnlyNetworkInterface (MpInf, true, &guid, &name, &errMsg); if(hr == S_OK) { ULONG ip, mask; hr = VBoxNetCfgWinGenHostOnlyNetworkNetworkIp(&ip, &mask); if(hr == S_OK) { /* ip returned by VBoxNetCfgWinGenHostOnlyNetworkNetworkIp is a network ip, * i.e. 192.168.xxx.0, assign 192.168.xxx.1 for the hostonly adapter */ ip = ip | (1 << 24); hr = VBoxNetCfgWinEnableStaticIpConfig(&guid, ip, mask); if(hr != S_OK) { printf("VBoxNetCfgWinEnableStaticIpConfig failed: hr = 0x%x\n", hr); } else { r = 0; } } else { printf("VBoxNetCfgWinGenHostOnlyNetworkNetworkIp failed: hr = 0x%x\n", hr); } } else { printf("VBoxNetCfgWinCreateHostOnlyNetworkInterface failed: hr = 0x%x\n", hr); } } else { printf("GetFullPathNameW failed: winEr = %d\n", WinEr); } #endif CoUninitialize(); } else { wprintf(L"Error initializing COM (0x%x)\n", hr); } VBoxNetCfgWinSetLogging(NULL); return r; }