I am attempting to convert a LPTSTR variable to tstring(ie, wstring in a unicode application and string in ANSI).
How do I perform this conversion?
My code attempts to perform the conversion but it causes the error: “Debug Assertion Failed! Expression: invalid null pointer”:
#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;
}
You’re passing a null pointer to
std::basic_string<>‘s constructor – no good. Assuming you just want an empty string ifbufferis null, then do the following: