I have the following C Header/Code Example:
Header file
struct category_info {
int id;
const char *name;
const char *description;
};
DLLEXPORT
void* xyz_categories_info(struct category_info **info, size_t *info_count);
Example C Snippet
struct category_info *catinfo;
size_t catcount;
size_t i;
int max_name_len = 0;
void *catmem = xyz_categories_info(&catinfo, &catcount)
Which I would like to convert to c#...
My First GUESS (and its a guess) is:
[StructLayout(LayoutKind.Sequential)]
public struct category_info
{
int id;
[MarshalAs(UnmanagedType.LPStr)]
StringBuilder name;
[MarshalAs(UnmanagedType.LPStr)]
StringBuilder description;
};
[DllImport("mydll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr xyz_categories_info([Out]category_info cat, [Out]int catSize);
But it just doesn't look right..
Any suggestions.. Once the above is declared correctly in C#.. How should it be accessed in C#
category_info catinfo;
catmem = xyz_categories_info(out catinfo, out catcount);
??????
Any help greatly appreciated.
Thanks
================================================================================
Update 2
The memory allocated in xyz_categories_info is freed using this C call:
void xyz_categories_info_free(void *p);
Below is an example of it being used in C.... Hope this explains it a bit more..
category_buffer = xyz_categories_info(&category_info, &category_count);
if( !category_buffer )
{
// Failed Log a message and exit.
exit(1);
}
for(j=0; j<category_count; j++)
{
if( category_info[j].id == 0 )
continue;
printf("id: %d name: '%s' description: '%s'\n",
category_info[j].id,
category_info[j].name,
category_info[j].description
);
}
xyz_categories_info_free(category_buffer);