VirtualBox

Ignore:
Timestamp:
Aug 16, 2013 1:39:49 PM (12 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
88074
Message:

tstVBoxAPILinux: a few fixes and make sure to remove the registered VM at the end

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/testcase/tstVBoxAPILinux.cpp

    r44528 r47813  
    8989#include <VBox/com/VirtualBox.h>
    9090
     91#include <iprt/stream.h>
     92
    9193
    9294/*
     
    113115    nsresult rc;
    114116
    115     printf("----------------------------------------------------\n");
    116     printf("VM List:\n\n");
     117    RTPrintf("----------------------------------------------------\n");
     118    RTPrintf("VM List:\n\n");
    117119
    118120    /*
     
    141143                    machine->GetName(getter_Copies(machineName));
    142144                    char *machineNameAscii = ToNewCString(machineName);
    143                     printf("\tName:        %s\n", machineNameAscii);
     145                    RTPrintf("\tName:        %s\n", machineNameAscii);
    144146                    free(machineNameAscii);
    145147                }
    146148                else
    147149                {
    148                     printf("\tName:        <inaccessible>\n");
     150                    RTPrintf("\tName:        <inaccessible>\n");
    149151                }
    150152
     
    152154                machine->GetId(getter_Copies(iid));
    153155                const char *uuidString = ToNewCString(iid);
    154                 printf("\tUUID:        %s\n", uuidString);
     156                RTPrintf("\tUUID:        %s\n", uuidString);
    155157                free((void*)uuidString);
    156158
     
    160162                    machine->GetSettingsFilePath(getter_Copies(configFile));
    161163                    char *configFileAscii = ToNewCString(configFile);
    162                     printf("\tConfig file: %s\n", configFileAscii);
     164                    RTPrintf("\tConfig file: %s\n", configFileAscii);
    163165                    free(configFileAscii);
    164166
    165167                    PRUint32 memorySize;
    166168                    machine->GetMemorySize(&memorySize);
    167                     printf("\tMemory size: %uMB\n", memorySize);
     169                    RTPrintf("\tMemory size: %uMB\n", memorySize);
    168170
    169171                    nsXPIDLString typeId;
     
    174176                    osType->GetDescription(getter_Copies(osName));
    175177                    char *osNameAscii = ToNewCString(osName);
    176                     printf("\tGuest OS:    %s\n\n", osNameAscii);
     178                    RTPrintf("\tGuest OS:    %s\n\n", osNameAscii);
    177179                    free(osNameAscii);
    178180                    osType->Release();
     
    184186        }
    185187    }
    186     printf("----------------------------------------------------\n\n");
     188    RTPrintf("----------------------------------------------------\n\n");
    187189}
    188190
     
    208210    if (NS_FAILED(rc))
    209211    {
    210         printf("Error: could not create machine! rc=%08X\n", rc);
     212        RTPrintf("Error: could not create machine! rc=%Rhrc\n", rc);
    211213        return;
    212214    }
     
    230232     */
    231233    nsCOMPtr<IGuestOSType> osType;
    232     rc = virtualBox->GetGuestOSType(NS_LITERAL_STRING("win2k").get(),
     234    rc = virtualBox->GetGuestOSType(NS_LITERAL_STRING("Windows2000").get(),
    233235                                    getter_AddRefs(osType));
    234236    if (NS_FAILED(rc))
    235237    {
    236         printf("Error: could not find guest OS type! rc=%08X\n", rc);
     238        RTPrintf("Error: could not find guest OS type! rc=%Rhrc\n", rc);
    237239    }
    238240    else
    239241    {
    240         machine->SetOSTypeId (NS_LITERAL_STRING("win2k").get());
     242        machine->SetOSTypeId (NS_LITERAL_STRING("Windows2000").get());
    241243    }
    242244
     
    252254    if (NS_FAILED(rc))
    253255    {
    254         printf("Error: could not register machine! rc=%08X\n", rc);
     256        RTPrintf("Error: could not register machine! rc=%Rhrc\n", rc);
    255257        printErrorInfo();
    256258        return;
    257259    }
    258260
     261    nsCOMPtr<IMachine> origMachine = machine;
     262
    259263    /*
    260264     * In order to manipulate the registered machine, we must open a session
     
    262266     */
    263267    nsCOMPtr<ISession> session;
     268    nsCOMPtr<IMachine> sessionMachine;
    264269    {
    265270        nsCOMPtr<nsIComponentManager> manager;
     
    267272        if (NS_FAILED(rc))
    268273        {
    269             printf("Error: could not get component manager! rc=%08X\n", rc);
     274            RTPrintf("Error: could not get component manager! rc=%Rhrc\n", rc);
    270275            return;
    271276        }
     
    276281        if (NS_FAILED(rc))
    277282        {
    278             printf("Error, could not instantiate session object! rc=0x%x\n", rc);
     283            RTPrintf("Error, could not instantiate session object! rc=0x%x\n", rc);
    279284            return;
    280285        }
     
    283288        if (NS_FAILED(rc))
    284289        {
    285             printf("Error, could not lock the machine for the session! rc=0x%x\n", rc);
     290            RTPrintf("Error, could not lock the machine for the session! rc=0x%x\n", rc);
    286291            return;
    287292        }
     
    292297         * it from the opened session object.
    293298         */
    294         rc = session->GetMachine(getter_AddRefs(machine));
    295         if (NS_FAILED(rc))
    296         {
    297             printf("Error, could not get machine session! rc=0x%x\n", rc);
     299        rc = session->GetMachine(getter_AddRefs(sessionMachine));
     300        if (NS_FAILED(rc))
     301        {
     302            RTPrintf("Error, could not get machine session! rc=0x%x\n", rc);
    298303            return;
    299304        }
     
    309314    if (NS_FAILED(rc))
    310315    {
    311         printf("Failed creating a hard disk object! rc=%08X\n", rc);
     316        RTPrintf("Failed creating a hard disk object! rc=%Rhrc\n", rc);
    312317    }
    313318    else
     
    326331        if (NS_FAILED(rc))
    327332        {
    328             printf("Failed creating hard disk image! rc=%08X\n", rc);
     333            RTPrintf("Failed creating hard disk image! rc=%Rhrc\n", rc);
    329334        }
    330335        else
     
    340345            if (NS_FAILED(rc) || NS_FAILED(resultCode))
    341346            {
    342                 printf("Error: could not create hard disk! rc=%08X\n",
     347                RTPrintf("Error: could not create hard disk! rc=%Rhrc\n",
    343348                       NS_FAILED(rc) ? rc : resultCode);
    344349            }
     
    348353                 * Now that it's created, we can assign it to the VM.
    349354                 */
    350                 rc = machine->AttachDevice(NS_LITERAL_STRING("IDE Controller").get(), // controller identifier
     355                rc = sessionMachine->AttachDevice(
     356                                           NS_LITERAL_STRING("IDE Controller").get(), // controller identifier
    351357                                           0,                              // channel number on the controller
    352358                                           0,                              // device number on the controller
     
    355361                if (NS_FAILED(rc))
    356362                {
    357                     printf("Error: could not attach hard disk! rc=%08X\n", rc);
     363                    RTPrintf("Error: could not attach hard disk! rc=%Rhrc\n", rc);
    358364                }
    359365            }
     
    374380                                getter_AddRefs(dvdImage));
    375381    if (NS_FAILED(rc))
    376         printf("Error: could not open CD image! rc=%08X\n", rc);
     382        RTPrintf("Error: could not open CD image! rc=%Rhrc\n", rc);
    377383    else
    378384    {
     
    380386         * Now assign it to our VM
    381387         */
    382         rc = machine->MountMedium(NS_LITERAL_STRING("IDE Controller").get(), // controller identifier
     388        rc = sessionMachine->MountMedium(
     389                                  NS_LITERAL_STRING("IDE Controller").get(), // controller identifier
    383390                                  2,                              // channel number on the controller
    384391                                  0,                              // device number on the controller
     
    387394        if (NS_FAILED(rc))
    388395        {
    389             printf("Error: could not mount ISO image! rc=%08X\n", rc);
     396            RTPrintf("Error: could not mount ISO image! rc=%Rhrc\n", rc);
    390397        }
    391398        else
     
    394401             * Last step: tell the VM to boot from the CD.
    395402             */
    396             rc = machine->SetBootOrder (1, DeviceType::DVD);
     403            rc = sessionMachine->SetBootOrder (1, DeviceType::DVD);
    397404            if (NS_FAILED(rc))
    398405            {
    399                 printf("Could not set boot device! rc=%08X\n", rc);
     406                RTPrintf("Could not set boot device! rc=%Rhrc\n", rc);
    400407            }
    401408        }
     
    405412     * Save all changes we've just made.
    406413     */
    407     rc = machine->SaveSettings();
     414    rc = sessionMachine->SaveSettings();
    408415    if (NS_FAILED(rc))
    409     {
    410         printf("Could not save machine settings! rc=%08X\n", rc);
    411     }
     416        RTPrintf("Could not save machine settings! rc=%Rhrc\n", rc);
    412417
    413418    /*
     
    416421     */
    417422    session->UnlockMachine();
     423
     424    com::SafeIfaceArray<IMedium> aMedia;
     425    rc = machine->Unregister((CleanupMode_T)CleanupMode_DetachAllReturnHardDisksOnly,
     426                             ComSafeArrayAsOutParam(aMedia));
     427    if (NS_FAILED(rc))
     428        RTPrintf("Unregistering the machine failed! rc=%Rhrc\n", rc);
     429    else
     430    {
     431        ComPtr<IProgress> pProgress;
     432        rc = machine->DeleteConfig(ComSafeArrayAsInParam(aMedia), pProgress.asOutParam());
     433        if (NS_FAILED(rc))
     434            RTPrintf("Deleting of machine failed! rc=%Rhrc\n", rc);
     435        else
     436        {
     437            rc = pProgress->WaitForCompletion(-1);
     438            PRInt32 resultCode;
     439            pProgress->GetResultCode(&resultCode);
     440            if (NS_FAILED(rc) || NS_FAILED(resultCode))
     441                RTPrintf("Failed to delete the machine! rc=%Rhrc\n",
     442                         NS_FAILED(rc) ? rc : resultCode);
     443        }
     444    }
    418445}
    419446
     
    432459    if (sizeof(PRUnichar) != sizeof(wchar_t))
    433460    {
    434         printf("Error: sizeof(PRUnichar) {%lu} != sizeof(wchar_t) {%lu}!\n"
     461        RTPrintf("Error: sizeof(PRUnichar) {%lu} != sizeof(wchar_t) {%lu}!\n"
    435462               "Probably, you forgot the -fshort-wchar compiler option.\n",
    436463               (unsigned long) sizeof(PRUnichar),
     
    460487        if (NS_FAILED(rc))
    461488        {
    462             printf("Error: XPCOM could not be initialized! rc=0x%x\n", rc);
     489            RTPrintf("Error: XPCOM could not be initialized! rc=0x%x\n", rc);
    463490            return -1;
    464491        }
     
    473500        if (!registrar)
    474501        {
    475             printf("Error: could not query nsIComponentRegistrar interface!\n");
     502            RTPrintf("Error: could not query nsIComponentRegistrar interface!\n");
    476503            return -1;
    477504        }
     
    491518        if (NS_FAILED(rc))
    492519        {
    493             printf("Error: could not get main event queue! rc=%08X\n", rc);
     520            RTPrintf("Error: could not get main event queue! rc=%Rhrc\n", rc);
    494521            return -1;
    495522        }
     
    507534        if (NS_FAILED(rc))
    508535        {
    509             printf("Error: could not get component manager! rc=%08X\n", rc);
     536            RTPrintf("Error: could not get component manager! rc=%Rhrc\n", rc);
    510537            return -1;
    511538        }
     
    518545        if (NS_FAILED(rc))
    519546        {
    520             printf("Error, could not instantiate VirtualBox object! rc=0x%x\n", rc);
     547            RTPrintf("Error, could not instantiate VirtualBox object! rc=0x%x\n", rc);
    521548            return -1;
    522549        }
    523         printf("VirtualBox object created\n");
     550        RTPrintf("VirtualBox object created\n");
    524551
    525552        ////////////////////////////////////////////////////////////////////////////////
     
    554581    XPCOMGlueShutdown();
    555582#endif
    556     printf("Done!\n");
     583    RTPrintf("Done!\n");
    557584    return 0;
    558585}
     
    575602    if (res != NULL)
    576603    {
    577         snprintf(res, 39, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
     604        RTStrPrintf(res, 39, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
    578605                 guid->m0, (PRUint32)guid->m1, (PRUint32)guid->m2,
    579606                 (PRUint32)guid->m3[0], (PRUint32)guid->m3[1], (PRUint32)guid->m3[2],
     
    610637                {
    611638                    /* got extended error info */
    612                     printf ("Extended error info (IVirtualBoxErrorInfo):\n");
     639                    RTPrintf ("Extended error info (IVirtualBoxErrorInfo):\n");
    613640                    PRInt32 resultCode = NS_OK;
    614641                    info->GetResultCode (&resultCode);
    615                     printf ("  resultCode=%08X\n", resultCode);
     642                    RTPrintf ("  resultCode=%08X\n", resultCode);
    616643                    nsXPIDLString component;
    617644                    info->GetComponent (getter_Copies (component));
    618                     printf ("  component=%s\n", NS_ConvertUTF16toUTF8(component).get());
     645                    RTPrintf ("  component=%s\n", NS_ConvertUTF16toUTF8(component).get());
    619646                    nsXPIDLString text;
    620647                    info->GetText (getter_Copies (text));
    621                     printf ("  text=%s\n", NS_ConvertUTF16toUTF8(text).get());
     648                    RTPrintf ("  text=%s\n", NS_ConvertUTF16toUTF8(text).get());
    622649                }
    623650                else
    624651                {
    625652                    /* got basic error info */
    626                     printf ("Basic error info (nsIException):\n");
     653                    RTPrintf ("Basic error info (nsIException):\n");
    627654                    nsresult resultCode = NS_OK;
    628655                    ex->GetResult (&resultCode);
    629                     printf ("  resultCode=%08X\n", resultCode);
     656                    RTPrintf ("  resultCode=%08X\n", resultCode);
    630657                    nsXPIDLCString message;
    631658                    ex->GetMessage (getter_Copies (message));
    632                     printf ("  message=%s\n", message.get());
     659                    RTPrintf ("  message=%s\n", message.get());
    633660                }
    634661
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette