Giter Site home page Giter Site logo

rodrigo-speller / dllimportx Goto Github PK

View Code? Open in Web Editor NEW
17.0 1.0 2.0 182 KB

Dynamic Unmanaged Imports ( .NET DllImportX )

License: MIT License

C# 45.30% C++ 16.33% Shell 0.18% Batchfile 0.78% PowerShell 9.34% C 28.07%
dllimport dllimport-attribute unmanaged cross-platform

dllimportx's Introduction

DllImportX

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.

Get DllImportX

The quickest way to get the latest release of DllImportX is to add it to your project using NuGet (https://www.nuget.org/packages/DllImportX)

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 bugs, please report them using the GitHub issue tracker.

License

This software is distributed under the terms of the MIT license (see LICENSE.txt).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.