BUG: Visual C++ .NET Compiler Generates Two Different Native Types for the Same Boolean Return Type (327097)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++ .NET (2003)

This article was previously published under Q327097

SYMPTOMS

The Visual C++ .NET compiler generates both NATIVE_TYPE_U1 and NATIVE_TYPE_VARIANTBOOL for the same Boolean return type that is marked with MarshalAs(UnmanagedType::VariantBool).

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. In Visual C++ .NET, create a new Managed C++ Application project that is named TestBoolean.
  2. Double-click the TestBoolean.cpp file in Solution Explorer and then replace the code in that file with the following code:
    // This is the main project file for the Visual C++ Application project that is 
    // generated by using an Application Wizard.
    
    #include "stdafx.h"
    
    #using <mscorlib.dll>
    #include <tchar.h>
    
    using namespace System;
    
    __gc class TestBoolean
    {
    public:
    	[returnvalue:System::Runtime::InteropServices::MarshalAs
    	(System::Runtime::InteropServices::UnmanagedType::VariantBool)]
    	bool RetValBoolean ([System::Runtime::InteropServices::MarshalAs
    	(System::Runtime::InteropServices::UnmanagedType::VariantBool)] bool b)
    	{
    		return b;
    	}
    };
    
    int _tmain(void)
    {
    	TestBoolean *test = new TestBoolean();
    	Console::WriteLine(test->RetValBoolean(true));
    	return 0;
    }
    						
  3. Press CTRL+SHIFT+B to build the project.
  4. From the command prompt, type the following:
    	c:\>ildasm.exe /adv
    						
    This starts ildasm in the advanced mode.
  5. Open TestBoolean.exe in ildasm.

  6. On the View menu, click Metalnfo and then click Show!.

    Examine the metadata that is produced. You see that the compiler emits two different native types for the return value.
    Global fields
    -------------------------------------------------------
    
    Global MemberRefs
    -------------------------------------------------------
    
    TypeDef #1
    -------------------------------------------------------
    	TypDefName: TestBoolean  (02000002)
    	Flags     : [NotPublic] [AutoLayout] [Class] [AnsiClass]  (00000000)
    	Extends   : 01000008 [TypeRef] System.Object
    	Method #1 
    	-------------------------------------------------------
    		MethodName: RetValBoolean (06000003)
    		Flags     : [Public] [ReuseSlot]  (00000006)
    		RVA       : 0x00001000
    		ImplFlags : [IL] [Managed]  (00000000)
    		CallCnvntn: [DEFAULT]
    		hasThis 
    		ReturnType: Boolean
    		1 Arguments
    			Argument #1:  Boolean
    		3 Parameters
    			(0) ParamToken : (08000001) Name :  flags: [HasFieldMarshal]  (00002000)
    				NATIVE_TYPE_U1 
    			(0) ParamToken : (08000002) Name :  flags: [HasFieldMarshal]  (00002000)
    				NATIVE_TYPE_VARIANTBOOL 
    			(1) ParamToken : (08000003) Name : b flags: [HasFieldMarshal]  (00002000)
    				NATIVE_TYPE_VARIANTBOOL 
    					

Modification Type:MinorLast Reviewed:9/27/2005
Keywords:kbHotfixServer kbQFE kbbug kbCOMInterop kbnofix KB327097