使用C#访问非托管C ++ DLL时AccessViolationException [英] AccessViolationException when accessing unmanaged C++ DLL with C#

查看:614
本文介绍了使用C#访问非托管C ++ DLL时AccessViolationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问Double Dummy Solver dll( http://privat.bahnhof .se / wb758135 / bridge / dll.html )来自C#项目的非托管C ++代码,但是我收到以下错误消息:

I am trying to access a Double Dummy Solver dll (http://privat.bahnhof.se/wb758135/bridge/dll.html ) of unmanaged C++ code from a C# project, but I get the following error message:


Dds.Net.dll中出现类型为System.AccessViolationException
的未处理异常

An unhandled exception of type 'System.AccessViolationException' occurred in Dds.Net.dll

附加信息:尝试读取或写入受保护的内存。
这通常表明其他内存已损坏。

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

错误似乎在调用方法
par取三个参数

The error seems to be around calling the method Par which takes the three arguments


struct ddTableResults * tablep,struct parResults * presp,int脆弱

struct ddTableResults *tablep, struct parResults *presp, int vulnerable

具体来说,与传递第二个参数有关,描述为:

Specifically, related to passing in the 2nd parameter which is described to be:


char parScore [2] [16];
char parContractsString [2] [128];

struct parResults char parScore[2][16]; char parContractsString [2][128];

这是我的代码:
我的c#结构:

Here is my code: My c# struct:

using System.Runtime.InteropServices;

namespace Dds.Net.Integration
{
    [StructLayout(LayoutKind.Sequential)]
    internal struct ParResults
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst =32)]
        public char[,] parScore;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
        public char[,] parContractString;


    }
}

dllimport调用函数:

dllimport to call the function :

[DllImport("dds.dll")]
        public static extern int Par(DdTableResults tablep, int vulnerable, ParResults parResults);

有什么想法可以做到这一点吗?

Any idea of what I can do to get this working?

非常感谢!

推荐答案

据我所知,你的c ++签名是

as far as I understand, you c++ signature is

int Par(struct ddTableResults *tablep, struct parResults *presp, int vulnerable)

c#也许是

[DllImport("dds.dll")]
public static extern int Par(ref DdTableResults tablep, ref ParResults parResults, int vulnerable);

c ++想要一个poitner到 DdTableResults code> ParResults ,没有 ref c#将按值传递结构。

c++ wants a poitner to DdTableResults and ParResults, without ref c# will pass structure by value.

这篇关于使用C#访问非托管C ++ DLL时AccessViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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