reload dll functions from another dll, C#
I have 2 unmanaged dlls which have exactly same set of function (but slightly different logic).
开发者_如何学编程How can I switch between these 2 ddls during runtime?
now I have:
[DllImport("one.dll")]
public static extern string _test_mdl(string s);
Expanding on the existing answers here. You comment that you don't want to change existing code. You don't have to do that.
[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 keep using _test_mdl in your existing code, and just place the if-statement in a new version of that method, calling the correct underlying method.
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);
}
I haven't ever had to use this, but I think the EntryPoint can be specified in the declaration. Try 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);
DllImportAttribute.EntryPoint Field
You could still use dynamic loading and call LoadLibraryEx
(P/Invoke), GetProcAddress
(P/Invoke) and Marshal.GetDelegateForFunctionPointer
(System.Runtime.InterOpServices).
;)
精彩评论