一.
1 LPWSTR ConvertLPCSTRToLPWSTR (char* pCstring)
2 {
3 LPWSTR pszOut = NULL;
4 if (pCstring != NULL)
5 {
6 int nInputStrLen = strlen (pCstring);
7 // Double NULL Termination
8 int nOutputStrLen = MultiByteToWideChar(CP_ACP, 0, pCstring, nInputStrLen, NULL, 0) + 2;
9 pszOut = new WCHAR [nOutputStrLen];
10 if (pszOut)
11 {
12 memset (pszOut, 0x00, sizeof (WCHAR)*nOutputStrLen);
13 MultiByteToWideChar (CP_ACP, 0, pCstring, nInputStrLen, pszOut, nInputStrLen);
14 }
15 }
16 return pszOut;
17 }
18
After using this method do remember to delete the allocated string.
eg:
char * str = new char[strlen("ASHOK") + 1];
strcpy(str, "ASHOK");
WCHAR * pwStr = ConvertLPCSTRToLPWSTR(str);
//Use this pwStr
......
.....
//delete the wide char pointer.
delete []pwStr;
二.
1 #define xmalloc(s) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (s))
2 #define xfree(p) HeapFree (GetProcessHeap(), 0, (p))
3
4
5
6 wchar_t *str2wstrptr(const char *pStr)
7 {
8 PSTR pMultiByteStr = (PSTR)pStr;
9 PWSTR pWideCharStr;
10 int nLenOfWideCharStr;
11 // 利用API函数MultiByteToWideChar()来把a转化成unicode字符
12 nLenOfWideCharStr = MultiByteToWideChar( CP_ACP, 0, pMultiByteStr, -1, NULL, 0);
13 pWideCharStr = (PWSTR)xmalloc(nLenOfWideCharStr * sizeof(WCHAR));
14 MultiByteToWideChar( CP_ACP, 0, pMultiByteStr, -1, pWideCharStr, nLenOfWideCharStr );
15 return pWideCharStr;
16 }
17
eg:
wchar_t* ptitle= str2wstrptr("字符串转化");
xfree(ptitle);