Home Ask Login Register

Developers Planet

Your answer is one click away!

Roman Gin February 2016

C# P/Invoke 2 dimensional array

I am trying to call a C function from 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; 

typedef struct  {
    IPCAM_GENERIC_EVENT_ID      EventId;   
    LONGINT                     RelatedTriggerId;
    LONGINT                     ObsoleteEvent;

typedef struct
    LONGINT                     NumOfEvents;
    IPCAM_GENERIC_EVENT_INFO    *GenericEventsList;

int __stdcall GetGenericEvents( 
/*  Inputs: */
    COMM_HANDLE                         Handle,                         
    LONGINT                             MaxNumOfChannelsInTable,
    LONGINT                             MaxNumOfEventsPerChannel,

/*  Outputs: */    
    LONGINT                             *NumOfChannels,

and the C# equivalent is as follows:

    public struct IPCAM_GENERIC_EVENT_ID
        [MarshalAs(UnmanagedType.LPArray, SizeConst = 16)]
        public byte[] Guid;

        [MarshalAs(UnmanagedType.LPArray, SizeConst = 256)]
        public char[] Name;

    public struct IPCAM_GENERIC_EVENT_INFO
        public IPCAM_GENERIC_EVENT_ID EventId;
        public int RelatedTriggerId;
        public int ObsoleteEvent;

        public int NumOfEvents;

        [MarshalAs(UnmanagedType.LPArray, SizeConst = 100)]
        public IPCAM_GENERIC_EVENT_INFO[] GenericEventsList;

public static extern int GetGenericEvents(
    /*  Inputs: */
    int Handle,
    int MaxNumOfChannelsInTable,
    int MaxNumOfEve        


Cecilio Pardo February 2016

The field:


is a pointer, declared as:


But you are declaring it as an array, included in the struct:

[MarshalAs(UnmanagedType.LPArray, SizeConst = 100)]
public IPCAM_GENERIC_EVENT_INFO[] GenericEventsList;

You may try to use an IntPtr and Marshal.AllocHGlobal. Check this out: How use pinvoke for C struct array pointer to C#

Post Status

Asked in February 2016
Viewed 2,047 times
Voted 10
Answered 1 times


Leave an answer

Quote of the day: live life