diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2021-12-18 09:51:38 +0100 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2021-12-18 13:20:47 +0100 |
commit | b329502b23c59ff605ff02c3054f1bd2599dde08 (patch) | |
tree | 92ff2aac73f150e42f18b2c57549b9b755653b8f /extensions | |
parent | 6e37b340874e7f8db0d346a4755b5fc30f2a08d6 (diff) |
Simplify BSTR RAII
Change-Id: I0eaff245a4f5dec9c86ab2c04a329e0d2a7edca5
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/126984
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'extensions')
-rw-r--r-- | extensions/source/config/WinUserInfo/WinUserInfoBe.cxx | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/extensions/source/config/WinUserInfo/WinUserInfoBe.cxx b/extensions/source/config/WinUserInfo/WinUserInfoBe.cxx index a54a6b467bc6..a412769ee639 100644 --- a/extensions/source/config/WinUserInfo/WinUserInfoBe.cxx +++ b/extensions/source/config/WinUserInfo/WinUserInfoBe.cxx @@ -103,15 +103,14 @@ public: auto pADsys = sal::systools::COMReference<IADsADSystemInfo>().CoCreateInstance( CLSID_ADSystemInfo, nullptr, CLSCTX_INPROC_SERVER); - BSTR sUserDN; - HRESULT hr = pADsys->get_UserName(&sUserDN); + smartBSTR sUserDN; + HRESULT hr = pADsys->get_UserName(&sUserDN.ptr); if (FAILED(hr)) throw sal::systools::ComError("get_UserName failed", hr); - BSTRGuard aUserNameGuard(sUserDN, SysFreeString); // If this user is an AD user, then without an active connection to the domain, all the // above will succeed, and m_sUserDN will be correctly initialized, but the following // call to ADsGetObject will fail, and we will attempt reading cached values. - m_sUserDN = o3tl::toU(sUserDN); + m_sUserDN = o3tl::toU(sUserDN.ptr); OUString sLdapUserDN = "LDAP://" + m_sUserDN; sal::systools::COMReference<IADsUser> pUser; hr = ADsGetObject(o3tl::toW(sLdapUserDN.getStr()), IID_IADsUser, @@ -160,26 +159,29 @@ public: virtual OUString GetMail() override { return m_aMap[mail]; } private: - typedef std::unique_ptr<OLECHAR, decltype(&SysFreeString)> BSTRGuard; + struct smartBSTR + { + BSTR ptr = nullptr; + ~smartBSTR() { SysFreeString(ptr); } + }; typedef HRESULT (__stdcall IADsUser::*getstrfunc)(BSTR*); static OUString Str(IADsUser* pUser, getstrfunc func) { - BSTR sBstr; - if (FAILED((pUser->*func)(&sBstr))) + smartBSTR sBstr; + if (FAILED((pUser->*func)(&sBstr.ptr))) return ""; - BSTRGuard aBstrGuard(sBstr, SysFreeString); - return OUString(o3tl::toU(sBstr)); + return OUString(o3tl::toU(sBstr.ptr)); } static OUString Str(IADsUser* pUser, const wchar_t* property) { - BSTRGuard sBstrProp(SysAllocString(property), SysFreeString); + smartBSTR sBstrProp{ SysAllocString(property) }; struct AutoVariant : public VARIANT { AutoVariant() { VariantInit(this); } ~AutoVariant() { VariantClear(this); } } varArr; - if (FAILED(pUser->GetEx(sBstrProp.get(), &varArr))) + if (FAILED(pUser->GetEx(sBstrProp.ptr, &varArr))) return ""; SAFEARRAY* sa = V_ARRAY(&varArr); LONG nStart, nEnd; |