Reload DLL functions from anothe dll, C #

I have 2 unmanaged dlls that have exactly the same set of functions (but slightly different logic).

How can I switch between these two ddls at runtime?

Now I have:

[DllImport("one.dll")] public static extern string _test_mdl(string s); 
+4
source share
4 answers

Extending existing answers here. You will comment that you do not want to change the existing code. You do not have to do this.

 [DllImport("one.dll", EntryPoint = "_test_mdl")] public static extern string _test_mdl1(string s); [DllImport("two.dll", EntryPoint = "_test_mdl")] public static extern string _test_mdl2(string s); public static string _test_mdl(string s) { if (condition) return _test_mdl1(s); else return _test_mdl2(s); } 

You continue to use _test_mdl in your existing code and just put the if statement in the new version of this method, invoking the correct base method.

+4
source

Define them in different C # classes?

 static class OneInterop { [DllImport("one.dll")] public static extern string _test_mdl(string s); } static class TwoInterop { [DllImport("two.dll")] public static extern string _test_mdl(string s); } 
+4
source

I have never had to use this, but I think that EntryPoint can be specified in an ad. Try the following:

  [DllImport("one.dll", EntryPoint = "_test_mdl")] public static extern string _test_mdl1(string s); [DllImport("two.dll", EntryPoint = "_test_mdl")] public static extern string _test_mdl2(string s); 

DllImportAttribute.EntryPoint field

+4
source

You can still use dynamic loading and call LoadLibraryEx (P / Invoke), GetProcAddress (P / Invoke) and Marshal.GetDelegateForFunctionPointer (System.Runtime.InterOpServices).

;)

+1
source

Source: https://habr.com/ru/post/1314841/


All Articles