String conversion in c++/CLI
-
Hi, I am porting my vc2003 app to vc2008 and have come across somthing like this...
// this defined
LPCTSTR lpsz = _T("myString");// then later
// where Afunc is defined as
// SomeObject* AFunc( String *str );
SomeObject *obj = Afunc( lpsz );under vc2003 this compiles and works file however in vc2008 it seems that i need to do...
// this defined
LPCTSTR lpsz = _T("myString");// then later
// where Afunc is defined as
// SomeObject^ Afunc( String ^str );
SomeObject ^obj = Afunc( gcnew String(lpsz) );the documentation on string literals in C++/CLI migration primer suggest that the following could be done.
SomeObject ^obj = AFunc( safe_cast<string^>(lpsz) );
this is wrong as it not compile. I not sure the
gcnew string( lpsz )
method is ideal. is there a better way or what is the propper way of doing this. thanks Robin -
Hi, I am porting my vc2003 app to vc2008 and have come across somthing like this...
// this defined
LPCTSTR lpsz = _T("myString");// then later
// where Afunc is defined as
// SomeObject* AFunc( String *str );
SomeObject *obj = Afunc( lpsz );under vc2003 this compiles and works file however in vc2008 it seems that i need to do...
// this defined
LPCTSTR lpsz = _T("myString");// then later
// where Afunc is defined as
// SomeObject^ Afunc( String ^str );
SomeObject ^obj = Afunc( gcnew String(lpsz) );the documentation on string literals in C++/CLI migration primer suggest that the following could be done.
SomeObject ^obj = AFunc( safe_cast<string^>(lpsz) );
this is wrong as it not compile. I not sure the
gcnew string( lpsz )
method is ideal. is there a better way or what is the propper way of doing this. thanks RobinRobin Imrie wrote:
I not sure the gcnew string( lpsz ) method is ideal. is there a better way or what is the propper way of doing this.
in general
gcnew
is how you create managed objects in C++/CLI so that is always a correct way to do it. Of course strings can be different like in Native C++ based on what you need them for. In your code you are creating a native string and then converting it to a managed string. If you only need a managed string then you could do this.System::String^ lpsz = "myString";
There is a series of introductory C++/CLI articles here on CodeProject. I suggest you take advantage of them.
led mike
-
Robin Imrie wrote:
I not sure the gcnew string( lpsz ) method is ideal. is there a better way or what is the propper way of doing this.
in general
gcnew
is how you create managed objects in C++/CLI so that is always a correct way to do it. Of course strings can be different like in Native C++ based on what you need them for. In your code you are creating a native string and then converting it to a managed string. If you only need a managed string then you could do this.System::String^ lpsz = "myString";
There is a series of introductory C++/CLI articles here on CodeProject. I suggest you take advantage of them.
led mike
Thanks mike for that i had suspected as much. Will give the C++/CLI articles a good look.