VC++: .NET 'String' to 'char *' and non-.NET Classes

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

Guest

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 said:
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
 
#include said:
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.
 
Frank R Eid said:

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
 
You have at least three more options:

Thanks again;

Going for the 'Marshall::StringToHGlobalAnsi()' sulution.

Frank
 
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

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Frank said:
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= temp;

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.
 
Back
Top