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::WriteLine( str );
}
};
#pragma unmanaged
class WillNotCompile {
public:
#pragma managed
static void Mng( String* str ) {
Console::WriteLine( str );
}
#pragma unmanaged
static void Umg() {
}
};
#pragma managed
int _tmain()
{
WillCompile::Mng( S"This works." );
Console::WriteLine(S"Press Enter to continue.");
Console::ReadLine();
return 0;
} 14 6425
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.
--------------------
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.
--------------------
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::WriteLine( str )
#pragma unmanage
}
#pragma unmanage
class WillCompile2
public
#pragma manage
static void Mng( String* str )
Console::WriteLine( str )
#pragma unmanage
static void Umg()
#pragma manage
} http://msdn.microsoft.com/library/en...dUnmanaged.asp
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 ThisIsManagedEvenWithPragmaUnamanaged {
public:
static void Umg() {
}
#pragma managed
static void Mng( String* str ) {
Console::WriteLine( str );
}
};
I expected an error message, not a conversion to a managed class.
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*@martinsound.com> wrote in message
news:%2****************@tk2msftngp13.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 ThisIsManagedEvenWithPragmaUnamanaged { public: static void Umg() { } #pragma managed static void Mng( String* str ) { Console::WriteLine( str ); } };
I expected an error message, not a conversion to a managed class.
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.
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 ThisCompilesAsGc {
#pragma unmanaged
static void Umg() { }
#pragma managed
static void Mng( String* str ) { Console::WriteLine( str ); }
};
class ThisDoesNotCompile {
#pragma managed
static void Mng( String* str ) { Console::WriteLine( str ); }
#pragma unmanaged
static void Umg() { }
//error C3280: 'ThisDoesNotCompile::Umg' : a member-function of a managed type cannot be compiled as an unmanaged function
};
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.
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_classes7.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::WriteLine( 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
It is not a bug. Please put #pragma managed before the definition of the class. Don't use #pragma unmanaged, which let the method in the class
all unmanaged and so can't use gcroot.
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.
Yan-Hong Huang[MSFT] wrote: It is not a bug. Please put #pragma managed before the definition of the class. Don't use #pragma unmanaged, which let the method in the class all unmanaged and so can't use gcroot.
Ok, but I was trying to get all methods 'unmanaged' apart from one which
is used to call a method on the gcroot object.
Cheers
Russell
Russell Hind wrote: Yan-Hong Huang[MSFT] wrote: It is not a bug. Please put #pragma managed before the definition of the class. Don't use #pragma unmanaged, which let the method in the class all unmanaged and so can't use gcroot.
Ok, but I was trying to get all methods 'unmanaged' apart from one which is used to call a method on the gcroot object.
The code in question was:
__gc class GCTest_c;
#pragma unmanaged
class Test_c {
public:
Test_c(void) { }
private:
gcroot<GCTest_c*> m_Test;
};
You have to assume that any use of the gcroot<T> object will require
doing managed things, including constructing it and destroying it.
Therefore, all the constructors and the destructor of the class must be
compiled as managed code, even if they don't explicitly reference the
gcroot<T> object.
--
David Olsen qg********@yahoo.com
David Olsen wrote: The code in question was:
__gc class GCTest_c; #pragma unmanaged class Test_c { public: Test_c(void) { } private: gcroot<GCTest_c*> m_Test; };
You have to assume that any use of the gcroot<T> object will require doing managed things, including constructing it and destroying it. Therefore, all the constructors and the destructor of the class must be compiled as managed code, even if they don't explicitly reference the gcroot<T> object.
Thanks, I'd forgotten about the default copy constructors etc that are
generated. I wonder if they really need to be managed as they only copy
the gcroot object?
Cheers
Russell
Russell Hind wrote: David Olsen wrote:
The code in question was:
__gc class GCTest_c; #pragma unmanaged class Test_c { public: Test_c(void) { } private: gcroot<GCTest_c*> m_Test; };
You have to assume that any use of the gcroot<T> object will require doing managed things, including constructing it and destroying it. Therefore, all the constructors and the destructor of the class must be compiled as managed code, even if they don't explicitly reference the gcroot<T> object.
Thanks, I'd forgotten about the default copy constructors etc that are generated. I wonder if they really need to be managed as they only copy the gcroot object?
But gcroot objects are never simply copied. The gcroot copy constructor
creates a new GC handle, which uses managed types and calls managed
functions. Every single function in gcroot<T> (including constructors,
destructor, and assignment operators) calls managed functions. Every
single function is also inline. So to be safe, all uses of gcroot
should be within managed code.
--
David Olsen qg********@yahoo.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |