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



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • 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.
  1. 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.
  2. 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.
  3. Save the file as Managedcode.cpp
  4. 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
  5. Run the following command to run the Microsoft intermediate language (MSIL) disassembler to view the Managedcode.exe assembly:
    ildasm managedcode.exe
  6. 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.
  1. Open the Visual Studio .NET 2003 or Visual Studio 2005 command prompt.
  2. Open a text editor such as Notepad and then paste the following code in a blank file:
    class X{}; //native class.
  3. Save the file as Native.h
  4. 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*){};
    };
  5. 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
  6. 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);
    }
  7. 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.
  8. 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.

REFERENCES

For more information about using the Ildasm.exe utility to view assemblies, visit the following MSDN Web site:

Modification Type:MajorLast Reviewed:1/12/2006
Keywords:kbProgramming kbCompiler kbinfo KB822330 kbAudDeveloper kbAudITPRO