Skip to content

Add support for MarshalAs(UnmanagedType.BSTR) on UNIX systems #11002

@odhanson

Description

@odhanson

Although Marshal.StringToBStr et al. works fine on Mac. The following code

delegate void Callback([MarshalAs(UnmanagedType.BStr)]string s);

        [DllImport("libTestDynLib.dylib")]
        static extern void MyTest([MarshalAs(UnmanagedType.BStr)]string s, Callback c);

        static void Main(string[] args)
        {
            MyTest("This is a test", s =>
            {
                Console.WriteLine(s);
            });
        }

Is failing on Mac:
System.Runtime.InteropServices.MarshalDirectiveException: "Cannot marshal 'parameter dotnet/coreclr#1': Invalid managed/unmanaged type combination (String parameters and return types must be paired with LPStr, LPWStr, or LPTStr)."
at TestBSTR.Program.MyTest(String s, Callback c)\n at TestBSTR.Program.Main(String[] args) in /Users/odhanson/Projects/MyGuidTest/TestBSTR/Program.cs:15

Seems like (for looking at the code) this has only been implemented for windows (as part of COM support).

We are currently in the process of porting a FX application to Core, that has extensive interop with existing native libraries (that are cross platform). The BSTR is used extensively and we would definitely need support for this type of marshaling.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions