Calling a managed method with an out parameter from C++

O

obiwanjacobi

Hi there,

This is probably a really dump question but I'm stuck and need a leg-
up with this.

I'm writing a C++ interop layer that couples a managed plugin to an
unmanaged host (specifically its a managed implementation for a
Virtual Studio Technology -VST- plugin). So I'm writing a thin C++
layer that does little more than marshal calls to the managed layer.
In this scenario I need to call a managed method with an out parameter
(or ref, I control the method, but it has more than one return value)
from C++. My last C++ experience dates back from when .NET 1.0 was in
beta, so I'm a little rusty ;-), I've been doing C# ever since.

So the compiler keeps telling me it can convert all kinds of pointer
types I've tried to "MyType%". I have no clue how to get a '%' pointer/
reference. This is what I've got so far (an doesn't work) - left out
some details to focus on problem-:

managed:
bool MyMethod(out string extraReturn);

unmanaged:
gcroot<System::String^> myString; -or-
System::String myString;
bool success = MyMethod(string); -or-
bool success = MyMethod(&string);

Please do not fear to give me the RTFM advice, as long as you point
out where I can find the "manual" ;-)

Thanx,
Marc Jacobi
 
A

adebaene

Hi there,

This is probably a really dump question but I'm stuck and need a leg-
up with this.

I'm writing a C++ interop layer that couples a managed plugin to an
unmanaged host (specifically its a managed implementation for a
Virtual Studio Technology -VST- plugin). So I'm writing a thin C++
layer that does little more than marshal calls to the managed layer.
In this scenario I need to call a managed method with an out parameter
(or ref, I control the method, but it has more than one return value)
from C++. My last C++ experience dates back from when .NET 1.0 was in
beta, so I'm a little rusty ;-), I've been doing C# ever since.

So the compiler keeps telling me it can convert all kinds of pointer
types I've tried to "MyType%". I have no clue how to get a '%' pointer/
reference. This is what I've got so far (an doesn't work) - left out
some details to focus on problem-:

managed:
bool MyMethod(out string extraReturn);

unmanaged:
gcroot<System::String^> myString;  -or-
System::String myString;
bool success = MyMethod(string);  -or-
bool success = MyMethod(&string);

Please do not fear to give me the RTFM advice, as long as you point
out where I can find the "manual" ;-)

An "out" parameter in C# is trated as a tracking reference in C++/CLI
(see % operator). Since String is a reference type, the signature of
MyMethod, as seen by C++/CLI, is :
bool MyMethod(String^% extraReturn);

extraReturn is a tracking reference to a handle to a string.

Now, tracking reference (%) are treated the same way as native
reference in native C++ (&). Among other things, it means that when a
function parameter is a reference, you do not need to dereference the
object you pass as parameter : the indirection is done implictely by
the compiler. Therefore, to call your function from C++/CLI, use the
following:

String^ ret=nullptr;
bool b=MyMethod(ret); //simple, heh? !

Arnaud
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top