471,852 Members | 865 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

C++/CLI mixing managed/unmanaged

Hi,

This is probably a noobie question but:

I just created a new C++/CLI project in VS 2005. It created an empty
class:

public ref class Class1
{
// TODO: Add your methods for this class here.

};

I added a method and its implementation. I also added an unmanaged
instance (from a native class library I need to use) as a data member
and the compiler complained:

error C4368: cannot define 'mTest' as a member of managed
'TestProj::Class1': mixed types are not supported

Can I not have unmanaged data members in a managed class? If not, how
am I supposed to interop between managed and unmanaged code?

I took out the "public ref" prefix, and the code compiled.

Jul 2 '07 #1
2 16588
"jraul" <jr*******@yahoo.comwrote in message
news:11**********************@a26g2000pre.googlegr oups.com...
Hi,

This is probably a noobie question but:

I just created a new C++/CLI project in VS 2005. It created an empty
class:

public ref class Class1
{
// TODO: Add your methods for this class here.

};

I added a method and its implementation. I also added an unmanaged
instance (from a native class library I need to use) as a data member
and the compiler complained:

error C4368: cannot define 'mTest' as a member of managed
'TestProj::Class1': mixed types are not supported

Can I not have unmanaged data members in a managed class? If not, how
am I supposed to interop between managed and unmanaged code?

I took out the "public ref" prefix, and the code compiled.
No, unfortunately you cannot have a managed C++/CLI class that contains a
native member. Sorry. The implementation of your managed C++/CLI class
(i.e. when you define its methods) can use native local variables, etc.

-- David
Jul 2 '07 #2

"jraul" <jr*******@yahoo.comwrote in message
news:11**********************@a26g2000pre.googlegr oups.com...
Hi,

This is probably a noobie question but:

I just created a new C++/CLI project in VS 2005. It created an empty
class:

public ref class Class1
{
// TODO: Add your methods for this class here.

};

I added a method and its implementation. I also added an unmanaged
instance (from a native class library I need to use) as a data member
and the compiler complained:

error C4368: cannot define 'mTest' as a member of managed
'TestProj::Class1': mixed types are not supported

Can I not have unmanaged data members in a managed class? If not, how
am I supposed to interop between managed and unmanaged code?
You have to instead use a pointer to the unmanaged data, and allocate it
with "new" in your constructor and "delete" in your destructor and
finalizer. That way the unmanaged data is stored on the native heap, where
the garbage collector won't try to move it (having the gc move unmanaged
data around would be a total disaster, because unmanaged code might be using
it).
>
I took out the "public ref" prefix, and the code compiled.

Jul 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Pascal Cloup | last post: by
2 posts views Thread by Pascal Cloup | last post: by
reply views Thread by Pascal Cloup | last post: by
reply views Thread by Pascal Cloup | last post: by
9 posts views Thread by Pascal Cloup | last post: by
2 posts views Thread by quat | last post: by
reply views Thread by quat | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
aboka
reply views Thread by aboka | last post: by

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.