Platform.Invoke 1.2.0

.NET Standard 2.0 .NET Framework 3.5
dotnet add package Platform.Invoke --version 1.2.0
NuGet\Install-Package Platform.Invoke -Version 1.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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
#r "nuget: Platform.Invoke, 1.2.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Platform.Invoke as a Cake Addin
#addin nuget:?package=Platform.Invoke&version=1.2.0

// Install Platform.Invoke as a Cake Tool
#tool nuget:?package=Platform.Invoke&version=1.2.0


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.


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,

    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);
                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");
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net35 net40 net403 net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Platform.Invoke:

Package Downloads
FubarDev.PamSharp The ID prefix of this package has been reserved for one of the owners of this package by

PAM bindings for .NET

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 3,914 3/8/2018
1.1.0 913 2/1/2018
1.0.4 1,492 6/18/2014
1.0.3 1,088 4/1/2014
1.0.2 1,100 3/28/2014
1.0.1 1,044 3/28/2014
1.0.0 1,068 3/28/2014

# 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


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


Initial Release