为32位系统编译的C#程序和C ++ DLL在64位系统上崩溃 [英] C# program and C++ DLL compiled for 32-bit system crash on 64-bit system

查看:266
本文介绍了为32位系统编译的C#程序和C ++ DLL在64位系统上崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用DLL的C#应用​​程序,因为我需要C ++来访问user32 API的某些非托管功能(我不能为此使用PInvoke)。我同时为x86体系结构编译了应用程序和DLL,并且在Windows 7 32位上一切正常。现在的问题是,在Windows 7 64位上,当我尝试使用依赖于DLL的功能时,应用程序崩溃了(但是其余所有工作正常)。

I have a C# application that makes use of a DLL as I need C++ to access some unmanaged functionalities of the user32 API (I cannot use PInvoke for that). I compile both the application and the DLL for x86 architectures, and everything works fine on Windows 7 32 bits. Now the problem is, on Windows 7 64 bits, the application crashes when I try to use the feature that relies on the DLL (but all the rest works fine).

我怀疑这是32/64位的问题,因此我尝试重新编译用于x64体系结构的DLL,现在我可以在运行时选择在x86和x64之间加载哪个DLL。但是当我尝试使用依赖于DLL的功能时,它仍然崩溃(当我尝试将64位DLL加载到32位程序中时,这很有意义)。我还没有尝试为x64编译应用程序和DLL。我怀疑它可以用,但是需要我有两个不同的安装程序,我不想去那里。有任何线索吗?

I suspect that this is a 32/64 bits issue, so I tried re-compiling the DLL for x64 architectures, and now I can choose at runtime which DLL to load between the x86 and the x64. But it still crashes when I try to use the feature that relies on the DLL (which makes sense as I try to load a 64-bit DLL into a 32-bit program). I haven't tried yet to compile both the application and the DLL for x64. I suspect it would work, however it would require me to have two different installers, and I don't want to go there. Any clue?

推荐答案

与非托管代码互操作时,您需要确保.Net应用程序在同一子系统上运行(32-位或64位)。如您所说,您要加载的DLL是针对x86的,请强制.Net仅针对x86平台进行构建。您可以在项目的属性中的构建标签上找到此设置。默认值为任何CPU,将设置更改为x86以匹配您的非托管DLL,无论您是在64位还是32位OS上运行,都应该没事。

When interoping with unmanaged code, you need to ensure your .Net app runs on the same subsystem (32-bit or 64-bit). As you've stated the DLL you're loading is for x86, force the .Net to build for only the x86 platform. This setting is found in your project's properties, on the build tab. The default is any CPU, change the setting to x86 to match your unmanaged DLL and you should be fine regardless if you run on a 64-bit or 32-bit OS.

这篇关于为32位系统编译的C#程序和C ++ DLL在64位系统上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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