0

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();
}
wallycz
  • 312
  • 3
  • 11
  • Pretty unlikely to get a C# compiler error when compiling C++/CX code. This question doesn't make much sense. – IInspectable Feb 14 '16 at 23:03
  • This C# error is from Component 3, which references both C++/CX WinRT components 1 and 2. What exactly doesn't make sense? – wallycz Feb 15 '16 at 00:01
  • I only see code for 1 class. – Peter Torr - MSFT Feb 15 '16 at 01:22
  • That's not possible of course, *internal* methods can only be called from C# code that lives in the same assembly. That C++ code is never part of the C# assembly. It must be *public*, also ensures that the interop plumbing is taken care of. No alternative. – Hans Passant Feb 15 '16 at 14:03
  • Thanks for your comment, but i don't want to call internal method from C#, but from another C++/CX component, you probably missed it. There is alternative - make this method public and transform reference of NativeType to pointer encapsulated to int, but its ugly. I only search for better alternative. – wallycz Feb 15 '16 at 14:27

1 Answers1

0

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.

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51
  • Reviving an old topic... Is there a way to include some headers and reference the winmd file as well ? Because I get a class redefinition doing so (which makes sense). – Tryum Nov 27 '18 at 12:52
  • Why do you need to reference the WinMD file? Can you provide a complete example of your problem? – Peter Torr - MSFT Nov 27 '18 at 23:45
  • Because some other non exposed part of the components are needed, and exporting the headers was not part of the initial plan. I guess the way to do it is rework the component so that all header files defining public interfaces are exportable as well. – Tryum Nov 28 '18 at 07:59