C++ WIN32 程序 ,从屏幕输入 数据 然后给WCHAR 数组赋值

 1     char lingshi_sBuf[20] = { 0 };
 2     cout << "输入测试项目" << endl;
 3     cin.get(lingshi_sBuf, 20);//输入字符串,当输入是Enter键时结束输入
 4     cout << "当前项目为:" << lingshi_sBuf << endl;
 5     //获取输入缓存大小
 6     int lingshi_sBufSize = strlen(lingshi_sBuf);
 7     //获取输出缓存大小
 8     //VC++ 默认使用ANSI,故取第一个参数为CP_ACP
 9     DWORD lingshi_dBufSize = MultiByteToWideChar(CP_ACP, 0, lingshi_sBuf, lingshi_sBufSize, NULL, 0);
10     printf("需要wchar_t%u个\n", lingshi_dBufSize);
11     wchar_t* lingshi_dBuf = new wchar_t[lingshi_dBufSize];
12     wmemset(lingshi_dBuf, 0, lingshi_dBufSize);
13     //进行转换
14     int lingshi_nRet = MultiByteToWideChar(CP_ACP, 0, lingshi_sBuf, lingshi_sBufSize, lingshi_dBuf, lingshi_dBufSize);
15      
16     if (lingshi_nRet <= 0)
17     {
18         cout << "转换失败" << endl;
19         DWORD dwErr = GetLastError();
20         switch (dwErr)
21         {
22         case ERROR_INSUFFICIENT_BUFFER:
23             printf("ERROR_INSUFFICIENT_BUFFER\n");
24             break;
25         case ERROR_INVALID_FLAGS:
26             printf("ERROR_INVALID_FLAGS\n");
27             break;
28         case ERROR_INVALID_PARAMETER:
29             printf("ERROR_INVALID_PARAMETER\n");
30             break;
31         case ERROR_NO_UNICODE_TRANSLATION:
32             printf("ERROR_NO_UNICODE_TRANSLATION\n");
33             break;
34         }
35     }
36     else
37     {
38         cout << "转换成功" << endl;
39         cout << lingshi_dBuf;
40     }
41     memcpy(&g_block_data_name[0], &lingshi_dBuf[0], 2);//tick
42     memcpy(&g_block_data_name[1], &lingshi_dBuf[1], 2);//tick
43     memcpy(&g_block_data_name[2], &lingshi_dBuf[2], 2);//tick
44     memcpy(&g_block_data_name[3], &lingshi_dBuf[3], 2);//tick
45     delete lingshi_dBuf;

以上代码,应该还有内存泄漏的问题

 

posted @ 2021-08-19 10:59  wenluderen  阅读(356)  评论(2编辑  收藏  举报