[vbox-dev] A question about virtualbox sdk api

Klaus Espenlaub klaus.espenlaub at oracle.com
Wed Oct 9 10:24:12 UTC 2013


Hi,

On 09.10.2013 10:24, magicdmer at gmail.com wrote:
> Hi!
> I'm new to Virtualbox Development. I have a few problems when i use
> virtualboxsdk.
>
>     *IMedium* pIMedium = NULL;*
>     *rc = IMediumAttachmentVector[j]->get_Medium(&pIMedium);*
>     *if(SUCCEEDED(rc) && pIMedium)*
>     *{*
>     *        IMedium* BaseMedium = NULL;*
>     *        rc = pIMedium->get_Base(&BaseMedium);*
>     *        if(SUCCEEDED(rc) && BaseMedium)*
>     *        {*
>     *                BSTR bstrName;*
>     *                BaseMedium->get_Name(&bstrName);*
>     *                LONG64 aLogicalSize = 0;*
>     *                if(Device.Type == DeviceType_HardDisk)*
>     *                {*
>     *                        rc =
>     pIMedium->get_LogicalSize(&aLogicalSize);  //here can't get the real
>     size, always zero*
>     *                }*
>     *else*
>     *{*
>     *                        rc = pIMedium->get_Size(&aLogicalSize);*
>     *                }*
>     *                SAFE_RELEASE(BaseMedium);*
>     *        }*
>     *        SAFE_RELEASE(pIMedium);*
>     *}*
>
> The API "get_LogicalSize" can't get the real size ,it is always zero and
> return value is ok. but when i open virtualbox (virtualbox.exe) , the
> function Execute successfully and get the real size. Does it need some
> preconditions ?

Yes. As it's very expensive to get all medium information straight when 
the API service starts (in big install this can delay API availability 
by many seconds or even minutes), it is done only on request. And the 
GUI does that when enumerating all media.

This is clearly documented in the general explanations to IMedium, see 
https://www.virtualbox.org/sdkref/interface_i_medium.html#_details

As usual it's a good idea to look at our "sample" code, e.g. the code in 
VBoxManage which shows the medium information or lists media. There is 
always a call to IMedium::RefreshState nearby, either unconditional or 
preceded by a check if the current state of the medium is 
MediumState_Inaccessible.

Keep in mind that VirtualBox uses its own API pretty much everywhere, 
even though it's not always obvious because we use "glue" to make one 
piece of code build with both COM (Windows) and XPCOM (all other 
platforms), and the GUI even has generated client wrappers which do 
automatic string type conversions. Pretty much everything is used in 
some form by VirtualBox itself.

Klaus



More information about the vbox-dev mailing list