我有一台装有Ubuntu的VirtualBox机器,上面连接了一个桥接适配器。
我想做的是获取机器的IP。
如果我手动启动机器并使用VBoxManage实用程序,如下所示:
VBoxManage guestproperty get "Ubuntu Test Machine" /VirtualBox/GuestInfo/Net/0/V4/IP
它工作正常。
然而,我需要的是使用VBox SDK以编程方式获取IP。
下面是代码中最重要的部分(我将在最后发布整个代码):
HRESULT hr = machine->GetGuestPropertyValue(L"/VirtualBox/GuestInfo/Net/0/V4/IP", &ip);
if (SUCCEEDED(hr))
{
printf("The machine's IP is: %s", ip);
}
else
printf("Error retrieving machine IP! rc = 0x%x\n", hr);
我得到以下错误:
Error retrieving machine IP! rc = 0x80070057
显然,错误代码意味着
无效参数
.
谁能告诉我那个参数有什么问题吗?或者如果我做错了什么?
以下是整个代码(基本上是VBox SDK示例目录中的示例代码,只做了一些修改):
int testStartVM(IVirtualBox *virtualBox)
{
HRESULT rc;
IMachine *machine = NULL;
BSTR machineName = SysAllocString(L"Ubuntu Test Machine");
rc = virtualBox->FindMachine(machineName, &machine);
if (FAILED(rc))
{
}
else
{
ISession *session = NULL;
IConsole *console = NULL;
IProgress *progress = NULL;
BSTR sessiontype = SysAllocString(L"headless");
BSTR guid;
do
{
rc = machine->get_Id(&guid);
if (!SUCCEEDED(rc))
{
printf("Error retrieving machine ID! rc = 0x%x\n", rc);
break;
}
rc = CoCreateInstance(CLSID_Session,
NULL,
CLSCTX_INPROC_SERVER,
IID_ISession,
(void**)&session);
if (!SUCCEEDED(rc))
{
printf("Error creating Session instance! rc = 0x%x\n", rc);
break;
}
rc = machine->LaunchVMProcess(session, sessiontype,
NULL, &progress);
if (!SUCCEEDED(rc))
{
printf("Could not open remote session! rc = 0x%x\n", rc);
break;
}
printf("Starting VM, please wait ...\n");
rc = progress->WaitForCompletion(-1);
session->get_Console(&console);
machine->ShowConsoleWindow(0);
Sleep(40 * 1000);
BSTR ip;
**
HRESULT hr = machine->GetGuestPropertyValue(L"/VirtualBox/GuestInfo/Net/0/V4/IP", &ip);
if (SUCCEEDED(hr))
{
printf("The machine's IP is: %s", ip);
}
else
printf("Error retrieving machine IP! rc = 0x%x\n", hr);
printf("Press enter to power off VM and close the session...\n");
getchar();
rc = console->PowerDown(&progress);
printf("Powering off VM, please wait ...\n");
rc = progress->WaitForCompletion(-1);
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 main(int argc, char *argv[])
{
HRESULT rc;
IVirtualBox *virtualBox;
CoInitialize(NULL);
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);
return 1;
}
listVMs(virtualBox);
testStartVM(virtualBox);
virtualBox->Release();
CoUninitialize();
getchar();
return 0;
}