473,573 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::Clas s1': 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 16842
"jraul" <jr*******@yaho o.comwrote in message
news:11******** **************@ a26g2000pre.goo glegroups.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::Clas s1': 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*******@yaho o.comwrote in message
news:11******** **************@ a26g2000pre.goo glegroups.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::Clas s1': 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
1272
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 "../Plugin/iPlugin.h" //unmanaged plugin interface header public __gc PluginGui : public Windows::Forms::Form
0
861
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 the exception: .....System.NullReferenceException.... La référence d'objet n'est pas définie à une instance d'un objet ( after translation: The...
2
1070
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 the exception: .....System.NullReferenceException.... La référence d'objet n'est pas définie à une instance d'un objet ( after translation: The...
0
866
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 encountered a problem for generating the dll with the linker: The code line 'ce = new CEmbeded(); ' generated a link error :
0
826
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 encountered a problem for generating the dll with the linker: The code line 'ce = new CEmbeded(); ' generated a link error :
9
1468
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 explanation? Help please Pascal
13
2241
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 code for the listener that launches the listener thread. Managed ----------
2
4434
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 unmanaged, where the form is managed). Can I not have an unmanaged instance in a managed class?
0
1169
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( > D3DADAPTER_DEFAULT, > D3DDEVTYPE_HAL,
0
7747
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7987
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8180
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6363
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5557
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3706
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2167
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.