Marshaling a C++ Struct with fixed size 2D char array from C#

I have a c++ DLL that have one public function that accepts structure pointer. I am using this DLL into C# (VS 2010, Build Type x86, Allow unsafe Code).

The structure which I need to marshal is having one more structure as member parameter and this member parameter structure need to have C++ like 2D char array of fixed size. I have implemented this structure as suggested by Stack Overflow and MSDN forum links as mentioned below :

https://social.msdn.microsoft.com/Forums/vstudio/en-US/d1618999-8448-46ef-9788-4b18a62d25ea/marshaling-ac-twodimensional-fixed-length-char-array-as-a-structure-member
and
Marshaling a C++ two-dimensional fixed length char array as a structure member.

Finally I am using an Intptr variable xyz and using AllocHglobal(Marshal.Sizeof(abc)) to allocate memory and Marshal.StructtoPointer(abc, xyz, false) and then passing xyz in the C++ dll function call and I am getting an exception inside this function from C++ when I am trying to do a memset on the 2D char[][] struct ..

Exception is "Attempting to read write a protected memory". Apart from this struct, everything is fine. Please suggest any work around.


Actually the problem was the improper allocation of IntPtr . I was using Marshal.Sizeof(abc) instead of that I should use sizeof(STRUCTURE) . By doing this change I am successfully getting the struct parameter in C++ .

链接地址: http://www.djcxy.com/p/66604.html

上一篇: 使用c#和c ++之间的字符串封装结构数组。 字符串是空的

下一篇: 用C#编写固定大小的2D char数组的C ++ Struct