Error: could not find guest OS type! rc=80070057
Failed creating a hard disk object! rc=80BB0004
Error: could not open CD image! rc=80BB0001
why and how to solve, Thanks! I just contact with VirtualBox
source:
Code: Select all
void createVM(IVirtualBox *virtualBox)
{
nsresult rc;
/*
* First create a unnamed new VM. It will be unconfigured and not be saved
* in the configuration until we explicitely choose to do so.
*/
nsCOMPtr<IMachine> machine;
rc = virtualBox->CreateMachine(NULL, /* settings file */
NS_LITERAL_STRING("A brand new name").get(),
0, nsnull, /* groups (safearray)*/
nsnull, /* ostype */
nsnull, /* create flags */
getter_AddRefs(machine));
if (NS_FAILED(rc))
{
printf("Error: could not create machine! rc=%08X\n", rc);
return;
}
/*
* Set some properties
*/
/* alternative to illustrate the use of string classes */
rc = machine->SetName(NS_ConvertUTF8toUTF16("A new name").get());
rc = machine->SetMemorySize(128);
/*
* Now a more advanced property -- the guest OS type. This is
* an object by itself which has to be found first. Note that we
* use the ID of the guest OS type here which is an internal
* representation (you can find that by configuring the OS type of
* a machine in the GUI and then looking at the <Guest ostype=""/>
* setting in the XML file. It is also possible to get the OS type from
* its description (win2k would be "Windows 2000") by getting the
* guest OS type collection and enumerating it.
*/
nsCOMPtr<IGuestOSType> osType;
rc = virtualBox->GetGuestOSType(NS_LITERAL_STRING("win2k").get(),
getter_AddRefs(osType));
if (NS_FAILED(rc))
{
printf("Error: could not find guest OS type! rc=%08X\n", rc);
}
else
{
machine->SetOSTypeId (NS_LITERAL_STRING("win2k").get());
}
/*
* Register the VM. Note that this call also saves the VM config
* to disk. It is also possible to save the VM settings but not
* register the VM.
*
* Also note that due to current VirtualBox limitations, the machine
* must be registered *before* we can attach hard disks to it.
*/
rc = virtualBox->RegisterMachine(machine);
if (NS_FAILED(rc))
{
printf("Error: could not register machine! rc=%08X\n", rc);
printErrorInfo();
return;
}
/*
* In order to manipulate the registered machine, we must open a session
* for that machine. Do it now.
*/
nsCOMPtr<ISession> session;
{
nsCOMPtr<nsIComponentManager> manager;
rc = NS_GetComponentManager (getter_AddRefs (manager));
if (NS_FAILED(rc))
{
printf("Error: could not get component manager! rc=%08X\n", rc);
return;
}
rc = manager->CreateInstanceByContractID (NS_SESSION_CONTRACTID,
nsnull,
NS_GET_IID(ISession),
getter_AddRefs(session));
if (NS_FAILED(rc))
{
printf("Error, could not instantiate session object! rc=0x%x\n", rc);
return;
}
rc = machine->LockMachine(session, LockType_Write);
if (NS_FAILED(rc))
{
printf("Error, could not lock the machine for the session! rc=0x%x\n", rc);
return;
}
/*
* After the machine is registered, the initial machine object becomes
* immutable. In order to get a mutable machine object, we must query
* it from the opened session object.
*/
rc = session->GetMachine(getter_AddRefs(machine));
if (NS_FAILED(rc))
{
printf("Error, could not get machine session! rc=0x%x\n", rc);
return;
}
}
/*
* Create a virtual harddisk
*/
nsCOMPtr<IMedium> hardDisk = 0;
rc = virtualBox->CreateHardDisk(NS_LITERAL_STRING("VDI").get(),
NS_LITERAL_STRING("TestHardDisk.vdi").get(),
getter_AddRefs(hardDisk));
if (NS_FAILED(rc))
{
printf("Failed creating a hard disk object! rc=%08X\n", rc);
}
else
{
/*
* We have only created an object so far. No on disk representation exists
* because none of its properties has been set so far. Let's continue creating
* a dynamically expanding image.
*/
nsCOMPtr <IProgress> progress;
rc = hardDisk->CreateBaseStorage(100, // size in megabytes
MediumVariant_Standard,
getter_AddRefs(progress)); // optional progress object
if (NS_FAILED(rc))
{
printf("Failed creating hard disk image! rc=%08X\n", rc);
}
else
{
/*
* Creating the image is done in the background because it can take quite
* some time (at least fixed size images). We have to wait for its completion.
* Here we wait forever (timeout -1) which is potentially dangerous.
*/
rc = progress->WaitForCompletion(-1);
PRInt32 resultCode;
progress->GetResultCode(&resultCode);
if (NS_FAILED(rc) || NS_FAILED(resultCode))
{
printf("Error: could not create hard disk! rc=%08X\n",
NS_FAILED(rc) ? rc : resultCode);
}
else
{
/*
* Now that it's created, we can assign it to the VM.
*/
rc = machine->AttachDevice(NS_LITERAL_STRING("IDE Controller").get(), // controller identifier
0, // channel number on the controller
0, // device number on the controller
DeviceType_HardDisk,
hardDisk);
if (NS_FAILED(rc))
{
printf("Error: could not attach hard disk! rc=%08X\n", rc);
}
}
}
}
/*
* It's got a hard disk but that one is new and thus not bootable. Make it
* boot from an ISO file. This requires some processing. First the ISO file
* has to be registered and then mounted to the VM's DVD drive and selected
* as the boot device.
*/
nsCOMPtr<IMedium> dvdImage;
rc = virtualBox->OpenMedium(NS_LITERAL_STRING("/home/vbox/isos/winnt4ger.iso").get(),
DeviceType_DVD,
AccessMode_ReadOnly,
false /* fForceNewUuid */,
getter_AddRefs(dvdImage));
if (NS_FAILED(rc))
printf("Error: could not open CD image! rc=%08X\n", rc);
else
{
/*
* Now assign it to our VM
*/
rc = machine->MountMedium(NS_LITERAL_STRING("IDE Controller").get(), // controller identifier
2, // channel number on the controller
0, // device number on the controller
dvdImage,
PR_FALSE); // aForce
if (NS_FAILED(rc))
{
printf("Error: could not mount ISO image! rc=%08X\n", rc);
}
else
{
/*
* Last step: tell the VM to boot from the CD.
*/
rc = machine->SetBootOrder (1, DeviceType::DVD);
if (NS_FAILED(rc))
{
printf("Could not set boot device! rc=%08X\n", rc);
}
}
}
/*
* Save all changes we've just made.
*/
rc = machine->SaveSettings();
if (NS_FAILED(rc))
{
printf("Could not save machine settings! rc=%08X\n", rc);
}
/*
* It is always important to close the open session when it becomes not
* necessary any more.
*/
session->UnlockMachine();
}