Passing a reference to a string from C# to C++

G

Guest

I am currently trying to pass a string (two actually; sResponse and sErrMsg
in the code) by reference from a C# windows application to a (managed) C++
dll and the main goal is to change the value of the string in the C++ code.

I understand that C# by default passes by value so therefore you must use
"ref" or "out" to make the reference. I am unable to do this because Visual
Studio 2003 does not recognize that (I think) the method I am calling
(httpGet) uses pass by reference. Also no matter how I reference the string
in the C++ code I cannot change the value of the string itself.

Any help at all will be greatly appreciated.

This is my C# code and below it is my C++ code
private void button1_Click(object sender, System.EventArgs e)
{
string sURL = textBox1.Text;
string sResponse = "this is a test";
int nResult = 0;
string sErrMsg = "2";

WCRobotInternet test = new WCRobotInternet();
bool result = test.httpGet(sURL, sResponse, nResult, sErrMsg);
label1.Text = sResponse;
label2.Text = sErrMsg;
}

bool WCRobotInternet::httpGet(String* sURL, String& sResponse, int nResult,
String& sErrMsg)
{
sResponse = ?
sErrMsg = ?
}
Thanks
 
W

Willy Denoyette [MVP]

|I am currently trying to pass a string (two actually; sResponse and sErrMsg
| in the code) by reference from a C# windows application to a (managed) C++
| dll and the main goal is to change the value of the string in the C++
code.
|
| I understand that C# by default passes by value so therefore you must use
| "ref" or "out" to make the reference. I am unable to do this because
Visual
| Studio 2003 does not recognize that (I think) the method I am calling
| (httpGet) uses pass by reference. Also no matter how I reference the
string
| in the C++ code I cannot change the value of the string itself.
|
| Any help at all will be greatly appreciated.
|
| This is my C# code and below it is my C++ code
| private void button1_Click(object sender, System.EventArgs e)
| {
| string sURL = textBox1.Text;
| string sResponse = "this is a test";
| int nResult = 0;
| string sErrMsg = "2";
|
| WCRobotInternet test = new WCRobotInternet();
| bool result = test.httpGet(sURL, sResponse, nResult, sErrMsg);
| label1.Text = sResponse;
| label2.Text = sErrMsg;
| }
|
| bool WCRobotInternet::httpGet(String* sURL, String& sResponse, int
nResult,
| String& sErrMsg)
| {
| sResponse = ?
| sErrMsg = ?
| }
| Thanks
|

Strings are immutable in .NET so it makes no sense to pass the by ref, you
can't change them anyway.
What you need to do is pass a StringBuilder.

Willy.
 
L

Lee

You are mixing \cli class references (C# string, C++/cli String^) and
c++ pointers. To get the reference to the string passed from C# you
need to use a "tracking reference". So httpGet should be declared as
such:

bool WCRobotInternet::httpGet(String^% sURL, String^% sResponse, int
nResult,
String& sErrMsg)

Here is a link to a good c++\cli FAQ that deals with this issue:

http://www.winterdom.com/cppclifaq/archives/000421.html

L. Lee Saunders
 
W

Willy Denoyette [MVP]

| You are mixing \cli class references (C# string, C++/cli String^) and
| c++ pointers. To get the reference to the string passed from C# you
| need to use a "tracking reference". So httpGet should be declared as
| such:
|
| bool WCRobotInternet::httpGet(String^% sURL, String^% sResponse, int
| nResult,
| String& sErrMsg)
|
| Here is a link to a good c++\cli FAQ that deals with this issue:
|
| http://www.winterdom.com/cppclifaq/archives/000421.html
|
| L. Lee Saunders
|

No, he's not. this is C# calling a native C++ program.

Willy.
 

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