Specifying "out" parameters in C++/CLI

I'm building a managed wrapper in C++/CLI and am trying to build a client for it in C#.

However there seems to be a few problems.

I have a prototype in C++/CLI which looks like the following:

int f1(int nType, array<MY_TYPE ^> ^%pOut, int ^%npOut)

There are a few problems with the following though. I want that the last two parameters should be output only parameters. In C# I want them to be specified with "out" keyword and not "ref".

However when I try to compile my code I get:

MY_TYPE [] Custom;
int nCustomCount;

Obj.f1(CUSTOM_FLAG, out Custom, out nCustomCount);

Gives:

error CS1502: The best overloaded method match for
'f1(int, ref MY_TYPE[], ref System.ValueType)' has some invalid arguments

error CS1503: Argument '3': cannot convert from 'ref int' to
'ref System.ValueType'

I'm in doubt how to change the prototype to become output parameters instead of references and secondly why the last 'int' became a 'System.ValueType'.

Thanks in advance.



Answer this question

Specifying "out" parameters in C++/CLI

  • jrboddie

    f1(int nType, [out] array<MY_TYPE ^> ^%pOut, [out] int ^%npOut)


  • R&amp;#233;mi

    Thank you. However the code does not compile. The working version should be:

    f1(int nType, [Out] array<MY_TYPE ^> ^%pOut, [Out] int %npOut)

    -- Henrik

  • Art Gaisin

    Yes your absolutely right. I just remembered the [out] attribute. I didn't saw that you used a handle pointer on a int!

  • Specifying "out" parameters in C++/CLI