Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can't cast a CString to an LPCSTR

1,995 views
Skip to first unread message

Edward Mitchell

unread,
Apr 28, 2009, 11:09:12 AM4/28/09
to
Taking code from an old project, I now get an error that I can't convert a
CString to an LPCSTR.

CString sBuf;
...T("Response from QueryInfo is:\n%s"), (LPCSTR) sBuf);

I've always been able to cast to a const string. Why the error now?
error C2440: 'type cast' : cannot convert from 'CString' to 'LPCSTR'

I'm using VS2008. LPCSTR is typedefed to const CHAR*.

Ed Mitchell
--
Edward E.L. Mitchell
12214 Lakewood Ct
Fort Myers, FL 33908
Phone: (239)415-7039
Web: www.racesail.org


Guido Franzke

unread,
Apr 28, 2009, 11:16:48 AM4/28/09
to
Try LPCTSTR:

CString sBuf;
...T("Response from QueryInfo is:\n%s"), (LPCTSTR) sBuf);

HTH


"Edward Mitchell" <EELMi...@newsgroup.nospam> schrieb im Newsbeitrag
news:eyB$LNByJH...@TK2MSFTNGP03.phx.gbl...

AliR (VC++ MVP)

unread,
Apr 28, 2009, 11:20:44 AM4/28/09
to
Is this a UNICODE project? You should be using LPCTSTR.

AliR.


"Edward Mitchell" <EELMi...@newsgroup.nospam> wrote in message
news:eyB$LNByJH...@TK2MSFTNGP03.phx.gbl...

Joseph M. Newcomer

unread,
Apr 28, 2009, 12:33:15 PM4/28/09
to
This is not surprising. Your code is incorrect. You should have written (LPCTSTR). Fix
your code. In VS2008, apps are by default Unicode, and your code makes no sense. It was
always wrong, you just finally detected that it is wrong.
joe

On Tue, 28 Apr 2009 11:09:12 -0400, "Edward Mitchell" <EELMi...@newsgroup.nospam>
wrote:

>Taking code from an old project, I now get an error that I can't convert a
>CString to an LPCSTR.
>
>CString sBuf;
>...T("Response from QueryInfo is:\n%s"), (LPCSTR) sBuf);
>
>I've always been able to cast to a const string. Why the error now?
>error C2440: 'type cast' : cannot convert from 'CString' to 'LPCSTR'
>
>I'm using VS2008. LPCSTR is typedefed to const CHAR*.
>
>Ed Mitchell

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Edward Mitchell

unread,
Apr 28, 2009, 12:35:56 PM4/28/09
to
It is a UNICODE project since I took the default. The LPCTSTR fixed the
problem.

Thanks.

Ed

"AliR (VC++ MVP)" <Al...@online.nospam> wrote in message
news:qaFJl.15517$%54....@nlpi070.nbdc.sbc.com...

Mikel

unread,
Apr 28, 2009, 12:59:10 PM4/28/09
to
On 28 abr, 18:35, "Edward Mitchell" <EELMitch...@newsgroup.nospam>
wrote:

> It is a UNICODE project since I took the default.  The LPCTSTR fixed the
> problem.
>
> Thanks.
>
> Ed
>
> "AliR (VC++ MVP)" <A...@online.nospam> wrote in messagenews:qaFJl.15517$%54....@nlpi070.nbdc.sbc.com...> Is this a UNICODE project?   You should be using LPCTSTR.
>
> > AliR.
>
> > "Edward Mitchell" <EELMitch...@newsgroup.nospam> wrote in message

> >news:eyB$LNByJH...@TK2MSFTNGP03.phx.gbl...
> >> Taking code from an old project, I now get an error that I can't convert
> >> a CString to an LPCSTR.
>
> >> CString sBuf;
> >> ...T("Response from QueryInfo is:\n%s"), (LPCSTR) sBuf);
>
> >> I've always been able to cast to a const string.  Why the error now?
> >> error C2440: 'type cast' : cannot convert from 'CString' to 'LPCSTR'
>
> >> I'm using VS2008.  LPCSTR is typedefed to const CHAR*.
>
> >> Ed Mitchell
> >> --
> >> Edward E.L. Mitchell
> >> 12214 Lakewood Ct
> >> Fort Myers, FL 33908
> >> Phone: (239)415-7039
> >> Web:www.racesail.org

Sorry if I sound rude, but did you understand why "The LPCTSTR fixed
the problem"? I say it because often, people accept the solution
without understanding it...

Leo Violette

unread,
Apr 29, 2009, 1:25:23 AM4/29/09
to
:) Reminded me of this post:
http://blogs.msdn.com/oldnewthing/archive/2009/04/16/9551818.aspx


"Mikel" <mikel...@gmail.com> wrote in message
news:8b0101e0-c92b-4066...@c18g2000prh.googlegroups.com...

Edward Mitchell

unread,
May 2, 2009, 10:46:02 AM5/2/09
to

----- Original Message -----
From: "Mikel" <mikel...@gmail.com>
Newsgroups: microsoft.public.vc.mfc
Sent: Tuesday, April 28, 2009 12:59 PM
Subject: Re: Can't cast a CString to an LPCSTR


On 28 abr, 18:35, "Edward Mitchell" <EELMitch...@newsgroup.nospam>
wrote:
> It is a UNICODE project since I took the default. The LPCTSTR fixed the
> problem.
>
> Thanks.
>
> Ed
>

...


> Sorry if I sound rude, but did you understand why "The LPCTSTR fixed
> the problem"? I say it because often, people accept the solution
> without understanding it...


I've been pretty careful to always use a _T('...") for all the strings so I
know the "T" version varies between a WCHAR and a CHAR. Since my original
code worked in one of the earlier versions of VS, I presume the default must
have changed to the WCHAR when I come to use the 2008 version.

I was motivated to search for any LPCSTRs but there was only one!

Ed


Joseph M. Newcomer

unread,
May 2, 2009, 12:24:52 PM5/2/09
to
By default, VS2005 and beyond create Unicode apps. Therefore, any code which presumes
that a CString can become a LPSTR or LPCSTR is erroneous. Always was, but it wasn't
detected as an error because the CStrings were 8-bit CStrings so the erroneous (in
general) 8-bit casting operations worked by accident.
joe

0 new messages