Click here to Skip to main content
15,910,603 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
Generalarm animation Pin
Anonymous17-Jan-03 14:58
Anonymous17-Jan-03 14:58 
GeneralNo output from << operator in console app Pin
malmi17-Jan-03 10:51
malmi17-Jan-03 10:51 
GeneralRe: No output from << operator in console app - code correction Pin
malmi17-Jan-03 11:08
malmi17-Jan-03 11:08 
GeneralRe: No output from << operator in console app - code correction Pin
Jeff J18-Jan-03 0:17
Jeff J18-Jan-03 0:17 
GeneralRe: No output from << operator in console app - code correction Pin
malmi18-Jan-03 6:02
malmi18-Jan-03 6:02 
GeneralStupid question about VC6 to VC.Net migration reason Pin
Member 13901616-Jan-03 0:23
Member 13901616-Jan-03 0:23 
GeneralSystem::String/StringBuilder as out parameter Pin
Paul Selormey14-Jan-03 22:23
Paul Selormey14-Jan-03 22:23 
GeneralRe: System::String/StringBuilder as out parameter Pin
Jeff J15-Jan-03 10:16
Jeff J15-Jan-03 10:16 
If all you are looking to do is get a String/StringBuilder back when passed as a parameter, then passing by reference will achieve what you want. Your current example would need to be modified to something like:

void GetString(StringBuilder **pOut)<br />
{<br />
   (*pOut)->set_Length(0);<br />
   (*pOut)->Append(S"Out--string");<br />
}


This is very COM-like, and both C# and VB.Net prototypes will ask for a reference, such as:

void GetString(ref StringBuilder pOut); //in C#

which would be called like:

GetString(ref pOut);    //C#<br />
GetString(ByRef pOut);  //VB.Net<br />
GetString(&pOut);       //MC++


For various reasons, just asking for a pointer to a parameter is not enough with the CLR (unlike in C++); one must ask for a pointer to a pointer in order for the modification to get back to the caller. Your current version may well work when the StringBuilder is not reallocated, but all bets are off after reallocation, since the callee will then have a pointer to a new object (and this is always the case when passing String).


I believe there is also a way to make the call look like:

void GetString(out StringBuilder pOut); //C#

but I have not bothered so far, as I am waiting for finalisation of VS 2003 with possibly further MC++ refinements, and I have also not really needed to do so.


When passing by the CLR's definition of "by ref", you will no longer need to pre-size the StringBuilder before calling GetString():

int _tmain()<br />
{<br />
   StringBuilder *pOut = new StringBuilder();<br />
   GetString(&pOut);<br />
   Console::WriteLine(pOut->ToString());<br />
   return 0;<br />
}


That is probably obvious to you, but it is mainly why I have used passing by ref. For example, I have written some MC++ functions that are expected to be called repeatedly. They take StringBuilders by ref specifically so that internal arrays rarely need to be reallocated, and I suspect you might be trying to do the same. I have also done this with plain CLR strings, though reallocation would need to occur on each call:

void GetString(String **ppStr)<br />
{<br />
   *ppStr = new String(S"Out--string");<br />
}


However, if the function has no length of string to return, then chars do not have to be allocated for nothing. I hope some of this is valid for your purposes Smile | :)

Cheers
GeneralRe: System::String/StringBuilder as out parameter Pin
Paul Selormey15-Jan-03 15:40
Paul Selormey15-Jan-03 15:40 
GeneralRe: System::String/StringBuilder as out parameter Pin
Paul Selormey15-Jan-03 15:48
Paul Selormey15-Jan-03 15:48 
GeneralRe: System::String/StringBuilder as out parameter Pin
Jeff J16-Jan-03 13:29
Jeff J16-Jan-03 13:29 
GeneralHelp me!!! A issue of wrapping unmanaged class with Managed C++ Pin
Anonymous14-Jan-03 3:23
Anonymous14-Jan-03 3:23 
GeneralRe: Help me!!! A issue of wrapping unmanaged class with Managed C++ Pin
Anonymous14-Jan-03 21:43
Anonymous14-Jan-03 21:43 
GeneralRe: Help me!!! A issue of wrapping unmanaged class with Managed C++ Pin
Paul Selormey14-Jan-03 22:25
Paul Selormey14-Jan-03 22:25 
Generalstatic constructors - still cannot get it! Pin
Paul Selormey14-Jan-03 0:55
Paul Selormey14-Jan-03 0:55 
Generalstd::string marshalling Pin
Member 1043939-Jan-03 12:39
Member 1043939-Jan-03 12:39 
GeneralRe: std::string marshalling Pin
Jeff J9-Jan-03 20:24
Jeff J9-Jan-03 20:24 
Generalweb project with Visual C++ Pin
naradaji8-Jan-03 4:23
naradaji8-Jan-03 4:23 
GeneralRe: web project with Visual C++ Pin
AlexO8-Jan-03 5:23
AlexO8-Jan-03 5:23 
GeneralRe: web project with Visual C++ Pin
naradaji8-Jan-03 20:54
naradaji8-Jan-03 20:54 
QuestionHow can build an exe file by VS .NET to execute it on Windows_98? Pin
Behzad Ebrahimi7-Jan-03 0:13
Behzad Ebrahimi7-Jan-03 0:13 
AnswerRe: How can build an exe file by VS .NET to execute it on Windows_98? Pin
Paul Ingles7-Jan-03 1:26
Paul Ingles7-Jan-03 1:26 
AnswerRe: How can build an exe file by VS .NET to execute it on Windows_98? Pin
Brian Olej8-Jan-03 13:00
Brian Olej8-Jan-03 13:00 
Generalcaught between managed and unmanaged C++ Pin
Member 10439331-Dec-02 5:26
Member 10439331-Dec-02 5:26 
GeneralRe: caught between managed and unmanaged C++ Pin
Jeff J1-Jan-03 18:22
Jeff J1-Jan-03 18:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.