我正在尝试将 LPTSTR 变量转换为 tstring(即,unicode 应用程序中的 wstring 和 ANSI 中的字符串)。
如何执行此转换?
我的代码尝试执行转换,但它导致错误:“调试断言失败!表达式:无效的空指针”:
#ifdef UNICODE
#define tstring std::wstring
#else
#define tstring std::string
#endif
tstring TVManager::getDevicePropertyTEST(HDEVINFO hDevInfo, SP_DEVINFO_DATA deviceInfoData, DWORD flag)
{
DWORD dataT = 0;
DWORD buffersize = 0;
LPTSTR buffer = NULL;
while (!SetupDiGetDeviceRegistryProperty(hDevInfo, &deviceInfoData, flag, &dataT,
(PBYTE)buffer, buffersize, &buffersize))
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Change the buffer size.
if (buffer)
LocalFree(buffer);
buffer = (LPTSTR)LocalAlloc(LPTR, buffersize);
}
else {
// Insert error handling here.
debug_print_ex("Else happened:", buffer);
break;
}
}
tstring propertyValue = tstring(buffer); // ERROR OCCURS HERE
if (buffer)
LocalFree(buffer);
return propertyValue;
}