Changeset 4246 in vbox for trunk/src/VBox/Main/testcase
- Timestamp:
- Aug 20, 2007 6:57:44 PM (17 years ago)
- Location:
- trunk/src/VBox/Main/testcase
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/testcase/Makefile.kmk
r4071 r4246 24 24 else ifeq ($(USERNAME),dmik) 25 25 PROGRAMS = tstAPI 26 PROGRAMS.linux = tstVBoxAPILinux 27 PROGRAMS.win = tstVBoxAPIWin 26 28 endif # VBOX_WITH_TESTCASES 27 29 … … 56 58 tstVBoxAPILinux_TEMPLATE = VBOXR3 57 59 tstVBoxAPILinux_SOURCES = tstVBoxAPILinux.cpp 58 tstVBoxAPILinux_CXXFLAGS = -Wno-non-virtual-dtor 60 tstVBoxAPILinux_CXXFLAGS = -Wno-non-virtual-dtor -fshort-wchar 59 61 tstVBoxAPILinux_INCS = \ 60 62 $(VBOX_XPCOM_INCS) \ -
trunk/src/VBox/Main/testcase/tstVBoxAPILinux.cpp
r4071 r4246 29 29 * Include the XPCOM headers 30 30 */ 31 #include <nsIServiceManager.h>32 #include <nsIComponentRegistrar.h>33 31 #include <nsXPCOMGlue.h> 34 32 #include <nsMemory.h> 35 33 #include <nsString.h> 36 #include <nsIProgrammingLanguage.h> 37 #include <ipcIService.h> 38 #include <ipcCID.h> 39 #include <ipcIDConnectService.h> 40 #include <nsIEventQueueService.h> 41 42 /* 43 * Some XPCOM declarations that haven't made it 44 * into the official headers yet 45 */ 46 #define IPC_DCONNECTSERVICE_CONTRACTID \ 47 "@mozilla.org/ipc/dconnect-service;1" 48 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); 49 static nsIEventQueue* gEventQ = nsnull; 50 34 #include <nsIServiceManager.h> 35 #include <nsEventQueueUtils.h> 36 37 #include <nsIExceptionService.h> 51 38 52 39 /* … … 60 47 */ 61 48 char *nsIDToString(nsID *guid); 62 49 void printErrorInfo(); 63 50 64 51 … … 150 137 nsresult rc; 151 138 /* 152 * First create a new VM. It will be unconfigured and not be saved139 * First create a unnamed new VM. It will be unconfigured and not be saved 153 140 * in the configuration until we explicitely choose to do so. 154 141 */ 155 IMachine *machine; 156 rc = virtualBox->CreateMachine(nsnull, NS_LITERAL_STRING("A brand new VM").get(), &machine); 142 nsCOMPtr <IMachine> machine; 143 rc = virtualBox->CreateMachine(nsnull, NS_LITERAL_STRING("A brand new name").get(), 144 getter_AddRefs(machine)); 145 if (NS_FAILED(rc)) 146 { 147 printf("Error: could not create machine! rc=%08X\n", rc); 148 return; 149 } 157 150 158 151 /* … … 160 153 */ 161 154 /* alternative to illustrate the use of string classes */ 162 char vmName[] = "A new name"; 163 rc = machine->SetName(NS_ConvertUTF8toUTF16((char*)vmName).get()); 155 rc = machine->SetName(NS_ConvertUTF8toUTF16("A new name").get()); 164 156 rc = machine->SetMemorySize(128); 165 157 … … 174 166 * guest OS type collection and enumerating it. 175 167 */ 176 IGuestOSType *osType = nsnull;177 char win2k[] = "win2k";178 rc = virtualBox->GetGuestOSType(NS_ConvertUTF8toUTF16((char*) win2k).get(), &osType);179 if (NS_FAILED(rc) || !osType)180 { 181 printf("Error: could not find guest OS type! rc = 0x%x\n", rc);168 nsCOMPtr <IGuestOSType> osType; 169 rc = virtualBox->GetGuestOSType(NS_LITERAL_STRING("win2k").get(), 170 getter_AddRefs(osType)); 171 if (NS_FAILED(rc)) 172 { 173 printf("Error: could not find guest OS type! rc=%08X\n", rc); 182 174 } 183 175 else 184 176 { 185 machine->SetOSTypeId (NS_ConvertUTF8toUTF16((char*) win2k).get()); 186 osType->Release(); 177 machine->SetOSTypeId (NS_LITERAL_STRING("win2k").get()); 178 } 179 180 /* 181 * Register the VM. Note that this call also saves the VM config 182 * to disk. It is also possible to save the VM settings but not 183 * register the VM. 184 * 185 * Also note that due to current VirtualBox limitations, the machine 186 * must be registered *before* we can attach hard disks to it. 187 */ 188 rc = virtualBox->RegisterMachine(machine); 189 if (NS_FAILED(rc)) 190 { 191 printf("Error: could not register machine! rc=%08X\n", rc); 192 printErrorInfo(); 193 return; 194 } 195 196 /* 197 * In order to manipulate the registered machine, we must open a session 198 * for that machine. Do it now. 199 */ 200 nsCOMPtr<ISession> session; 201 { 202 nsCOMPtr<nsIComponentManager> manager; 203 rc = NS_GetComponentManager (getter_AddRefs (manager)); 204 if (NS_FAILED(rc)) 205 { 206 printf("Error: could not get component manager! rc=%08X\n", rc); 207 return; 208 } 209 rc = manager->CreateInstanceByContractID (NS_SESSION_CONTRACTID, 210 nsnull, 211 NS_GET_IID(ISession), 212 getter_AddRefs(session)); 213 if (NS_FAILED(rc)) 214 { 215 printf("Error, could not instantiate Session object! rc=0x%x\n", rc); 216 return; 217 } 218 219 nsID *machineUUID = nsnull; 220 machine->GetId(&machineUUID); 221 rc = virtualBox->OpenSession(session, *machineUUID); 222 nsMemory::Free(machineUUID); 223 if (NS_FAILED(rc)) 224 { 225 printf("Error, could not open session! rc=0x%x\n", rc); 226 return; 227 } 228 229 /* 230 * After the machine is registered, the initial machine object becomes 231 * immutable. In order to get a mutable machine object, we must query 232 * it from the opened session object. 233 */ 234 rc = session->GetMachine(getter_AddRefs(machine)); 235 if (NS_FAILED(rc)) 236 { 237 printf("Error, could not get sessioned machine! rc=0x%x\n", rc); 238 return; 239 } 187 240 } 188 241 … … 190 243 * Create a virtual harddisk 191 244 */ 192 IHardDisk *hardDisk = 0; 193 IVirtualDiskImage *vdi = 0; 194 char vdiName[] = "TestHardDisk.vdi"; 245 nsCOMPtr<IHardDisk> hardDisk = 0; 246 nsCOMPtr<IVirtualDiskImage> vdi = 0; 195 247 rc = virtualBox->CreateHardDisk(HardDiskStorageType::VirtualDiskImage, 196 &hardDisk);248 getter_AddRefs(hardDisk)); 197 249 if (NS_SUCCEEDED (rc)) 198 250 { 199 rc = hardDisk->QueryInterface(NS_GET_IID(IVirtualDiskImage), (void **)&vdi); 251 rc = hardDisk->QueryInterface(NS_GET_IID(IVirtualDiskImage), 252 (void **)(getter_AddRefs(vdi))); 200 253 if (NS_SUCCEEDED (rc)) 201 rc = vdi->SetFilePath(NS_ ConvertUTF8toUTF16((char*)vdiName).get());202 } 203 204 if (NS_FAILED(rc) || !hardDisk || !vdi)205 { 206 printf("Failed creating a hard disk object! \n");254 rc = vdi->SetFilePath(NS_LITERAL_STRING("TestHardDisk.vdi").get()); 255 } 256 257 if (NS_FAILED(rc)) 258 { 259 printf("Failed creating a hard disk object! rc=%08X\n", rc); 207 260 } 208 261 else … … 213 266 * a dynamically expanding image. 214 267 */ 215 IProgress *progress;268 nsCOMPtr <IProgress> progress; 216 269 rc = vdi->CreateDynamicImage(100, // size in megabytes 217 &progress);// optional progress object218 if (NS_FAILED(rc) || !progress)219 { 220 printf("Failed creating hard disk image! \n");270 getter_AddRefs(progress)); // optional progress object 271 if (NS_FAILED(rc)) 272 { 273 printf("Failed creating hard disk image! rc=%08X\n", rc); 221 274 } 222 275 else … … 230 283 nsresult resultCode; 231 284 progress->GetResultCode(&resultCode); 232 progress->Release(); 233 if (NS_FAILED(rc) || (resultCode != 0)) 285 if (NS_FAILED(rc) || NS_FAILED(resultCode)) 234 286 { 235 printf("Error: could not create hard disk!\n"); 287 printf("Error: could not create hard disk! rc=%08X\n", 288 NS_FAILED(rc) ? rc : resultCode); 236 289 } 237 290 else … … 243 296 if (NS_FAILED(rc)) 244 297 { 245 printf("Error: could not register hard disk! \n");298 printf("Error: could not register hard disk! rc=%08X\n", rc); 246 299 } 247 300 else … … 260 313 if (NS_FAILED(rc)) 261 314 { 262 printf("Error: could not attach hard disk! \n");315 printf("Error: could not attach hard disk! rc=%08X\n", rc); 263 316 } 264 317 } … … 266 319 } 267 320 } 268 if (vdi)269 vdi->Release();270 if (hardDisk)271 hardDisk->Release();272 321 273 322 /* … … 278 327 */ 279 328 nsID uuid = {0}; 280 IDVDImage *dvdImage = nsnull; 281 char isoFile[] = "/home/achimha/isoimages/winnt4ger.iso"; 282 283 rc = virtualBox->OpenDVDImage(NS_ConvertUTF8toUTF16((char*)isoFile).get(), 329 nsCOMPtr<IDVDImage> dvdImage; 330 331 rc = virtualBox->OpenDVDImage(NS_LITERAL_STRING("/home/achimha/isoimages/winnt4ger.iso").get(), 284 332 uuid, /* NULL UUID, i.e. a new one will be created */ 285 &dvdImage);286 if (NS_FAILED(rc) || !dvdImage)287 { 288 printf("Error: could not open CD image! \n");333 getter_AddRefs(dvdImage)); 334 if (NS_FAILED(rc)) 335 { 336 printf("Error: could not open CD image! rc=%08X\n", rc); 289 337 } 290 338 else … … 294 342 */ 295 343 rc = virtualBox->RegisterDVDImage(dvdImage); 296 if (NS_FAILED(rc) || !dvdImage)297 { 298 printf("Error: could not register CD image! \n");344 if (NS_FAILED(rc)) 345 { 346 printf("Error: could not register CD image! rc=%08X\n", rc); 299 347 } 300 348 else … … 305 353 nsID *isoUUID = nsnull; 306 354 dvdImage->GetId(&isoUUID); 307 IDVDDrive *dvdDrive = nsnull;308 machine->GetDVDDrive( &dvdDrive);355 nsCOMPtr<IDVDDrive> dvdDrive; 356 machine->GetDVDDrive(getter_AddRefs(dvdDrive)); 309 357 rc = dvdDrive->MountImage(*isoUUID); 310 358 nsMemory::Free(isoUUID); 311 359 if (NS_FAILED(rc)) 312 360 { 313 printf("Error: could not mount ISO image! \n");361 printf("Error: could not mount ISO image! rc=%08X\n", rc); 314 362 } 315 363 else … … 321 369 if (NS_FAILED(rc)) 322 370 { 323 printf("Could not set boot device! \n");371 printf("Could not set boot device! rc=%08X\n", rc); 324 372 } 325 373 } 326 dvdDrive->Release(); 327 dvdImage->Release(); 328 } 329 } 330 331 /* 332 * Register the VM. Note that this call also saves the VM config 333 * to disk. It is also possible to save the VM settings but not 334 * register the VM. 335 */ 336 virtualBox->RegisterMachine(machine); 337 338 machine->Release(); 374 } 375 } 376 377 /* 378 * Save all changes we've just made. 379 */ 380 rc = machine->SaveSettings(); 381 if (NS_FAILED(rc)) 382 { 383 printf("Could not save machine settings! rc=%08X\n", rc); 384 } 385 386 /* 387 * It is always important to close the open session when it becomes not 388 * necessary any more. 389 */ 390 session->Close(); 339 391 } 340 392 … … 344 396 int main(int argc, char *argv[]) 345 397 { 398 /* 399 * Check that PRUnichar is equal in size to what compiler composes L"" 400 * strings from; otherwise NS_LITERAL_STRING macros won't work correctly 401 * and we will get a meaningless SIGSEGV. This, of course, must be checked 402 * at compile time in xpcom/string/nsTDependentString.h, but XPCOM lacks 403 * compile-time assert macros and I'm not going to add them now. 404 */ 405 if (sizeof(PRUnichar) != sizeof(wchar_t)) 406 { 407 printf("Error: sizeof(PRUnichar) {%d} != sizeof(wchar_t) {%d}!\n" 408 "Probably, you forgot the -fshort-wchar compiler option.\n", 409 sizeof(PRUnichar), sizeof(wchar_t)); 410 return -1; 411 } 412 346 413 nsresult rc; 347 414 348 415 /* 349 * This is the standard XPCOM init proce ssing.416 * This is the standard XPCOM init procedure. 350 417 * What we do is just follow the required steps to get an instance 351 * of our main interface, which is IVirtualBox 418 * of our main interface, which is IVirtualBox. 352 419 */ 353 420 XPCOMGlueStartup(nsnull); 354 nsCOMPtr<nsIServiceManager> serviceManager; 355 rc = NS_InitXPCOM2(getter_AddRefs(serviceManager), nsnull, nsnull); 356 if (NS_FAILED(rc)) 357 { 358 printf("Error: XPCOM could not be initialized! rc=0x%x\n", rc); 359 return -1; 360 } 361 362 // register our component 363 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(serviceManager); 364 if (!registrar) 365 { 366 printf("Error: could not query nsIComponentRegistrar interface!\n"); 367 return -1; 368 } 369 registrar->AutoRegister(nsnull); 370 371 // Create the Event Queue for this thread 372 nsCOMPtr<nsIEventQueueService> eqs = do_GetService(kEventQueueServiceCID, &rc); 373 if (NS_FAILED( rc )) 374 { 375 printf("Error: could not get event queue service! rc=%08X\n", rc); 376 return -1; 377 } 378 379 rc = eqs->CreateThreadEventQueue(); 380 if (NS_FAILED( rc )) 381 { 382 printf("Error: could not create thread event queue! rc=%08X\n", rc); 383 return -1; 384 } 385 386 rc = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ); 387 if (NS_FAILED( rc )) 388 { 389 printf("Error: could not get tread event queue! rc=%08X\n", rc); 390 return -1; 391 } 392 393 // get ipc service 394 nsCOMPtr<ipcIService> ipcServ = do_GetService(IPC_SERVICE_CONTRACTID, &rc); 395 if (NS_FAILED( rc )) 396 { 397 printf("Error: could not get ipc service! rc=%08X\n", rc); 398 return -1; 399 } 400 401 // get dconnect service 402 nsCOMPtr<ipcIDConnectService> dcon = do_GetService(IPC_DCONNECTSERVICE_CONTRACTID, &rc); 403 if (NS_FAILED( rc )) 404 { 405 printf("Error: could not get dconnect service! rc=%08X\n", rc); 406 return -1; 407 } 408 409 PRUint32 serverID = 0; 410 rc = ipcServ->ResolveClientName("VirtualBoxServer", &serverID); 411 if (NS_FAILED( rc )) 412 { 413 printf("Error: could not get VirtualBox server ID! rc=%08X\n", rc); 414 return -1; 415 } 416 417 nsCOMPtr<nsIComponentManager> manager = do_QueryInterface(registrar); 418 if (!manager) 419 { 420 printf("Error: could not query nsIComponentManager interface!\n"); 421 return -1; 422 } 423 424 /* 425 * Now XPCOM is ready and we can start to do real work. 426 * IVirtualBox is the root interface of VirtualBox and will be 427 * retrieved from the XPCOM component registrar. We use the 428 * XPCOM provided smart pointer nsCOMPtr for all objects because 429 * that's very convenient and removes the need deal with reference 430 * counting and freeing. 431 */ 432 nsCOMPtr<IVirtualBox> virtualBox; 433 rc = dcon->CreateInstanceByContractID(serverID, NS_VIRTUALBOX_CONTRACTID, 434 NS_GET_IID(IVirtualBox), 435 getter_AddRefs(virtualBox)); 436 if (NS_FAILED(rc)) 437 { 438 printf("Error, could not instantiate VirtualBox object! rc=0x%x\n", rc); 439 return -1; 440 } 441 printf("VirtualBox object created\n"); 442 443 //////////////////////////////////////////////////////////////////////////////// 444 //////////////////////////////////////////////////////////////////////////////// 445 //////////////////////////////////////////////////////////////////////////////// 446 447 448 listVMs(virtualBox); 449 450 createVM(virtualBox); 451 452 453 //////////////////////////////////////////////////////////////////////////////// 454 //////////////////////////////////////////////////////////////////////////////// 455 //////////////////////////////////////////////////////////////////////////////// 456 457 /* this is enough to free the IVirtualBox instance -- smart pointers rule! */ 458 virtualBox = nsnull; 459 460 /* 461 * Process events that might have queued up in the XPCOM event 462 * queue. If we don't process them, the server might hang. 463 */ 464 gEventQ->ProcessPendingEvents(); 465 466 /* 467 * Perform the standard XPCOM shutdown procedure 421 422 /* 423 * Note that we scope all nsCOMPtr variables in order to have all XPCOM 424 * objects automatically released before we call NS_ShutdownXPCOM at the 425 * end. This is an XPCOM requirement. 426 */ 427 { 428 nsCOMPtr<nsIServiceManager> serviceManager; 429 rc = NS_InitXPCOM2(getter_AddRefs(serviceManager), nsnull, nsnull); 430 if (NS_FAILED(rc)) 431 { 432 printf("Error: XPCOM could not be initialized! rc=0x%x\n", rc); 433 return -1; 434 } 435 436 #if 0 437 /* 438 * Register our components. This step is only necessary if this executable 439 * implements XPCOM components itself which is not the case for this 440 * simple example. 441 */ 442 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(serviceManager); 443 if (!registrar) 444 { 445 printf("Error: could not query nsIComponentRegistrar interface!\n"); 446 return -1; 447 } 448 registrar->AutoRegister(nsnull); 449 #endif 450 451 /* 452 * Make sure the main event queue is created. This event queue is 453 * responsible for dispatching incoming XPCOM IPC messages. The main 454 * thread should run this event queue's loop during lengthy non-XPCOM 455 * operations to ensure messages from the VirtualBox server and other 456 * XPCOM IPC clients are processed. This use case doesn't perform such 457 * operations so it doesn't run the event loop. 458 */ 459 nsCOMPtr<nsIEventQueue> eventQ; 460 rc = NS_GetMainEventQ(getter_AddRefs (eventQ)); 461 if (NS_FAILED(rc)) 462 { 463 printf("Error: could not get main event queue! rc=%08X\n", rc); 464 return -1; 465 } 466 467 /* 468 * Now XPCOM is ready and we can start to do real work. 469 * IVirtualBox is the root interface of VirtualBox and will be 470 * retrieved from the XPCOM component manager. We use the 471 * XPCOM provided smart pointer nsCOMPtr for all objects because 472 * that's very convenient and removes the need deal with reference 473 * counting and freeing. 474 */ 475 nsCOMPtr<nsIComponentManager> manager; 476 rc = NS_GetComponentManager (getter_AddRefs (manager)); 477 if (NS_FAILED(rc)) 478 { 479 printf("Error: could not get component manager! rc=%08X\n", rc); 480 return -1; 481 } 482 483 nsCOMPtr<IVirtualBox> virtualBox; 484 rc = manager->CreateInstanceByContractID (NS_VIRTUALBOX_CONTRACTID, 485 nsnull, 486 NS_GET_IID(IVirtualBox), 487 getter_AddRefs(virtualBox)); 488 if (NS_FAILED(rc)) 489 { 490 printf("Error, could not instantiate VirtualBox object! rc=0x%x\n", rc); 491 return -1; 492 } 493 printf("VirtualBox object created\n"); 494 495 //////////////////////////////////////////////////////////////////////////////// 496 //////////////////////////////////////////////////////////////////////////////// 497 //////////////////////////////////////////////////////////////////////////////// 498 499 500 listVMs(virtualBox); 501 502 createVM(virtualBox); 503 504 505 //////////////////////////////////////////////////////////////////////////////// 506 //////////////////////////////////////////////////////////////////////////////// 507 //////////////////////////////////////////////////////////////////////////////// 508 509 /* this is enough to free the IVirtualBox instance -- smart pointers rule! */ 510 virtualBox = nsnull; 511 512 /* 513 * Process events that might have queued up in the XPCOM event 514 * queue. If we don't process them, the server might hang. 515 */ 516 eventQ->ProcessPendingEvents(); 517 } 518 519 /* 520 * Perform the standard XPCOM shutdown procedure. 468 521 */ 469 522 NS_ShutdownXPCOM(nsnull); … … 498 551 return res; 499 552 } 553 554 /** 555 * Helper function to print XPCOM exception information set on the current 556 * thread after a failed XPCOM method call. This function will also print 557 * extended VirtualBox error info if it is available. 558 */ 559 void printErrorInfo() 560 { 561 nsresult rc; 562 563 nsCOMPtr <nsIExceptionService> es; 564 es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc); 565 if (NS_SUCCEEDED (rc)) 566 { 567 nsCOMPtr <nsIExceptionManager> em; 568 rc = es->GetCurrentExceptionManager (getter_AddRefs (em)); 569 if (NS_SUCCEEDED (rc)) 570 { 571 nsCOMPtr<nsIException> ex; 572 rc = em->GetCurrentException (getter_AddRefs (ex)); 573 if (NS_SUCCEEDED (rc) && ex) 574 { 575 nsCOMPtr <IVirtualBoxErrorInfo> info; 576 info = do_QueryInterface(ex, &rc); 577 if (NS_SUCCEEDED(rc) && info) 578 { 579 /* got extended error info */ 580 printf ("Extended error info (IVirtualBoxErrorInfo):\n"); 581 nsresult resultCode = NS_OK; 582 info->GetResultCode (&resultCode); 583 printf (" resultCode=%08X\n", resultCode); 584 nsXPIDLString component; 585 info->GetComponent (getter_Copies (component)); 586 printf (" component=%s\n", NS_ConvertUTF16toUTF8(component).get()); 587 nsXPIDLString text; 588 info->GetText (getter_Copies (text)); 589 printf (" text=%s\n", NS_ConvertUTF16toUTF8(text).get()); 590 } 591 else 592 { 593 /* got basic error info */ 594 printf ("Basic error info (nsIException):\n"); 595 nsresult resultCode = NS_OK; 596 ex->GetResult (&resultCode); 597 printf (" resultCode=%08X\n", resultCode); 598 nsXPIDLCString message; 599 ex->GetMessage (getter_Copies (message)); 600 printf (" message=%s\n", message.get()); 601 } 602 603 /* reset the exception to NULL to indicate we've processed it */ 604 em->SetCurrentException (NULL); 605 606 rc = NS_OK; 607 } 608 } 609 } 610 }
Note:
See TracChangeset
for help on using the changeset viewer.