Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I must convert from LPCWSTR to LPCSTR.
I need it for this code:

WFilePath                       path;
WIN32_FIND_DATAA                 fd;
HANDLE                          h;

	h = FindFirstFileA(path.GetString(), &fd); 


What I have tried:

I tried to serarch on internet but I don't find
Posted

Something like this perhaps?
C++
#include <string>

LPCSTR ToLPCSTR(LPCWSTR wstr) {
  std::wstring ws(wstr);
  return ws.c_str();
}
Be aware that it's not possible to convert every widestring character to a CStr, so you may end up with data loss.
 
Share this answer
 
Comments
Member 14594285 23-Feb-24 9:53am    
yes but I have an error using this function
Pete O'Hanlon 23-Feb-24 10:04am    
What error do you have? Saying you have an error is next to useless if we don't know what the problem is.
CPallini 24-Feb-24 4:49am    
I think that's wrong, Pete.
wstring::c_str() returns a (const wchar_t *).
Moreover, it returns a pointer to local data.
Either use all UNICODE or all ASCII, to avoid such issues. And you can call FindFirstFileW which is the UNICODE version, to make it simpler. It is generally better not to use the A or W suffixes on system calls. The compiler will choose the correct version based on your project settings. If you must use mixed character sets then you can use the WideCharToMultiByte function (stringapiset.h) - Win32 apps | Microsoft Learn[^] to convert UNICODE to ASCII.
 
Share this answer
 
Comments
Member 14594285 23-Feb-24 10:09am    
WFilePath path, patharc123;
WIN32_FIND_DATA fd;
HANDLE h;


if I use:
h = FindFirstFile(path.GetString(), &fd);

fd isn't valorised
Richard MacCutchan 23-Feb-24 10:54am    
What is WFilePath defined as?
Pete O'Hanlon 23-Feb-24 10:14am    
5 from me.
Richard MacCutchan 23-Feb-24 10:55am    
Thanks Pete.
CPallini 24-Feb-24 4:29am    
Indeed. 5.
In additional to solution 2, I can say following. In some cases conversion can be done easy with the ATL helper conversion macros which can be used inline: W2A, A2W and their variations. To use that macro you can call once in the view scope - for example at the beginning of the function or right before those macro usage another macro USES_CONVERSION. how that all work see in the next example:
USES_CONVERSION;
wchar_t wText[] = L" Unicode Text";
printf("ANSI: %s",W2A(wText));

TCHAR tText[] = _T("TCHAR Text");
printf("ANSI: %s",T2A(tText));

char aText[] = "MBCS Text";
_tcprintf(_T("TCHAR: %s"),A2T(aText));

Those macro allocate new string right in the stack which is freed while the function exist. I remember situation there I had stack overflow due that as those macro were used right in the main thread function loop, so keep that in mind.

Also in some other cases in additional to WideCharToMultiByte, or if you are not on Windows Platform you can use the function wcstombs[^] from the standard library.

And another option you can use the sprintf[^] /swprintf[^] functions with the string "%S" format, which means on Windows that passed argument should be converted depending to the function is used. For example if you use sprintf you can pass unicode text and if you use swprintf you can pass ansi text. See the next example:

// convert unicode string to the ansi string
char aText[100] = {0};
wchar_t wText[] = L"UNICODE Text";
sprintf(aText,"ANSI: %S",wText);


Regards,
Maxim
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900