DllImportX 0.0.1

dotnet add package DllImportX --version 0.0.1
NuGet\Install-Package DllImportX -Version 0.0.1
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="DllImportX" Version="0.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DllImportX --version 0.0.1
#r "nuget: DllImportX, 0.0.1"
#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 DllImportX as a Cake Addin
#addin nuget:?package=DllImportX&version=0.0.1

// Install DllImportX as a Cake Tool
#tool nuget:?package=DllImportX&version=0.0.1

DllImportX is an attribute to work around the classic DllImport's limitations.

The .Net platform does not allow to define dynamic values in the attributes assignments to perform environment based unmanaged library linking.

With DllImporX, you can dynamically set the DllImport attribute at runtime changing everyone attributes as necessary!

DllImportX's attribute supports all the classic DllImport parameters. All you need is add the 'X' in the DllImport attribute and build your instance.

How to use

If you have an unmanaged static-link library with this functions:

// sample.h
// Unmanaged C sample functions

void cVoidSample();
int cIntSample(int intParam);
void cOutIntSample(int* outIntParam);
void cRefIntSample(int* refIntParam);
void cAnsiStringSample(const char* str);
void cPtrSample(void* ptr);

You must only define an interface in your project:

// ISample.cs

using System;
using System.Runtime.InteropServices;

namespace DllImportXSample
{
    public interface ISample
    {
        [DllImportX("Sample.dll")]
        void cVoidSample();

        [DllImportX("Sample.dll")]
        int cIntSample(int intParam);

        [DllImportX("Sample.dll", EntryPoint = "cOutIntSample")]
        void OutIntSample(out int outIntParam);

        [DllImportX("Sample.dll", EntryPoint = "cRefIntSample"))]
        void cRefIntSample(ref int refIntParam);

        [DllImportX("Sample.dll", EntryPoint = "cAnsiString"))]
        void AnsiStringSample([In][MarshalAs(UnmanagedType.LPStr)] string str);

        [DllImportX("Sample.dll")]
        void cPtrSample(IntPtr ptr);
    }
}

And build your instance!

using System.Runtime.InteropServices;
using DllImportXSample;

// ...
    
var sampleInstance = DllImportXFactory
    .Build<ISample>(entry => {
        // Change the path to the unmanged static-link library
        // based in the runtime enviroment architeture
        entry.DllName = (IntPtr.Size == 8 ? "x64/" : "x86/") + entry.DllName;
    });

// ready

sampleInstance.AnsiStringSample("Hello world.");

// ...

Bug Reports

If you find any bug, please report them using the GitHub issue tracker.

License

This software is distributed under the terms of the MIT license.

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 netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.5 is compatible.  netstandard1.6 was computed.  netstandard2.0 was computed.  netstandard2.1 was computed. 
.NET Framework net35 is compatible.  net40 was computed.  net403 was computed.  net45 was computed.  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 tizen30 was computed.  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 DllImportX:

Package Downloads
Neoload.Ranorex

This package is a Neoload Ranorex Plugin. For more information see https://github.com/Neotys-Labs/Ranorex

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.1 9,142 8/23/2017