473,379 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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

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
Jun 27 '08 #1
1 11181
On 20 avr, 15:30, obiwanjacobi <obiwanjac...@hotmail.comwrote:
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

Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Abhishek Srivastava | last post by:
Hello All, I am facing a simple problem. I have a method which accepts two parameters. Both are of type DateTime. However when calling this method there are times when the caller does not...
4
by: zlatko | last post by:
Hello. I am new to .net . I have a managed C++ library. It looks like this. // header file namespace cppnetdll { public __gc class Class1 { public: static int foo_int(void);
3
by: Ray | last post by:
I am trying to add calls to managed C++ class from unmanaged C in the same DLL (I saw somewhere while trying to solve something else, that if you have them both in the same DLL that you could...
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
1
by: Jesse McGrew | last post by:
Hi all, I'm trying to make a plugin DLL for a third-party application, using VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of the application, and my actual plugin code...
9
by: Herby | last post by:
Is possible to have a managed method within a Native(un-managed) class within a \clr project? E.g. class myClass { public: #pragma managed void myMethod(void);
1
by: MC-Advantica | last post by:
Does anyone have a simple "Hello World" like application that demonstrates unmanaged C++ calling managed C++ developed in VS2005? I'm confused by many posts as they discuss managed extensions from...
0
by: Rik Moed | last post by:
Hi all, I encounter a problem with a library method that should create a multi dimensional string array. Below is the method:...
9
by: =?Utf-8?B?bGlhbnF0bGl0?= | last post by:
best way to use: an out method parameter or a method with return ?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.