LPWSTR 字符串转 TCHAR字符串
时间: 2024-01-03 13:23:04 浏览: 76
以下是将LPWSTR字符串转换为TCHAR字符串的两种方法:
方法1:使用MultiByteToWideChar和wcstombs函数进行转换
```cpp
#include <iostream>
#include <windows.h>
char* LPWSTRToTCHAR(LPWSTR lpwstr) {
int len = WideCharToMultiByte(CP_ACP, 0, lpwstr, -1, NULL, 0, NULL, NULL);
char* tchar = new char[len];
WideCharToMultiByte(CP_ACP, 0, lpwstr, -1, tchar, len, NULL, NULL);
return tchar;
}
int main() {
LPWSTR lpwstr = L"Hello World"; char* tchar = LPWSTRToTCHAR(lpwstr);
std::cout << "TCHAR string: " << tchar << std::endl;
delete[] tchar;
return 0;
}
```
方法2:使用WideCharToMultiByte函数进行转换
```cpp
#include <iostream>
#include <windows.h>
char* LPWSTRToTCHAR(LPWSTR lpwstr) {
int len = WideCharToMultiByte(CP_ACP, 0, lpwstr, -1, NULL, 0, NULL, NULL);
char* tchar = new char[len];
WideCharToMultiByte(CP_ACP, 0, lpwstr, -1, tchar, len, NULL, NULL);
return tchar;
}
int main() {
LPWSTR lpwstr = L"Hello World";
char* tchar = LPWSTRToTCHAR(lpwstr);
std::cout << "TCHAR string: " << tchar << std::endl;
delete[] tchar;
return 0;
}
```
阅读全文