int vboxRegister(void) { uint32_t uVersion; if (VBoxCGlueInit(&uVersion) == 0) vboxConnectDriver.hypervisorDriver = vboxGetHypervisorDriver(uVersion); if (vboxConnectDriver.hypervisorDriver) { vboxConnectDriver.networkDriver = vboxGetNetworkDriver(uVersion); vboxConnectDriver.storageDriver = vboxGetStorageDriver(uVersion); } else { vboxConnectDriver.hypervisorDriver = &vboxDriverDummy; } if (virRegisterConnectDriver(&vboxConnectDriver, false) < 0) return -1; return 0; }
int vboxRegister(void) { virDriverPtr driver; virNetworkDriverPtr networkDriver; virStorageDriverPtr storageDriver; uint32_t uVersion; /* * If the glue layer does not initialize, we register a driver * with a dummy open method, so we can report nicer errors * if the user requests a vbox:// URI which we know will * never work */ driver = &vboxDriverDummy; networkDriver = &vbox22NetworkDriver; storageDriver = &vbox22StorageDriver; /* Init the glue and get the API version. */ if (VBoxCGlueInit() == 0) { uVersion = g_pVBoxFuncs->pfnGetVersion(); DEBUG("VBoxCGlueInit found API version: %d.%d.%d (%u)", uVersion / 1000000, uVersion % 1000000 / 1000, uVersion % 1000, uVersion); /* Select driver implementation based on version. * Note that the VirtualBox development usually happens at build * number 51, thus the version ranges in the if statements below. */ if (uVersion >= 2001052 && uVersion < 2002051) { DEBUG0("VirtualBox API version: 2.2"); driver = &vbox22Driver; networkDriver = &vbox22NetworkDriver; storageDriver = &vbox22StorageDriver; } else if (uVersion >= 2002051 && uVersion < 3000051) { DEBUG0("VirtualBox API version: 3.0"); driver = &vbox30Driver; networkDriver = &vbox30NetworkDriver; storageDriver = &vbox30StorageDriver; } else if (uVersion >= 3000051 && uVersion < 3001051) { DEBUG0("VirtualBox API version: 3.1"); driver = &vbox31Driver; networkDriver = &vbox31NetworkDriver; storageDriver = &vbox31StorageDriver; } else if (uVersion >= 3001051 && uVersion < 3002051) { DEBUG0("VirtualBox API version: 3.2"); driver = &vbox32Driver; networkDriver = &vbox32NetworkDriver; storageDriver = &vbox32StorageDriver; } else { DEBUG0("Unsupport VirtualBox API version"); } } else { DEBUG0("VBoxCGlueInit failed, using dummy driver"); } if (virRegisterDriver(driver) < 0) return -1; if (virRegisterNetworkDriver(networkDriver) < 0) return -1; if (virRegisterStorageDriver(storageDriver) < 0) return -1; return 0; }
int main(int argc, char **argv) { IVirtualBoxClient *vboxclient = NULL; IVirtualBox *vbox = NULL; ISession *session = NULL; ULONG revision = 0; BSTR versionUtf16 = NULL; BSTR homefolderUtf16 = NULL; HRESULT rc; /* Result code of various function (method) calls. */ (void)argc; printf("Starting main()\n"); if (VBoxCGlueInit()) { fprintf(stderr, "%s: FATAL: VBoxCGlueInit failed: %s\n", argv[0], g_szVBoxErrMsg); return EXIT_FAILURE; } { unsigned ver = g_pVBoxFuncs->pfnGetVersion(); printf("VirtualBox version: %u.%u.%u\n", ver / 1000000, ver / 1000 % 1000, ver % 1000); ver = g_pVBoxFuncs->pfnGetAPIVersion(); printf("VirtualBox API version: %u.%u\n", ver / 1000, ver % 1000); } g_pVBoxFuncs->pfnClientInitialize(NULL, &vboxclient); if (!vboxclient) { fprintf(stderr, "%s: FATAL: could not get VirtualBoxClient reference\n", argv[0]); return EXIT_FAILURE; } printf("----------------------------------------------------\n"); rc = IVirtualBoxClient_get_VirtualBox(vboxclient, &vbox); if (FAILED(rc) || !vbox) { PrintErrorInfo(argv[0], "FATAL: could not get VirtualBox reference", rc); return EXIT_FAILURE; } rc = IVirtualBoxClient_get_Session(vboxclient, &session); if (FAILED(rc) || !session) { PrintErrorInfo(argv[0], "FATAL: could not get Session reference", rc); return EXIT_FAILURE; } #ifdef USE_ACTIVE_EVENT_LISTENER # ifdef WIN32 rc = LoadTypeInfo(&IID_IEventListener, &g_pTInfoIEventListener); if (FAILED(rc) || !g_pTInfoIEventListener) { PrintErrorInfo(argv[0], "FATAL: could not get type information for IEventListener", rc); return EXIT_FAILURE; } # endif /* WIN32 */ #endif /* USE_ACTIVE_EVENT_LISTENER */ /* * Now ask for revision, version and home folder information of * this vbox. Were not using fancy macros here so it * remains easy to see how we access C++'s vtable. */ /* 1. Revision */ rc = IVirtualBox_get_Revision(vbox, &revision); if (SUCCEEDED(rc)) printf("\tRevision: %u\n", revision); else PrintErrorInfo(argv[0], "GetRevision() failed", rc); /* 2. Version */ rc = IVirtualBox_get_Version(vbox, &versionUtf16); if (SUCCEEDED(rc)) { char *version = NULL; g_pVBoxFuncs->pfnUtf16ToUtf8(versionUtf16, &version); printf("\tVersion: %s\n", version); g_pVBoxFuncs->pfnUtf8Free(version); g_pVBoxFuncs->pfnComUnallocString(versionUtf16); } else PrintErrorInfo(argv[0], "GetVersion() failed", rc); /* 3. Home Folder */ rc = IVirtualBox_get_HomeFolder(vbox, &homefolderUtf16); if (SUCCEEDED(rc)) { char *homefolder = NULL; g_pVBoxFuncs->pfnUtf16ToUtf8(homefolderUtf16, &homefolder); printf("\tHomeFolder: %s\n", homefolder); g_pVBoxFuncs->pfnUtf8Free(homefolder); g_pVBoxFuncs->pfnComUnallocString(homefolderUtf16); } else PrintErrorInfo(argv[0], "GetHomeFolder() failed", rc); listVMs(argv[0], vbox, session); ISession_UnlockMachine(session); printf("----------------------------------------------------\n"); /* * Do as mom told us: always clean up after yourself. */ #ifdef USE_ACTIVE_EVENT_LISTENER # ifdef WIN32 if (g_pTInfoIEventListener) { ITypeInfo_Release(g_pTInfoIEventListener); g_pTInfoIEventListener = NULL; } # endif /* WIN32 */ #endif /* USE_ACTIVE_EVENT_LISTENER */ if (session) { ISession_Release(session); session = NULL; } if (vbox) { IVirtualBox_Release(vbox); vbox = NULL; } if (vboxclient) { IVirtualBoxClient_Release(vboxclient); vboxclient = NULL; } g_pVBoxFuncs->pfnClientUninitialize(); VBoxCGlueTerm(); printf("Finished main()\n"); return 0; }
int main(int argc, char **argv) { IVirtualBox *vbox = NULL; ISession *session = NULL; PRUint32 revision = 0; PRUnichar *versionUtf16 = NULL; PRUnichar *homefolderUtf16 = NULL; nsresult rc; /* Result code of various function (method) calls. */ printf("Starting Main\n"); /* * VBoxComInitialize does all the necessary startup action and * provides us with pointers to vbox and session handles. * It should be matched by a call to VBoxComUninitialize(vbox) * when done. */ if (VBoxCGlueInit() != 0) { fprintf(stderr, "%s: FATAL: VBoxCGlueInit failed: %s\n", argv[0], g_szVBoxErrMsg); return EXIT_FAILURE; } g_pVBoxFuncs->pfnComInitialize(IVIRTUALBOX_IID_STR, &vbox, ISESSION_IID_STR, &session); if (vbox == NULL) { fprintf(stderr, "%s: FATAL: could not get vbox handle\n", argv[0]); return EXIT_FAILURE; } if (session == NULL) { fprintf(stderr, "%s: FATAL: could not get session handle\n", argv[0]); return EXIT_FAILURE; } /* * Now ask for revision, version and home folder information of * this vbox. Were not using fancy macros here so it * remains easy to see how we access C++'s vtable. */ printf("----------------------------------------------------\n"); /* 1. Revision */ rc = vbox->vtbl->GetRevision(vbox, &revision); if (NS_SUCCEEDED(rc)) { printf("\tRevision: %u\n", revision); } else { fprintf(stderr, "%s: GetRevision() returned %08x\n", argv[0], (unsigned)rc); } /* 2. Version */ rc = vbox->vtbl->GetVersion(vbox, &versionUtf16); if (NS_SUCCEEDED(rc)) { char *version = NULL; g_pVBoxFuncs->pfnUtf16ToUtf8(versionUtf16, &version); printf("\tVersion: %s\n", version); g_pVBoxFuncs->pfnUtf8Free(version); g_pVBoxFuncs->pfnComUnallocMem(versionUtf16); } else { fprintf(stderr, "%s: GetVersion() returned %08x\n", argv[0], (unsigned)rc); } /* 3. Home Folder */ rc = vbox->vtbl->GetHomeFolder(vbox, &homefolderUtf16); if (NS_SUCCEEDED(rc)) { char *homefolder = NULL; g_pVBoxFuncs->pfnUtf16ToUtf8(homefolderUtf16, &homefolder); printf("\tHomeFolder: %s\n", homefolder); g_pVBoxFuncs->pfnUtf8Free(homefolder); g_pVBoxFuncs->pfnComUnallocMem(homefolderUtf16); } else { fprintf(stderr, "%s: GetHomeFolder() returned %08x\n", argv[0], (unsigned)rc); } listVMs(vbox, session); session->vtbl->UnlockMachine(session); printf("----------------------------------------------------\n"); /* * Do as mom told us: always clean up after yourself. */ g_pVBoxFuncs->pfnComUninitialize(); VBoxCGlueTerm(); printf("Finished Main\n"); return 0; }