Top  Previous  Next

BSTR is normally defined as an OLECHAR* and is normally a pointer to a wide string.


So this is valid:


   CComBSTR bstrSketchName ;

   pSketch->get_Name (&bstrSketchName) ;

   TRACE (L"The name of the sketch just made is %s\n",bstrSketchName) ;


Remember that with TRACE the format specifier requires L to indicate wide char string types (wchar_t). See here for another example.


CComBSTR is a class which encapsulates BSTR:


class CComBSTR



       BSTR m_str;




Here's an example of going from CComBSTR to CString:


   BSTR bstrThisUserParamName ;


   const CString kcsThisParamName(bstrThisUserParamName);



So use CComBSTR instead of BSTR whenever you can, since BSTR requires more explicit memory handling, a call to ::SysFreeString after use for example:


  BSTR DisplayName;

  HRESULT hr = pSketch->get_Name(&DisplayName);

  ... do things...



As an aside I doubt that anyone is still using ASCII, almost all Inventor programmers are using UNICODE. For that reason I no longer use the _T macro, which was used to conditionally compile for both ASCII and UNICODE.


You of course have to check what type of string a given function is requesting. In case of calling Inventor API functions the strings are required as BSTR (sometimes wrapped in a VARIANT) which you can get in multiple ways. All of them can create it from wchar_t: SysAllocString, CComBSTR, _bstr_t


Here is how to get a CString from a _bstr_t.



       _bstr_t FullName (pPartCompDef->iPartMember->GetReferencedFile()->GetFullFileName());

       const CString kcsFulName((wchar_t*)FullName);






Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)