A-tG / Dynamic-wrapper-for-unmanaged-dll

Helps to load an unmanaged library at runtime.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dynamic wrapper for unmanaged dll

nuget

Visual Studio's C# Shared Project. Helps to dynamically load an unmanaged library at runtime.

Designed to work on Windows, but should be crossplatform with .NET 5.0

Abstract (base) class, requires implementation. Example (partial class)

How to add procedures from the DLL:

Shortest way, without <T>

   private delegate int ProcedureNameFromDLL(IntPtr someParam1, ref int someParam2);
   private ProcedureNameFromDLL myFunc;
   public int MethodName(IntPtr someParam1, ref int someParam2)
   {
        // do something with parameters here if needed
        return myFunc(someParam1, ref someParam2);
   }

   // And initialize "myFunc" somewhere (for example in constructor):
   GetReadyDelegate(ref myFunc);

or

   private delegate int myNameForDelegate(IntPtr someParam1, ref int someParam2);
   private myNameForDelegate myFunc;
   public int MethodName(IntPtr someParam1, ref int someParam2)
   {
        // do something with parameters here if needed
        return myFunc(someParam1, ref someParam2);
   }

   // And initialize "myFunc" somewhere (for example in constructor):
   GetReadyDelegate(ref myfunc, "ProcedureNameFromDLL");

With <T>

   private delegate int ProcedureNameFromDLL(IntPtr someParam1, ref int someParam2);
   private ProcedureNameFromDLL myFunc;
   public int MethodName(IntPtr someParam1, ref int someParam2)
   {
        // do something with parameters here if needed
        return myFunc(someParam1, ref someParam2);
   }

   // And initialize "myFunc" somewhere (for example in constructor):
   myFunc = GetReadyDelegate<ProcedureNameFromDLL>();

or

   private delegate int myNameForDelegate(IntPtr someParam1, ref int someParam2);
   private myNameForDelegate myFunc;
   public int MethodName(IntPtr someParam1, ref int someParam2)
   {
        // do something with parameters here if needed
        return myFunc(someParam1, ref someParam2);
   }

   // And initialize "myFunc" somewhere (for example in constructor):
   myFunc = GetReadyDelegate<myNameForDelegate>("ProcedureNameFromDLL");

To achieve compatibility with different versions of DLL:

   private delegate int ProcedureNameFromDLL(IntPtr someParam1, ref int someParam2);
   private ProcedureNameFromDLL myFunc;
   public int MethodName(IntPtr someParam1, ref int someParam2)
   {
        if (myFunc is null) return SOME_ERROR_CODE;
        // do something with parameters here if needed
        return myFunc(someParam1, ref someParam2);
   }

   // And initialize "myFunc" somewhere (for example in constructor),
   // TryGetReadyDelegate() will not throw exception if procedure is not found:
   bool isProcReceived = TryGetReadyDelegate(ref myFunc);

or

   private delegate int myNameForDelegate(IntPtr someParam1, ref int someParam2);
   private myNameForDelegate myFunc;
   public int MethodName(IntPtr someParam1, ref int someParam2)
   {
        if (myFunc is null) return SOME_ERROR_CODE;
        // do something with parameters here if needed
        return myFunc(someParam1, ref someParam2);
   }

   // And initialize "myFunc" somewhere (for example in constructor),
   // TryGetReadyDelegate() will not throw exception if procedure is not found:
   bool isProcReceived = TryGetReadyDelegate(ref myFunc, "ProcedureNameFromDLL");

Do you like my projects? Donate

Available methods

About

Helps to load an unmanaged library at runtime.

License:MIT License


Languages

Language:C# 100.0%