Equivalent of the out c# keyword in C++

  • Thread starter Thread starter Elp
  • Start date Start date
E

Elp

[Follow-up to microsoft.public.dotnet.langage.vc]

Hi,

I'm developping a small Managed C++ Dll intented to be called from my C#
program. The C++ function should take a reference to a string in parameter
in ordrer to write to this string if an error occurs. That way the calling
program can have info about the error.
If i had to do that in c#, i would have declared my function in this way:

bool myFunction(out string errorMessage) {}

and called it this way:
string error = null;
bool res = myFunction(out error);

How can i do to have the same behaviour if myFunction is in managed C++ but
called from C#?

thanks
 
using namespace System::Runtime::InteropServices;

bool myFunction([Out] String **error) {}
 
I don't think you can.

"out" is a C#-only construct. It's encoded in the metadata as "ref", with a
custom annotation (attribute, IIRC) that says that it's really an out.

Doing ref in C++ is the best you can do.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
 
This is not true. Check
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vcmex/html/vcconManagedExtens
ionsForCFrequentlyAskedQuestions.htm, under "Interoperability" -- there's an
item that says "I want to call a Managed Extensions function from C# but not
use a C# ref parameter in the call. How can I specify an out parameter in
Managed Extensions?"

See my earlier post for the solution.


Eric Gunnerson said:
I don't think you can.

"out" is a C#-only construct. It's encoded in the metadata as "ref", with a
custom annotation (attribute, IIRC) that says that it's really an out.

Doing ref in C++ is the best you can do.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
Elp said:
[Follow-up to microsoft.public.dotnet.langage.vc]

Hi,

I'm developping a small Managed C++ Dll intented to be called from my C#
program. The C++ function should take a reference to a string in parameter
in ordrer to write to this string if an error occurs. That way the calling
program can have info about the error.
If i had to do that in c#, i would have declared my function in this way:

bool myFunction(out string errorMessage) {}

and called it this way:
string error = null;
bool res = myFunction(out error);

How can i do to have the same behaviour if myFunction is in managed C++ but
called from C#?

thanks
 
Try something like this:

using namespace System::Runtime::InteropServices;

bool myFunction([Out] String** text)
{
*text = "Across The Universe Of Time";
return true;
}

Note that the Managed C++ compiler is unable to check that text is
actually being assigned to. Out is just an attribute and doesn't affect
the generated code (only the metadata).

- Magnus
 
If you are doing this, it should more properly and efficiently be

*text = S"Across The Universe Of Time";

Note the S prefix to make a String literal.


Magnus Krisell said:
Try something like this:

using namespace System::Runtime::InteropServices;

bool myFunction([Out] String** text)
{
*text = "Across The Universe Of Time";
return true;
}

Note that the Managed C++ compiler is unable to check that text is
actually being assigned to. Out is just an attribute and doesn't affect
the generated code (only the metadata).

- Magnus


Elp said:
[Follow-up to microsoft.public.dotnet.langage.vc]

Hi,

I'm developping a small Managed C++ Dll intented to be called from my C#
program. The C++ function should take a reference to a string in parameter
in ordrer to write to this string if an error occurs. That way the calling
program can have info about the error.
If i had to do that in c#, i would have declared my function in this way:

bool myFunction(out string errorMessage) {}

and called it this way:
string error = null;
bool res = myFunction(out error);

How can i do to have the same behaviour if myFunction is in managed C++ but
called from C#?

thanks
 
If you are doing this, it should more properly and efficiently be
*text = S"Across The Universe Of Time";

Note the S prefix to make a String literal.

Thanks, I wasn't aware of this.
I should have checked the IL before my posting.

- Magnus
 
Back
Top