From c36ea074d80f61c789e69fc52dbd0869bac69a56 Mon Sep 17 00:00:00 2001 From: Mario Liebisch Date: Thu, 25 Sep 2014 13:02:29 +0200 Subject: [PATCH] Joystick fixes/update for non-MSVC compilers * This fixes building on non-MSVC compilers for Windows since they don't know `_tcsnlen()`. * Changed logic so SFML tries to retrieve the paths from the user key first, machine key second (typical behavior for most registry settings). * Removed the registry key length checks since that check is performed inside`RegOpenKeyEx()` anyway and cut-off keys might point to the wrong keys. * Updated the error string retrieval to properly handle errors. --- src/SFML/Window/Win32/JoystickImpl.cpp | 33 +++++++++++--------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/SFML/Window/Win32/JoystickImpl.cpp b/src/SFML/Window/Win32/JoystickImpl.cpp index 32c9c742..22bb467c 100644 --- a/src/SFML/Window/Win32/JoystickImpl.cpp +++ b/src/SFML/Window/Win32/JoystickImpl.cpp @@ -51,10 +51,14 @@ namespace // Get a system error string from an error code std::string getErrorString(DWORD error) { - TCHAR errorMessage[256]; - DWORD messageLength = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errorMessage, 256, NULL); - errorMessage[std::min(messageLength, 255)] = 0; - return sf::String(errorMessage).toAnsiString(); + PTCHAR buffer; + + if (FormatMessage(FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, reinterpret_cast(&buffer), 0, NULL) == 0) + return "Unknown error."; + + sf::String message = buffer; + LocalFree(buffer); + return message.toAnsiString(); } // Get the joystick's name @@ -69,20 +73,17 @@ namespace std::basic_string subkey; subkey = REGSTR_PATH_JOYCONFIG; - subkey += TEXT("\\"); + subkey += TEXT('\\'); subkey += caps.szRegKey; - subkey += TEXT("\\"); + subkey += TEXT('\\'); subkey += REGSTR_KEY_JOYCURR; - if (subkey.size() > 255) - subkey.erase(255); - - rootKey = HKEY_LOCAL_MACHINE; + rootKey = HKEY_CURRENT_USER; result = RegOpenKeyEx(rootKey, subkey.c_str(), 0, KEY_READ, ¤tKey); if (result != ERROR_SUCCESS) { - rootKey = HKEY_CURRENT_USER; + rootKey = HKEY_LOCAL_MACHINE; result = RegOpenKeyEx(rootKey, subkey.c_str(), 0, KEY_READ, ¤tKey); if (result != ERROR_SUCCESS) @@ -99,9 +100,6 @@ namespace subkey += indexString.str(); subkey += REGSTR_VAL_JOYOEMNAME; - if (subkey.size() > 255) - subkey.erase(255); - TCHAR keyData[256]; DWORD keyDataSize = sizeof(keyData); @@ -115,12 +113,9 @@ namespace } subkey = REGSTR_PATH_JOYOEM; - subkey += TEXT("\\"); + subkey += TEXT('\\'); subkey.append(keyData, keyDataSize / sizeof(TCHAR)); - if (subkey.size() > 255) - subkey.erase(255); - result = RegOpenKeyEx(rootKey, subkey.c_str(), 0, KEY_READ, ¤tKey); if (result != ERROR_SUCCESS) @@ -140,7 +135,7 @@ namespace return joystickDescription; } - keyData[_tcsnlen(keyData, 255)] = 0; + keyData[255] = TEXT('\0'); // Ensure null terminator in case the data is too long. joystickDescription = keyData; return joystickDescription;