Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frank R Eid - 17 Mar 2005 19:27 GMT
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.