C++/CLI and bitfield marshalling

G

Guest

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.
 
B

Brandon Bray [MSFT]

.. said:
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);
}
 
G

Guest

inline.


Brandon Bray said:
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);
}



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?
 
B

Brandon Bray [MSFT]

.. said:
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.
 

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