473,699 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mixing Mananged and Unmanaged in an Unmanaged class

Jon
Whether I can compile a class or not, depends on the order of functions in my class.

My question is (see example below):
1) Is it a bug that the class 'WillCompile' will compile and execute, or
2) or is it a bug that the class 'WillNotCompile ' will not compile?.

#include "stdafx.h"
#include <iostream>
#include <memory>
using namespace std;

#using <mscorlib.dll >

using namespace System;

#pragma unmanaged
class WillCompile {
public:
static void Umg() {
}
#pragma managed
static void Mng( String* str ) {
Console::WriteL ine( str );
}
};

#pragma unmanaged
class WillNotCompile {
public:
#pragma managed
static void Mng( String* str ) {
Console::WriteL ine( str );
}
#pragma unmanaged
static void Umg() {
}
};

#pragma managed
int _tmain()
{
WillCompile::Mn g( S"This works." );

Console::WriteL ine(S"Press Enter to continue.");
Console::ReadLi ne();
return 0;
}
Nov 17 '05 #1
14 6479
Hi Jon,

Currently I am looking for some resource to investigate this issue. We will
reply here with more information as soon as possible.
If you have any more concerns on it, please feel free to post here.
Thanks for your understanding!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

Nov 17 '05 #2
Hi Jon
1) Is it a bug that the class 'WillCompile' will compile and execute, or 2) or is it a bug that the class 'WillNotCompile ' will not compile?.


Our Visual C++ team member Ronald confirmed these 2 scenarios are both as
designed, simply the rule is: any code that touches managed types must be
compiled as MSIL, any code that does not can be compiled either way.

Do you have any particular expectation or concern on this problem, if so,
please feel free to reply this message.
Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

Nov 17 '05 #3
From my testing, it appears that whether the class is managed is determined at the end of the definition. This seems at least a little bit unclear

1) WillNotCompile2 is just like WillCompile except it has an extra #pragma unmanaged at the end. This appears to make the whole class unmanaged, which creates the compilier error

2) WillCompile2 is just like WillNotCompile except it has an extra #pragma managed at the end. This appears to make the whole class managed, which avoids the compilier error

3) Changing the function order doesn't seem to have any affect, the only thing I can see as affecting whether it compiles or not is what the last #pragma statement within the class definintion is

This is just what I've determined from testing, I could be wrong. The MSDN page on it only mentions that #pragma affects how functions are compilied. It appears that it is affecting how classes are compiled as well

#pragma unmanage
class WillNotCompile2
public
static void Umg()

#pragma manage
static void Mng( String* str )
Console::WriteL ine( str )

#pragma unmanage
}

#pragma unmanage
class WillCompile2
public
#pragma manage
static void Mng( String* str )
Console::WriteL ine( str )

#pragma unmanage
static void Umg()

#pragma manage
}
http://msdn.microsoft.com/library/en...dUnmanaged.asp
Nov 17 '05 #4
Jon
So then, here is my confusion.

Does not "#pragma unmanaged" before a class, force it to be unmanaged?

If so, is this not a compiler bug?

#pragma unmanaged
class ThisIsManagedEv enWithPragmaUna managed {
public:
static void Umg() {
}
#pragma managed
static void Mng( String* str ) {
Console::WriteL ine( str );
}
};

I expected an error message, not a conversion to a managed class.

Nov 17 '05 #5
The class does not become "managed" i.e. it does not all of a sudden become
a __gc class. What you are getting is a class with the methods you do not
mark as #pragma managed being compiled to native code and the ones you do
explicitly mark so being compiled to MSIL.

Ronald Laeremans
Visual C++ team

"Jon" <jo*@martinsoun d.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
So then, here is my confusion.

Does not "#pragma unmanaged" before a class, force it to be unmanaged?

If so, is this not a compiler bug?

#pragma unmanaged
class ThisIsManagedEv enWithPragmaUna managed {
public:
static void Umg() {
}
#pragma managed
static void Mng( String* str ) {
Console::WriteL ine( str );
}
};

I expected an error message, not a conversion to a managed class.

Nov 17 '05 #6
That doesn't really explain why one class would compile, while another one which just had the functions in a different order didn't compile. If the #pragma statements are truly only affecting the functions, then the only difference between the two classes is the function order. Thats something that usally doesn't affect whether it compiles or not.
Nov 17 '05 #7
Jon
Thanks. So #pragma (un)managed works only for functions.

So the confusion is when does a mixed manage/unmanged class
produces a compile error and when does it produces a __gc class?

class ThisCompilesAsG c {
#pragma unmanaged
static void Umg() { }
#pragma managed
static void Mng( String* str ) { Console::WriteL ine( str ); }
};

