469,336 Members | 5,515 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 16251
"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
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.