469,360 Members | 1,799 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

Equivalent of the out c# keyword in C++

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
Nov 16 '05 #1
7 7886
using namespace System::Runtime::InteropServices;

bool myFunction([Out] String **error) {}
"Elp" <ro********@REMOVEME.hotmail.com> wrote in message
news:OL**************@TK2MSFTNGP11.phx.gbl...
[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

Nov 16 '05 #2
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" <ro********@REMOVEME.hotmail.com> wrote in message
news:OL**************@TK2MSFTNGP11.phx.gbl...
[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

Nov 16 '05 #3
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 [MS]" <er****@online.microsoft.com> wrote in message
news:O7**************@TK2MSFTNGP11.phx.gbl...
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" <ro********@REMOVEME.hotmail.com> wrote in message
news:OL**************@TK2MSFTNGP11.phx.gbl...
[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


Nov 16 '05 #4
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" <ro********@REMOVEME.hotmail.com> wrote in message
news:OL**************@TK2MSFTNGP11.phx.gbl...
[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

Nov 16 '05 #5
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" <ma******@NOSPAMstudent.liu.se> wrote in message
news:uU*************@TK2MSFTNGP11.phx.gbl...
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" <ro********@REMOVEME.hotmail.com> wrote in message
news:OL**************@TK2MSFTNGP11.phx.gbl...
[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


Nov 16 '05 #6
> 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
Nov 16 '05 #7
Take a look at this and see if it gives you any ideas.

http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/

Elp wrote:
[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

Nov 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Phil Powell | last post: by
9 posts views Thread by Krishnan | last post: by
8 posts views Thread by FDude | last post: by
5 posts views Thread by tlemcenvisit | last post: by
10 posts views Thread by Bishoy | last post: by
12 posts views Thread by rodchar | last post: by
1 post views Thread by trialproduct2004 | last post: by
15 posts views Thread by tereglow | last post: by
3 posts views Thread by Friedman, Jason | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.