How to convert CString to LPCWSTR

Discussion in 'C++' started by 月夕, Jul 5, 2009.

  1. How to convert CString to LPCWSTR
    In my code, the function need a parameter LPCWSTR
    And I find this can work

    ///////////////////////////////////////////////////////////////////////////­//////////////////////////////////////////
    CFileFind finder;
    BOOL bWorking = finder.FindFile(_T("C:\\*.bmp"));
    bWorking = finder.FindNextFile();
    SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (LPTSTR)(LPCTSTR)
    finder.GetFilePath(), 0);
    ///////////////////////////////////////////////////////////////////////////­//////////////////////////////////////////


    why we have to add this two (LPTSTR)(LPCTSTR) casting symbols,why
    this
    works
    and is there any ways more usual.


    I was confused by the numerous types in MFC, such as unicode, ANISI,
    MultibyteChar and so on
    thanks in advance
    月夕, Jul 5, 2009
    #1
    1. Advertising

  2. * 月夕:
    > How to convert CString to LPCWSTR


    Neither CString nor LPCWSTR are part of standard C++. I.e. the question is off
    topic in this group (see the FAQ for suggestions of more appropriate groups to
    post in). However, regarding the code sample below:


    > In my code, the function need a parameter LPCWSTR
    > And I find this can work
    >
    > ///////////////////////////////////////////////////////////////////////////­//////////////////////////////////////////
    > CFileFind finder;
    > BOOL bWorking = finder.FindFile(_T("C:\\*.bmp"));


    Three things wrong with the above statement:

    1. You're using a 'BOOL' type instead of standard C++ 'bool'.

    2. You're using a Hungarian prefix 'b', and the rest of the name is meaningless
    and misleading: try to train yourself in choosing meaningful names (e.g. look
    at others' code).

    3. You're using Microsoft _T, which is only meaningful if you have to use the
    MFC library in a DLL *and* support both Windows 9x and modern Windows, which
    it's almost guaranteed is not your situation.

    As a general observation, it seems that you're employing the
    monkey-see-monkey-do approach to programming, just copying and modifying code
    snippets you find without understanding any of it.

    A better (in the sense of more productive) approach is to understand things.



    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Jul 5, 2009
    #2
    1. Advertising

  3. thank you above all, that help a lot.
    I found c++ is not only a standard, but also a faith to everyone in
    weaving a longlasting software architecture *_^
    月夕, Jul 6, 2009
    #3
  4. 月夕

    Jorgen Grahn Guest

    On Mon, 06 Jul 2009 01:21:30 -0500, Paavo Helde <> wrote:
    > =?UTF-8?B?5pyI5aSV?= <> kirjutas:
    >
    >> thank you above all, that help a lot.
    >> I found c++ is not only a standard, but also a faith to everyone in
    >> weaving a longlasting software architecture *_^


    I am not sure what that means, but I am replying to this:

    > You are right, for small and dirty hacks one can use Perl, bash shell
    > script or any other handy scripting language.


    Yeah, but that doesn't mean you aren't allowed to do quick hacks in
    C++. There's nothing wrong with that[1]. And "longlasting software
    architecture" sometimes mean "wasting time thinking about The
    Architecture, when that time was clearly needed for experimentation".

    /Jorgen

    [1] Except when Perl, Python or shell script is a better fit for the
    problem. Which it often is.

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Jul 6, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    926
    Chris Uppal
    Jan 16, 2006
  2. Michael Breidenstein

    char * convert to LPCWSTR

    Michael Breidenstein, Jun 27, 2005, in forum: C++
    Replies:
    3
    Views:
    8,588
    Howard
    Jun 27, 2005
  3. Vincent RICHOMME

    map and LPCWSTR

    Vincent RICHOMME, Feb 2, 2007, in forum: C++
    Replies:
    2
    Views:
    628
    Ondra Holub
    Feb 2, 2007
  4. Replies:
    2
    Views:
    1,176
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Feb 26, 2007
  5. Mike Copeland

    C-style Strings to LPCWSTR

    Mike Copeland, May 26, 2012, in forum: C++
    Replies:
    9
    Views:
    1,145
    Victor Bazarov
    May 27, 2012
Loading...

Share This Page