How to call internal method from ref class in another C++/CX WinRT component?

512 Views Asked by At

Is there a way to call internal method with native parameter from ref class in another C++/CX WinRT component? I know there is solution via pointers exposed as int, but is there any better way? Something like to include header files from other lib and not using managed reference (this way I got error message from C# Component3 "error CS0433: The type 'Class1' exists in both 'Component1' and 'Component2'" in other component that consumes these both)...

Component1/class1.h:

    public ref class Class1 sealed
    {
    internal:
        bool InternalMethodForComponent2(NativeType& param1);

    public:
        Class1();
        virtual ~Class1();

        int SomeMethodForComponent3();
    private:

    };

Component2/class2.cpp:

//#include "Component1/class1.h" - replaced by adding reference because of CS0433 in Component3

void Class2::SomeMethod(Class1^ obj)
{
    NativeType nt;
    nt.start = 1;

    ...

    obj->InternalMethodForComponent2(nt); //does not work - error C2039: 'InternalMethodForComponent2' : is not a member of 'Component1::Class1'
}

Component3/class3.cs:

void MethodInClass3()
{
    Class1 obj1 = new Class1();
    Class2 obj2 = new Class2();

    obj2.SomeMethod(obj1);
    var res = obj1.SomeMethodForComponent3();
}
1

There are 1 best solutions below

3
On

The correct way is to include the header Class1.h when defining Class2; by adding a reference to the WinMD (metadata) the compiler only knows about the public members. Adding the header allows the compiler to see the true C++ type, including the internal members.

The error you got when you included the header is hard to understand without a complete example of your code, although my best guess is that you had two namespaces and the reference to Class1 was ambiguous. For a start, you could just put Class1 and Class2 in the same .h/.cpp files to simplify things and avoid the external header reference altogether.