The Visual C++ compiler generates an incorrect Microsoft intermediate language (MSIL) data type for a managed pointer to an IntPtr variable (843415)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0

SYMPTOMS

When you compile Microsoft Visual C++ .NET code that contains managed data types, the Visual C++ .NET compiler (Cl.exe) generates an incorrect Microsoft intermediate language (MSIL) data type for a managed pointer to an IntPtr variable.

For example, if your Visual C++ .NET code contains a managed pointer to an IntPtr variable, the Visual C++ .NET compiler generates native int as the MSIL data type for the IntPtr variable and [mscorlib]System.IntPtr& as the MSIL data type for the pointer variable. The expected MSIL data type for the pointer variable is native int&.

Note When you run a program that uses MSIL code that contains an incorrect MSIL data type, a System.TypeLoadException exception may occur. This exception typically occurs when you run the Visual Studio Industry Partner (VSIP) program software development kit (SDK) sample programs.

RESOLUTION

To resolve this problem, obtain the latest service pack for Visual Studio .NET 2002. For more information, click the following article number to view the article in the Microsoft Knowledge Base:

837234 List of bugs that are fixed in Visual Studio .NET 2002 Service Pack 1

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section. This bug was corrected in Microsoft Visual Studio .NET 2002 Service Pack 1.

MORE INFORMATION

Steps to reproduce the problem

  1. Start a text editor, such as Notepad.
  2. Paste the following code in Notepad:
    #using <mscorlib.dll>
    int main()
    {
        // Declare and initialize an IntPtr variable.
        System::IntPtr ip = 0;
        // Declare and initialize a managed pointer to the IntPtr variable.
        System::IntPtr __gc *pip = &ip;
        return 0;
    }
  3. Save the Notepad document as a Visual C++ .NET file that is named CppCode.cpp.
  4. Start a Microsoft Visual Studio .NET command prompt.
  5. At the command prompt, change the directory path to the directory that you saved the CppCode.cpp file to.
  6. Run the following command to create an output file that targets the common language runtime:

    cl /clr CppCode.cpp

    By default, the CppCode.exe file is created.
  7. Run the following command to view the metadata and the disassembled code for the CppCode.exe file in the MSIL Disassembler (Ildasm.exe).

    Ildasm CppCode.exe

    The IL DASM window opens.
  8. Double-click the node that corresponds to the main method.

    A window appears that displays the MSIL code that is contained in the main method.
  9. If you are using the Microsoft .NET Framework 1.1, locate the following code:
      .locals init (valuetype [mscorlib]System.IntPtr& V_0,
               native int V_1)
    If you are using the Microsoft .NET Framework 1.0, locate the following code:
      .locals (valuetype [mscorlib]System.IntPtr& V_0,
               native int V_1)
    In the code that you located, notice that the Visual C++ .NET compiler generated [mscorlib]System.IntPtr& as the MSIL data type for the managed pointer to the IntPtr variable.

REFERENCES

For additional information, visit the following Microsoft Developer Network (MSDN) Web sites:

Modification Type:MinorLast Reviewed:1/12/2006
Keywords:kbManaged kbtshoot kbCPPonly kbLangCPP kbSample kbCodeGen kbCompiler kbbug KB843415 kbAudDeveloper