C# P/Invoke 2 dimensional array -
i trying call c function c# code:
typedef char iptriggernametype[256]; typedef unsigned long comm_handle; typedef byte guid_type[16]; typedef long longint; typedef struct { guid_type guid; iptriggernametype name; }ipcam_generic_event_id; typedef struct { ipcam_generic_event_id eventid; longint relatedtriggerid; longint obsoleteevent; }ipcam_generic_event_info; typedef struct { longint numofevents; ipcam_generic_event_info *genericeventslist; }vid_channel_generic_events_struct; int __stdcall getgenericevents( /* inputs: */ comm_handle handle, longint maxnumofchannelsintable, longint maxnumofeventsperchannel, /* outputs: */ longint *numofchannels, vid_channel_generic_events_struct *channelseventstable);
and c# equivalent follows:
[structlayout(layoutkind.sequential)] public struct ipcam_generic_event_id { [marshalas(unmanagedtype.lparray, sizeconst = 16)] public byte[] guid; [marshalas(unmanagedtype.lparray, sizeconst = 256)] public char[] name; }; [structlayout(layoutkind.sequential)] public struct ipcam_generic_event_info { public ipcam_generic_event_id eventid; public int relatedtriggerid; public int obsoleteevent; } [structlayout(layoutkind.sequential)] public struct vid_channel_generic_events_struct { public int numofevents; [marshalas(unmanagedtype.lparray, sizeconst = 100)] public ipcam_generic_event_info[] genericeventslist; } [dllimport(dllname)] public static extern int getgenericevents( /* inputs: */ int handle, int maxnumofchannelsintable, int maxnumofeventsperchannel, /* outputs: */ out int numofchannels, out vid_channel_generic_events_struct[] channelseventstable); int numofchannels = 16, actualnumofchannels = 0; int maxnumofevents = 100; vid_channel_generic_events_struct[] genericeventslist = new vid_channel_generic_events_struct[numofchannels]; (int = 0; < numofchannels; i++) { genericeventslist[i].genericeventslist = new ipcam_generic_event_info[maxnumofevents]; } getgenericevents(conn, numofchannels, maxnumofevents, out actualnumofchannels, out genericeventslist);
when calling c# method exception crashes application:
managed debugging assistant 'fatalexecutionengineerror' has detected problem in 'my.exe'.
additional information: runtime has encountered fatal error. address of error @ 0x72a6cf41, on thread 0x5a98. error code 0xc0000005. error may bug in clr or in unsafe or non-verifiable portions of user code. common sources of bug include user marshaling errors com-interop or pinvoke, may corrupt stack.
what doing wrong ?
the field:
vid_channel_generic_events_struct.genericeventslist
is pointer, declared as:
ipcam_generic_event_info *genericeventslist
but declaring array, included in struct:
[marshalas(unmanagedtype.lparray, sizeconst = 100)] public ipcam_generic_event_info[] genericeventslist;
you may try use intptr , marshal.allochglobal. check out: how use pinvoke c struct array pointer c#
Comments
Post a Comment