Fix crash formatting error messages on non-English systems, because vssprintf was returning -1.
This commit is contained in:
parent
aebf306484
commit
19b27cd333
@ -15,6 +15,11 @@ void SMX::Log(string s)
|
|||||||
g_LogCallback(s);
|
g_LogCallback(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SMX::Log(wstring s)
|
||||||
|
{
|
||||||
|
Log(WideStringToUTF8(s));
|
||||||
|
}
|
||||||
|
|
||||||
void SMX::SetLogCallback(function<void(const string &log)> callback)
|
void SMX::SetLogCallback(function<void(const string &log)> callback)
|
||||||
{
|
{
|
||||||
g_LogCallback = callback;
|
g_LogCallback = callback;
|
||||||
@ -69,6 +74,8 @@ wstring SMX::GetErrorString(int err)
|
|||||||
string SMX::vssprintf(const char *szFormat, va_list argList)
|
string SMX::vssprintf(const char *szFormat, va_list argList)
|
||||||
{
|
{
|
||||||
int iChars = vsnprintf(NULL, 0, szFormat, argList);
|
int iChars = vsnprintf(NULL, 0, szFormat, argList);
|
||||||
|
if(iChars == -1)
|
||||||
|
return string("Error formatting string: ") + szFormat;
|
||||||
|
|
||||||
string sStr;
|
string sStr;
|
||||||
sStr.resize(iChars+1);
|
sStr.resize(iChars+1);
|
||||||
@ -85,6 +92,27 @@ string SMX::ssprintf(const char *fmt, ...)
|
|||||||
return vssprintf(fmt, va);
|
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)
|
string SMX::BinaryToHex(const void *pData_, int iNumBytes)
|
||||||
{
|
{
|
||||||
const unsigned char *pData = (const unsigned char *) pData_;
|
const unsigned char *pData = (const unsigned char *) pData_;
|
||||||
@ -218,6 +246,20 @@ void SMX::GenerateRandom(void *pOut, int iSize)
|
|||||||
throw exception("CryptReleaseContext error");
|
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)
|
const char *SMX::CreateError(string error)
|
||||||
{
|
{
|
||||||
// Store the string in a static so it doesn't get deallocated.
|
// Store the string in a static so it doesn't get deallocated.
|
||||||
|
@ -12,6 +12,7 @@ using namespace std;
|
|||||||
namespace SMX
|
namespace SMX
|
||||||
{
|
{
|
||||||
void Log(string s);
|
void Log(string s);
|
||||||
|
void Log(wstring s);
|
||||||
|
|
||||||
// Set a function to receive logs written by SMX::Log. By default, logs are written
|
// Set a function to receive logs written by SMX::Log. By default, logs are written
|
||||||
// to stdout.
|
// to stdout.
|
||||||
@ -22,11 +23,14 @@ void StripCrnl(wstring &s);
|
|||||||
wstring GetErrorString(int err);
|
wstring GetErrorString(int err);
|
||||||
string vssprintf(const char *szFormat, va_list argList);
|
string vssprintf(const char *szFormat, va_list argList);
|
||||||
string ssprintf(const char *fmt, ...);
|
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 void *pData_, int iNumBytes);
|
||||||
string BinaryToHex(const string &sString);
|
string BinaryToHex(const string &sString);
|
||||||
bool GetRandomBytes(void *pData, int iBytes);
|
bool GetRandomBytes(void *pData, int iBytes);
|
||||||
double GetMonotonicTime();
|
double GetMonotonicTime();
|
||||||
void GenerateRandom(void *pOut, int iSize);
|
void GenerateRandom(void *pOut, int iSize);
|
||||||
|
string WideStringToUTF8(wstring s);
|
||||||
|
|
||||||
// Create a char* string that will be valid until the next call to CreateError.
|
// Create a char* string that will be valid until the next call to CreateError.
|
||||||
// This is used to return error messages to the caller.
|
// This is used to return error messages to the caller.
|
||||||
|
@ -37,7 +37,7 @@ static set<wstring> GetAllHIDDevicePaths(wstring &error)
|
|||||||
int iError = GetLastError();
|
int iError = GetLastError();
|
||||||
if(iError != ERROR_INSUFFICIENT_BUFFER)
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -50,7 +50,7 @@ static set<wstring> GetAllHIDDevicePaths(wstring &error)
|
|||||||
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
|
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
|
||||||
if(!SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, &DeviceInterfaceData, DeviceInterfaceDetailData, iSize, NULL, &DeviceInfoData))
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,8 +74,8 @@ static shared_ptr<AutoCloseHandle> OpenUSBDevice(LPCTSTR DevicePath, wstring &er
|
|||||||
|
|
||||||
if(OpenDevice == INVALID_HANDLE_VALUE)
|
if(OpenDevice == INVALID_HANDLE_VALUE)
|
||||||
{
|
{
|
||||||
// Many unrelated devices will fail to open, so don't return this as an error.
|
// 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;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user