By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 2,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

char* in C struct mapping to C# struct

P: n/a
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

Sep 22 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Vikas" <wa*****@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
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

Sep 22 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.