Base class method build with the Visual Studio 2012 C++/CLR compiler fails to call the correct overriden virtual function - by VBA Derks

Status : 


Sign in
to vote
ID 774800 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 12/19/2012 2:57:44 PM
Access Restriction Public


A .NET assembly created with the Visual Studio 2012 C++/CLR compiler (11.0.51106.01) fails to call the correct virtual override function. A non virtual function calls the base class method implementation while it should call the derived method. This defect becomes visible when the base class and the derived class are in different assemblies and the return type is a native C++ pointer.
Building the assemblies with the Visual Studio 2010 SP1 C++/CLR compiler gives the expected result.
Merging the base and derived class in one assembly gives the expected result.

=== Code Assembly Base.DLL ====
__interface IStorageConnect;

namespace Company { namespace EA { namespace Base {

public value struct IStorageConnect2

public ref class Base
    void Init();
    virtual IStorageConnect* Bad();
    virtual IStorageConnect2* Good();

#include "Base.h"
#include "IStorageConnect.h"

using namespace System;
using namespace Company::EA::Base;

#pragma make_public(IStorageConnect)

void Base::Init()

IStorageConnect* Base::Bad()
    return 0;

IStorageConnect2* Base::Good()
    return 0;

== Code Assembly Derived.DLL ===
public ref class Derived : Base
    virtual IStorageConnect* Bad() override
        return 0;

    virtual IStorageConnect2* Good() override
        return 0;

int main(array<System::String ^> ^)
    auto obj = gcnew Derived();

    return 0;

The expected console ouput is (and the output if build with VS2010):

The console output if build with VS2012 is:

Both executables running on .NET 4.5

The root cause seems to be the fact that VS2012 generates a different .NET definition for the native C++ interface pointer that is added to the global namespace. The same definition is generated in both assemblies. VS2012 generates a different definition then VS2010.
Code generated by VS2010 is seen by .NET as identical but code generated by VS2012 is seen as different types. This causes the .NET runtime to assume that the override doesn't match the base class method signature and will not call it.
Sign in to post a comment.
Posted by Xiang [MSFT] on 2/5/2013 at 10:47 AM
    Thanks for reporting the issue.
    A fix for this issue has been checked into the compiler sources. The fix should show up in the next release of Visual C++.

Xiang Fan
Visual C++ Team
Posted by Microsoft on 12/20/2012 at 12:36 AM
Thank you for submitting feedback on Visual Studio and .NET Framework. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information.
Posted by Macy [MSFT] on 12/19/2012 at 3:51 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(