Esempio n. 1
0
void DeleteFiles(BazisLib::String path)
{
	TCHAR tsz[MAX_PATH] = {0,};
	GetModuleFileName(GetModuleHandle(NULL), tsz, sizeof(tsz)/sizeof(tsz[0]));
	TCHAR *p = _tcsrchr(tsz, '\\');
	if (!p)
		return;
	p[0] = 0;
	if (path.empty())
		path = tsz;

	RemoveTreeRecursive((Path::Combine(path, _T("x86"))).c_str());
	RemoveTreeRecursive((Path::Combine(path, _T("x64"))).c_str());
	RemoveTreeRecursive((Path::Combine(path, _T("langfiles"))).c_str());
	File::Delete(Path::Combine(path, _T("vmnt.exe")));
	File::Delete(Path::Combine(path, _T("vmnt64.exe")));
	File::Delete(Path::Combine(path, _T("batchmnt.exe")));
	File::Delete(Path::Combine(path, _T("batchmnt64.exe")));
	File::Delete(Path::Combine(path, _T("BazisVirtualCD.inf")));
	File::Delete(Path::Combine(path, _T("VirtDiskBus.inf")));
	File::Delete(Path::Combine(path, _T("BazisVirtualCDBus.inf")));
	File::Delete(Path::Combine(path, _T("BazisVirtualCD.cat")));
	File::Delete(Path::Combine(path, _T("VirtDiskBus.cat")));
	File::Delete(Path::Combine(path, _T("BazisVirtualCDBus.cat")));
	File::Delete(Path::Combine(path, _T("uninstall.exe")));
	File::Delete(Path::Combine(path, _T("uninstall64.exe")));
	Directory::Remove(path);
	p[0] = '\\';
	MoveFileEx(tsz, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
}
Esempio n. 2
0
static HRESULT DoRevertVMToSnapshot()
{
	BazisLib::String rawMachineID = VBoxCmdLineToMachineID(GetCommandLineW());
	if (rawMachineID.empty())
		return E_FAIL;

	CComBSTR machineID = rawMachineID.c_str();

	CComPtr<IVirtualBox> pVirtualBox;
	HRESULT hR = pVirtualBox.CoCreateInstance(CLSID_VirtualBox, NULL, CLSCTX_LOCAL_SERVER);
	if (!SUCCEEDED(hR))
		return hR;

	CComPtr<IMachine> pMachine;
	hR = pVirtualBox->FindMachine(machineID, &pMachine);
	if (!SUCCEEDED(hR))
		return hR;

	CComPtr<ISession> pSession;
	hR = pSession.CoCreateInstance(CLSID_Session, NULL, CLSCTX_INPROC_SERVER);
	if (!SUCCEEDED(hR))
		return hR;

	hR = pMachine->LockMachine(pSession, LockType_Shared);
	if (!SUCCEEDED(hR))
	{
		if (hR == E_FAIL)
			MessageBox(HWND_DESKTOP, L"Cannot connect to VirtualBox. Ensure that both VirtualBox and Visual Studio are running from the same user account, either both elevated (UAC), or both not.", L"Error", MB_ICONERROR);
		return hR;
	}

	CComPtr<IConsole> pConsole;
	hR = pSession->get_Console(&pConsole);
	if (!SUCCEEDED(hR))
		return hR;

	CComPtr<ISnapshot> pSnapshot;
	hR = pMachine->get_CurrentSnapshot(&pSnapshot);
	if (!SUCCEEDED(hR))
	{
		pSession->UnlockMachine();
		return hR;
	}

	CComBSTR snapshotID;
	hR = pSnapshot->get_Id(&snapshotID);
	if (!SUCCEEDED(hR))
		return hR;

	CComPtr<IProgress> pProgress;
	hR = pConsole->PowerDown(&pProgress);
	if (!SUCCEEDED(hR))
		return hR;

	hR = WaitForCompletion(pProgress);
	if (!SUCCEEDED(hR))
		return hR;

	pProgress = NULL;
	pSession->UnlockMachine();

	BazisLib::DateTime dtStart = BazisLib::DateTime::Now();
	
	for (;;)
	{
		hR = pMachine->LockMachine(pSession, LockType_Shared);
		if (!SUCCEEDED(hR) && (dtStart.MillisecondsElapsed() >= 10000))
			return hR;
		if (SUCCEEDED(hR))
			break;
		Sleep(100);
	}

	pConsole = NULL;
	hR = pSession->get_Console(&pConsole);
	if (!SUCCEEDED(hR))
		return hR;

	hR = pConsole->RestoreSnapshot(pSnapshot, &pProgress);
	if (!SUCCEEDED(hR))
		return hR;

	hR = WaitForCompletion(pProgress);
	if (!SUCCEEDED(hR))
		return hR;
	
	pSession->UnlockMachine();

	CComBSTR sessionType = L"gui";
	pProgress = NULL;

	hR = pMachine->LaunchVMProcess(pSession, sessionType, NULL, &pProgress);
	if (!SUCCEEDED(hR))
		return hR;

	hR = WaitForCompletion(pProgress);
	if (!SUCCEEDED(hR))
		return hR;

	pSession->UnlockMachine();


	pProgress = NULL;

	return S_OK;
}