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

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

问题描述

我有一个使用 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 位操作系统上运行,您都应该没问题.

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.

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

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