diff options
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/IceUtil/StringUtil.cpp | 124 | ||||
-rw-r--r-- | cpp/src/iceserviceinstall/ServiceInstaller.cpp | 17 | ||||
-rw-r--r-- | cpp/src/iceserviceinstall/ServiceInstaller.h | 5 |
3 files changed, 83 insertions, 63 deletions
diff --git a/cpp/src/IceUtil/StringUtil.cpp b/cpp/src/IceUtil/StringUtil.cpp index a5fb7a35373..ec54ef9cf9c 100644 --- a/cpp/src/IceUtil/StringUtil.cpp +++ b/cpp/src/IceUtil/StringUtil.cpp @@ -11,6 +11,10 @@ #include <IceUtil/Unicode.h> #include <cstring> +#ifdef ICE_OS_WINRT +#include <IceUtil/ScopedArray.h> +#endif + using namespace std; using namespace IceUtil; @@ -496,67 +500,79 @@ IceUtilInternal::errorToString(int error, LPCVOID source) { if(error < WSABASEERR) { -#ifndef ICE_OS_WINRT - LPVOID lpMsgBuf = 0; -#else - int size = 256; - auto_ptr<BYTE> lpMsgBuf = auto_ptr<BYTE>(new BYTE[size]); -#endif - DWORD ok = 0; -#ifdef ICE_OS_WINRT - while(true) - { -#endif - ok = FormatMessageW( -#ifndef ICE_OS_WINRT - FORMAT_MESSAGE_ALLOCATE_BUFFER | -#endif - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS | - (source != NULL ? FORMAT_MESSAGE_FROM_HMODULE : 0), - source, - error, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language -#ifndef ICE_OS_WINRT - (LPWSTR)&lpMsgBuf, -#else - (LPWSTR)lpMsgBuf.get(), -#endif - 0, - NULL); #ifdef ICE_OS_WINRT - if(!ok && size < 65536) - { - DWORD err = GetLastError(); - if(err == ERROR_INSUFFICIENT_BUFFER) - { - size *= 4; - size = max(size, 65536); - lpMsgBuf = auto_ptr<BYTE>(new BYTE[size]); - continue; - } - } - break; - } -#endif - if(ok) + int size = 256; + IceUtil::ScopedArray<wchar_t> lpMsgBuf(new wchar_t[size]); + + DWORD stored = 0; + + while(stored == 0) { -#ifndef ICE_OS_WINRT - LPWSTR msg = (LPWSTR)lpMsgBuf; + stored = FormatMessageW( + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS | + (source != NULL ? FORMAT_MESSAGE_FROM_HMODULE : 0), + source, + error, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + lpMsgBuf.get(), + size, + NULL); + + if(stored == 0) + { + DWORD err = GetLastError(); + if(err == ERROR_INSUFFICIENT_BUFFER) + { + if(size == 65536) + { + break; // already at the max size + } + else + { + size *= 4; + size = max(size, 65536); + lpMsgBuf.reset(new wchar_t[size]); + } + } + else + { + break; + } + } + } + + LPWSTR msg = lpMsgBuf.get(); + #else - LPWSTR msg = (LPWSTR)lpMsgBuf.get(); + LPWSTR msg = 0; + + DWORD stored = FormatMessageW( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS | + (source != NULL ? FORMAT_MESSAGE_FROM_HMODULE : 0), + source, + error, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + reinterpret_cast<LPWSTR>(&msg), + 0, + NULL); #endif - assert(msg && wcslen((const wchar_t*)msg) > 0); - wstring result = (const wchar_t*)msg; - if(result[result.length() - 1] == '\n') + + if(stored > 0) + { + assert(msg && wcslen(msg) > 0); + wstring result = msg; + if(result[result.length() - 1] == L'\n') { result = result.substr(0, result.length() - 2); } #ifndef ICE_OS_WINRT - if(lpMsgBuf) + if(msg) { - LocalFree(lpMsgBuf); + LocalFree(msg); } #endif return IceUtil::wstringToString(result); @@ -564,9 +580,9 @@ IceUtilInternal::errorToString(int error, LPCVOID source) else { #ifndef ICE_OS_WINRT - if(lpMsgBuf) + if(msg) { - LocalFree(lpMsgBuf); + LocalFree(msg); } #endif ostringstream os; @@ -574,7 +590,7 @@ IceUtilInternal::errorToString(int error, LPCVOID source) return os.str(); } } - + switch(error) { case WSAEINTR: diff --git a/cpp/src/iceserviceinstall/ServiceInstaller.cpp b/cpp/src/iceserviceinstall/ServiceInstaller.cpp index 0eb36071ddb..7fddc88f59c 100644 --- a/cpp/src/iceserviceinstall/ServiceInstaller.cpp +++ b/cpp/src/iceserviceinstall/ServiceInstaller.cpp @@ -407,25 +407,26 @@ IceServiceInstaller::initializeSid(const string& name) { { DWORD sidSize = 32; - _sid = auto_ptr<SID>(new SID[sidSize]); + _sidBuffer.reset(new IceUtil::Byte[sidSize]); DWORD domainNameSize = 32; - auto_ptr<wchar_t> domainName(new wchar_t[domainNameSize]); + IceUtil::ScopedArray<wchar_t> domainName(new wchar_t[domainNameSize]); SID_NAME_USE nameUse; - while(LookupAccountNameW(0, IceUtil::stringToWstring(name).c_str(), _sid.get(), &sidSize, domainName.get(), + while(LookupAccountNameW(0, IceUtil::stringToWstring(name).c_str(), _sidBuffer.get(), &sidSize, domainName.get(), &domainNameSize, &nameUse) == false) { DWORD res = GetLastError(); if(res == ERROR_INSUFFICIENT_BUFFER) { - _sid = auto_ptr<SID>(new SID[sidSize]); - domainName = auto_ptr<wchar_t>(new wchar_t[domainNameSize]); + _sidBuffer.reset(new IceUtil::Byte[sidSize]); + domainName.reset(new wchar_t[domainNameSize]); continue; } throw "Could not retrieve Security ID for " + name + ": " + IceUtilInternal::errorToString(res); } + _sid = reinterpret_cast<SID*>(_sidBuffer.get()); } // @@ -451,7 +452,7 @@ IceServiceInstaller::initializeSid(const string& name) DWORD domainLen = 1024; SID_NAME_USE nameUse; - if(LookupAccountSidW(0, _sid.get(), accountName, &accountNameLen, domainName, &domainLen, &nameUse) == false) + if(LookupAccountSidW(0, _sid, accountName, &accountNameLen, domainName, &domainLen, &nameUse) == false) { DWORD res = GetLastError(); throw "Could not retrieve full account name for " + name + ": " + IceUtilInternal::errorToString(res); @@ -464,7 +465,7 @@ IceServiceInstaller::initializeSid(const string& name) { Trace trace(_communicator->getLogger(), "IceServiceInstaller"); wchar_t* sidString = 0; - ConvertSidToStringSidW(_sid.get(), &sidString); + ConvertSidToStringSidW(_sid, &sidString); trace << "SID: " << IceUtil::wstringToString(sidString) << "; "; LocalFree(sidString); trace << "Full name: " << _sidName; @@ -521,7 +522,7 @@ IceServiceInstaller::grantPermissions(const string& path, SE_OBJECT_TYPE type, b // Now check if _sid can read this file/dir/key // TRUSTEE_W trustee; - BuildTrusteeWithSidW(&trustee, _sid.get()); + BuildTrusteeWithSidW(&trustee, _sid); ACCESS_MASK accessMask = 0; res = GetEffectiveRightsFromAclW(acl, &trustee, &accessMask); diff --git a/cpp/src/iceserviceinstall/ServiceInstaller.h b/cpp/src/iceserviceinstall/ServiceInstaller.h index b061276e776..b59cf0e2d9a 100644 --- a/cpp/src/iceserviceinstall/ServiceInstaller.h +++ b/cpp/src/iceserviceinstall/ServiceInstaller.h @@ -11,6 +11,7 @@ #define ICE_SERVICE_INSTALLER_H #include <Ice/Ice.h> +#include <IceUtil/IceUtil.h> #include <AccCtrl.h> class IceServiceInstaller @@ -62,7 +63,9 @@ private: std::string _nodeName; std::string _glacier2InstanceName; - std::auto_ptr<SID> _sid; + SID* _sid; + IceUtil::ScopedArray<IceUtil::Byte> _sidBuffer; + std::string _sidName; bool _debug; |