Posts tagged C++/CX

I've recently got started with C++/CX to run some C++ from my C# projects. In some places I needed to return multiple values from a method and figured the C# out parameter modifier would be a good solution. But I couln't find too much documentation on how to do it, but it turned out to be quite simple, here is a sample which has a float out parameter:

public ref class MyClass sealed
    void GetValue(float* value)
      if (value != nullptr) *value = 1337.0f;

Which we can then access in e.g. C# (using the new C# 7 out syntax):

MyClass c = new MyClass();
c.GetValue(out float val);
Debug.WriteLine($"Value: {val}");

This also works for other types, such as Platform::String as well:

void GetValue(Platform::String^* value)
  if (value != nullptr) *value = "Hello world";