Skip to content

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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages