none
Importing BSTR parameter from COM

    Question

  • If I have a BSTR parameter in a COM interface, can I import into C# as a simple string even if it's listed as an out parameter?  I already have the [MarshalAs(UnmanagedType.BStr)] attribute in place.  Is that enough?

    Will Pittenger

    Sunday, April 23, 2017 8:30 PM

All replies

  • Hi Will Pittenger,

    >>If I have a BSTR parameter in a COM interface, can I import into C# as a simple string even if it's listed as an out parameter?  I already have the [MarshalAs(UnmanagedType.BStr)] attribute in place.  Is that enough?

    you could mark BSTR with MarshalAsAttribute so that Runtime specifically knows how the application wants this string to be marshaled. Forgetting this will result in heap corruption with similar call stack as shown below.

    You could also use Marshal.PtrToStringBSTR method to convert it to the managed string.

    For more information, please refer to:

    https://blogs.msdn.microsoft.com/varunsekhri/2007/02/28/marshaling-bstrs-in-cominterop-or-pinvoke/

    Best regards,

    Cole Wu


    MSDN Community Support<br/> Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact <a href="mailto:MSDNFSF@microsoft.com">MSDNFSF@microsoft.com</a>.

    Monday, April 24, 2017 9:00 AM
    Moderator
  • You didn't answer the question.  I don't want to manually marshal the string.  To much trouble since I may not be the caller.  It'd be a lot better if I could use the code below:

    void Method([Out][MarshalAs(UnmanagedType.BSTR)] out string strText);

    Is that legal or would that cause problems?  My alternative is the code below:

    void Method([In][Out][MarhsalAs(UnmanagedType.BStr)] ref StringBuilder strbText);


    Will Pittenger

    Monday, April 24, 2017 10:30 AM
  • Hi Will Pittenger,

    As I methion before, you could use the UnmanagedType enumeration with the System.Runtime.InteropServices.MarshalAsAttribute attribute to specify how types are marshaled during interoperation with unmanaged code.

    For more information, please refer to:

    https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedtype(v=vs.110).aspx

    >>Is that legal or would that cause problems?  My alternative is the code below:

    As far as I know, BSTR which is the default string in COM, on the String data type. please modify it like this:

    void Method([In, Out, MarshalAs( UnmanagedType.BStr)] ref string strbText);
    
    

    Best regards,

    Cole Wu


    MSDN Community Support<br/> Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact <a href="mailto:MSDNFSF@microsoft.com">MSDNFSF@microsoft.com</a>.

    Tuesday, April 25, 2017 3:04 AM
    Moderator
  • Well, [ref] was only required if I needed the StringBuilder so I could ensure a buffer was ready.

    Will Pittenger

    Tuesday, April 25, 2017 7:13 AM
  • Hi Will Pittenger,

    If you encounter any issues, please feel free let me know.

    Best regards,

    Cole Wu


    MSDN Community Support<br/> Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact <a href="mailto:MSDNFSF@microsoft.com">MSDNFSF@microsoft.com</a>.

    Wednesday, April 26, 2017 2:39 AM
    Moderator
  • You didn't sound like you knew for certain, so I unmarked it.

    Will Pittenger

    Wednesday, April 26, 2017 2:39 AM
  • Hi Will Pittenger,

    >>was only required if I needed the StringBuilder so I could ensure a buffer was ready

    If you need to use StringBuilder, I would suggest that you could LPStr, which is a single byte, null-terminated ANSI character string. You can use this member on the System.String and System.Text.StringBuilder data types instead of BSTR.

    Best regards,

    Cole Wu


    MSDN Community Support<br/> Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact <a href="mailto:MSDNFSF@microsoft.com">MSDNFSF@microsoft.com</a>.

    Wednesday, April 26, 2017 3:29 AM
    Moderator