简体   繁体   English

从C#调用C ++方法时获取内存异常错误

[英]Getting Memory Exception Error when calling C++ method from C#

I have C++ dll which has below method signature. 我有以下方法签名的C ++ dll。 I would like to know what should be compatible C# method to call C++ dll. 我想知道什么是兼容的C#方法来调用C ++ dll。 I am getting error on execution as attempted to read write protected memory. 尝试读取写保护的内存时,执行时出现错误。 The dll used here is third party dll. 这里使用的dll是第三方dll。 I am able to call simple methods without pointers from C++ dll. 我能够从C ++ dll调用没有指针的简单方法。

C++ Declaration: C ++声明:

int __stdcall Deduct(int V, unsigned char *AI);

C# Declaration C#声明

 [System.Runtime.InteropServices.DllImport("rwl.dll",EntryPoint = "_Deduct@8", CallingConvention = CallingConvention.Cdecl)]   
 public static extern long Deduct(long i,ref string AI);

As per the document of third party dll. 根据第三方dll的文件。

AI Used as input and output buffer. AI用作输入和输出缓冲区。 The buffer shall have at least 80 bytes Input Additional Information for the transaction. 缓冲区应至少有80个字节的事务输入附加信息。 It is a byte pointer containing 7 bytes. 它是一个包含7个字节的字节指针。

eg Assume the usage information is not used, 例如,假设未使用使用信息,

if receipt number = 1234567,
hex value = 0x12D687,
7 bytes AI = D6 87 00 00 00 D6 87
Output On return, the AI contains the UD.

Please help. 请帮忙。

I think the c# signature looks wrong. 我认为C#签名看起来不对。

 [System.Runtime.InteropServices.DllImport("rwl.dll",EntryPoint = "_Deduct@8", CallingConvention = CallingConvention.Cdecl)]   
 public static extern long Deduct(int i,ref char[] AI);

if it is expecting a char[80] as it's input you may need to declare it as such and null terminate it yourself as whatever is reading from it might be reading past the end of the array. 如果期望输入的是char [80],则可能需要这样声明它,并自己将其终止为null,因为从其中读取的内容可能正在读取数组的末尾。

usage 用法

char[] tmp = new char[80];
tmp[0] = (char)0x00;
int i = 0;
Deduct(i, ref tmp);

I'm not sure if this will work but hopefully it helps. 我不确定这是否行得通,但希望能有所帮助。

Besides string marshaling, I found a likely reason for getting an exception. 除了字符串封送处理之外,我还发现了引发异常的可能原因。

You have specified the return type and the first parameter as int in C, but long int C#. 您已在C中将返回类型和第一个参数指定为int ,但将int long C#。 This could cause problems. 这可能会引起问题。

C language specifies minimum bits of integer types which means that different for environments. C语言指定整数类型的最小位数,这意味着对于环境而言是不同的。

  • int in C : unsigned integer, at least 16 bits C中的int :无符号整数, 至少 16位
  • long in C : unsigned integer, at least 32 bits C中的long :无符号整数, 至少 32位
  • int in C# : 32-bit unsigned integer, always C#中的int :始终为32位无符号整数
  • long in C# : 64-bit unsigned integer, always C#中的long :始终为64位无符号整数

Most of major 32/64-bit OS/compilers use 32 bits for type int in C. However long is always 64-bit in C#. 大多数主要的32/64位OS /编译器在C语言中将int类型使用32位。但是,在C#中long始终为64位。 So the signature mismatches. 因此签名不匹配。 I would suggest to revise: 我建议修改:

public static extern int Deduct(int i, ref string AI); // Still not sure about string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM