From: David Webber on

"ma" <ma(a)nowhere.com> wrote in message
news:%230$KaAm5GHA.3452(a)TK2MSFTNGP05.phx.gbl...

> Hello,
> How can I convert a cstring into a constant char * correctly?

Think of a CString as a big black box with an ordinary nul-terminated string
of TCHARs inside it, as well as any methods it cares to use to manage the
content.

You can get a const pointer to the contents (LPCTSTR) and use it, but you
must not attempt to change the content, or the other bits of Cstring which
manage it will get out of synch.

CString s = ....;
LPCTSTR psz = s;

is fine.

GetBuffer() lets you grab the insides out of the CString, mess about with
it, and put it back. But you mustn't use the other CString methods until
you have put it all back together again, or the Cstring would become
confused. I don't really like it, because if you have to use it you are
essentially admitting that CString wasn't really what you wanted to use
anyway. And in hundreds of thousands of lines of code I haven't really
needed it once.

Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm


From: ma on
Hello,
I am writing a program to test some other code so I am not interested in
supporting UNICODE but I like to write old style C++ code.
I wrote the following code:

CFileDialog FileDlg(TRUE);

if (FileDlg.DoModal()!=IDOK){ // no file is selected

return;

};

OpenFile(FileDlg.GetPathName().GetString());



but I have the following error:

cannot convert from 'const wchar_t *' to 'char *'

apparently, I am using UNICODE when I don't need it. What should I do?



A more important question: If I want to support UNICODE and ANSI and I want
to write a function to accept char * how should I define it?



Best regards




"David Wilkinson" <no-reply(a)effisols.com> wrote in message
news:O0G%23Zbm5GHA.3592(a)TK2MSFTNGP05.phx.gbl...
> ma wrote:
>
>> Hello,
>> How can I convert a cstring into a constant char * correctly? I used
>> the following code but it is not working properly.
>>
>> (const char *)FileDlg.GetPathName().GetBuffer()
>>
>>
>>
>> what is the correct way of doing this?
>>
>>
>>
>> Regards
>>
>
> ma:
>
> Before you ask this question you should be sure you understand the issues
> related to CString in an ANSI or UNICODE build. If you do understand them,
> then you will probably know the answer to your question.
>
> David Wilkinson


From: David Webber on

"ma" <ma(a)nowhere.com> wrote in message
news:OUIB4hm5GHA.4832(a)TK2MSFTNGP06.phx.gbl...

> apparently, I am using UNICODE when I don't need it. What should I do?

Well you could change the UNICODE and _UNICODE definitions in your projct
file(s) but I would advise strongly against it. The answer is to get used
to Unicode.

VC++2003 created projects by default without Unicode
VC++2005 creates them by default WITH Unicode.

This reflects that fact that Unicode is taking over - quite rightly. The
world is getting smaller and continuing with character set which can only be
used to write English and a very few other languages is becoming less and
less tenable.

So instead of

char, char *, LPSTR, LPCSTR

use

TCHAR, TCHAR *, LPTSTR, and LPCTSTR

and instead of

"my string"

use

_T("my string")

and then for all the string APIs like strcpy, sprintf, atoi, etc, get used
to looking them up until using the versions (with slightly different names)
which take TCHAR strings becomes second nature.

> A more important question: If I want to support UNICODE and ANSI and I
> want to write a function to accept char * how should I define it?

Define it to accept TCHAR *.

TCHAR and friends are a (really rather clever) way Microsoft have devised to
support the transition to unicode. TCHAR is wchar_t or char according to
whether you are compiling for Unicode or the old system. Ultimately it
will be forgotton, I guess and instead of

const char *szString = "my string";

we'll all write

const wchar_t *szString = L"my string";

but for now

const TCHAR *szString = _T("my string");

is the best way to go.

Dave

--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm




From: eit on

> ma wrote:
> but I have the following error:
> cannot convert from 'const wchar_t *' to 'char *'

#include"atlconv.h"

Use
#ifdef UNICODE
OpenFile(A2W(FileDlg.GetPathName().GetBuffer(0)));
#else
OpenFile(FileDlg.GetPathName().GetBuffer(0));
#endif

Thanks
Sunil Singh


"Nobody is prefect and remain lives forever here except the Universal
Truth"

First  |  Prev  | 
Pages: 1 2
Prev: Manifest file
Next: Custom drawn list control