class ThisDoesNotComp ile {
#pragma managed
static void Mng( String* str ) { Console::WriteL ine( str ); }
#pragma unmanaged
static void Umg() { }
//error C3280: 'ThisDoesNotCom pile::Umg' : a member-function of a managed type cannot be compiled as an unmanaged function
};
Nov 17 '05 #8
Hi Russell,

From MSDN, we can see that:
"It is not valid to declare a member of an unmanaged class to have __gc pointer type. In order to "point" to a managed object from the C++
heap, the header file vcclr.h provides the type-safe wrapper template gcroot. Use of this template allows the programmer to embed a virtual
__gc pointer in an unmanaged class and treat it as if it were the underlying type"

Please refer to http://msdn.microsoft.com/library/de...sSpec_16_3.asp.

Also, don't use #pragma unmanaged, #pragma managed inside a class. If you want to define such functions, move it out of the classes.

Thanks.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 17 '05 #9
Yan-Hong Huang[MSFT] wrote:
Hi Russell,

From MSDN, we can see that:
"It is not valid to declare a member of an unmanaged class to have __gc pointer type. In order to "point" to a managed object from the C++
heap, the header file vcclr.h provides the type-safe wrapper template gcroot. Use of this template allows the programmer to embed a virtual
__gc pointer in an unmanaged class and treat it as if it were the underlying type"

Please refer to http://msdn.microsoft.com/library/de...sSpec_16_3.asp.

Also, don't use #pragma unmanaged, #pragma managed inside a class. If you want to define such functions, move it out of the classes.


I understand that, but my point was that this doesn't compiler

__gc class GCTest_c;

#pragma unmanaged
class Test_c
{
public:
Test_c(void)
{
}

private:
gcroot<GCTest_c *> m_Test;
};

There is no pragma in the class, the class and hopefully all functions
will be unmanaged, but it doesn't compile because of the gccroot variable.

Take the example from the link you posted. If you put #pragma unmanaged
abot class CppClass_c, it won't compile.

// mcpp_nested_cla sses7.cpp
// compile with: /clr
#using <mscorlib.dll >
#include <vcclr.h>
using namespace System;

#pragma unmanaged
class CppClass {
public:
gcroot<String*> str; // can use str as if it were String*
CppClass() {}
};

int main() {
CppClass c;
c.str = new String("hello") ;
Console::WriteL ine( c.str ); // no cast required
}

Is this a bug, or do all non gc classes that contain gccroot variables
have to be compiled as managed?

Thanks

Russell
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
489
by: Neil | last post by:
I am developing a demo in C# using Managed DirectX. I wanted to use a C++ static library in the demo (its a class for handling physics), so I decided to create my Graphics classes in C#, inherit from them in MC++. So far so good...... I modified my MC++ project to run as mixed mode (removed nochkclr.obj, added msvcrt.lib, etc etc), and created 2 unmanaged classes which handled the calls to the static lib (btw all the code works fine in...
4
1687
by: Daniel Lidström | last post by:
Hello, is it possible to ``use CS to code a class, and (M)C++ for the logic''? Sorry if this sounds vague, I'm not really sure myself what it means. Does anyone have any ideas? -- Daniel
13
2259
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
4443
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?
3
2184
by: frank | last post by:
Hi I've got aplication, which one is written in unmanaged c++ with stl, i've made for it gui in managed c++. Problem becomes when I'm starting to filling up for example datagrids, when I'm adding row to datagrid , some varibles (vectors etc) in unmanaged class are cleared or filled with null. I want mention also when i compile only unmanaged class for console project (without .net gui) problem never happends, everything works fine.
3
4700
by: frank | last post by:
Hi I've got aplication, which one is written in unmanaged c++ with stl, i've made for it gui in managed c++. Problem becomes when I'm starting to filling up for example datagrids, when I'm adding row to datagrid , some varibles (vectors etc) in unmanaged class are cleared or filled with null. I want mention also when i compile only unmanaged class for console project (without .net gui) problem never happends, everything works fine.
2
16852
by: jraul | last post by:
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.
2
3147
by: Cartoper | last post by:
I am working in VS2005 (.Net 2.0). I have a unmanaged C++ class that will be used by C#, once I get to that point. I would like to throw a System::ApplicationException from the unmanaged code, is there any problems with this? I am formating a string in one of these throws where I am using String::Format and one of the values is a unmanaged enum (ie int). How do I go about boxing it to use it in the String::Format call? When I try to...
2
12355
by: Jon Slaughter | last post by:
How difficult is it for one to integrate unmanaged C++ into C#? I know for functions one can use DLLimport but how does one go about doing it for classes? Do I have to completely reimplement the classes in managed C++ as a wrapper to the unmanaged C++ classes or is there an easier way? Essentially what I have done is written a C++ kernel mode driver and I want to use it from my C# program. Because it requires some setup outside the...
0
8685
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8880
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.