How do I pass a struct by reference in WinRT Component C++/CX

c++-cx, components, struct, windows-runtime

Solution

You cannot pass a struct by reference. All value types (including structs) in winrt are passed by value. Winrt structs are expected to be relatively small - they're intended to be used for holding things like Point and Rect.

In your case, you've indicated that the struct is an "out" parameter - an "out" parameter is write-only, its contents are ignored on input and are copied out on return. If you want a structure to be in and out, split it into two parameters - one "in" parameter and another "out" parameter (in/out parameters are not allowed in WinRT because they don't project to JS the way you expect them to project).

Problem

I have the following in my WinRT component: ``` public value struct WinRTStruct { int x; int y; }; public ref class WinRTComponent sealed { public: WinRTComponent(); int TestPointerParam(WinRTStruct * wintRTStruct); }; int WinRTComponent::TestPointerParam(WinRTStruct * wintRTStruct) { wintRTStruct->y = wintRTStruct->y + 100; return wintRTStruct->x; } ``` But, it seems that the value of winRTStruct->y and x are always 0 inside the method, when called from C#: ``` WinRTComponent comp = new WinRTComponent(); WinRTStruct winRTStruct; winRTStruct.x = 100; winRTStruct.y = 200; comp.TestPointerParam(out winRTStruct); textBlock8.Text = winRTStruct.y.ToString(); ``` What is the correct way to pass a struct by reference so it an be updated inside the method of a WinRTComponent written in C++/CX?

Original source