473,387 Members | 1,517 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,387 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 16814
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Vasco Lohrenscheit | last post by:
Hello, I have problems with pointers to unmanaged classes as parameters for virtual methods, and then overwriting the virtual methods in other assemblies: //GUI.dll #include...
0
by: Pascal Cloup | last post by:
Hello, I encounter problems during execution of code like the followng that is part of Dll of managed C++ classes. When attempting to create the not managed objet (instance of CA) i always get...
2
by: Pascal Cloup | last post by:
Hello, I encounter problems during execution of code like the followng that is part of Dll of managed C++ classes. When attempting to create the not managed objet (instance of CA) i always get...
0
by: Pascal Cloup | last post by:
Dear Ronald, You will wild find below all the lines of an example that reproduces the error. I use : Visual Studio 2003 vers 7.1.3088 ..NET Framework 1.1 vers 1.1.4322 I remember that i had...
0
by: Pascal Cloup | last post by:
Dear Ronald, You will wild find below all the lines of an example that reproduces the error. I use : Visual Studio 2003 vers 7.1.3088 ..NET Framework 1.1 vers 1.1.4322 I remember that i had...
9
by: Pascal Cloup | last post by:
Hello, I write a Dll of managed C++ class that contains a piece of code like the following liines. This code always fails when attempting to create a new not managed class. Someone has an...
13
by: DD | last post by:
I'm puzzled how to mix managed/unmanaged C++ in the following scenario: Unmanaged ------------ Callback listener class with method that should call an event in managed Form1. Initialization...
2
by: quat | last post by:
I am getting the error: error C4368: cannot define 'd3dPP' as a member of managed 'FormEx::Form1': mixed types are not supported I am trying to mixed managed and unmanaged code (d3dPP is...
0
by: quat | last post by:
I have two unmanaged pointer in a managed form class: IDirect3D9* d3dObject; IDirect3DDevice9* d3dDevice; In a member function of the form, I call: > d3dObject->CreateDevice( >...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.