Platform.Invoke 1.2.0

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 and Polyglot Notebooks. 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

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");
            }
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net35 is compatible.  net40 was computed.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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 NuGet.org.

PAM bindings for .NET

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 5,261 3/8/2018
1.1.0 1,034 2/1/2018
1.0.4 1,631 6/18/2014
1.0.3 1,211 4/1/2014
1.0.2 1,227 3/28/2014
1.0.1 1,168 3/28/2014
1.0.0 1,176 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

# 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