BSTR type in c#

I received a DLL which uses BSTR as string type:

example : given in the include file

DLLEXPORT BSTR WINAPI I2cGetInterfaceTypeStr (BSTR Interface);

how do i access this function in vc#. This doesn't work.

[DllImport("I2CAPI32.dll")] public static extern string I2cGetInterfaceTypeStr (string Interface);

Other functions of the DLL which don't use BSTR are working ok.

Can anyone help me.

Thanks, Jan



Answer this question

BSTR type in c#

  • Ftan

    Change --> public static extern string
    into
    public static extern IntPtr
    and use the Marshal.PtrToStringBSTR to copy the BSTR to a managed string.
     
    Willy.
     
    I received a DLL which uses BSTR as string type:

    example : given in the include file

    DLLEXPORT BSTR WINAPI I2cGetInterfaceTypeStr (BSTR Interface);

    how do i access this function in vc#. This doesn't work.

    [DllImport("I2CAPI32.dll")] public static extern string I2cGetInterfaceTypeStr (string Interface);

    Other functions of the DLL which don't use BSTR are working ok.

    Can anyone help me.

    Thanks, Jan

     

     


  • Khadragy

    There is something about BSTR on this link:
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/csref/html/vcwlkCOMInteropPart1CClientTutorial.asp

    This might help you a little further on finding how to use what your are trying to do :)


  • Martin Bussieres

    It's pretty unusual to see a function directly returning a BSTR like that, but I think this should work:

    [DllImport("I2CAPI32.dll", CharSet=CharSet.Unicode)]
    [return: MarshalAs(UnmanagedType.BStr)]
    public static extern string I2cGetInterfaceTypeStr([MarshalAs(UnmanagedType.BStr)] string Interface);

     

  • BSTR type in c#