Platform.Invoke 1.2.0

This library generates platform invoke implementations for interfaces.

Install-Package Platform.Invoke -Version 1.2.0
dotnet add package Platform.Invoke --version 1.2.0
<PackageReference Include="Platform.Invoke" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Platform.Invoke --version 1.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

PlatformInvoker

Loosely bound platform invocation for .NET.

Intended to remove the need for static classes
when using P/Invoke. Supply an interface to
the builder and get an implementation back.

What does it support?

  • Mocking P/Invoke functions without manually writing a wrapper.
  • Adding pre-call and post-call probing which allows exceptions
    for GetLastError() in the error handling pattern common in C
    libraries.
  • Marshal attributes still work.
  • Both 32-bit and 64-bit is supported.
  • Unix support on Mono and .NET Core
  • Can support libraries that implement extension systems like OpenAL and OpenGL by implementing
    ILibrary.

Example

using System;
using Platform.Invoke;
using System.Runtime.InteropServices;

namespace Example
{
    public interface IMessageBox
    {
        int MessageBox(IntPtr hWnd, [In]string lpText, [In]string lpCaption, uint uType);
    }

    public class Program
    {
        static void Main()
        {
            var msg = LibraryInterfaceFactory.Implement<IMessageBox>("user32");
            msg.MessageBox(IntPtr.Zero, "Hello World!", "Hello World!", 1);
        }
    }
}

Example with probing and attributes

using System;
using System.Reflection;

using Platform.Invoke;
using Platform.Invoke.Attributes;

namespace Example
{
    public enum ErrorCode : uint
    {
        NoError = 0,
        InvalidOperation = 0x0502,
    }

    [Library("opengl32")]
    [EntryPointFormat("gl{0}")]
    public interface IOpenGL
    {
        [SkipProbe] // Don't invoke probe actions on this method. It would cause infinite recursion.
        ErrorCode GetError();

        void ClearColor (float red, float green, float blue, float alpha);
    }

    public class Program
    {
        static void BeginCall(MethodInfo method, IOpenGL gl)
        {
            gl.GetError(); // Clear last error state
        }

        static void EndCall(MethodInfo method, IOpenGL gl)
        {
            var error = gl.GetError();
            if(error == ErrorCode.InvalidOperation)
                throw new InvalidOperationException();
        }


        private static void Main()
        {
            var opengl = LibraryInterfaceFactory.Implement<IOpenGL>(BeginCall, EndCall);
            try
            {
                opengl.ClearColor(0, 0, 0, 1);
                Console.WriteLine("Should have thrown InvalidOperationException since there is no context bound...");
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine("Expected exception :D");
            }
        }
    }
}

PlatformInvoker

Loosely bound platform invocation for .NET.

Intended to remove the need for static classes
when using P/Invoke. Supply an interface to
the builder and get an implementation back.

What does it support?

  • Mocking P/Invoke functions without manually writing a wrapper.
  • Adding pre-call and post-call probing which allows exceptions
    for GetLastError() in the error handling pattern common in C
    libraries.
  • Marshal attributes still work.
  • Both 32-bit and 64-bit is supported.
  • Unix support on Mono and .NET Core
  • Can support libraries that implement extension systems like OpenAL and OpenGL by implementing
    ILibrary.

Example

using System;
using Platform.Invoke;
using System.Runtime.InteropServices;

namespace Example
{
    public interface IMessageBox
    {
        int MessageBox(IntPtr hWnd, [In]string lpText, [In]string lpCaption, uint uType);
    }

    public class Program
    {
        static void Main()
        {
            var msg = LibraryInterfaceFactory.Implement<IMessageBox>("user32");
            msg.MessageBox(IntPtr.Zero, "Hello World!", "Hello World!", 1);
        }
    }
}

Example with probing and attributes

using System;
using System.Reflection;

using Platform.Invoke;
using Platform.Invoke.Attributes;

namespace Example
{
    public enum ErrorCode : uint
    {
        NoError = 0,
        InvalidOperation = 0x0502,
    }

    [Library("opengl32")]
    [EntryPointFormat("gl{0}")]
    public interface IOpenGL
    {
        [SkipProbe] // Don't invoke probe actions on this method. It would cause infinite recursion.
        ErrorCode GetError();

        void ClearColor (float red, float green, float blue, float alpha);
    }

    public class Program
    {
        static void BeginCall(MethodInfo method, IOpenGL gl)
        {
            gl.GetError(); // Clear last error state
        }

        static void EndCall(MethodInfo method, IOpenGL gl)
        {
            var error = gl.GetError();
            if(error == ErrorCode.InvalidOperation)
                throw new InvalidOperationException();
        }


        private static void Main()
        {
            var opengl = LibraryInterfaceFactory.Implement<IOpenGL>(BeginCall, EndCall);
            try
            {
                opengl.ClearColor(0, 0, 0, 1);
                Console.WriteLine("Should have thrown InvalidOperationException since there is no context bound...");
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine("Expected exception :D");
            }
        }
    }
}

Release Notes

# 1.2.0 - Re-added .NET 3.5 support

What's new:

 * .NET 3.5 Support back in
 * Added support for C# 7.2 `in` parameters
 * Removed unrequired dependencies for .NET 4.5

Breaking changes:
*  None that I'm aware of

The 3.5 support was removed in the last version because it wasn't properly built before (it was more or less mashed in separately). Now it's part of the build process. Hopefully no one needs this, but it wasn't a lot of work to get it in because of the new csproj format.

# 1.1.0 - Moved to .NET Standard and new project file, removed .NET 3.5 support.

What's new:

*  .NET Standard 2.0 support

Breaking changes:

* EntryPointAttribute, EntryPointFormatAttribute, LibraryAttribute and SkipProbeAttribute are now sealed.
* Removed .NET 3.5 support

# 1.0.4.0

What's new
* Added .NET 3.5 support
* Rewrote binding to not use convinience functions for attribute lookup (required by .NET 3.5)

* 1.0.0.0

Initial Release

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.2.0 492 3/8/2018
1.1.0 347 2/1/2018
1.0.4 901 6/18/2014
1.0.3 427 4/1/2014
1.0.2 431 3/28/2014
1.0.1 402 3/28/2014
1.0.0 402 3/28/2014