Hi all;
A) What's the simplest way to convert a 'String' to 'char *' (and the other
way araound) ?
B) I have some C++ classes that I cannot easily convert to .NET (because
they still are to be used in non-.NET apps). Are there any critical issues
why i cannot use these 'as is' in my .NET app (beside of the 'new' and
'delete' of them to 'manually' maintain memory) ?
---------------
Frank R Eid, Norway
William DePalo [MVP VC++] - 17 Mar 2005 20:01 GMT
> A) What's the simplest way
Simplest is subjective. What follow is my view:
> to convert a 'String' to 'char *'
#include <vcclr.h>
wchar_t __pin *pStr = PtrToStringChars(s);
Note that .Net uses 16 bit (UNICODE) characters.
> (and the other way araound) ?
char __nogc *p;
System::String *s;
s = new String(p, 0, lstrlen(p));
Regards,
Will
Frank R Eid - 17 Mar 2005 20:29 GMT
> #include <vcclr.h>
>
> wchar_t __pin *pStr = PtrToStringChars(s);
Thanks. but,
I was a bit hasty with the first message. What I ment was;
How to convert a .NET 'String' (16 bit) to a 'char *' (8 bit) with some sort
of unicode-to-ansi convertion.
The reason for doing this is the implementation of old C++ classes that I
cannot easily convert to .NET ...
Regards Frank.
William DePalo [MVP VC++] - 17 Mar 2005 20:51 GMT
> Thanks.
You are welcome.
> What I ment was; How to convert a .NET 'String'
> (16 bit) to a 'char *' (8 bit) with some sort
> of unicode-to-ansi convertion.
You have at least three more options:
1) .Net's Marshall::StringToHGlobalAnsi() which allocates
memory for the conversion and which you will have to
remember to free
2) the plain old crt function wcstombs()
3) Win32's WideCharToMultiByte().
Regards,
Will
Frank R Eid - 17 Mar 2005 23:21 GMT
> You have at least three more options:
Thanks again;
Going for the 'Marshall::StringToHGlobalAnsi()' sulution.
Frank
Jochen Kalmbach - 18 Mar 2005 07:45 GMT
Hi =?Utf-8?B?RnJhbmsgUiBFaWQ=?=,
> How to convert a .NET 'String' (16 bit) to a 'char *' (8 bit) with
> some sort of unicode-to-ansi convertion.
> The reason for doing this is the implementation of old C++ classes
> that I cannot easily convert to .NET ...
You should also be aware, that "StringToHGlobalAnsi" will always use CP_ACP
as conversion...
See: What is an ANSI string?
http://blog.kalmbachnet.de/?postid=19

Signature
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Jochen Kalmbach - 17 Mar 2005 20:12 GMT
Hi =?Utf-8?B?RnJhbmsgUiBFaWQ=?=,
> A) What's the simplest way to convert a 'String' to 'char *' (and the
> other way araound) ?
See: Convert from System::String* to TCHAR*/CString
http://blog.kalmbachnet.de/?postid=18

Signature
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Frank R Eid - 17 Mar 2005 20:33 GMT
> See: Convert from System::String* to TCHAR*/CString
> http://blog.kalmbachnet.de/?postid=18
Thanks. Got it.
Frank.
Ioannis Vranos - 18 Mar 2005 13:54 GMT
> Hi all;
>
> A) What's the simplest way to convert a 'String' to 'char *' (and the other
> way araound) ?
The natural candidate is wchar_t * (or better wstring). In both cases
String provides a member function ToCharArray that returns a managed,
*non-null-terminated*, wchar_t array.
You can do this for example:
#using <mscorlib.dll>
#include <string>
int main()
{
using namespace std;
using namespace System;
String *S= __gc new String("Some string");
wchar_t __pin *temp= &(S->ToCharArray())[0];
wchar_t *p=temp;
wstring s(p, p+3);
temp=0;
}
In your case where you want to copy it in a char * you can do this:
#using <mscorlib.dll>
int main()
{
using namespace System;
String *S= __gc new String("Some string");
// Unmanaged char array in the heap
char *pchar= new char[S->Length+1];
wchar_t __pin *temp= &(S->ToCharArray())[0];
for(long i=0; i<S->Length; ++i)
pchar[i]= temp[i];
pchar[S->Length]='\0';
// Unpin
temp=0;
}
The opposite is easy:
#using <mscorlib.dll>
int main()
{
using namespace System;
char sometext[]= "Some Text";
String *S= __gc new String(sometext);
}
> B) I have some C++ classes that I cannot easily convert to .NET (because
> they still are to be used in non-.NET apps). Are there any critical issues
> why i cannot use these 'as is' in my .NET app (beside of the 'new' and
> 'delete' of them to 'manually' maintain memory) ?
No problems, apart from when using them with managed types (like a
vector<Button *>). This will become easier in VC++ 2005.