dotnet/pinvoke – Win32 projected to nuget space

Instead of manualy writting DllImports, we just add a package reference to PInvoke.User32 and add following usings:

using PInvoke;
using static PInvoke.User32;

to be able to call naturally CreateWindow like that:

IntPtr hwnd = CreateWindow(
            "BUTTON",
            string.Empty, // empty window name
            WindowStyles.WS_OVERLAPPED,
            0,
            0,
            0,
            0,
            IntPtr.Zero,
            IntPtr.Zero,
            Process.GetCurrentProcess().Handle,
            IntPtr.Zero);

Some could prefer to omit the static using to enforce User32.CreateWindow usage.

The down side: it can bring us back many unneeded symbols (perhaps .Net 5.0 trimming can cope with that).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s