diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2017-09-20 20:20:44 +0300 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2017-09-22 20:53:30 +0200 |
commit | eef4c133e9649ebd690918bd7b83c2d5dc0dfcff (patch) | |
tree | 1a8d084c5b16056a15258770a05b9cd2d53a40bc /sw/source/ui/vba/vbasystem.cxx | |
parent | bb406680cebd6fa1e1bdb9e2de430cd9a1f44da0 (diff) |
Windows: avoid dependence on UNICODE define; prefer W functions
Change-Id: I95b90128e93f0d88ed73601bcc5a7ca9279d4cf1
Reviewed-on: https://gerrit.libreoffice.org/42560
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'sw/source/ui/vba/vbasystem.cxx')
-rw-r--r-- | sw/source/ui/vba/vbasystem.cxx | 44 |
1 files changed, 24 insertions, 20 deletions
diff --git a/sw/source/ui/vba/vbasystem.cxx b/sw/source/ui/vba/vbasystem.cxx index c98df33e3595..7781f6660c65 100644 --- a/sw/source/ui/vba/vbasystem.cxx +++ b/sw/source/ui/vba/vbasystem.cxx @@ -29,6 +29,9 @@ #pragma warning (push, 1) #pragma warning (disable: 4005) #endif +#if !defined WINVER +# define WINVER 0x0400 +#endif #if !defined WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN #endif @@ -36,7 +39,6 @@ #if defined _MSC_VER #pragma warning (pop) #endif -#include <tchar.h> #endif using namespace ::ooo::vba; @@ -97,7 +99,7 @@ uno::Any PrivateProfileStringListener::getValueEvent() } else { - // get key/value from windows register + // get key/value from Windows registry #ifdef _WIN32 HKEY hBaseKey = nullptr; OString sSubKey; @@ -105,21 +107,23 @@ uno::Any PrivateProfileStringListener::getValueEvent() if( hBaseKey != nullptr ) { HKEY hKey = nullptr; - LONG lResult; - LPCTSTR lpSubKey = TEXT( sSubKey.getStr()); - TCHAR szBuffer[1024]; - DWORD cbData = sizeof( szBuffer ); - lResult = RegOpenKeyEx( hBaseKey, lpSubKey, 0, KEY_QUERY_VALUE, &hKey ); + LPCSTR lpSubKey = sSubKey.getStr(); + // We use RegOpenKeyExA here for convenience, because we already have subkey name as 8-bit string + LONG lResult = RegOpenKeyExA( hBaseKey, lpSubKey, 0, KEY_QUERY_VALUE, &hKey ); if( ERROR_SUCCESS == lResult ) { - LPCTSTR lpValueName = TEXT(maKey.getStr()); - lResult = RegQueryValueEx( hKey, lpValueName, nullptr, nullptr, reinterpret_cast<LPBYTE>(szBuffer), &cbData ); + OUString sUValName = OStringToOUString(maKey, RTL_TEXTENCODING_DONTKNOW); + LPCWSTR lpValueName = SAL_W(sUValName.getStr()); + WCHAR szBuffer[1024]; + DWORD cbData = sizeof(szBuffer); + lResult = RegQueryValueExW( hKey, lpValueName, nullptr, nullptr, reinterpret_cast<LPBYTE>(szBuffer), &cbData ); RegCloseKey( hKey ); - sValue = OUString::createFromAscii(szBuffer); + // https://msdn.microsoft.com/en-us/ms724911 mentions that + // "the string may not have been stored with the proper terminating null characters" + szBuffer[std::min(size_t(cbData / sizeof(szBuffer[0])), SAL_N_ELEMENTS(szBuffer)-1)] = 0; + sValue = SAL_U(szBuffer); } } - - return uno::makeAny( sValue ); #else throw uno::RuntimeException("Only support on Windows" ); #endif @@ -142,7 +146,7 @@ void PrivateProfileStringListener::setValueEvent( const css::uno::Any& value ) } else { - //set value into windows register + //set value into Windows registry #ifdef _WIN32 HKEY hBaseKey = nullptr; OString sSubKey; @@ -150,15 +154,15 @@ void PrivateProfileStringListener::setValueEvent( const css::uno::Any& value ) if( hBaseKey != nullptr ) { HKEY hKey = nullptr; - LONG lResult; - LPCTSTR lpSubKey = TEXT( sSubKey.getStr()); - lResult = RegCreateKeyEx( hBaseKey, lpSubKey, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nullptr, &hKey, nullptr ); + LPCSTR lpSubKey = sSubKey.getStr(); + // We use RegCreateKeyExA here for convenience, because we already have subkey name as 8-bit string + LONG lResult = RegCreateKeyExA( hBaseKey, lpSubKey, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nullptr, &hKey, nullptr ); if( ERROR_SUCCESS == lResult ) { - OString aUTF8Value = OUStringToOString( aValue, RTL_TEXTENCODING_UTF8 ); - DWORD cbData = sizeof(TCHAR) * (_tcslen(aUTF8Value.getStr()) + 1); - LPCTSTR lpValueName = TEXT(maKey.getStr()); - lResult = RegSetValueEx( hKey, lpValueName, 0 /* Reserved */, REG_SZ, reinterpret_cast<BYTE const *>(aUTF8Value.getStr()), cbData ); + DWORD cbData = sizeof(WCHAR) * (aValue.getLength() + 1); + OUString sUValName = OStringToOUString(maKey, RTL_TEXTENCODING_DONTKNOW); + LPCWSTR lpValueName = SAL_W(sUValName.getStr()); + lResult = RegSetValueExW( hKey, lpValueName, 0 /* Reserved */, REG_SZ, reinterpret_cast<BYTE const *>(aValue.getStr()), cbData ); RegCloseKey( hKey ); } } |