473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 2559
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
7
by: pmatos | last post by:
Hi all, I have a struct with a: static map<int, int> rules; Now, I want it to always be initialized with the pairs (-1, 1), (-2, 2) and (-3, 3). I don't need to dynamically add or delete...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
1
by: geri.gan | last post by:
I have C API just like this: enum void getinfor(const struct inputinfor *a, const struct outputinfor ** b) i use p/invok to translate it to internal static extern void getinfor(ref...
5
by: lallous | last post by:
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;
17
by: dtschoepe | last post by:
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
1
by: js | last post by:
Hi, I'm writing a wrapper module of C API. To make a C struct data avaiable to Python, I need to map C struct into a PyObject. I'm thinking that I use a tuple or dict to represent the struct...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.