avmedia/source/win/framegrabber.cxx | 9 ++----- avmedia/source/win/framegrabber.hxx | 5 +++- avmedia/source/win/player.cxx | 5 +--- avmedia/source/win/player.hxx | 3 +- extensions/source/config/WinUserInfo/WinUserInfoBe.cxx | 21 ++++------------- include/systools/win32/comtools.hxx | 19 ++++++++++++--- sal/osl/w32/thread.cxx | 7 +++-- vcl/inc/win/saldata.hxx | 3 +- vcl/win/app/fileregistration.cxx | 14 ++--------- vcl/win/app/salinst.cxx | 6 ++-- vcl/win/dtrans/MtaOleClipb.cxx | 5 +--- 11 files changed, 46 insertions(+), 51 deletions(-)
New commits: commit c35f81148fb18ba070516edcbb4d614444899235 Author: Mike Kaganski <mike.kagan...@collabora.com> AuthorDate: Tue Dec 21 16:19:46 2021 +0300 Commit: Mike Kaganski <mike.kagan...@collabora.com> CommitDate: Tue Dec 21 17:03:30 2021 +0100 Use sal::systools::CoInitializeGuard and sal::systools::ThrowIfFailed Change-Id: Ifb40e7672671df03b5ffc89905ad1e7b68451b68 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/127232 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com> diff --git a/avmedia/source/win/framegrabber.cxx b/avmedia/source/win/framegrabber.cxx index 17fb229ef057..42e30219caf3 100644 --- a/avmedia/source/win/framegrabber.cxx +++ b/avmedia/source/win/framegrabber.cxx @@ -38,7 +38,6 @@ #include <vcl/graph.hxx> #include <vcl/dibtools.hxx> #include <o3tl/char16_t2wchar_t.hxx> -#include <systools/win32/comtools.hxx> constexpr OUStringLiteral AVMEDIA_WIN_FRAMEGRABBER_IMPLEMENTATIONNAME = u"com.sun.star.comp.avmedia.FrameGrabber_DirectX"; constexpr OUStringLiteral AVMEDIA_WIN_FRAMEGRABBER_SERVICENAME = u"com.sun.star.media.FrameGrabber_DirectX"; @@ -49,15 +48,13 @@ namespace avmedia::win { FrameGrabber::FrameGrabber() + : sal::systools::CoInitializeGuard(COINIT_APARTMENTTHREADED, false, + sal::systools::CoInitializeGuard::WhenFailed::NoThrow) { - ::CoInitializeEx( nullptr, COINIT_APARTMENTTHREADED ); } -FrameGrabber::~FrameGrabber() -{ - ::CoUninitialize(); -} +FrameGrabber::~FrameGrabber() = default; namespace { diff --git a/avmedia/source/win/framegrabber.hxx b/avmedia/source/win/framegrabber.hxx index fb4fda35905d..d1ca48e84230 100644 --- a/avmedia/source/win/framegrabber.hxx +++ b/avmedia/source/win/framegrabber.hxx @@ -21,6 +21,8 @@ #include "wincommon.hxx" #include <cppuhelper/implbase.hxx> +#include <systools/win32/comtools.hxx> + #include <com/sun/star/media/XFrameGrabber.hpp> struct IMediaDet; @@ -28,7 +30,8 @@ struct IMediaDet; namespace avmedia::win { class FrameGrabber : public ::cppu::WeakImplHelper< css::media::XFrameGrabber, - css::lang::XServiceInfo > + css::lang::XServiceInfo >, + public sal::systools::CoInitializeGuard { public: explicit FrameGrabber(); diff --git a/avmedia/source/win/player.cxx b/avmedia/source/win/player.cxx index 23fdb241b3f5..7f6660ab8462 100644 --- a/avmedia/source/win/player.cxx +++ b/avmedia/source/win/player.cxx @@ -64,13 +64,14 @@ static LRESULT CALLBACK MediaPlayerWndProc_2( HWND hWnd,UINT nMsg, WPARAM nPar1, Player::Player() : Player_BASE(m_aMutex), + sal::systools::CoInitializeGuard(COINIT_APARTMENTTHREADED, false, + sal::systools::CoInitializeGuard::WhenFailed::NoThrow), mnUnmutedVolume( 0 ), mnFrameWnd( nullptr ), mbMuted( false ), mbLooping( false ), mbAddWindow( true ) { - ::CoInitializeEx( nullptr, COINIT_APARTMENTTHREADED ); } @@ -78,8 +79,6 @@ Player::~Player() { if( mnFrameWnd ) ::DestroyWindow( mnFrameWnd ); - - ::CoUninitialize(); } diff --git a/avmedia/source/win/player.hxx b/avmedia/source/win/player.hxx index 20806251c8bc..1563d549a85e 100644 --- a/avmedia/source/win/player.hxx +++ b/avmedia/source/win/player.hxx @@ -51,7 +51,8 @@ typedef ::cppu::WeakComponentImplHelper< css::media::XPlayer, class Player : public cppu::BaseMutex, - public Player_BASE + public Player_BASE, + public sal::systools::CoInitializeGuard { public: diff --git a/extensions/source/config/WinUserInfo/WinUserInfoBe.cxx b/extensions/source/config/WinUserInfo/WinUserInfoBe.cxx index a412769ee639..ce4d147220c2 100644 --- a/extensions/source/config/WinUserInfo/WinUserInfoBe.cxx +++ b/extensions/source/config/WinUserInfo/WinUserInfoBe.cxx @@ -90,33 +90,22 @@ public: { try { - struct CoInitializeGuard - { - CoInitializeGuard() - { - if (HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); FAILED(hr)) - throw sal::systools::ComError("CoInitializeEx failed", hr); - } - ~CoInitializeGuard() { CoUninitialize(); } - } aCoInitializeGuard; + sal::systools::CoInitializeGuard aCoInitializeGuard(COINIT_APARTMENTTHREADED); auto pADsys = sal::systools::COMReference<IADsADSystemInfo>().CoCreateInstance( CLSID_ADSystemInfo, nullptr, CLSCTX_INPROC_SERVER); smartBSTR sUserDN; - HRESULT hr = pADsys->get_UserName(&sUserDN.ptr); - if (FAILED(hr)) - throw sal::systools::ComError("get_UserName failed", hr); + sal::systools::ThrowIfFailed(pADsys->get_UserName(&sUserDN.ptr), "get_UserName failed"); // 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.ptr); OUString sLdapUserDN = "LDAP://" + m_sUserDN; sal::systools::COMReference<IADsUser> pUser; - hr = ADsGetObject(o3tl::toW(sLdapUserDN.getStr()), IID_IADsUser, - reinterpret_cast<void**>(&pUser)); - if (FAILED(hr)) - throw sal::systools::ComError("ADsGetObject failed", hr); + sal::systools::ThrowIfFailed(ADsGetObject(o3tl::toW(sLdapUserDN.getStr()), IID_IADsUser, + reinterpret_cast<void**>(&pUser)), + "ADsGetObject failed"); // Fetch all the required information right now, when we know to have access to AD // (later the connection may already be lost) m_aMap[givenname] = Str(pUser, &IADsUser::get_FirstName); diff --git a/include/systools/win32/comtools.hxx b/include/systools/win32/comtools.hxx index c6f9c4f200f7..8829beb860e8 100644 --- a/include/systools/win32/comtools.hxx +++ b/include/systools/win32/comtools.hxx @@ -59,11 +59,24 @@ namespace sal::systools class CoInitializeGuard { public: - explicit CoInitializeGuard(DWORD dwCoInit, bool bThrowOnChangeMode = false) + enum class WhenFailed + { + NoThrow, // do not throw + Throw, // throw on failure + Abort, // std::abort on failure + }; + explicit CoInitializeGuard(DWORD dwCoInit, bool failChangeMode = false, + WhenFailed whenFailed = WhenFailed::Throw) { HRESULT hr = ::CoInitializeEx(nullptr, dwCoInit); - if (FAILED(hr) && (bThrowOnChangeMode || hr != RPC_E_CHANGED_MODE)) - throw ComError("CoInitializeEx failed", hr); + if (whenFailed != WhenFailed::NoThrow && FAILED(hr) + && (failChangeMode || hr != RPC_E_CHANGED_MODE)) + { + if (whenFailed == WhenFailed::Throw) + throw ComError("CoInitializeEx failed", hr); + else // if (whenFailed == Abort) + std::abort(); + } mbUninit = SUCCEEDED(hr); } CoInitializeGuard(const CoInitializeGuard&) = delete; // non-construction-copyable diff --git a/sal/osl/w32/thread.cxx b/sal/osl/w32/thread.cxx index e269e7cbd455..a82ac2ea8a19 100644 --- a/sal/osl/w32/thread.cxx +++ b/sal/osl/w32/thread.cxx @@ -27,6 +27,8 @@ #include <osl/time.h> #include <osl/interlck.h> #include <rtl/tencinfo.h> +#include <systools/win32/comtools.hxx> + #include <errno.h> namespace { @@ -54,14 +56,13 @@ static unsigned __stdcall oslWorkerWrapperFunction(void* pData) osl_TThreadImpl* pThreadImpl= static_cast<osl_TThreadImpl*>(pData); /* Initialize COM - Multi Threaded Apartment (MTA) for all threads */ - CoInitializeEx(nullptr, COINIT_MULTITHREADED); /* spawned by oslCreateThread */ + sal::systools::CoInitializeGuard aGuard(COINIT_MULTITHREADED, false, + sal::systools::CoInitializeGuard::WhenFailed::NoThrow); /* call worker-function with data */ pThreadImpl->m_WorkerFunction(pThreadImpl->m_pData); - CoUninitialize(); - return 0; } diff --git a/vcl/inc/win/saldata.hxx b/vcl/inc/win/saldata.hxx index 80286d87d347..32accf042f37 100644 --- a/vcl/inc/win/saldata.hxx +++ b/vcl/inc/win/saldata.hxx @@ -28,6 +28,7 @@ #include <svdata.hxx> #include <salwtype.hxx> +#include <systools/win32/comtools.hxx> #include <tools/long.hxx> #include <win/wincomp.hxx> @@ -61,7 +62,7 @@ struct SalIcon SalIcon *pNext; }; -class SalData +class SalData : public sal::systools::CoInitializeGuard { public: SalData(); diff --git a/vcl/win/app/fileregistration.cxx b/vcl/win/app/fileregistration.cxx index 2347e46c4b61..75bb0010ba21 100644 --- a/vcl/win/app/fileregistration.cxx +++ b/vcl/win/app/fileregistration.cxx @@ -112,14 +112,9 @@ static bool IsDefaultAppInstalledInReg() void LaunchRegistrationUI() { - const bool bUninit = SUCCEEDED(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED)); - comphelper::ScopeGuard g([bUninit]() { - if (bUninit) - CoUninitialize(); - }); - try { + sal::systools::CoInitializeGuard aGuard(COINIT_APARTMENTTHREADED); if (IsWindows10OrGreater()) { LaunchModernSettingsDialogDefaultApps(); @@ -150,11 +145,8 @@ void CheckFileExtRegistration(weld::Window* pDialogParent) if (!IsDefaultAppInstalledInReg()) return; - const bool bUninit = SUCCEEDED(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED)); - comphelper::ScopeGuard g([bUninit]() { - if (bUninit) - CoUninitialize(); - }); + sal::systools::CoInitializeGuard aGuard(COINIT_APARTMENTTHREADED, false, + sal::systools::CoInitializeGuard::WhenFailed::NoThrow); sal::systools::COMReference<IApplicationAssociationRegistration> pAAR; try { diff --git a/vcl/win/app/salinst.cxx b/vcl/win/app/salinst.cxx index a34448884057..c7538b519851 100644 --- a/vcl/win/app/salinst.cxx +++ b/vcl/win/app/salinst.cxx @@ -242,6 +242,9 @@ void SalData::initKeyCodeMap() // SalData SalData::SalData() + : sal::systools::CoInitializeGuard(COINIT_APARTMENTTHREADED, false, + sal::systools::CoInitializeGuard::WhenFailed::NoThrow) + // put main thread in Single Threaded Apartment (STA) { mhInst = nullptr; // default instance handle mnCmdShow = 0; // default frame show style @@ -296,7 +299,6 @@ SalData::SalData() SetSalData( this ); initNWF(); - CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); // put main thread in Single Threaded Apartment (STA) static Gdiplus::GdiplusStartupInput gdiplusStartupInput; Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr); } @@ -306,8 +308,6 @@ SalData::~SalData() deInitNWF(); SetSalData( nullptr ); - CoUninitialize(); - if (gdiplusToken) Gdiplus::GdiplusShutdown(gdiplusToken); } diff --git a/vcl/win/dtrans/MtaOleClipb.cxx b/vcl/win/dtrans/MtaOleClipb.cxx index 00fbf4143386..c1caea416d13 100644 --- a/vcl/win/dtrans/MtaOleClipb.cxx +++ b/vcl/win/dtrans/MtaOleClipb.cxx @@ -697,7 +697,8 @@ unsigned int WINAPI CMtaOleClipboard::clipboardChangedNotifierThreadProc( LPVOID CMtaOleClipboard* pInst = static_cast< CMtaOleClipboard* >( pParam ); OSL_ASSERT( nullptr != pInst ); - CoInitializeEx( nullptr, COINIT_APARTMENTTHREADED ); + sal::systools::CoInitializeGuard aGuard(COINIT_APARTMENTTHREADED, false, + sal::systools::CoInitializeGuard::WhenFailed::NoThrow); // assuming we don't need a lock for // a boolean variable like m_bRun... @@ -733,8 +734,6 @@ unsigned int WINAPI CMtaOleClipboard::clipboardChangedNotifierThreadProc( LPVOID aGuard.clear( ); } - CoUninitialize( ); - return 0; }