Fix crash formatting error messages on non-English systems, because vssprintf was returning -1.

master
Glenn Maynard 5 years ago
parent aebf306484
commit 19b27cd333
  1. 42
      sdk/Windows/Helpers.cpp
  2. 4
      sdk/Windows/Helpers.h
  3. 6
      sdk/Windows/SMXDeviceSearch.cpp

@ -15,6 +15,11 @@ void SMX::Log(string s)
g_LogCallback(s);
}
void SMX::Log(wstring s)
{
Log(WideStringToUTF8(s));
}
void SMX::SetLogCallback(function<void(const string &log)> callback)
{
g_LogCallback = callback;
@ -69,6 +74,8 @@ wstring SMX::GetErrorString(int err)
string SMX::vssprintf(const char *szFormat, va_list argList)
{
int iChars = vsnprintf(NULL, 0, szFormat, argList);
if(iChars == -1)
return string("Error formatting string: ") + szFormat;
string sStr;
sStr.resize(iChars+1);
@ -85,6 +92,27 @@ string SMX::ssprintf(const char *fmt, ...)
return vssprintf(fmt, va);
}
wstring SMX::wvssprintf(const wchar_t *szFormat, va_list argList)
{
int iChars = _vsnwprintf(NULL, 0, szFormat, argList);
if(iChars == -1)
return wstring(L"Error formatting string: ") + szFormat;
wstring sStr;
sStr.resize(iChars+1);
_vsnwprintf((wchar_t *) sStr.data(), iChars+1, szFormat, argList);
sStr.resize(iChars);
return sStr;
}
wstring SMX::wssprintf(const wchar_t *fmt, ...)
{
va_list va;
va_start(va, fmt);
return wvssprintf(fmt, va);
}
string SMX::BinaryToHex(const void *pData_, int iNumBytes)
{
const unsigned char *pData = (const unsigned char *) pData_;
@ -218,6 +246,20 @@ void SMX::GenerateRandom(void *pOut, int iSize)
throw exception("CryptReleaseContext error");
}
string SMX::WideStringToUTF8(wstring s)
{
if(s.empty())
return "";
int iBytes = WideCharToMultiByte( CP_ACP, 0, s.data(), s.size(), NULL, 0, NULL, FALSE );
string ret;
ret.resize(iBytes);
WideCharToMultiByte( CP_ACP, 0, s.data(), s.size(), (char *) ret.data(), iBytes, NULL, FALSE );
return ret;
}
const char *SMX::CreateError(string error)
{
// Store the string in a static so it doesn't get deallocated.

@ -12,6 +12,7 @@ using namespace std;
namespace SMX
{
void Log(string s);
void Log(wstring s);
// Set a function to receive logs written by SMX::Log. By default, logs are written
// to stdout.
@ -22,11 +23,14 @@ void StripCrnl(wstring &s);
wstring GetErrorString(int err);
string vssprintf(const char *szFormat, va_list argList);
string ssprintf(const char *fmt, ...);
wstring wvssprintf(const wchar_t *szFormat, va_list argList);
wstring wssprintf(const wchar_t *fmt, ...);
string BinaryToHex(const void *pData_, int iNumBytes);
string BinaryToHex(const string &sString);
bool GetRandomBytes(void *pData, int iBytes);
double GetMonotonicTime();
void GenerateRandom(void *pOut, int iSize);
string WideStringToUTF8(wstring s);
// Create a char* string that will be valid until the next call to CreateError.
// This is used to return error messages to the caller.

@ -37,7 +37,7 @@ static set<wstring> GetAllHIDDevicePaths(wstring &error)
int iError = GetLastError();
if(iError != ERROR_INSUFFICIENT_BUFFER)
{
Log(ssprintf("SetupDiGetDeviceInterfaceDetail failed: %ls", GetErrorString(iError).c_str()));
Log(wssprintf(L"SetupDiGetDeviceInterfaceDetail failed: %ls", GetErrorString(iError).c_str()));
continue;
}
}
@ -50,7 +50,7 @@ static set<wstring> GetAllHIDDevicePaths(wstring &error)
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
if(!SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, &DeviceInterfaceData, DeviceInterfaceDetailData, iSize, NULL, &DeviceInfoData))
{
Log(ssprintf("SetupDiGetDeviceInterfaceDetail failed: %ls", GetErrorString(GetLastError()).c_str()));
Log(wssprintf(L"SetupDiGetDeviceInterfaceDetail failed: %ls", GetErrorString(GetLastError()).c_str()));
continue;
}
@ -75,7 +75,7 @@ static shared_ptr<AutoCloseHandle> OpenUSBDevice(LPCTSTR DevicePath, wstring &er
if(OpenDevice == INVALID_HANDLE_VALUE)
{
// Many unrelated devices will fail to open, so don't return this as an error.
Log(ssprintf("Error opening device %ls: %ls", DevicePath, GetErrorString(GetLastError()).c_str()));
Log(wssprintf(L"Error opening device %ls: %ls", DevicePath, GetErrorString(GetLastError()).c_str()));
return nullptr;
}

Loading…
Cancel
Save