char* in C struct mapping to C# struct

V

Vikas

Hi
I have been browsing C struct to C# mapping emails on the newsgroups,
but I haven't been able to find a solution to my problem.

My C structure looks like this:

struct myCstruct {
char * data;
size_t size;
};

I want to directly map it into C# as a structure there. But all the
emails have examples of mapping constant size character arrays (usually
as strings). This I would like to map as a byte[].

The aim is that C provides a function callback and passes to the
callback this structure. Then C# uses public delegate and directly
invokes that callback.

I can insert another C callback or function that changes the arguments
to the C callback and splits the structure into separate char* and
size_t elements, but that is not my intention.
I want to directly call C# from C.

Any help will be appreciated.

Thanks and regards,
Vikas
 
N

Nicholas Paldino [.NET/C# MVP]

Vikas,

Your struct will look like this:

[StructLayout(LayoutKind.Sequential)]
struct myCstruct
{
public IntPtr data;
public int size;
}

You will have to manually marshal the data that you want, I believe.

You ^might^ be able to get away with declaring it as a string, but I am
not sure if .NET handles the memory allocation for you (and subsequent
teardown). To be honest, it doesn't come up much because most structures
are self-contained in that respect.

Of course, it is easy enough to try.

Hope this helps.
 

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

Top