Beginner C++/CLI questions

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I've got what I'm sure are some dumb questions. I've got some existing C++
code that I want to move to a C++/CLI assembly to make it easier to use in a
..NET app as well as continuing to use it in an existing C++ app. It's simple
code, but I'm not sure if I'm setting up my function definitions properly.

If I've got a function that takes a string would this be correct:

void PassInString(String^% string1);

What about when I need to return a string from a function. Could someone
show me a simple code snippet? I've tried some things, but I keep getting
errors.

Thanks for any help, I really appreciate it.

Thanks,
Nick
 
If I've got a function that takes a string would this be correct:
void PassInString(String^% string1);

You only need String ^ string1
What about when I need to return a string from a function. Could someone
show me a simple code snippet? I've tried some things, but I keep getting
errors.

String ^ fn()
{
String ^ s = gcnew String( "Whatever" );
return s;
}

Dave
 
This is equivalent to C#

void PassInString(ref String string1) { ... }
or
void PassInString(out String string1) { ... }

You would use System.Runtime.InteropServices.OutAttribute to specify the
latter.
You only need String ^ string1

This is normal pass-by-value of a handle to an immutable string -- the
caller's copy cannot be changed.
 
Thanks Dave, I really appreciate it. Out of curiousity, what does the % do?
Anything?

Thanks,
Nick
 
Thanks Dave, I really appreciate it. Out of curiousity, what does the % do?
Anything?

Oh yes. It signifies a "tracking reference" - have a look on MSDN for
the details.

Dave
 
Hi,

I think "Ben Voigt [C++ MVP]" has explained the meaning of "%" in function
call. It means passing the CLR types by reference with tracking references.

Actually, there are 2 ways to return the result to the caller:
1. Through the return value.
2. Through the parameter by reference.(Using "%" in function parameter)

The code below demonstrates both 2 approaches:

void fnRef(String^% str)
{
str = str + "Whatever";
}

String ^ fn(String^ str)
{
String ^ s = gcnew String( str + "Whatever" );
return s;
}

int main(array<System::String ^> ^args)
{
String ^ str="abc";
String ^ result = fn(str);
Console::WriteLine(result);


fnRef(result);
Console::WriteLine(result);
return 0;
}

Please refer to the link below for several usage of "%" in C++/CLI:
http://msdn2.microsoft.com/en-us/library/8903062a(VS.80).aspx

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top