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

3

6

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.

Dollar answered 10/4, 2013 at 7:40 Comment(0)
C
2

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.

Cohligan answered 10/4, 2013 at 11:49 Comment(0)
W
9

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.

Wry answered 12/4, 2013 at 19:48 Comment(1)
Helpful answer. Do you have a reference to this in official docs?Wistful
C
2

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.

Cohligan answered 10/4, 2013 at 11:49 Comment(0)
C
1

You can use:

  1. _Out_opt_
  2. _Out_

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

Clump answered 10/4, 2013 at 17:34 Comment(1)
These are SAL annotations and only affect static code analysis. In normal compilation they evaluate to nothing.Cereal

© 2022 - 2024 — McMap. All rights reserved.