Skip to content

Generate wrapper structs for unmanaged delegates #778

@JeremyKuhne

Description

@JeremyKuhne

Is your feature request related to a problem? Please describe.
APIs take native function pointers, which are defined in the metadata, but we have no wrapping projection for them.

For example, in the Win32Metadata:

[StructLayout(LayoutKind.Auto, CharSet = CharSet.Auto)]
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate LRESULT WNDPROC([In] HWND param0, [In] uint param1, [In] WPARAM param2, [In] LPARAM param3);

Describe the solution you'd like

Create a "handle" struct like we do for HANDLE objects. Here is how I've wrapped this manually:

public unsafe readonly struct WNDPROC
{
    public readonly delegate* unmanaged<HWND, uint, WPARAM, LPARAM, LRESULT> Value;

    public WNDPROC(delegate* unmanaged<HWND, uint, WPARAM, LPARAM, LRESULT> value) => Value = value;
    public bool IsNull => Value is null;

    public static implicit operator WNDPROC(delegate* unmanaged<HWND, uint, WPARAM, LPARAM, LRESULT> value)
        => new(value);
    public static implicit operator delegate* unmanaged<HWND, uint, WPARAM, LPARAM, LRESULT>(WNDPROC value)
        => value.Value;
}

With that you should be able to use the struct in the APIs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions