Microsoft KB Archive/843415

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

Article ID: 843415

Article Last Modified on 5/18/2007

-

APPLIES TO


 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * 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 &quot;Applies to&quot; section. This bug was corrected in Microsoft Visual Studio .NET 2002 Service Pack 1.



Steps to reproduce the problem
 Start a text editor, such as Notepad.  Paste the following code in Notepad: 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; }  Save the Notepad document as a Visual C++ .NET file that is named CppCode.cpp. Start a Microsoft Visual Studio .NET command prompt. At the command prompt, change the directory path to the directory that you saved the CppCode.cpp file to. Run the following command to create an output file that targets the common language runtime:
 * 1) using <mscorlib.dll>

cl /clr CppCode.cpp

By default, the CppCode.exe file is created.</li> 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.</li> 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.</li>  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. </li></ol>

<div class="references_section">