【问题标题】:C# app calling 64-bit C/C++ DLL crashes, but 32-bit version runs调用 64 位 C/C++ DLL 的 C# 应用程序崩溃,但 32 位版本运行
【发布时间】:2018-12-27 21:39:04
【问题描述】:

我的 C/C++ DLL 是为 32 位和 64 位 Windows 环境构建的。两位使用 .NET(C# 和 VB.NET)的程序员说,当他们为 32 位构建客户端时,一切运行正常。但是当他们为 64 位构建时,运行以访问冲突结束。

我不是 .NET 程序员,但我可以加载他们的进程并在 C++ 调试器中单步执行 DLL 代码。 .NET 运行肯定会遇到当客户端是我用 C++/VCL 编写的客户端时不会发生的损坏内存。

我的问题:.NET 项目设置中是否有一些特殊的东西可以连接到 64 位 C/C++ DLL,这与 32 位 下的工作方式不同?

可能相关,但我无法证明:DLL 是使用 Character Setting=Not Set 为 UTF-8/ASCII 构建的。我不知道 VB.NET 客户端是否是为 Unicode 构建的,但我认为这会产生问题。

已编辑以添加来自 VB.NET、C# 客户端和 C++ DLL 的声明

Declare Function Edit_RunEdits Lib "EDITS50.DLL" (ByVal smfID As Integer, _
                                                  ByVal edit_set_tag As String, _
                                                  ByVal layout_tag As String, _
                                                  ByVal data As String, _
                                                  ByVal edit_options As Integer, _
                                                  ByRef errors_count As Integer, _
                                                  ByVal owner As IntPtr, _
                                                  ByVal callback_func As IntPtr) As Integer

这是 C# 声明:

[DllImportAttribute("EDITS50.dll", EntryPoint = "Edit_RunEdits")]
    public static extern int Edit_RunEdits(int smfID, 
         [InAttribute()] [MarshalAsAttribute(UnmanagedType.LPStr)] string edit_set_tag, 
         [InAttribute()] [MarshalAsAttribute(UnmanagedType.LPStr)] string layout_tag, 
         [InAttribute()] [MarshalAsAttribute(UnmanagedType.LPStr)] string data, 
         int edit_options, 
         ref int errors_count, 
         System.IntPtr owner, 
         System.IntPtr callback_func);

这是 C++ 方面:

#define EDIT_API  __declspec(dllexport) __stdcall
extern "C" int EDIT_API Edit_RunEdits( const int smfID, const char* edit_set_tag, 
const char* layout_tag, 
const char* data, 
const int edit_options, 
int* errors_count, 
void* owner, 
void* callback_func);

【问题讨论】:

  • 很多事情都可能出错 - 一切都必须“恰到好处”才能使这种互操作(.NET 到 C++)工作。既然你什么都没有描述,那真的没什么好说的了。
  • 除非您解释/展示 C# 代码如何准确地 p/调用(调用)本机 DLL 导出的函数以及如何准确地声明这些导出函数/b>,我们唯一能说的就是:“是的,C# 代码调用 DLL 导出的函数的方式在某种程度上有问题......”
  • 谢谢。我将请一位程序员(同时使用 C# 和 C++)提供准确的信息。
  • 请参阅上面的附加信息。
  • 对不起...再来一个。我刚刚在上面添加了 C# 版本的声明。

标签: c# c++ vb.net dll


【解决方案1】:

我相信我可能已经找到了问题所在。该项目是为 AnyCPU 构建的,但在 x64 设置下,Prefer32Bit 设置为“true”。我要求 .NET 程序员尝试为 x64 显式构建,并查找并覆盖 Prefer32Bit 的所有实例(将它们设置为“false”)。

谢谢大家的反馈。

【讨论】:

    猜你喜欢
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2013-04-20
    相关资源
    最近更新 更多