我尝试开发一个使用 USB 硬件设备的 C# 接口(interface)。我通过制造商的 PInvoke 模式访问 API DLL。 有两个同名的 DLL。但一种适用于 32 位系统,另一种适用于 64 位系统。 我希望我的应用程序对每个系统使用正确的 API。
所以我开始检查正在使用的平台:
bool is64Bit = System.Environment.Is64BitOperatingSystem
我定义了一个字符串变量并设置了推荐.DLL的名称。
像这样:
string dll;
if (is64bit)
{
dll = "APINAME64.DLL";
}
else
{
dll = "APINAME32.DLL"
}
[DllImport(dll, SetLastError=true)]
public static extern bool ImmConfigureIME();
但这仍然不起作用。编译器需要 Pinvoke 的 const 字符串。
有人知道如何解决这个问题吗?
最佳答案
您可以在代码中声明这两个 DLL 并为它们指定不同的名称。然后,使用 EntryName
属性确保它们具有 native dll 的正确入口点:
[DllImport("APINAME64.dll", EntryName="ImmConfigureIME" SetLastError=true)]
public static extern bool ImmConfigureIME64();
[DllImport("APINAME32.dll", EntryName="ImmConfigureIME" SetLastError=true)]
public static extern bool ImmConfigureIME32();
bool is64Bit = System.Environment.Is64BitOperatingSystem;
if (is64bit)
{
ImmConfigureIME64();
}
else
{
ImmConfigureIME32();
}
关于c# - 处理 32 位和 64 位 DLL 的 Pinvoke,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25802649/