Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I define 'out' parameters a in C++CX Windows Runtime Component?

By the looks of Google it seems like this might not be possible, but:

How do I define an 'out' parameter in a C++/CX 'ref class'?

If your answer is that this isn't possible, please provide a reference.

like image 244
Omer Raviv Avatar asked Apr 10 '13 07:04

Omer Raviv


2 Answers

Any parameter which is of type T* (where T is a ABI-legal type) will be treated by the compiler as an out parameter, and decorated in metadata as such. The following code:

namespace TestMakePublic {
 public ref class Class1 sealed
 {
 public:
   void foo(int* out1, Object^* out2){}
 };
}

Produces a function in metadata which looks like this (ildasm output):

.method public hidebysig newslot virtual final 
        instance void  foo([out] int32& out1,
                           [out] object& out2) runtime managed
{
  .override TestMakePublic.__IClass1PublicNonVirtuals::foo
} // end of method Class1::foo

Note that WinRT does not support "in/out" parameters, so the value of out1 and out2 are only valid for returning from the function, and cannot be trusted as inputs to foo.

like image 97
Andy Rich Avatar answered Oct 03 '22 15:10

Andy Rich


It is a C# specific keyword, COM has it too in the IDL syntax. The equivalent in MSVC++ is the [out] attribute.

But no, the compiler is going to reject that with C3115 if you try to use it. Keep in mind that you use the C++/CX language extension to write code that's used by other languages. Which in general support to notion of [out] very poorly. Neither C++, Javascript or .NET languages like vb.net support it. You can see this as well in the .h files in C:\Program Files (x86)\Windows Kits\8.0\Include\WinRT, generated from the .idl files in that same directory that does have the [out] attribute. It was stripped in the .h file by midl.

It doesn't matter anyway since your code will be used in-process so there's no benefit at all from [out] being able to optimize the marshaling of the argument value. Just a plain pointer gets the job done. Having to initialize the argument value in C# code is however inevitable lossage.

like image 28
Hans Passant Avatar answered Oct 03 '22 16:10

Hans Passant