MORE INFORMATION
This section discusses the design change in Visual C++ .NET
2003 or Visual C++ 2005.
When you create an unmanaged (native) class, the Visual C++ .NET 2003
or Visual C++ 2005 compiler always sets the class visibility to public while generating the
assembly. If you do not specify the
__gc keyword or the
__nogc keyword before the class declaration, the class uses
__nogc. The keyword
__nogc on a class indicates that the class is an unmanaged C++ class.
Sample 1
The following sample code illustrates the differences in
assemblies between Visual C++ .NET 2002 and Visual C++ .NET 2003 or Visual C++ 2005.
- Open the Visual Studio .NET 2003 or Visual Studio 2005 command prompt: click
Start, point to Programs, point to
Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005, point to Visual
Studio .NET Tools or Visual Studio Tools, and then click Visual Studio .NET 2003
Command Prompt or Visual Studio 2005 Command Prompt.
- Open a text editor such as Notepad and then paste the
following code in a blank file:
#using <mscorlib.dll>
class X{}; //native class.
__gc public class A {
public:
A(){};
A(X*){};
};
void main()
{
X x;
}
Note In Visual Studio 2005, use /clr:oldsyntas. - Save the file as
Managedcode.cpp
- From the Visual Studio .NET 2003 or Visual Studio 2005 command prompt, run the
following command to create an executable file:
cl /clr managedcode.cpp
- Run the following command to run the Microsoft intermediate
language (MSIL) disassembler to view the Managedcode.exe assembly:
ildasm managedcode.exe
- Verify the declaration for class X. You may notice the declaration for class X is public and that it appears as follows:
.class value public sequential ansi sealed
extends [mscorlib]System.ValueType
The Microsoft Visual C++ .NET 2002 compiler sets unmanaged
classes to private. When you compile the same code in Visual Studio .NET 2002
and you open the assembly by using MSIL Disassembler, you may notice that the
declaration for class
X is private.
.class value private sequential ansi sealed
extends [mscorlib]System.ValueType
To obtain similar behavior as the behavior in Visual
C++ .NET 2002, you may use the
/d1PrivateNativeTypes compiler switch. Compile the sample Managedcode.cpp file from the
Visual Studio .NET 2003 command prompt by using the following command:
cl /clr managedcode.cpp /d1PrivateNativeTypes
Notice that the same declaration for class
X exists in both in Visual C++ .NET 2003 and Visual C++ .NET 2002.
View the assembly by using MSIL Disassembler:
ildasm managedcode.exe
Sample 2
The following sample describes how you can restrict the
accessibility of a native class in Visual C++ .NET 2003 or in Visual C++ 2005.
- Open the Visual Studio .NET 2003 or Visual Studio 2005 command prompt.
- Open a text editor such as Notepad and then paste the
following code in a blank file:
class X{}; //native class.
- Save the file as Native.h
- Create a Managed1.cpp file by using Notepad and then paste
the following code:
#using <mscorlib.dll>
#include "native.h"
__gc public class A {
public:
A(){};
A(X*){};
};
- To build the project as a dynamic link library (.dll) file:
run the following command from the Visual Studio .NET 2003 command prompt:
cl /clr /NOENTRY /LD managed1.cpp
- Refer the native class from an assembly. Create a new
Managed2.cpp file by using Notepad, and then paste the following code:
// managed2.cpp
#using <mscorlib.dll>
#include "native.h"
#using "managed1.dll"
int main()
{
X x;
A *pA = new A(&x);
}
- Run the following command to build an executable file:
cl /clr managed2.cpp
You can build successfully in Visual C++ .NET 2003 or in Visual C++ 2005. - Repeat steps 2 through 7 from the Visual Studio .NET 2003
command prompt.
You receive the following compiler errors:
error C3376: 'X' : type is inaccessible
error C3377:
'A::.ctor' : cannot import method - a parameter type or the return type is
inaccessible
error C2660: 'A::A' : function does not take 1
parameters
When you use the Visual C++ .NET 2002 compiler, an error is
generated because the constructor for
A class uses a private type
X class. Therefore, the compiler cannot successfully import the
definition of
A.
In Visual C++ .NET 2003 or in Visual Studio 2005, native classes are treated as
public.
While you use the new compiler switch,
/d1PrivateNativeTypes, you may restrict the access of native class to other assemblies.
In Visual C++ .NET 2003 or in Visual C++ 2005, it is recommended that you compile by using the
/d1PrivateNativeTypes option.