I am creating a new question here as I now know how to ask this question, but I'm still a newb in PInvoke.
I have a C API, with the following structures in it:
typedef union pu
{
struct dpos d;
struct epo e;
struct bpos b;
struct spos c;
} PosT ;
typedef struct dpos
{
int id;
char id2[6];
int num;
char code[10];
char type[3];
} DPosT ;
and the following API function:
int addPos(..., PosT ***posArray,...)
the way I call this in C like this:
int main(int argc, const char *argv[])
{
...
PosT **posArray = NULL;
...
ret_sts = addPos(..., &posArray, ...);
...
}
inside addPos() memory will be allocated to posArray and it will also be populated. allocation is like this using calloc:
int addPos(..., PosT ***posArray, ...)
{
PosT **ptr;
...
*posArray = (PosT **) calloc(size, sizeof(PosT *));
*ptr = (PosT *)calloc(...);
...
(*posArray)[(*cntr)++] = *ptr;
...
/* Population */
...
}
I have another function that will be called to deallocate that memory.
Now I want to do the same in C#,
I have created this in my C# class:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DPositionT
{
public int Id;
[MarshalAs(UnmanagedType.LPStr, SizeConst = Constants.Id2Len)]
public string Id2;
public int Num;
[MarshalAs(UnmanagedType.LPStr, SizeConst = Constants.CodeLen)]
public string Code;
[MarshalAs(UnmanagedType.LPStr, SizeConst = Constants.TypeLen)]
public string type;
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit )]
struct PosT
{
[System.Runtime.InteropServices.FieldOffset(0)]
DPosT d;
};
I have only defined d, as I am only going to use this member of the union in my client code.
Now in order to call addPos() how should I create and pass posArray ?
your help is very much appreciated.