locked
Boxing in WinRT C++

Answers

  • It seems that boxes look like Windows::Foundation::IReference<T> in C++ (where T is the boxed value type).

    There may be simpler ways to create these, but here's one way:

     

    Platform::Object^ o = Windows::Foundation::PropertyValue::CreateInt32(42);
    

    And you can verify that this implements IReference<int> as required:

     

     

    Windows::Foundation::IReference<int>^ ri = dynamic_cast<Windows::Foundation::IReference<int>^>(o);
    

    That dynamic_cast returns non-null indicating that the cast succeeds, and you therefore have a boxed int.

     

    I'm not sure if that's how C# does it. But if you write a C++ WinRT component that accepts an object, and you call GetRuntimeClass on the object passed in, a boxed int from C# reports its class as IReference<int>, which is a little odd - that's not a type name, it's an interface name.

    There may be some more 'official' way to do it, but this should at least work - hope it gets you unstuck.


    • Edited by IanG Sunday, September 25, 2011 11:49 PM Fixed garbled grammar in 1st sentence
    • Proposed as answer by Marian LuparuMicrosoft employee Tuesday, September 27, 2011 12:57 AM
    • Marked as answer by jmorrill Tuesday, September 27, 2011 12:58 AM
    Sunday, September 25, 2011 11:48 PM

All replies

  • It seems that boxes look like Windows::Foundation::IReference<T> in C++ (where T is the boxed value type).

    There may be simpler ways to create these, but here's one way:

     

    Platform::Object^ o = Windows::Foundation::PropertyValue::CreateInt32(42);
    

    And you can verify that this implements IReference<int> as required:

     

     

    Windows::Foundation::IReference<int>^ ri = dynamic_cast<Windows::Foundation::IReference<int>^>(o);
    

    That dynamic_cast returns non-null indicating that the cast succeeds, and you therefore have a boxed int.

     

    I'm not sure if that's how C# does it. But if you write a C++ WinRT component that accepts an object, and you call GetRuntimeClass on the object passed in, a boxed int from C# reports its class as IReference<int>, which is a little odd - that's not a type name, it's an interface name.

    There may be some more 'official' way to do it, but this should at least work - hope it gets you unstuck.


    • Edited by IanG Sunday, September 25, 2011 11:49 PM Fixed garbled grammar in 1st sentence
    • Proposed as answer by Marian LuparuMicrosoft employee Tuesday, September 27, 2011 12:57 AM
    • Marked as answer by jmorrill Tuesday, September 27, 2011 12:58 AM
    Sunday, September 25, 2011 11:48 PM
  • Thanks Ian!  I'll give it a go when I get home!
    Monday, September 26, 2011 1:25 AM
  • This Windows::Foundation::PropertyValue::CreateInt32(int) seems to work for boxing a value, but still can't seem to unbox a Platform::Object^ to something like an Int32.

    Any other clues?
    Monday, September 26, 2011 8:21 PM
  • That's what IReference<T> is for. It has a Value property that retrieves the boxed value. So the second line of code in my previous post wasn't just of hypothetical interest. :)

    I don't know why that CreateInt32 method doesn't just return an IReference<T> directly, since IReference<T> appears to derive from IPropertyValue in any case.

    Monday, September 26, 2011 10:11 PM
  • Sorry Ian, that looks all to be correct.  The problem is I was putting too much faith the "pre release" intellisense and didn't realize my compiler error was actually a line above :).

     

    -Jer

    Tuesday, September 27, 2011 1:25 AM