472,958 Members | 2,339 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1807

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
9
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then...
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" }
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
22
by: tshad | last post by:
If I am passing a variable by reference to another routine by reference, do I need to dereference first? string testString; .... FirstSub(ref firstString) { HandleString(ref firstString); ...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
7
by: Johannes Bauer | last post by:
Hello Group, please consider the following code #include <vector> #include <iostream> #define USE_CONST #define USE_STRING
11
by: =?Utf-8?B?U3VqZWV0?= | last post by:
If there are long strings (like 1MB or 2MB) is it more performant to pass those by ref to methods or by value?
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.