How to convert CString to LPCWSTR

Æ

月夕

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
 
A

Alf P. Steinbach

* 月夕:
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
 
Æ

月夕

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 *_^
 
J

Jorgen Grahn

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top