Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DllImportGenerator] ICustomMarshaler ~support #963

Open
Tracked by #60595
SupinePandora43 opened this issue Apr 14, 2021 · 2 comments
Open
Tracked by #60595

[DllImportGenerator] ICustomMarshaler ~support #963

SupinePandora43 opened this issue Apr 14, 2021 · 2 comments
Labels
area-DllImportGenerator Source Generated stubs for P/Invokes in C#

Comments

@SupinePandora43
Copy link

SupinePandora43 commented Apr 14, 2021

Note that this example doesn't make use of cookie and marshaler instances, and need additional code to work.

Input

internal class NullMarshaler : ICustomMarshaler {
    public static ICustomMarshaler GetInstance(string pstrCookie){
        return new NullMarshaler();
    }
    public IntPtr MarshalManagedToNative(object obj){
        return IntPtr.Zero;
    }
    public object MarshalNativeToManaged(IntPtr native){
        return null;
    }
}
public static class Methods {
    [GeneratedDllImport("example")]
    [return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(NullMarshaler))]
    public static partial string ExampleMethod([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(NullMarshaler))]string input);
}

Example Generated output

public static partial string ExampleMethod(string input){
    IntPtr __input_gen_native = default;
    string __retVal = default;
    IntPtr __retVal_gen_native = default;
    //
    // Marshal
    //
    ICustomMarshaler marshaler = NullMarshaler.GetInstance(null);
    __input_gen_native = marshaler.MarshalManagedToNative(input);
    //
    // Invoke
    //
    __retVal_gen_native = ExampleMethod__PInvoke(__input_gen_native);
    //
    // Unmarshal
    //
    __retVal = (string)marshaler.MarshalNativeToManaged(__retVal_gen_native);
    return __retVal;
}
[DllImport("example", EntryPoint = "ExampleMethod")]
private static IntPtr ExampleMethod__PInvoke__(IntPtr input);

Probably MarshalUsing should be used instead of custom marshalers.

@MichalStrehovsky MichalStrehovsky added the area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation label Apr 14, 2021
@jkotas jkotas added area-DllImportGenerator Source Generated stubs for P/Invokes in C# and removed area-NativeAOT-coreclr .NET runtime optimized for ahead of time compilation labels Apr 15, 2021
@SupinePandora43
Copy link
Author

the only thing how it relates to NativeAot is that NativeAot doesn't support Custom Marshalers.

@jkoritzinsky
Copy link
Member

We've decided that for V1 of the DllImportGenerator that we plan to not support ICustomMarshaler-based marshalling due to its limitations around value types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-DllImportGenerator Source Generated stubs for P/Invokes in C#
Projects
None yet
Development

No branches or pull requests

4 participants