Omer Raviv
Omer Raviv

Reputation: 11827

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.

Upvotes: 6

Views: 2738

Answers (3)

Andy Rich
Andy Rich

Reputation: 1962

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.

Upvotes: 9

You can use:

  1. _Out_opt_
  2. _Out_

But these are available only for private, internal, and protected members AFAIK.

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941635

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.

Upvotes: 2

Related Questions