jkgame

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  1 随笔 :: 0 文章 :: 0 评论 :: 0 Trackbacks

2009年5月21日 #

一.

 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, 0x00sizeof (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);






posted @ 2009-05-21 07:23 JK 阅读(1637) | 评论 (0)编辑 收藏

仅列出标题