I am building a COM wrapper written in C# around a C dll and am getting an error when I call one particular function which takes a reference to a pre-initialized array of structs.
The error is: "An unhandled exception of type 'System.ExecutionEngineException occurred in sXEposCOM.dll"
The C function has a signature as follows:
UINT16 sXGetSupportedSchemes( SXEposScheme_t *pSchemes ); And the SXEposScheme_t struct in the C dll is declared as:
typedef struct _SXEposScheme_t { BYTE schemeID[SX_EPOS_SID_LENGTH]; char schemeName[SX_EPOS_MAX_SCHEME_NAME_LENGTH]; } SXEposScheme_t; In my C# wrapper (sXEposCOM) I have the following:
private const int SX_EPOS_SID_LENGTH = 2; private const int SX_EPOS_MAX_NUM_SCHEMES = 10; [DllImport("SXEposDll.dll")] private static extern UInt16 sXGetSupportedSchemes(ref SXEposScheme_t [] pSchemes); [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct SXEposScheme_t { [MarshalAs( UnmanagedType.ByValArray, SizeConst = SX_EPOS_SID_LENGTH)] public byte[] schemeID; [MarshalAs( UnmanagedType.ByValArray, SizeConst = SX_EPOS_MAX_SCHEME_NAME_LENGTH)] public char [] schemeName; } public int GetSupportedSchemes() { uint result = 0; uint schemeCnt = 0; GetNumSchemes(ref schemeCnt); //This call to the C dll succeeds and returns 2 to schemeCnt SquidEposScheme_t[] schemes = new SXEposScheme_t[schemeCnt]; result = sXGetSupportedSchemes(ref schemes); //Errors on this line with An unhandled exception of type 'System.ExecutionEngineException occurred in sXEposCOM.dll ... ... return result; } Can anyone tell me why I am getting this error?