Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Frank R Eid
Guest
 
Posts: n/a
#1: Nov 17 '05
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++]
Guest
 
Posts: n/a
#2: Nov 17 '05

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


"Frank R Eid" <FrankREid@discussions.microsoft.com> wrote in message
news:24ED0C7D-6001-45F4-B11B-38E17EFF512F@microsoft.com...[color=blue]
> A) What's the simplest way[/color]

Simplest is subjective. What follow is my view:
[color=blue]
> to convert a 'String' to 'char *'[/color]

#include <vcclr.h>

wchar_t __pin *pStr = PtrToStringChars(s);

Note that .Net uses 16 bit (UNICODE) characters.
[color=blue]
> (and the other way araound) ?[/color]

char __nogc *p;
System::String *s;

s = new String(p, 0, lstrlen(p));

Regards,
Will


Jochen Kalmbach
Guest
 
Posts: n/a
#3: Nov 17 '05

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


Hi =?Utf-8?B?RnJhbmsgUiBFaWQ=?=,

[color=blue]
> A) What's the simplest way to convert a 'String' to 'char *' (and the
> other way araound) ?[/color]

See: Convert from System::String* to TCHAR*/CString
http://blog.kalmbachnet.de/?postid=18

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Frank R Eid
Guest
 
Posts: n/a
#4: Nov 17 '05

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


> #include <vcclr.h>[color=blue]
>
> wchar_t __pin *pStr = PtrToStringChars(s);[/color]

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
Guest
 
Posts: n/a
#5: Nov 17 '05

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


> See: Convert from System::String* to TCHAR*/CString[color=blue]
> http://blog.kalmbachnet.de/?postid=18[/color]

Thanks. Got it.

Frank.
William DePalo [MVP VC++]
Guest
 
Posts: n/a
#6: Nov 17 '05

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


"Frank R Eid" <FrankREid@discussions.microsoft.com> wrote in message
news:D97F7641-06B1-46BE-8CBC-509F9A0E275E@microsoft.com...[color=blue]
> Thanks.[/color]

You are welcome.
[color=blue]
> 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.[/color]

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
Guest
 
Posts: n/a
#7: Nov 17 '05

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


> You have at least three more options:

Thanks again;

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

Frank

Jochen Kalmbach
Guest
 
Posts: n/a
#8: Nov 17 '05

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


Hi =?Utf-8?B?RnJhbmsgUiBFaWQ=?=,
[color=blue]
> 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 ...[/color]

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/
Ioannis Vranos
Guest
 
Posts: n/a
#9: Nov 17 '05

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


Frank R Eid wrote:
[color=blue]
> Hi all;
>
> A) What's the simplest way to convert a 'String' to 'char *' (and the other
> way araound) ?[/color]


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);
}


[color=blue]
> 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) ?[/color]


No problems, apart from when using them with managed types (like a
vector<Button *>). This will become easier in VC++ 2005.
Closed Thread