Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm receiving in c# a structure pointer from a c++ library callback


c++ structure

the packing is 1 

typedef 
{
int data1[8];
int data2[8];
int data3;
int data3;	
} SomeStruct;


in c# i have this equivalent 

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1), Serializable]
public struct SomeStruct
{
	[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
	public int[] data1
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public int [] data2;
        public int data3;
        public int data4;
};


What I have tried:

var data contains the right IntPtr  

SomeStruct ss = (SomeStruct )Marshal.PtrToStructure(pointer, typeof(SomeStruct));

But im getting garbage, what im doing wrong?, thanks
Posted
Updated 19-Jan-17 8:12am

1 solution

Have you tried the UnmanagedType.LPArray attribute?
C#
[MarshalAs(UnmanagedType.LPArray, SizeConst = 8)]
public int[] data1

The example here uses it: Default Marshaling for Arrays[^] - search for "When a C-style array is imported" and look at the example under "Managed Signature"
 
Share this answer
 
Comments
lista 19-Jan-17 15:02pm    
i tried before and i get this "Arrays fields must be paired with ByValArray or SafeArray"

Im going to read the link you sent, thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900