我有两个未托管的dll文件,它们拥有完全相同的函数集(但略有不同的逻辑)。
在运行时如何在这两个dll之间切换?
现在我的代码是:
[DllImport("one.dll")]
public static extern string _test_mdl(string s);
将它们定义在不同的C#类中?
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);
}
我从未使用过这个功能,但我认为EntryPoint可以在声明中指定。请尝试以下操作:
[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);
在现有的答案基础上进行扩展。您提到您不想更改现有的代码。您不必这样做。
[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);
}
LoadLibraryEx
(P/Invoke)、GetProcAddress
(P/Invoke) 和 Marshal.GetDelegateForFunctionPointer
(System.Runtime.InterOpServices)。
DllImport
属性中设置EntryPoint
属性。 - Stephen Cleary