问题描述
我在我的解决方案使用的DllImport
。
我的问题是,我有两个版本的DLL对应一个专为32位,另一个用于64位相同。
I am using DllImport
in my solution.
My problem is that I have two versions of the same DLL one built for 32 bit and another for 64 bit.
它们都提供了相同的功能,具有相同的名称和相同的签名。我的问题是,我不得不使用其中两个公开这些,然后在运行时使用的IntPtr
尺寸的静态方法来确定正确的调用。
They both expose the same functions with identical names and identical signatures.My problem is that I have to use two static methods which expose these and then at run time use IntPtr
size to determine the correct one to invoke.
private static class Ccf_32
{
[DllImport(myDllName32)]
public static extern int func1();
}
private static class Ccf_64
{
[DllImport(myDllName64)]
public static extern int func1();
}
我必须这样做,因为 myDllName32
和 myDllName64
必须是恒定的,我还没有找到一种方法来设置它在运行时。
I have to do this because myDllName32
and myDllName64
must be constant and I have not found a way to set it at run time.
有没有人有这个,所以我可以摆脱code重复和不断的完善的解决方案的IntPtr
尺寸检查。
Does anyone have an elegant solution for this so I could get rid of the code duplication and the constant IntPtr
size checking.
如果我可以设置文件名,我将只需要检查一次,我可以摆脱大量的重复code的。
If I could set the file name, I would only have to check once and I could get rid of a ton of repeated code.
推荐答案
您也许可以做到这一点与#如果
关键字。如果你定义一个名为的win32
条件编译符号,下面的code将使用Win32块,如果你删除它,将使用其它块:
You can probably achieve this with the #if
keyword. If you define a conditional compiler symbol called win32
, the following code will use the win32-block, if you remove it it will use the other block:
#if win32
private static class ccf_32
{
[DllImport(myDllName32)]
public static extern int func1();
}
#else
private static class ccf_64
{
[DllImport(myDllName64)]
public static extern int func1();
}
#endif
这可能意味着,你可以删除类包装,你现在有:
This probably means that you can remove the class wrapping that you have now:
private static class ccf
{
#if win32
[DllImport(myDllName32)]
public static extern int func1();
#else
[DllImport(myDllName64)]
public static extern int func1();
#endif
}
为了方便起见,我想你可以控制编译符号创建构建配置。
For convenience, I guess you could create build configurations for controlling the compilation symbol.
这篇关于在C#中设置dllimport的编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!