C#: Access 32-bit/64-bit DLL depending on platform

时光怂恿深爱的人放手 提交于 2019-12-03 09:04:21

You need to make sure that you're only using P/Invoke calls against a 64bit DLL when compiling in 64bit.

One option is to move all of your "methods" into a standard interface (or abstract base class), and provide 2 implementations, one 32bit and one 64bit. You can have a factory method construct the appropriate instance of the class depending on the size of IntPtr.

This allows an "AnyCPU" app to correctly, at runtime, determine which DLL to P/Invoke into, and does work.

Create a helper class that wraps both 64bit and 32bit DLLS, and use IntPtr.Size to determine which to call.

if (IntPtr.Size == 8)
{
    Helper.SomeMethod64();
}
else
{
    Helper.SomeMethod32();
}

You can flag the .Net app to only target x86 architecture

I had similar problem but it was with Unrar.dll being either 32 or 64bit.

There can be two approaches to make it work:

a)

#if x64
// ... define all x64 imports here
#else
// ... define all x86 imports here
#endif

And compile application for 32Bit and 64bit.

b) Another way was to create an interface for the imports and implement 32 bit and 64 bits versions separately.

If 32 bit, instantiate the 32 bit implementation, else instantiate the 64 bit implementation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!