C#:访问32位/ 64位的DLL取决于平台 [英] C#: Access 32-bit/64-bit DLL depending on platform

查看:199
本文介绍了C#:访问32位/ 64位的DLL取决于平台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用一个自写的32位C ++ DLL从我们的C#应用​​程序。现在,我们已经注意到,当C#应用程序在64位系统上运行的64位运行时自动使用,当然还有32位的DLL不能在64位运行时访问。

we use a self-written 32bit C++ DLL from our C# applications. Now we've noticed that when the C# applications are run on a 64bit system, the 64bit runtime is automatically used and of course the 32bit DLL can not be accessed from the 64bit runtime.

我的问题是:是否有使用32位的DLL的方法吗?如果不是,我创建的DLL的64位版本,会是容易可以让应用程序选择哪一个为P / Invoke来?

My question is: is there a way of using the 32bit DLL? If not, if I created a 64bit version of the DLL, would it be easily possible to let the application choose which one to P/Invoke to?

我想创建两个辅助类在C#中:一个是进口的功能从32位DLL和一个由64位DLL进口,然后创建一个功能的包装类每个导入函数的调用无论是32位进口商或取决于操作系统的bittynes​​s64位进口国。将这项工作?

I'm thinking of creating two helper classes in C#: One that imports the functions from the 32bit DLL and one that imports from the 64bit DLL, then creating a wrapper class with one function for each imported function that calls either the 32bit importer or the 64bit importer depending on the "bittyness" of the OS. Would that work?

还是有另一种简单的方法来做事?

Or is there another easy way to do things?

推荐答案

您需要确保你只使用P / Invoke对一个64位的DLL在64位编译时调用。

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

一种选择是将所有的方法进入了一个标准接口(或抽象基类),并提供2实现,一个32位和一个64位。你可以有一个工厂方法构造类的相应实例依据的IntPtr的大小。

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.

这使得值为anycpu应用程序,以正确的,在运行时,确定哪些DLL中的P / Invoke成,并执行工作。

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

这篇关于C#:访问32位/ 64位的DLL取决于平台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