VB.Net to C++ code Marshalling

GreenVicted

Member
Joined
Nov 18, 2010
Messages
6
Programming Experience
1-3
I have a structure in VB.NET that has a dynamic array of double as a parameter.I am passing an array of structure (GElement) to a C++ DLL. I tried converting my array of GElement into a block of unmanaged memory, it works in VB.NET but crashes in my C++ DLL. I get the same error just trying to simply pass an array of GElement to my DLL instead of a Intptr, here is the error code:
VB.NET:
0xC0000005: Access violation reading location 0x00000004

here is my function and structure in the DLL:
VB.NET:
void _stdcall TestGElement(GElement* ge,long& nbge)
{
	
	nbge=5;
}

struct GElement
{
   double Step2D;
  double Step3D ;
  long NBVals;
  double* donnees;
        
};

My code in VB.NET (DLL call and the structure):

VB.NET:
 Private Declare Sub TestGElement Lib "Test_GElement_Dll" (ByVal ge As System.IntPtr, ByRef nbge As Long)

 <StructLayout(LayoutKind.Sequential)>
        Public Structure GElement
            <MarshalAs(UnmanagedType.R8)> Public Step2D As Double
            <MarshalAs(UnmanagedType.R8)> Public Step3D As Double
            <MarshalAs(UnmanagedType.U8)> Public NBVals As Long
            <MarshalAs(UnmanagedType.ByValArray)> Public Donnees() As Double
        End Structure
I know it is easier if the array inside the structure as an already defined constant size but that can't be the case here...

Can someone Help me do this plz?
 
Last edited:
Back
Top