[vbox-dev] Help with BSTR and PCRTUTF16

Klaus Espenlaub klaus.espenlaub at oracle.com
Tue Apr 16 04:44:26 PDT 2013


Hi Ribhi,

BSTR and PCRTUTF16 should only differ by 'const', and similarly RTUTF16* 
should be compatible with IN_BSTR (again the latter is const) which one 
of the com::Bstr constructors accepts.

So you need to check which of your compiler flags declares going from 
non-const to const types (i.e. the trouble free direction) as invalid. I 
can understand any compiler complaining about the other direction, but 
this one is safe.

BTW, I really hope that you're not serious by "making a DLL wrapper 
around vboxmanage" - we always advised against using VBoxManage for 
serious work, because it has extremely limited error reporting capabilities.

The proper approach in any application is using the API, period.

Klaus

On 15.04.2013 18:06, Ribhi Kamal wrote:
> Hi all,
> I'm making a DLL wrapper around vboxmanage using Visual Studio 2010. For
> some reason VS gives me error C2664/C2440 when it is trying to convert
> from BSTR to PCRTUTF16.
> 1>C:\vboxbuild\virtualbox\latest\src\include\VBox/com/Guid.h(112): error
> C2664: 'RTUuidFromUtf16' : cannot convert parameter 2 from 'BSTR' to
> 'PCRTUTF16'
> 1>          Types pointed to are unrelated; conversion requires
> reinterpret_cast, C-style cast or function-style cast
> 1>C:\vboxbuild\virtualbox\latest\src\include\VBox/com/Guid.h(198): error
> C2440: '<function-style-cast>' : cannot convert from 'RTUTF16 [37]' to
> 'com::Bstr'
> 1>          No constructor could take the source type, or constructor
> overload resolution was ambiguous
>
> I worked around that by using the casings below. But I was wondering why
> compiling vboxmanage doesn't produce the same errors. I tried to make my
> compile options match the vbox build options as much as possible but I'm
> sure I missed something. Below is are my options used to compile part of
> the project:
>
> /Zi /nologo /W3 /WX-
> /O2 /Oy-
> /Gm- /EHsc /MD /GS
> /fp:precise
> /Zc:wchar_t
> /Zc:forScope-
> /GR
> -wd4065 -wd4996 -wd4250
> /Fp"Release\VboxCom.pch" /Fa"Release\" /Fo"Release\"
> /Fd"Release\vcWindows7.1SDK.pdb" /Gd /analyze- /errorReport:queue
>   /D "__WIN__" /D "RT_OS_WINDOWS" /D "_WIN32_WINNT=0x0501" /D "WIN32" /D
> "__WIN32__" /D "RT_ARCH_X86" /D "__X86__" /D "_X86_" /D "_LIB" /D "VBOX"
> /D "VBOX_OSE" /D "VBOX_WITH_64_BITS_GUESTS" /D "IN_RING3" /D "UNICODE"
> /D "NDEBUG=1" /D "_REENTRANT" /D "_UNICODE"
>
> Any help would be greatly appreciated. Below are the castings that I had
> to make (V4.2.12):
>
> --------------------------- include/VBox/com/Guid.h
> ---------------------------
> index c55d69d..01884d2 100644
> @@ -109,7 +109,7 @@ public:
>       Guid(const Bstr &that)
>       {
>           int rc = !that.isEmpty()
> -               ? ::RTUuidFromUtf16(&mUuid, that.raw())
> +               ? ::RTUuidFromUtf16(&mUuid, (PCRTUTF16)that.raw())
>                  : VERR_INVALID_UUID_FORMAT;
>           if (RT_FAILURE(rc))
>               ::RTUuidClear(&mUuid);
> @@ -195,7 +195,7 @@ public:
>
>           RTUTF16 buf[RTUUID_STR_LENGTH];
>           ::RTUuidToUtf16(&mUuid, buf, RTUUID_STR_LENGTH);
> -        return Bstr(buf);
> +        return Bstr((CBSTR)buf);
>       }
>
>       bool isEmpty() const
>
> ------------- src/VBox/Frontends/VBoxManage/VBoxManageDebugVM.cpp
> -------------
> index 5901c53..72addfe 100644
> @@ -87,7 +87,7 @@ static RTEXITCODE
> handleDebugVM_GetRegisters(HandlerArg *pArgs, IMachineDebugger
>                       size_t cchMaxName  = 8;
>                       for (size_t i = 0; i < aBstrNames.size(); i++)
>                       {
> -                        size_t cchName = RTUtf16Len(aBstrNames[i]);
> +                        size_t cchName =
> RTUtf16Len((PCRTUTF16)aBstrNames[i]);
>                           if (cchName > cchMaxName)
>                               cchMaxName = cchName;
>                       }
>
> ------------------------ src/VBox/Main/glue/string.cpp
> ------------------------
> index 9865af5..51af313 100644
> @@ -179,7 +179,7 @@ void Utf8Str::copyFrom(CBSTR a_pbstr)
>                   && vrc != VERR_NO_MEMORY)
>               {
>                   /* ASSUME: input is valid Utf-16. Fake out of memory
> error. */
> -                AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc,
> RTUtf16Len(a_pbstr) * sizeof(RTUTF16), a_pbstr));
> +                AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc,
> RTUtf16Len((PCRTUTF16)a_pbstr) * sizeof(RTUTF16), a_pbstr));
>               }
>
>               m_cch = 0;
> @@ -221,7 +221,7 @@ HRESULT Utf8Str::copyFromEx(CBSTR a_pbstr)
>                   && vrc != VERR_NO_MEMORY)
>               {
>                   /* ASSUME: input is valid Utf-16. Fake out of memory
> error. */
> -                AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc,
> RTUtf16Len(a_pbstr) * sizeof(RTUTF16), a_pbstr));
> +                AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc,
> RTUtf16Len((PCRTUTF16)a_pbstr) * sizeof(RTUTF16), a_pbstr));
>               }
>
>               m_cch = 0;
>
> --
> -- Ribhi



More information about the vbox-dev mailing list