Microsoft KB Archive/822330

= How to use the /d1PrivateNativeTypes compiler switch to make native classes private in Visual C++ .NET 2003 or in Visual C++ 2005 =

Article ID: 822330

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-





SUMMARY
This article discusses a design change in Visual C++ .NET 2003 and in Visual C++ 2005 from the earlier version of the product, Visual C++ .NET 2002. This article also discusses the /d1PrivaNativeTypes compiler switch.

Unlike Visual C++ .NET 2002 (whose native classes are private), by default in Visual C++ .NET 2003 and in Visual Studio 2005, native classes are public. You can use the native classes in other assemblies. You can use the /d1PrivateNativeTypes compiler switch in Visual C++ .NET 2003 or in Visual C++ 2005 to obtain similar behavior as the behavior in Visual C++ .NET 2002.



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: 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 </ol>
 * 1) using <mscorlib.dll>

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. <ol> Open the Visual Studio .NET 2003 or Visual Studio 2005 command prompt.</li>  Open a text editor such as Notepad and then paste the following code in a blank file: class X{}; //native class. </li> Save the file as Native.h </li>  Create a Managed1.cpp file by using Notepad and then paste the following code:
 * 1) using <mscorlib.dll>

__gc public class A { public: A{}; A(X*){}; }; </li>  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 </li>  Refer the native class from an assembly. Create a new Managed2.cpp file by using Notepad, and then paste the following code: // managed2.cpp
 * 1) include &quot;native.h&quot;


 * 1) using <mscorlib.dll>


 * 1) include &quot;native.h&quot;
 * 2) using &quot;managed1.dll&quot;

int main {   X x;

A *pA = new A(&x); } </li>  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. </li> 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

</li></ol>

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.

<div class="references_section">