470,874 Members | 1,464 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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

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

Feb 17 '06 #1
3 1729

"Bart Sipes" <Ba*******@discussions.microsoft.com> wrote in message
news:22**********************************@microsof t.com...
|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.
Feb 17 '06 #2
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

Feb 17 '06 #3

"Lee" <sa******@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
| 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.
Feb 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by domeceo | last post: by
7 posts views Thread by Ken Allen | last post: by
9 posts views Thread by Just Me | last post: by
12 posts views Thread by Andrew Bullock | last post: by
22 posts views Thread by tshad | last post: by
7 posts views Thread by Johannes Bauer | last post: by
11 posts views Thread by =?Utf-8?B?U3VqZWV0?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.