1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "ole2uno.hxx"
22 #include <comphelper/windowserrorstring.hxx>
23 #include <osl/thread.hxx>
24 #include <sal/log.hxx>
26 void o2u_attachCurrentThread()
28 static osl::ThreadData oleThreadData
;
30 if (!bool(reinterpret_cast<sal_IntPtr
>(oleThreadData
.getData())))
32 HRESULT hr
= CoInitializeEx(nullptr, COINIT_MULTITHREADED
);
34 { // FIXME: is it a problem that this ends up in STA currently?
35 assert(RPC_E_CHANGED_MODE
== hr
);
36 // Let's find out explicitly what apartment mode we are in.
37 if (hr
== RPC_E_CHANGED_MODE
)
38 SAL_INFO("extensions.olebridge", "CoInitializeEx failed (expectedly): "
39 << WindowsErrorStringFromHRESULT(hr
));
41 SAL_WARN("extensions.olebridge",
42 "CoInitializeEx failed: " << WindowsErrorStringFromHRESULT(hr
));
44 APTTYPEQUALIFIER nAptTypeQualifier
;
45 if (SUCCEEDED(CoGetApartmentType(&nAptType
, &nAptTypeQualifier
)))
47 SAL_INFO("extensions.olebridge",
49 << (nAptType
== APTTYPE_STA
? OUString("single-threaded") :
50 (nAptType
== APTTYPE_MTA
? OUString("multi-threaded") :
51 (nAptType
== APTTYPE_NA
? OUString("neutral") :
52 (nAptType
== APTTYPE_MAINSTA
? OUString("main single-threaded") :
53 ("unknown (") + OUString::number(nAptType
) + ")"))))
55 << (nAptTypeQualifier
== APTTYPEQUALIFIER_NONE
? OUString() :
56 (nAptTypeQualifier
== APTTYPEQUALIFIER_IMPLICIT_MTA
? OUString(" (implicit)") :
57 (nAptTypeQualifier
== APTTYPEQUALIFIER_NA_ON_MTA
? OUString(" (on MTA)") :
58 (nAptTypeQualifier
== APTTYPEQUALIFIER_NA_ON_STA
? OUString(" (on STA)") :
59 (nAptTypeQualifier
== APTTYPEQUALIFIER_NA_ON_IMPLICIT_MTA
? OUString(" (on implicit MTA)") :
60 (nAptTypeQualifier
== APTTYPEQUALIFIER_NA_ON_MAINSTA
? OUString(" (on main STA)") :
61 (" (with unknown qualifier (" + OUString::number(nAptTypeQualifier
) + "))")))))))
65 oleThreadData
.setData(reinterpret_cast<void*>(true));
69 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */