How to best convert CString to BSTR to pass it as an “in” parameter into a COM method?

前端 未结 3 705
心在旅途
心在旅途 2021-01-12 03:18

I need to convert a CString instance into a properly allocated BSTR and pass that BSTR into a COM method. To have code that compiles a

相关标签:
3条回答
  • 2021-01-12 03:23

    One of the confusing aspects of Windows programming is managing the conversion of Visual Basic style strings to/from C language style strings. It isn't that it is so difficult, it is just difficult to remember the details. It is usually not done often, and the MSDN documentation is so voluminous that it is difficult to find answers to your questions. But, the worst part is that you could perform some typecast that compiles fine, but doesn't work the way you expect. This results in code that doesn't work, and the bugs are hard to track down. After some experience, you learn to make sure your string conversions are doing what you expect.

    C strings are arrays of characters terminated by a NULL character. Visual Basic strings differ in that the length of the string precede the characters in the string. So, a VB string knows its own length. In addition, all VB strings are Unicode (16 bits per character). String Types

    BSTR/C String conversions are required if:

    You are doing COM programming in C/C++
    You are writing multiple language applications, such as C++ DLL's accessed by Visual Basic applications.
    
    0 讨论(0)
  • 2021-01-12 03:35

    One of _bstr_t constructors allows you to simply attach to existing BSTR so that you can have the exception that you want from CString::AllocSysString when BSTR allocation fails.

    // _bstr_t simply attaches to BSTR, doesn't reallocate it
    interface->CallMethod( _bstr_t(sourceString.AllocSysString(), false) );
    

    The _bstr_t constructor documentation says:

    _bstr_t(
       BSTR bstr,
       bool fCopy 
    );
    

    fCopy
    If false, the bstr argument is attached to the new object without making a copy by calling SysAllocString.

    On the other hand, CComBSTR constructor doesn't seem to have the corresponding signature; although it can be used as well if BSTR allocation failure exception is not really needed, as mentioned by Phil Booth in his answer.

    0 讨论(0)
  • 2021-01-12 03:41

    CComBSTR has overloaded constructors for both char* and wchar_t*, which make the call to SysAllocString() on your behalf. So the explicit allocation in your code snippet is actually unnecessary. The following would work just as well:

    ATL::CComBSTR converted = sourceString;
    interface->CallMethod(converted);
    

    Furthermore, if you have no need to use the converted BSTR elsewhere in your code, you can perform the object construction in-place in the method call, like so:

    interface->CallMethod(ATL::CComBSTR(sourceString));
    

    The same applies to the _bstr_t class, which can be used instead of CComBSTR if you don't want a dependency on the ATL.

    0 讨论(0)
提交回复
热议问题