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++ / June 2006

Tip: Looking for answers? Try searching our database.

how convert CString to char *?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Neo - 07 Jun 2006 08:33 GMT
how convert CString to char *?

regards,
Mohammad Omer Nasir
Bruno van Dooren - 07 Jun 2006 09:17 GMT
> how convert CString to char *?

For VC2005
http://msdn2.microsoft.com/en-us/library/awkwbzyc.aspx

For VC2003 you'd do the same. except you won't use the 'safe' runtime
functions.
CString theString( "This is a test" );
LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
_tcscpy(lpsz, theString);
//... modify lpsz as much as you want

Signature

Kind regards,
   Bruno van Dooren
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

Tom Serface - 07 Jun 2006 16:49 GMT
You can just call GetBuffer() if you want to access the string directly.

Tom

> how convert CString to char *?
>
> regards,
> Mohammad Omer Nasir
David Wilkinson - 07 Jun 2006 17:24 GMT
> how convert CString to char *?
>
> regards,
> Mohammad Omer Nasir

Mohammad:

Very possibly, you only need a const char*, in which case you can just do

CString str("Hello world\n");
const char* s  = str;

David Wilkinson
Mihai N. - 08 Jun 2006 06:15 GMT
> how convert CString to char *?

There are already 3 answers, all good, but depends what you need.

If you need a copy that you want to change, then go with new/_tcscpy [Bruno]
If you need to change the string in place, then GetBuffer() [Tom]
If you need read-only access to the buffer, then use implicit cast [David]

But I would like to add a special case: if the application is Unicode, then
the CString is also Unicode, meaning you don't get char*, but wchar_t*.
In this case David's code will not compile.

If you really need char* even for a Unicode application,
then you need to do a conversion to the (most likely) ANSI code page.
You can use WideCharToMultiByte or the T2A conversion macro.
But you have to make sure this is what you really need, because you will
damage all characters outside the code page used for conversion.

Signature

Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email

Neo - 08 Jun 2006 09:03 GMT
thanks gays for answer.

regards,
Mohammad Omer Nasir.

> > how convert CString to char *?
>
[quoted text clipped - 19 lines]
> ------------------------------------------
> Replace _year_ with _ to get the real email

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.