公式の VirtualBox ボードで助けを得ることができなかったので、VirtualBox API の扱い方を知っている人がいるかどうか尋ねたいと思います。 次の問題は、2 台の Win 8 x64 コンピュータでのみ発生し、Win 7 x86 ラップトップでは発生しません! 理由はわかりませんが、Wikipedia によると、Microsoft は Win 8 で COM を大幅に変更しました。Win 7 で実行可能ファイルを起動すると、すべて正常に動作します。
基本的な通信は機能し、使用可能なすべての仮想マシンを取得できるため、Com 接続は少なくとも部分的に機能します。しかし、VM を起動しようとすると、理解できない奇妙なエラーが発生します。出力は次のとおりです。
私はSDKでVirtualBox 4.2を使用しています。これはソースコードです(ほとんどの例から適応されています):
#include <stdio.h>
#include <tchar.h>
#include "VirtualBox.h"
#define SAFE_RELEASE(x) \
if (x) { \
x->Release(); \
x = NULL; \
}
int listVMs(IVirtualBox *virtualBox)
{
HRESULT rc;
/*
* First we have to get a list of all registered VMs
*/
SAFEARRAY *machinesArray = NULL;
rc = virtualBox->get_Machines(&machinesArray);
if (SUCCEEDED(rc))
{
IMachine **machines;
rc = SafeArrayAccessData (machinesArray, (void **) &machines);
if (SUCCEEDED(rc))
{
for (ULONG i = 0; i < machinesArray->rgsabound[0].cElements; ++i)
{
BSTR str;
rc = machines[i]->get_Name(&str);
if (SUCCEEDED(rc))
{
printf("Available Machine: %S\n", str);
SysFreeString(str);
}
}
SafeArrayUnaccessData (machinesArray);
}
SafeArrayDestroy (machinesArray);
}
return 0;
}
int testErrorInfo(IVirtualBox *virtualBox)
{
HRESULT rc;
/* Try to find a machine that doesn't exist */
IMachine *machine = NULL;
BSTR machineName = SysAllocString(L"Test Machine");
rc = virtualBox->FindMachine(machineName, &machine);
if (FAILED(rc))
{
IErrorInfo *errorInfo;
rc = GetErrorInfo(0, &errorInfo);
if (FAILED(rc))
printf("Error getting error info! rc = 0x%x\n", rc);
else
{
BSTR errorDescription = NULL;
rc = errorInfo->GetDescription(&errorDescription);
if (FAILED(rc) || !errorDescription)
printf("Error getting error description! rc = 0x%x\n", rc);
else
{
printf("Successfully retrieved error description: %S\n", errorDescription);
SysFreeString(errorDescription);
}
errorInfo->Release();
}
}
else
printf("No Errors \n");
SAFE_RELEASE(machine);
SysFreeString(machineName);
return 0;
}
int testStartVM(IVirtualBox *virtualBox, OLECHAR *MachineName)
{
HRESULT rc;
/* Try to start a VM*/
IMachine *machine = NULL;
BSTR machineName = SysAllocString(MachineName);
rc = virtualBox->FindMachine(machineName, &machine);
if (FAILED(rc))
{
IErrorInfo *errorInfo;
rc = GetErrorInfo(0, &errorInfo);
if (FAILED(rc))
printf("Error getting error info! rc = 0x%x\n", rc);
else
{
BSTR errorDescription = NULL;
rc = errorInfo->GetDescription(&errorDescription);
if (FAILED(rc) || !errorDescription)
printf("Error getting error description! rc = 0x%x\n", rc);
else
{
printf("Successfully retrieved error description: %S\n", errorDescription);
SysFreeString(errorDescription);
}
SAFE_RELEASE(errorInfo);
}
}
else
{
ISession *session = NULL;
IConsole *console = NULL;
IProgress *progress = NULL;
BSTR sessiontype = SysAllocString(L"gui");
BSTR guid;
do
{
rc = machine->get_Id(&guid); /* Get the GUID of the machine. */
if (!SUCCEEDED(rc))
{
printf("Error retrieving machine ID! rc = 0x%x\n", rc);
break;
}
/* Create the session object. */
testErrorInfo(virtualBox);
printf("GetLastError : %d\n", GetLastError());
rc = CoCreateInstance(CLSID_Session, //the VirtualBox base object
NULL, //no aggregation
CLSCTX_INPROC_SERVER, //the object lives in a server process on this machine
IID_ISession, //IID of the interface
(void**)&session);
/*
//
//
//Error in CoCreateInstance
//
//
*/
if (!SUCCEEDED(rc))
{
printf("Error creating Session instance! rc = 0x%x\n", rc);
printf("GetLastError : %d\n", GetLastError());
break;
}
/* Start a VM session using the delivered VBox GUI. */
rc = machine->LaunchVMProcess(session, sessiontype,
NULL, &progress);
if (!SUCCEEDED(rc))
{
printf("Could not open remote session! rc = 0x%x\n", rc);
break;
}
/* Wait until VM is running. */
printf ("Starting VM, please wait ...\n");
rc = progress->WaitForCompletion (-1);
/* Get console object. */
session->get_Console(&console);
/* Bring console window to front. */
machine->ShowConsoleWindow(0);
printf ("Press enter to power off VM and close the session...\n");
getchar();
/* Power down the machine. */
rc = console->PowerDown(&progress);
/* Wait until VM is powered down. */
printf ("Powering off VM, please wait ...\n");
rc = progress->WaitForCompletion (-1);
/* Close the session. */
rc = session->UnlockMachine();
} while (0);
SAFE_RELEASE(console);
SAFE_RELEASE(progress);
SAFE_RELEASE(session);
SysFreeString(guid);
SysFreeString(sessiontype);
SAFE_RELEASE(machine);
}
SysFreeString(machineName);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
IVirtualBox *virtualBox;
HRESULT rc = CoCreateInstance(CLSID_VirtualBox,
NULL,
CLSCTX_LOCAL_SERVER,
IID_IVirtualBox,
(void**)&virtualBox);
if (!SUCCEEDED(rc))
{
printf("Error creating VirtualBox instance! rc = 0x%x\n", rc);
getchar();
return 0;
}
else
{
printf("Successfully created VirtualBox instance \n");
testErrorInfo(virtualBox);
listVMs(virtualBox);
}
testStartVM(virtualBox, L"Full Tilt");
virtualBox->Release();
CoUninitialize();
getchar();
return 0;
}
呼び出し時にクラッシュすることを意味します
rc = CoCreateInstance(CLSID_Session, //the VirtualBox base object
NULL, //no aggregation
CLSCTX_INPROC_SERVER, //the object lives in a server process on this machine
IID_ISession, //IID of the interface
(void**)&session);
それは投げます。
0x80040154 aka REGDB_E_CLASSNOTREG
そして GetLastError はこれを返します:
ERROR_SXS_KEY_NOT_FOUND
14007 (0x36B7)
The requested lookup key was not found in any active activation context.
いろいろ試してみましたが、問題を解決できませんでした。どんな種類の助けも大歓迎です:)
ありがとう!