converting double pointer in C#

  • Thread starter Thread starter John Gallagher
  • Start date Start date
J

John Gallagher

I'm experiencing some confusion with converting double pointer in C#.
i'm calling a C api, but returning no values. I suspect I have issues
with my memory allocation in c#. Any help or pointers would be
appreciated. Thanks.

C code example:
typedef struct
{
unsigned char **test_list;
int count;
} GET_LIST;

memset( list, 0, sizeof( GET_LIST ) );
memset( display_list, 0, sizeof( GET_LIST ) );

GET_LIST *list,
GET_LIST *display_list

list->test_list=(uchar**)malloc( 10 * sizeof( uchar * ) );
display_list->test_list=(uchar**)malloc(10 * sizeof( uchar* ));

for (i = 0; i < list->10; i++ )
list->test_list = (uchar *)malloc( 256 * sizeof( uchar ) );

for (i = 0; i < display_opt->10; i++ )
display_list->test_list = (uchar *)malloc( 256 * sizeof( uchar ));

C# code example:
public struct GET_LIST
{
public IntPtr test_list;
public Int32 count;
}

_list = new GET_LIST();
_display_list = new GET_LIST();

_list.test_list = Marshal.AllocCoTaskMem(10 * 256);
intRetCode = TestWrapper.api_call(ref _list );
if (_list.count > 0 )
{
_display_list.test_list = Marshal.AllocCoTaskMem(10 * 256);;
intRetCode = TestWrapper.disp_fnct(ref _list, ref _display_list );
}
 
John,

I'd do it like this

[DllImport("kernel32.dll", EntryPoint="RtlMoveMemory")]
static extern void CopyMemory(IntPtr dest, IntPtr[] src, int cb);

IntPtr[] ptrs = new IntPtr[10];
for ( int i = 0; i < ptrs.Length; i++ )
ptrs = Marshal.AllocHGlobal( 256 );
_list.test_list = Marshal.AllocHGlobal(10 * IntPtr.Size);
CopyMemory(_list.test_list, ptrs, 10 * IntPtr.Size);


And don't forget to free the memory when you're done.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top