Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi. i want to convert type PCWSTR to char *.tnx


XML
HWND hwnd;
CComHeapPtr spszLocation;
if (FAILED(GetBrowserInfo(svar.pdispVal, &hwnd,&spszLocation))) continue;
LPCWSTR tmp=static_cast(spszLocation);
size_t wcsChars = wcslen( tmp);
MD5 md5MdFive;
unsigned char ucMdfiveExit[16];
md5MdFive.ConvertMD5(spszLocation,(unsigned long)iDataSize,ucMdfiveExit);
std::wcout << hwnd<< L" "<< static_cast<PCWSTR>(spszLocation)<< std::endl;




in the code above the first parameter of the convertmd5 is const unsigned char * but i have LPCWSTR
Posted
Updated 2-Sep-13 20:38pm
v2

 
Share this answer
 
Something like this

C++
wstring wstr( L"abcdef" );
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
char* buffer = new char[len + 1];
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0';


or to be specific to your case:

C++
PCWSTR pstr(L"abcdef");
int len = WideCharToMultiByte(CP_ACP, 0, pstr, wcslen(pstr), NULL, 0, NULL, NULL);
char* buffer = new char[len + 1];
WideCharToMultiByte(CP_ACP, 0, pstr, wcslen(pstr), buffer, len, NULL, NULL);
buffer[len] = '\0';
 
Share this answer
 
v3
Comments
Mojtaba Setoodeh 3-Sep-13 2:22am    
hi. i have a code like this:
PCWSTR tmp=static_cast<PCWSTR>(spszLocation);
now i have a value int the tmp that i need it to be const unsigned char *. but i could not convert it.
[no name] 3-Sep-13 2:25am    
Improve your question by adding the code.
Mojtaba Setoodeh 3-Sep-13 2:33am    
HWND hwnd;
CComHeapPtr<wchar> spszLocation;
if (FAILED(GetBrowserInfo(svar.pdispVal, &hwnd,&spszLocation))) continue;
LPCWSTR tmp=static_cast<lpcwstr>(spszLocation);
size_t wcsChars = wcslen( tmp);
MD5 md5MdFive;
unsigned char ucMdfiveExit[16];
md5MdFive.ConvertMD5(spszLocation,(unsigned long)iDataSize,ucMdfiveExit);
std::wcout << hwnd<< L" "<< static_cast<PCWSTR>(spszLocation)<< std::endl;
in the code above the first parameter of the convertmd5 is const unsigned char * but i have LPCWSTR
Mojtaba Setoodeh 3-Sep-13 3:27am    
tnx budy. good help

See the MSDN topic about the wcstombs function.

 
Share this answer
 
 
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