473,897 Members | 3,115 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 6517
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
1695
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
2286
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
4449
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
2194
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
4707
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
16869
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
3156
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
12373
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
9992
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
9839
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
11254
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...
1
10943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10480
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
7187
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4701
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
2
4295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.