468,167 Members | 2,011 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,167 developers. It's quick & easy.

char* in C struct mapping to C# struct

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
1 2257
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.

Similar topics

7 posts views Thread by pmatos | last post: by
5 posts views Thread by Roy Hills | last post: by
5 posts views Thread by lallous | last post: by
17 posts views Thread by dtschoepe | last post: by
5 posts views Thread by =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post: by
1 post views Thread by js | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.