473,402 Members | 2,053 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,402 software developers and data experts.

C++/CLI and bitfield marshalling

Hi,

How do we marshall a type like this from a C++/CLI class wrapper to an
unmanaged method?

typedef struct
{
UINT32 blah : 1;
UINT32 blah2 : 1;
UINT32 blah3: 1;
UINT32 someValue : 12;
}SOMESTRUCT;
Thanks.
Nov 17 '05 #1
4 5431
.. wrote:
How do we marshall a type like this from a C++/CLI class wrapper to an
unmanaged method?

typedef struct
{
UINT32 blah : 1;
UINT32 blah2 : 1;
UINT32 blah3: 1;
UINT32 someValue : 12;
}SOMESTRUCT;


The C++ language doesn't really have the notion of an unmanaged method. A
method that is implemented in native code cannot access CLR types, but
otherwise that's it. The API can have CLR types or it can have native types.

Of course, ref classes do not allow bit fields (mostly because the
efficiency win is probably outweighed by the working set caused by the CLR).
Wrapping is about containing a native type though, not creating a replica of
it with a CLR type. That said, here's an example in the new C++ syntax.

// Native type with bitfield
struct X {
int a : 1;
int b : 2;
int c : 3;
};

// CLR type that wraps X
ref class W {
X * x;

public:
W(int a, int b, int c) {
x = new X;
x->a = a;
x->b = b;
x->c = c;
}

~W() {
delete x;
}

!W() {
delete x;
}

property X* MyX {
X* get() { return x; }
}
};
// Native API
void F(X* x) { /*...*/ }

// Managed API
void F(W^ w) {
F(w->MyX);
}

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.
Nov 17 '05 #2
inline.
"Brandon Bray [MSFT]" <br******@online.microsoft.com> wrote in message
news:uK****************@TK2MSFTNGP11.phx.gbl...
. wrote:
How do we marshall a type like this from a C++/CLI class wrapper to an unmanaged method?

typedef struct
{
UINT32 blah : 1;
UINT32 blah2 : 1;
UINT32 blah3: 1;
UINT32 someValue : 12;
}SOMESTRUCT;
The C++ language doesn't really have the notion of an unmanaged method. A
method that is implemented in native code cannot access CLR types, but
otherwise that's it. The API can have CLR types or it can have native

types.
Of course, ref classes do not allow bit fields (mostly because the
efficiency win is probably outweighed by the working set caused by the CLR). Wrapping is about containing a native type though, not creating a replica of it with a CLR type. That said, here's an example in the new C++ syntax.

// Native type with bitfield
struct X {
int a : 1;
int b : 2;
int c : 3;
};

// CLR type that wraps X
ref class W {
X * x;

public:
W(int a, int b, int c) {
x = new X;
x->a = a;
x->b = b;
x->c = c;
}

~W() {
delete x;
}

!W() {
delete x;
}

Whats this "!" syntax? I never seen it before. *dumb mode ON*

property X* MyX {
X* get() { return x; }
}
};
// Native API
void F(X* x) { /*...*/ }

// Managed API
void F(W^ w) {
F(w->MyX);
}

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.
Basically I have these classses that I need to get visible int he managed
world in my C# project.
I have done the usual mixed mode wrapper with a library being a proxy for
every method and have a few structs that I have to pass in and out so I
mirrord those. But these bitfields got me stumped.
So IF I understand that right I basically wrap this bitfield and set it via
a ctor in the CLR type, unless I specify propsets for every member, right?

Nov 17 '05 #3
.. wrote:
Whats this "!" syntax? I never seen it before.
It is how you write finalizers in the new syntax. It's similar to how
destructors are written with the ~ syntax.
I have done the usual mixed mode wrapper with a library being a proxy for
every method and have a few structs that I have to pass in and out so I
mirrord those. But these bitfields got me stumped.
So IF I understand that right I basically wrap this bitfield and set it
via a ctor in the CLR type, unless I specify propsets for every member,
right?


Exactly what you do depends on the abstraction. The example I showed simply
gave a way to wrap an existing type that has bit fields into a managed type
that C# can understand. The point is that there is no need to encode bit
types inside a ref or value class because you can simply wrap the native
type.

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.

Nov 17 '05 #4
.. wrote:
Whats this "!" syntax? I never seen it before.
It is how you write finalizers in the new syntax. It's similar to how
destructors are written with the ~ syntax.
I have done the usual mixed mode wrapper with a library being a proxy for
every method and have a few structs that I have to pass in and out so I
mirrord those. But these bitfields got me stumped.
So IF I understand that right I basically wrap this bitfield and set it
via a ctor in the CLR type, unless I specify propsets for every member,
right?


Exactly what you do depends on the abstraction. The example I showed simply
gave a way to wrap an existing type that has bit fields into a managed type
that C# can understand. The point is that there is no need to encode bit
types inside a ref or value class because you can simply wrap the native
type.

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Davide Bruzzone | last post by:
Greetings all... I need to create a number of bitfield structs whose contents are smaller than the size of an int. For example: typedef struct foo FOO; struct foo { unsigned char fieldOne:...
4
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is...
3
by: Andy Venikov | last post by:
Sometimes you want to use a bitfield to hold an enum value. In such cases you would only use as many bits as are needed to encode the full set of enum values. And it is a pain to recompute and...
4
by: Animesh | last post by:
Hi All, I don't know whethher this is possible or not. This is the result of a bad design problem. Here I go; I have a structure like this: typedef struct _s_index_entry { char *doc_id;...
2
by: Howard Kaikow | last post by:
I've got the following in a VB 6 project: Private Type PROCESSENTRY32 dwSize As Long cntUsage As Long th32ProcessID As Long th32DefaultHeapID As Long th32ModuleID As Long cntThreads As Long...
2
by: RJ Lohan | last post by:
Howdy, I have a legacy DLL for which I have a problem marshalling a parameter type of char**. The function header (C++) is as so; extern "C" __declspec(dllexport) int __stdcall...
7
by: arne | last post by:
Hi all, cleaning up some elderly code, I stumbled across the following: /**************************************************/ struct { uint bf:8; char a1; char a2;
6
by: shaun roe | last post by:
For a bit of seasonal festive fun, I thought I'd try making a bitfield function, i.e. a function returning, for example, the value of bits 1 to 5 of a word as an integer, when the exact bits are...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.