Mapping char *str[]

  • Thread starter Thread starter lallous
  • Start date Start date
L

lallous

Hello

I am wrapping a C DLL into C# project.

Being a newbie in C#, I wonder how one can map the following:

struct X
{
char *str[];
int count;
};

and the following callback:

typedef void (__stdcall *cb)(int a, int b, char *s[]);

Which is used as a param in another C function to be mapped:

void register_cb(char *description, cb c);

Thanks,
Elias
 
Elias,

In this case, you would have to map str to an IntPtr, it's essentially a
double pointer to a character, an array of arrays, it would seem (or at
least a reference to an array).

You would have to handle the marshaling of this structure yourself.

As for cb, is it a structure? If so, you will have to return an IntPtr,
and then marshal that back to managed code manually. Also, you have to
figure out how to release the memory that is allocated to return the
structure (since you are returning a pointer to it, one assumes you
allocated memory for it).

Once you have that in place, passing the structure to your other
function should be easy.

Hope this helps.
 
Hello, Nicholas!

NPN> As for cb, is it a structure? If so, you will have to return an
NPN> IntPtr, and then marshal that back to managed code manually.

It is function pointer ( typedef void (__stdcall *cb)(int a, int b, char *s[]); )
So it can be a delegate param in the void register_cb(char *description, cb c); function

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Vadym,

Oops, sorry, duh on my part.

In the case of the function pointer, you will need to create a delegate:

public delegate void MyCallback(int a, int b, IntPtr s);

You would have to marshal the array manually again.
 
Hello Nicholas and Vadym,

I wish you can elaborate more on how to (exactly):
In this case, you would have to map str to an IntPtr, it's essentially
a double pointer to a character, an array of arrays, it would seem (or at
least a reference to an array).

You would have to handle the marshaling of this structure yourself.

I want to easily be able to access the 2d string array from C# w/o a hassle,
so mapping to IntPtr helps?
AFAIK, a char * is mapped to "string" what if char** is mapped to array of
string?
 

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

Similar Threads


Back
Top