[13659] | 1 | /* $Id: VirtualBoxClientImpl.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
| 3 | * VirtualBox COM class implementation
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[1] | 26 | */
|
---|
| 27 |
|
---|
[67914] | 28 | #define LOG_GROUP LOG_GROUP_MAIN_VIRTUALBOXCLIENT
|
---|
| 29 | #include "LoggingNew.h"
|
---|
| 30 |
|
---|
[34417] | 31 | #include "VirtualBoxClientImpl.h"
|
---|
[25860] | 32 |
|
---|
| 33 | #include "AutoCaller.h"
|
---|
[34417] | 34 | #include "VBoxEvents.h"
|
---|
[44970] | 35 | #include "VBox/com/ErrorInfo.h"
|
---|
[90828] | 36 | #include "VBox/com/listeners.h"
|
---|
[1] | 37 |
|
---|
[34642] | 38 | #include <iprt/asm.h>
|
---|
[34417] | 39 | #include <iprt/thread.h>
|
---|
| 40 | #include <iprt/critsect.h>
|
---|
[91312] | 41 | #include <iprt/path.h>
|
---|
[34417] | 42 | #include <iprt/semaphore.h>
|
---|
[30681] | 43 | #include <iprt/cpp/utils.h>
|
---|
[66411] | 44 | #include <iprt/utf16.h>
|
---|
[63814] | 45 | #ifdef RT_OS_WINDOWS
|
---|
[76380] | 46 | # include <iprt/err.h>
|
---|
[63814] | 47 | # include <iprt/ldr.h>
|
---|
| 48 | # include <msi.h>
|
---|
[66411] | 49 | # include <WbemIdl.h>
|
---|
[63814] | 50 | #endif
|
---|
[30681] | 51 |
|
---|
[92144] | 52 | #include <new>
|
---|
[34417] | 53 |
|
---|
[92144] | 54 |
|
---|
[34417] | 55 | /** Waiting time between probing whether VBoxSVC is alive. */
|
---|
| 56 | #define VBOXCLIENT_DEFAULT_INTERVAL 30000
|
---|
| 57 |
|
---|
| 58 |
|
---|
[34642] | 59 | /** Initialize instance counter class variable */
|
---|
| 60 | uint32_t VirtualBoxClient::g_cInstances = 0;
|
---|
| 61 |
|
---|
[63639] | 62 | LONG VirtualBoxClient::s_cUnnecessaryAtlModuleLocks = 0;
|
---|
[34642] | 63 |
|
---|
[90828] | 64 | #ifdef VBOX_WITH_MAIN_NLS
|
---|
| 65 |
|
---|
| 66 | /* listener class for language updates */
|
---|
| 67 | class VBoxEventListener
|
---|
| 68 | {
|
---|
| 69 | public:
|
---|
| 70 | VBoxEventListener()
|
---|
| 71 | {}
|
---|
| 72 |
|
---|
| 73 |
|
---|
[90879] | 74 | HRESULT init(void *)
|
---|
[90828] | 75 | {
|
---|
| 76 | return S_OK;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[90879] | 79 | HRESULT init()
|
---|
| 80 | {
|
---|
| 81 | return S_OK;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[90828] | 84 | void uninit()
|
---|
| 85 | {
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | virtual ~VBoxEventListener()
|
---|
| 89 | {
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent *aEvent)
|
---|
| 93 | {
|
---|
| 94 | switch(aType)
|
---|
| 95 | {
|
---|
| 96 | case VBoxEventType_OnLanguageChanged:
|
---|
| 97 | {
|
---|
[92144] | 98 | /*
|
---|
| 99 | * Proceed with uttmost care as we might be racing com::Shutdown()
|
---|
| 100 | * and have the ground open up beneath us.
|
---|
| 101 | */
|
---|
| 102 | LogFunc(("VBoxEventType_OnLanguageChanged\n"));
|
---|
[90881] | 103 | VirtualBoxTranslator *pTranslator = VirtualBoxTranslator::tryInstance();
|
---|
[90879] | 104 | if (pTranslator)
|
---|
| 105 | {
|
---|
| 106 | ComPtr<ILanguageChangedEvent> pEvent = aEvent;
|
---|
| 107 | Assert(pEvent);
|
---|
| 108 |
|
---|
[92144] | 109 | /* This call may fail if we're racing COM shutdown. */
|
---|
[90879] | 110 | com::Bstr bstrLanguageId;
|
---|
[92144] | 111 | HRESULT hrc = pEvent->COMGETTER(LanguageId)(bstrLanguageId.asOutParam());
|
---|
| 112 | if (SUCCEEDED(hrc))
|
---|
| 113 | {
|
---|
| 114 | try
|
---|
| 115 | {
|
---|
| 116 | com::Utf8Str strLanguageId(bstrLanguageId);
|
---|
| 117 | LogFunc(("New language ID: %s\n", strLanguageId.c_str()));
|
---|
| 118 | pTranslator->i_loadLanguage(strLanguageId.c_str());
|
---|
| 119 | }
|
---|
| 120 | catch (std::bad_alloc &)
|
---|
| 121 | {
|
---|
| 122 | LogFunc(("Caught bad_alloc"));
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | else
|
---|
| 126 | LogFunc(("Failed to get new language ID: %Rhrc\n", hrc));
|
---|
[90879] | 127 |
|
---|
| 128 | pTranslator->release();
|
---|
| 129 | }
|
---|
[90828] | 130 | break;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | default:
|
---|
| 134 | AssertFailed();
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | return S_OK;
|
---|
| 138 | }
|
---|
| 139 | };
|
---|
| 140 |
|
---|
[90879] | 141 | typedef ListenerImpl<VBoxEventListener> VBoxEventListenerImpl;
|
---|
[90828] | 142 |
|
---|
[92403] | 143 | VBOX_LISTENER_DECLARE(VBoxEventListenerImpl)
|
---|
[90828] | 144 |
|
---|
| 145 | #endif /* VBOX_WITH_MAIN_NLS */
|
---|
| 146 |
|
---|
[1] | 147 | // constructor / destructor
|
---|
| 148 | /////////////////////////////////////////////////////////////////////////////
|
---|
[90849] | 149 |
|
---|
| 150 | /** @relates VirtualBoxClient::FinalConstruct() */
|
---|
[34417] | 151 | HRESULT VirtualBoxClient::FinalConstruct()
|
---|
[1] | 152 | {
|
---|
[94926] | 153 | HRESULT hrc = init();
|
---|
[35638] | 154 | BaseFinalConstruct();
|
---|
[94926] | 155 | return hrc;
|
---|
[1] | 156 | }
|
---|
| 157 |
|
---|
[34417] | 158 | void VirtualBoxClient::FinalRelease()
|
---|
[1] | 159 | {
|
---|
[34417] | 160 | uninit();
|
---|
[35638] | 161 | BaseFinalRelease();
|
---|
[1] | 162 | }
|
---|
| 163 |
|
---|
[66274] | 164 |
|
---|
[1] | 165 | // public initializer/uninitializer for internal purposes only
|
---|
| 166 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 167 |
|
---|
| 168 | /**
|
---|
[34417] | 169 | * Initializes the VirtualBoxClient object.
|
---|
[1] | 170 | *
|
---|
| 171 | * @returns COM result indicator
|
---|
| 172 | */
|
---|
[34417] | 173 | HRESULT VirtualBoxClient::init()
|
---|
[1] | 174 | {
|
---|
[60065] | 175 | LogFlowThisFuncEnter();
|
---|
[1] | 176 |
|
---|
[13659] | 177 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
[21878] | 178 | AutoInitSpan autoInitSpan(this);
|
---|
| 179 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
[13659] | 180 |
|
---|
[60065] | 181 | /* Important: DO NOT USE any kind of "early return" (except the single
|
---|
| 182 | * one above, checking the init span success) in this method. It is vital
|
---|
| 183 | * for correct error handling that it has only one point of return, which
|
---|
| 184 | * does all the magic on COM to signal object creation success and
|
---|
| 185 | * reporting the error later for every API method. COM translates any
|
---|
| 186 | * unsuccessful object creation to REGDB_E_CLASSNOTREG errors or similar
|
---|
| 187 | * unhelpful ones which cause us a lot of grief with troubleshooting. */
|
---|
[34642] | 188 |
|
---|
[94926] | 189 | HRESULT hrc = S_OK;
|
---|
[60065] | 190 | try
|
---|
| 191 | {
|
---|
| 192 | if (ASMAtomicIncU32(&g_cInstances) != 1)
|
---|
[91503] | 193 | AssertFailedStmt(throw setError(E_FAIL, "Attempted to create more than one VirtualBoxClient instance"));
|
---|
[34642] | 194 |
|
---|
[60065] | 195 | mData.m_ThreadWatcher = NIL_RTTHREAD;
|
---|
| 196 | mData.m_SemEvWatcher = NIL_RTSEMEVENT;
|
---|
[66279] | 197 |
|
---|
[94926] | 198 | hrc = mData.m_pVirtualBox.createLocalObject(CLSID_VirtualBox);
|
---|
| 199 | if (FAILED(hrc))
|
---|
[63814] | 200 | #ifdef RT_OS_WINDOWS
|
---|
[94926] | 201 | throw i_investigateVirtualBoxObjectCreationFailure(hrc);
|
---|
[63814] | 202 | #else
|
---|
[94926] | 203 | throw hrc;
|
---|
[63814] | 204 | #endif
|
---|
[59996] | 205 |
|
---|
[60065] | 206 | /* VirtualBox error return is postponed to method calls, fetch it. */
|
---|
| 207 | ULONG rev;
|
---|
[94926] | 208 | hrc = mData.m_pVirtualBox->COMGETTER(Revision)(&rev);
|
---|
| 209 | if (FAILED(hrc))
|
---|
| 210 | throw hrc;
|
---|
[1] | 211 |
|
---|
[94926] | 212 | hrc = unconst(mData.m_pEventSource).createObject();
|
---|
| 213 | AssertComRCThrow(hrc, setError(hrc, "Could not create EventSource for VirtualBoxClient"));
|
---|
| 214 | hrc = mData.m_pEventSource->init();
|
---|
| 215 | AssertComRCThrow(hrc, setError(hrc, "Could not initialize EventSource for VirtualBoxClient"));
|
---|
[60065] | 216 |
|
---|
[63639] | 217 | /* HACK ALERT! This is for DllCanUnloadNow(). */
|
---|
| 218 | s_cUnnecessaryAtlModuleLocks++;
|
---|
| 219 | AssertMsg(s_cUnnecessaryAtlModuleLocks == 1, ("%d\n", s_cUnnecessaryAtlModuleLocks));
|
---|
| 220 |
|
---|
[91312] | 221 | int vrc;
|
---|
[90828] | 222 | #ifdef VBOX_WITH_MAIN_NLS
|
---|
| 223 | /* Create the translator singelton (must work) and try load translations (non-fatal). */
|
---|
| 224 | mData.m_pVBoxTranslator = VirtualBoxTranslator::instance();
|
---|
| 225 | if (mData.m_pVBoxTranslator == NULL)
|
---|
[91503] | 226 | throw setError(VBOX_E_IPRT_ERROR, "Failed to create translator instance");
|
---|
[91312] | 227 |
|
---|
| 228 | char szNlsPath[RTPATH_MAX];
|
---|
[91314] | 229 | vrc = RTPathAppPrivateNoArch(szNlsPath, sizeof(szNlsPath));
|
---|
| 230 | if (RT_SUCCESS(vrc))
|
---|
| 231 | vrc = RTPathAppend(szNlsPath, sizeof(szNlsPath), "nls" RTPATH_SLASH_STR "VirtualBoxAPI");
|
---|
[91312] | 232 |
|
---|
| 233 | if (RT_SUCCESS(vrc))
|
---|
| 234 | {
|
---|
[91314] | 235 | vrc = mData.m_pVBoxTranslator->registerTranslation(szNlsPath, true, &mData.m_pTrComponent);
|
---|
| 236 | if (RT_SUCCESS(vrc))
|
---|
| 237 | {
|
---|
[94926] | 238 | hrc = i_reloadApiLanguage();
|
---|
| 239 | if (SUCCEEDED(hrc))
|
---|
[91314] | 240 | i_registerEventListener(); /* for updates */
|
---|
| 241 | else
|
---|
[94926] | 242 | LogRelFunc(("i_reloadApiLanguage failed: %Rhrc\n", hrc));
|
---|
[91314] | 243 | }
|
---|
[91312] | 244 | else
|
---|
[91314] | 245 | LogRelFunc(("Register translation failed: %Rrc\n", vrc));
|
---|
[91312] | 246 | }
|
---|
[90828] | 247 | else
|
---|
[91314] | 248 | LogRelFunc(("Path constructing failed: %Rrc\n", vrc));
|
---|
[90828] | 249 | #endif
|
---|
[60065] | 250 | /* Setting up the VBoxSVC watcher thread. If anything goes wrong here it
|
---|
| 251 | * is not considered important enough to cause any sort of visible
|
---|
| 252 | * failure. The monitoring will not be done, but that's all. */
|
---|
[91312] | 253 | vrc = RTSemEventCreate(&mData.m_SemEvWatcher);
|
---|
[60065] | 254 | if (RT_FAILURE(vrc))
|
---|
| 255 | {
|
---|
| 256 | mData.m_SemEvWatcher = NIL_RTSEMEVENT;
|
---|
[94926] | 257 | AssertRCStmt(vrc, throw setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Failed to create semaphore (vrc=%Rrc)"), vrc));
|
---|
[60065] | 258 | }
|
---|
| 259 |
|
---|
| 260 | vrc = RTThreadCreate(&mData.m_ThreadWatcher, SVCWatcherThread, this, 0,
|
---|
| 261 | RTTHREADTYPE_INFREQUENT_POLLER, RTTHREADFLAGS_WAITABLE, "VBoxSVCWatcher");
|
---|
| 262 | if (RT_FAILURE(vrc))
|
---|
| 263 | {
|
---|
| 264 | RTSemEventDestroy(mData.m_SemEvWatcher);
|
---|
| 265 | mData.m_SemEvWatcher = NIL_RTSEMEVENT;
|
---|
[94926] | 266 | AssertRCStmt(vrc, throw setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Failed to create watcher thread (vrc=%Rrc)"), vrc));
|
---|
[60065] | 267 | }
|
---|
| 268 | }
|
---|
| 269 | catch (HRESULT err)
|
---|
[34417] | 270 | {
|
---|
[60065] | 271 | /* we assume that error info is set by the thrower */
|
---|
[94926] | 272 | hrc = err;
|
---|
[34417] | 273 | }
|
---|
[60065] | 274 | catch (...)
|
---|
[34417] | 275 | {
|
---|
[94926] | 276 | hrc = VirtualBoxBase::handleUnexpectedExceptions(this, RT_SRC_POS);
|
---|
[34417] | 277 | }
|
---|
[1] | 278 |
|
---|
[60546] | 279 | /* Confirm a successful initialization when it's the case. Must be last,
|
---|
[60065] | 280 | * as on failure it will uninitialize the object. */
|
---|
[94926] | 281 | if (SUCCEEDED(hrc))
|
---|
[60065] | 282 | autoInitSpan.setSucceeded();
|
---|
| 283 | else
|
---|
[94926] | 284 | autoInitSpan.setFailed(hrc);
|
---|
[13659] | 285 |
|
---|
[94926] | 286 | LogFlowThisFunc(("hrc=%Rhrc\n", hrc));
|
---|
[60065] | 287 | LogFlowThisFuncLeave();
|
---|
| 288 | /* Unconditionally return success, because the error return is delayed to
|
---|
| 289 | * the attribute/method calls through the InitFailed object state. */
|
---|
| 290 | return S_OK;
|
---|
[1] | 291 | }
|
---|
| 292 |
|
---|
[63814] | 293 | #ifdef RT_OS_WINDOWS
|
---|
[69791] | 294 |
|
---|
[13659] | 295 | /**
|
---|
[63814] | 296 | * Looks into why we failed to create the VirtualBox object.
|
---|
| 297 | *
|
---|
| 298 | * @returns hrcCaller thru setError.
|
---|
| 299 | * @param hrcCaller The failure status code.
|
---|
| 300 | */
|
---|
| 301 | HRESULT VirtualBoxClient::i_investigateVirtualBoxObjectCreationFailure(HRESULT hrcCaller)
|
---|
| 302 | {
|
---|
[69792] | 303 | HRESULT hrc;
|
---|
| 304 |
|
---|
[69791] | 305 | # ifdef VBOX_WITH_SDS
|
---|
[66629] | 306 | /*
|
---|
[69791] | 307 | * Check that the VBoxSDS service is configured to run as LocalSystem and is enabled.
|
---|
| 308 | */
|
---|
[69804] | 309 | WCHAR wszBuffer[256];
|
---|
| 310 | uint32_t uStartType;
|
---|
| 311 | int vrc = i_getServiceAccountAndStartType(L"VBoxSDS", wszBuffer, RT_ELEMENTS(wszBuffer), &uStartType);
|
---|
[69791] | 312 | if (RT_SUCCESS(vrc))
|
---|
| 313 | {
|
---|
[69804] | 314 | LogRelFunc(("VBoxSDS service is running under the '%ls' account with start type %u.\n", wszBuffer, uStartType));
|
---|
[69791] | 315 | if (RTUtf16Cmp(wszBuffer, L"LocalSystem") != 0)
|
---|
| 316 | return setError(hrcCaller,
|
---|
[69804] | 317 | tr("VBoxSDS is misconfigured to run under the '%ls' account instead of the SYSTEM one.\n"
|
---|
[70585] | 318 | "Reinstall VirtualBox to fix it. Alternatively you can fix it using the Windows Service Control "
|
---|
[91718] | 319 | "Manager or by running 'sc config VBoxSDS obj=LocalSystem' on a command line."), wszBuffer);
|
---|
[69804] | 320 | if (uStartType == SERVICE_DISABLED)
|
---|
| 321 | return setError(hrcCaller,
|
---|
| 322 | tr("The VBoxSDS windows service is disabled.\n"
|
---|
[70585] | 323 | "Reinstall VirtualBox to fix it. Alternatively try reenable the service by setting it to "
|
---|
| 324 | " 'Manual' startup type in the Windows Service management console, or by runing "
|
---|
| 325 | "'sc config VBoxSDS start=demand' on the command line."));
|
---|
[69791] | 326 | }
|
---|
[70585] | 327 | else if (vrc == VERR_NOT_FOUND)
|
---|
| 328 | return setError(hrcCaller,
|
---|
| 329 | tr("The VBoxSDS windows service was not found.\n"
|
---|
| 330 | "Reinstall VirtualBox to fix it. Alternatively you can try start VirtualBox as Administrator, this "
|
---|
| 331 | "should automatically reinstall the service, or you can run "
|
---|
| 332 | "'VBoxSDS.exe --regservice' command from an elevated Administrator command line."));
|
---|
[69791] | 333 | else
|
---|
| 334 | LogRelFunc(("VirtualBoxClient::i_getServiceAccount failed: %Rrc\n", vrc));
|
---|
[69804] | 335 | # endif
|
---|
[69791] | 336 |
|
---|
| 337 | /*
|
---|
[63814] | 338 | * First step is to try get an IUnknown interface of the VirtualBox object.
|
---|
| 339 | *
|
---|
| 340 | * This will succeed even when oleaut32.msm (see @bugref{8016}, @ticketref{12087})
|
---|
| 341 | * is accidentally installed and messes up COM. It may also succeed when the COM
|
---|
| 342 | * registration is partially broken (though that's unlikely to happen these days).
|
---|
| 343 | */
|
---|
| 344 | IUnknown *pUnknown = NULL;
|
---|
[69792] | 345 | hrc = CoCreateInstance(CLSID_VirtualBox, NULL, CLSCTX_LOCAL_SERVER, IID_IUnknown, (void **)&pUnknown);
|
---|
[63814] | 346 | if (FAILED(hrc))
|
---|
| 347 | {
|
---|
| 348 | if (hrc == hrcCaller)
|
---|
| 349 | return setError(hrcCaller, tr("Completely failed to instantiate CLSID_VirtualBox: %Rhrc"), hrcCaller);
|
---|
| 350 | return setError(hrcCaller, tr("Completely failed to instantiate CLSID_VirtualBox: %Rhrc & %Rhrc"), hrcCaller, hrc);
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | /*
|
---|
| 354 | * Try query the IVirtualBox interface (should fail), if it succeed we return
|
---|
| 355 | * straight away so we have more columns to spend on long messages below.
|
---|
| 356 | */
|
---|
| 357 | IVirtualBox *pVirtualBox;
|
---|
| 358 | hrc = pUnknown->QueryInterface(IID_IVirtualBox, (void **)&pVirtualBox);
|
---|
| 359 | if (SUCCEEDED(hrc))
|
---|
| 360 | {
|
---|
| 361 | pVirtualBox->Release();
|
---|
| 362 | pUnknown->Release();
|
---|
| 363 | return setError(hrcCaller,
|
---|
| 364 | tr("Failed to instantiate CLSID_VirtualBox the first time, but worked when checking out why ... weird"));
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | /*
|
---|
| 368 | * Check for oleaut32.msm traces in the registry.
|
---|
| 369 | */
|
---|
| 370 | HKEY hKey;
|
---|
| 371 | LSTATUS lrc = RegOpenKeyExW(HKEY_CLASSES_ROOT, L"CLSID\\{00020420-0000-0000-C000-000000000046}\\InprocServer32",
|
---|
| 372 | 0 /*fFlags*/, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | STANDARD_RIGHTS_READ, &hKey);
|
---|
| 373 | if (lrc == ERROR_SUCCESS)
|
---|
| 374 | {
|
---|
| 375 | wchar_t wszBuf[8192];
|
---|
| 376 | DWORD cbBuf = sizeof(wszBuf) - sizeof(wchar_t);
|
---|
| 377 | DWORD dwType = 0;
|
---|
| 378 | lrc = RegQueryValueExW(hKey, L"InprocServer32", NULL /*pvReserved*/, &dwType, (BYTE *)&wszBuf[0], &cbBuf);
|
---|
| 379 | if (lrc == ERROR_SUCCESS)
|
---|
| 380 | {
|
---|
| 381 | wszBuf[cbBuf / sizeof(wchar_t)] = '\0';
|
---|
| 382 | bool fSetError = false;
|
---|
| 383 |
|
---|
| 384 | /*
|
---|
| 385 | * Try decode the string and improve the message.
|
---|
| 386 | */
|
---|
| 387 | typedef UINT (WINAPI *PFNMSIDECOMPOSEDESCRIPTORW)(PCWSTR pwszDescriptor,
|
---|
| 388 | LPWSTR pwszProductCode /*[40]*/,
|
---|
| 389 | LPWSTR pwszFeatureId /*[40]*/,
|
---|
| 390 | LPWSTR pwszComponentCode /*[40]*/,
|
---|
| 391 | DWORD *poffArguments);
|
---|
| 392 | PFNMSIDECOMPOSEDESCRIPTORW pfnMsiDecomposeDescriptorW;
|
---|
| 393 | pfnMsiDecomposeDescriptorW = (PFNMSIDECOMPOSEDESCRIPTORW)RTLdrGetSystemSymbol("msi.dll", "MsiDecomposeDescriptorW");
|
---|
| 394 | if ( pfnMsiDecomposeDescriptorW
|
---|
| 395 | && ( dwType == REG_SZ
|
---|
| 396 | || dwType == REG_MULTI_SZ))
|
---|
| 397 | {
|
---|
| 398 | wchar_t wszProductCode[RTUUID_STR_LENGTH + 2 + 16] = { 0 };
|
---|
| 399 | wchar_t wszFeatureId[RTUUID_STR_LENGTH + 2 + 16] = { 0 };
|
---|
| 400 | wchar_t wszComponentCode[RTUUID_STR_LENGTH + 2 + 16] = { 0 };
|
---|
| 401 | DWORD offArguments = ~(DWORD)0;
|
---|
| 402 | UINT uRc = pfnMsiDecomposeDescriptorW(wszBuf, wszProductCode, wszFeatureId, wszComponentCode, &offArguments);
|
---|
| 403 | if (uRc == 0)
|
---|
| 404 | {
|
---|
| 405 | /*
|
---|
| 406 | * Can we resolve the product code into a name?
|
---|
| 407 | */
|
---|
| 408 | typedef UINT (WINAPI *PFNMSIOPENPRODUCTW)(PCWSTR, MSIHANDLE *);
|
---|
| 409 | PFNMSIOPENPRODUCTW pfnMsiOpenProductW;
|
---|
| 410 | pfnMsiOpenProductW = (PFNMSIOPENPRODUCTW)RTLdrGetSystemSymbol("msi.dll", "MsiOpenProductW");
|
---|
| 411 |
|
---|
| 412 | typedef UINT (WINAPI *PFNMSICLOSEHANDLE)(MSIHANDLE);
|
---|
| 413 | PFNMSICLOSEHANDLE pfnMsiCloseHandle;
|
---|
| 414 | pfnMsiCloseHandle = (PFNMSICLOSEHANDLE)RTLdrGetSystemSymbol("msi.dll", "MsiCloseHandle");
|
---|
| 415 |
|
---|
| 416 | typedef UINT (WINAPI *PFNGETPRODUCTPROPERTYW)(MSIHANDLE, PCWSTR, PWSTR, PDWORD);
|
---|
| 417 | PFNGETPRODUCTPROPERTYW pfnMsiGetProductPropertyW;
|
---|
| 418 | pfnMsiGetProductPropertyW = (PFNGETPRODUCTPROPERTYW)RTLdrGetSystemSymbol("msi.dll", "MsiGetProductPropertyW");
|
---|
| 419 | if ( pfnMsiGetProductPropertyW
|
---|
| 420 | && pfnMsiCloseHandle
|
---|
| 421 | && pfnMsiOpenProductW)
|
---|
| 422 | {
|
---|
| 423 | MSIHANDLE hMsi = 0;
|
---|
| 424 | uRc = pfnMsiOpenProductW(wszProductCode, &hMsi);
|
---|
| 425 | if (uRc == 0)
|
---|
| 426 | {
|
---|
| 427 | static wchar_t const * const s_apwszProps[] =
|
---|
| 428 | {
|
---|
| 429 | INSTALLPROPERTY_INSTALLEDPRODUCTNAME,
|
---|
| 430 | INSTALLPROPERTY_PRODUCTNAME,
|
---|
| 431 | INSTALLPROPERTY_PACKAGENAME,
|
---|
| 432 | };
|
---|
| 433 |
|
---|
| 434 | wchar_t wszProductName[1024];
|
---|
| 435 | DWORD cwcProductName;
|
---|
| 436 | unsigned i = 0;
|
---|
| 437 | do
|
---|
| 438 | {
|
---|
| 439 | cwcProductName = RT_ELEMENTS(wszProductName) - 1;
|
---|
| 440 | uRc = pfnMsiGetProductPropertyW(hMsi, s_apwszProps[i], wszProductName, &cwcProductName);
|
---|
| 441 | }
|
---|
| 442 | while ( ++i < RT_ELEMENTS(s_apwszProps)
|
---|
| 443 | && ( uRc != 0
|
---|
| 444 | || cwcProductName < 2
|
---|
| 445 | || cwcProductName >= RT_ELEMENTS(wszProductName)) );
|
---|
| 446 | uRc = pfnMsiCloseHandle(hMsi);
|
---|
| 447 | if (uRc == 0 && cwcProductName >= 2)
|
---|
| 448 | {
|
---|
| 449 | wszProductName[RT_MIN(cwcProductName, RT_ELEMENTS(wszProductName) - 1)] = '\0';
|
---|
| 450 | setError(hrcCaller,
|
---|
| 451 | tr("Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, but CLSID_VirtualBox w/ IUnknown works.\n"
|
---|
| 452 | "PSDispatch looks broken by the '%ls' (%ls) program, suspecting that it features the broken oleaut32.msm module as component %ls.\n"
|
---|
| 453 | "\n"
|
---|
| 454 | "We suggest you try uninstall '%ls'.\n"
|
---|
| 455 | "\n"
|
---|
| 456 | "See also https://support.microsoft.com/en-us/kb/316911 "),
|
---|
| 457 | wszProductName, wszProductCode, wszComponentCode, wszProductName);
|
---|
| 458 | fSetError = true;
|
---|
| 459 | }
|
---|
| 460 | }
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | /* MSI uses COM and may mess up our stuff. So, we wait with the fallback till afterwards in this case. */
|
---|
| 464 | if (!fSetError)
|
---|
| 465 | {
|
---|
| 466 | setError(hrcCaller,
|
---|
| 467 | tr("Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, CLSID_VirtualBox w/ IUnknown works.\n"
|
---|
| 468 | "PSDispatch looks broken by installer %ls featuring the broken oleaut32.msm module as component %ls.\n"
|
---|
| 469 | "\n"
|
---|
| 470 | "See also https://support.microsoft.com/en-us/kb/316911 "),
|
---|
| 471 | wszProductCode, wszComponentCode);
|
---|
| 472 | fSetError = true;
|
---|
| 473 | }
|
---|
| 474 | }
|
---|
| 475 | }
|
---|
| 476 | if (!fSetError)
|
---|
| 477 | setError(hrcCaller, tr("Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, CLSID_VirtualBox w/ IUnknown works.\n"
|
---|
| 478 | "PSDispatch looks broken by some installer featuring the broken oleaut32.msm module as a component.\n"
|
---|
| 479 | "\n"
|
---|
| 480 | "See also https://support.microsoft.com/en-us/kb/316911 "));
|
---|
| 481 | }
|
---|
| 482 | else if (lrc == ERROR_FILE_NOT_FOUND)
|
---|
| 483 | setError(hrcCaller, tr("Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, but CLSID_VirtualBox w/ IUnknown works.\n"
|
---|
| 484 | "PSDispatch looks fine. Weird"));
|
---|
| 485 | else
|
---|
| 486 | setError(hrcCaller, tr("Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, but CLSID_VirtualBox w/ IUnknown works.\n"
|
---|
| 487 | "Checking out PSDispatch registration ended with error: %u (%#x)"), lrc, lrc);
|
---|
| 488 | RegCloseKey(hKey);
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 | pUnknown->Release();
|
---|
| 492 | return hrcCaller;
|
---|
| 493 | }
|
---|
[69791] | 494 |
|
---|
| 495 | # ifdef VBOX_WITH_SDS
|
---|
[69806] | 496 | /**
|
---|
| 497 | * Gets the service account name and start type for the given service.
|
---|
| 498 | *
|
---|
| 499 | * @returns IPRT status code (for some reason).
|
---|
| 500 | * @param pwszServiceName The name of the service.
|
---|
| 501 | * @param pwszAccountName Where to return the account name.
|
---|
| 502 | * @param cwcAccountName The length of the account name buffer (in WCHARs).
|
---|
| 503 | * @param puStartType Where to return the start type.
|
---|
| 504 | */
|
---|
[69804] | 505 | int VirtualBoxClient::i_getServiceAccountAndStartType(const wchar_t *pwszServiceName,
|
---|
| 506 | wchar_t *pwszAccountName, size_t cwcAccountName, uint32_t *puStartType)
|
---|
[69791] | 507 | {
|
---|
| 508 | AssertPtr(pwszServiceName);
|
---|
| 509 | AssertPtr(pwszAccountName);
|
---|
| 510 | Assert(cwcAccountName);
|
---|
| 511 | *pwszAccountName = '\0';
|
---|
[69804] | 512 | *puStartType = SERVICE_DEMAND_START;
|
---|
[69791] | 513 |
|
---|
| 514 | int vrc;
|
---|
| 515 |
|
---|
| 516 | // Get a handle to the SCM database.
|
---|
[69804] | 517 | SC_HANDLE hSCManager = OpenSCManagerW(NULL /*pwszMachineName*/, NULL /*pwszDatabaseName*/, SC_MANAGER_CONNECT);
|
---|
[69791] | 518 | if (hSCManager != NULL)
|
---|
| 519 | {
|
---|
| 520 | SC_HANDLE hService = OpenServiceW(hSCManager, pwszServiceName, SERVICE_QUERY_CONFIG);
|
---|
| 521 | if (hService != NULL)
|
---|
| 522 | {
|
---|
| 523 | DWORD cbNeeded = sizeof(QUERY_SERVICE_CONFIGW) + _1K;
|
---|
| 524 | if (!QueryServiceConfigW(hService, NULL, 0, &cbNeeded))
|
---|
| 525 | {
|
---|
| 526 | Assert(GetLastError() == ERROR_INSUFFICIENT_BUFFER);
|
---|
[69804] | 527 | LPQUERY_SERVICE_CONFIGW pSc = (LPQUERY_SERVICE_CONFIGW)RTMemTmpAllocZ(cbNeeded + _1K);
|
---|
[69791] | 528 | if (pSc)
|
---|
| 529 | {
|
---|
| 530 | DWORD cbNeeded2 = 0;
|
---|
[69804] | 531 | if (QueryServiceConfigW(hService, pSc, cbNeeded + _1K, &cbNeeded2))
|
---|
[69791] | 532 | {
|
---|
[69804] | 533 | *puStartType = pSc->dwStartType;
|
---|
[69791] | 534 | vrc = RTUtf16Copy(pwszAccountName, cwcAccountName, pSc->lpServiceStartName);
|
---|
| 535 | if (RT_FAILURE(vrc))
|
---|
| 536 | LogRel(("Error: SDS service name is too long (%Rrc): %ls\n", vrc, pSc->lpServiceStartName));
|
---|
| 537 | }
|
---|
| 538 | else
|
---|
| 539 | {
|
---|
| 540 | int dwError = GetLastError();
|
---|
| 541 | vrc = RTErrConvertFromWin32(dwError);
|
---|
[70296] | 542 | LogRel(("Error: Failed querying '%ls' service config: %Rwc (%u) -> %Rrc; cbNeeded=%d cbNeeded2=%d\n",
|
---|
[70585] | 543 | pwszServiceName, dwError, dwError, vrc, cbNeeded, cbNeeded2));
|
---|
[69791] | 544 | }
|
---|
| 545 | RTMemTmpFree(pSc);
|
---|
| 546 | }
|
---|
| 547 | else
|
---|
| 548 | {
|
---|
[69804] | 549 | LogRel(("Error: Failed allocating %#x bytes of memory for service config!\n", cbNeeded + _1K));
|
---|
[69791] | 550 | vrc = VERR_NO_TMP_MEMORY;
|
---|
| 551 | }
|
---|
| 552 | }
|
---|
| 553 | else
|
---|
| 554 | {
|
---|
| 555 | AssertLogRelMsgFailed(("Error: QueryServiceConfigW returns success with zero buffer!\n"));
|
---|
| 556 | vrc = VERR_IPE_UNEXPECTED_STATUS;
|
---|
| 557 | }
|
---|
| 558 | CloseServiceHandle(hService);
|
---|
| 559 | }
|
---|
| 560 | else
|
---|
| 561 | {
|
---|
| 562 | int dwError = GetLastError();
|
---|
| 563 | vrc = RTErrConvertFromWin32(dwError);
|
---|
[70296] | 564 | LogRel(("Error: Could not open service '%ls': %Rwc (%u) -> %Rrc\n", pwszServiceName, dwError, dwError, vrc));
|
---|
[69791] | 565 | }
|
---|
| 566 | CloseServiceHandle(hSCManager);
|
---|
| 567 | }
|
---|
| 568 | else
|
---|
| 569 | {
|
---|
| 570 | int dwError = GetLastError();
|
---|
| 571 | vrc = RTErrConvertFromWin32(dwError);
|
---|
| 572 | LogRel(("Error: Could not open SCM: %Rwc (%u) -> %Rrc\n", dwError, dwError, vrc));
|
---|
| 573 | }
|
---|
| 574 | return vrc;
|
---|
| 575 | }
|
---|
| 576 | # endif /* VBOX_WITH_SDS */
|
---|
| 577 |
|
---|
[63814] | 578 | #endif /* RT_OS_WINDOWS */
|
---|
| 579 |
|
---|
| 580 | /**
|
---|
[13659] | 581 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
| 582 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
| 583 | */
|
---|
[34417] | 584 | void VirtualBoxClient::uninit()
|
---|
[13659] | 585 | {
|
---|
[21878] | 586 | LogFlowThisFunc(("\n"));
|
---|
[1] | 587 |
|
---|
[13659] | 588 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
[21878] | 589 | AutoUninitSpan autoUninitSpan(this);
|
---|
[13659] | 590 | if (autoUninitSpan.uninitDone())
|
---|
[92144] | 591 | {
|
---|
| 592 | LogFlowThisFunc(("already done\n"));
|
---|
[13659] | 593 | return;
|
---|
[92144] | 594 | }
|
---|
[13659] | 595 |
|
---|
[90879] | 596 | #ifdef VBOX_WITH_MAIN_NLS
|
---|
| 597 | i_unregisterEventListener();
|
---|
| 598 | #endif
|
---|
| 599 |
|
---|
[34417] | 600 | if (mData.m_ThreadWatcher != NIL_RTTHREAD)
|
---|
| 601 | {
|
---|
| 602 | /* Signal the event semaphore and wait for the thread to terminate.
|
---|
| 603 | * if it hangs for some reason exit anyway, this can cause a crash
|
---|
| 604 | * though as the object will no longer be available. */
|
---|
| 605 | RTSemEventSignal(mData.m_SemEvWatcher);
|
---|
| 606 | RTThreadWait(mData.m_ThreadWatcher, 30000, NULL);
|
---|
| 607 | mData.m_ThreadWatcher = NIL_RTTHREAD;
|
---|
| 608 | RTSemEventDestroy(mData.m_SemEvWatcher);
|
---|
| 609 | mData.m_SemEvWatcher = NIL_RTSEMEVENT;
|
---|
| 610 | }
|
---|
[90828] | 611 | #ifdef VBOX_WITH_MAIN_NLS
|
---|
| 612 | if (mData.m_pVBoxTranslator != NULL)
|
---|
| 613 | {
|
---|
| 614 | mData.m_pVBoxTranslator->release();
|
---|
| 615 | mData.m_pVBoxTranslator = NULL;
|
---|
[91312] | 616 | mData.m_pTrComponent = NULL;
|
---|
[90828] | 617 | }
|
---|
| 618 | #endif
|
---|
[66902] | 619 | mData.m_pToken.setNull();
|
---|
[35135] | 620 | mData.m_pVirtualBox.setNull();
|
---|
[34642] | 621 |
|
---|
| 622 | ASMAtomicDecU32(&g_cInstances);
|
---|
[92144] | 623 |
|
---|
| 624 | LogFlowThisFunc(("returns\n"));
|
---|
[13659] | 625 | }
|
---|
| 626 |
|
---|
[34417] | 627 | // IVirtualBoxClient properties
|
---|
[1] | 628 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 629 |
|
---|
| 630 | /**
|
---|
[34417] | 631 | * Returns a reference to the VirtualBox object.
|
---|
[1] | 632 | *
|
---|
| 633 | * @returns COM status code
|
---|
[34417] | 634 | * @param aVirtualBox Address of result variable.
|
---|
[1] | 635 | */
|
---|
[52442] | 636 | HRESULT VirtualBoxClient::getVirtualBox(ComPtr<IVirtualBox> &aVirtualBox)
|
---|
[1] | 637 | {
|
---|
[35135] | 638 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
[52442] | 639 | aVirtualBox = mData.m_pVirtualBox;
|
---|
[1] | 640 | return S_OK;
|
---|
| 641 | }
|
---|
| 642 |
|
---|
| 643 | /**
|
---|
[34417] | 644 | * Create a new Session object and return a reference to it.
|
---|
[1] | 645 | *
|
---|
| 646 | * @returns COM status code
|
---|
[34417] | 647 | * @param aSession Address of result variable.
|
---|
[1] | 648 | */
|
---|
[52442] | 649 | HRESULT VirtualBoxClient::getSession(ComPtr<ISession> &aSession)
|
---|
[1] | 650 | {
|
---|
[34417] | 651 | /* this is not stored in this object, no need to lock */
|
---|
| 652 | ComPtr<ISession> pSession;
|
---|
[94926] | 653 | HRESULT hrc = pSession.createInprocObject(CLSID_Session);
|
---|
| 654 | if (SUCCEEDED(hrc))
|
---|
[52442] | 655 | aSession = pSession;
|
---|
[94926] | 656 | return hrc;
|
---|
[1] | 657 | }
|
---|
| 658 |
|
---|
| 659 | /**
|
---|
[34417] | 660 | * Return reference to the EventSource associated with this object.
|
---|
[1] | 661 | *
|
---|
| 662 | * @returns COM status code
|
---|
[34417] | 663 | * @param aEventSource Address of result variable.
|
---|
[1] | 664 | */
|
---|
[52442] | 665 | HRESULT VirtualBoxClient::getEventSource(ComPtr<IEventSource> &aEventSource)
|
---|
[1] | 666 | {
|
---|
[13659] | 667 | /* this is const, no need to lock */
|
---|
[52442] | 668 | aEventSource = mData.m_pEventSource;
|
---|
| 669 | return aEventSource.isNull() ? E_FAIL : S_OK;
|
---|
[1] | 670 | }
|
---|
| 671 |
|
---|
[52442] | 672 | // IVirtualBoxClient methods
|
---|
| 673 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 674 |
|
---|
[44970] | 675 | /**
|
---|
| 676 | * Checks a Machine object for any pending errors.
|
---|
| 677 | *
|
---|
| 678 | * @returns COM status code
|
---|
| 679 | * @param aMachine Machine object to check.
|
---|
| 680 | */
|
---|
[52442] | 681 | HRESULT VirtualBoxClient::checkMachineError(const ComPtr<IMachine> &aMachine)
|
---|
[44970] | 682 | {
|
---|
| 683 | BOOL fAccessible = FALSE;
|
---|
[94926] | 684 | HRESULT hrc = aMachine->COMGETTER(Accessible)(&fAccessible);
|
---|
| 685 | if (FAILED(hrc))
|
---|
| 686 | return setError(hrc, tr("Could not check the accessibility status of the VM"));
|
---|
[44970] | 687 | else if (!fAccessible)
|
---|
| 688 | {
|
---|
| 689 | ComPtr<IVirtualBoxErrorInfo> pAccessError;
|
---|
[94926] | 690 | hrc = aMachine->COMGETTER(AccessError)(pAccessError.asOutParam());
|
---|
| 691 | if (FAILED(hrc))
|
---|
| 692 | return setError(hrc, tr("Could not get the access error message of the VM"));
|
---|
[44970] | 693 | else
|
---|
| 694 | {
|
---|
| 695 | ErrorInfo info(pAccessError);
|
---|
| 696 | ErrorInfoKeeper eik(info);
|
---|
| 697 | return info.getResultCode();
|
---|
| 698 | }
|
---|
| 699 | }
|
---|
| 700 | return S_OK;
|
---|
| 701 | }
|
---|
| 702 |
|
---|
[34417] | 703 | // private methods
|
---|
| 704 | /////////////////////////////////////////////////////////////////////////////
|
---|
[1] | 705 |
|
---|
[66411] | 706 |
|
---|
[69240] | 707 | /// @todo AM Add pinging of VBoxSDS
|
---|
[34417] | 708 | /*static*/
|
---|
| 709 | DECLCALLBACK(int) VirtualBoxClient::SVCWatcherThread(RTTHREAD ThreadSelf,
|
---|
| 710 | void *pvUser)
|
---|
[1] | 711 | {
|
---|
[34417] | 712 | NOREF(ThreadSelf);
|
---|
| 713 | Assert(pvUser);
|
---|
| 714 | VirtualBoxClient *pThis = (VirtualBoxClient *)pvUser;
|
---|
| 715 | RTSEMEVENT sem = pThis->mData.m_SemEvWatcher;
|
---|
| 716 | RTMSINTERVAL cMillies = VBOXCLIENT_DEFAULT_INTERVAL;
|
---|
[1] | 717 |
|
---|
[34417] | 718 | /* The likelihood of early crashes are high, so start with a short wait. */
|
---|
[94926] | 719 | int vrc = RTSemEventWait(sem, cMillies / 2);
|
---|
[1] | 720 |
|
---|
[34417] | 721 | /* As long as the waiting times out keep retrying the wait. */
|
---|
| 722 | while (RT_FAILURE(vrc))
|
---|
| 723 | {
|
---|
| 724 | {
|
---|
[94926] | 725 | HRESULT hrc = S_OK;
|
---|
[35135] | 726 | ComPtr<IVirtualBox> pV;
|
---|
| 727 | {
|
---|
| 728 | AutoReadLock alock(pThis COMMA_LOCKVAL_SRC_POS);
|
---|
| 729 | pV = pThis->mData.m_pVirtualBox;
|
---|
| 730 | }
|
---|
[34417] | 731 | if (!pV.isNull())
|
---|
| 732 | {
|
---|
| 733 | ULONG rev;
|
---|
[94926] | 734 | hrc = pV->COMGETTER(Revision)(&rev);
|
---|
| 735 | if (FAILED_DEAD_INTERFACE(hrc))
|
---|
[34417] | 736 | {
|
---|
[94926] | 737 | LogRel(("VirtualBoxClient: detected unresponsive VBoxSVC (hrc=%Rhrc)\n", hrc));
|
---|
[35135] | 738 | {
|
---|
| 739 | AutoWriteLock alock(pThis COMMA_LOCKVAL_SRC_POS);
|
---|
| 740 | /* Throw away the VirtualBox reference, it's no longer
|
---|
| 741 | * usable as VBoxSVC terminated in the mean time. */
|
---|
| 742 | pThis->mData.m_pVirtualBox.setNull();
|
---|
| 743 | }
|
---|
[85300] | 744 | ::FireVBoxSVCAvailabilityChangedEvent(pThis->mData.m_pEventSource, FALSE);
|
---|
[34417] | 745 | }
|
---|
| 746 | }
|
---|
| 747 | else
|
---|
| 748 | {
|
---|
| 749 | /* Try to get a new VirtualBox reference straight away, and if
|
---|
| 750 | * this fails use an increased waiting time as very frequent
|
---|
| 751 | * restart attempts in some wedged config can cause high CPU
|
---|
| 752 | * and disk load. */
|
---|
[50174] | 753 | ComPtr<IVirtualBox> pVirtualBox;
|
---|
[66902] | 754 | ComPtr<IToken> pToken;
|
---|
[94926] | 755 | hrc = pVirtualBox.createLocalObject(CLSID_VirtualBox);
|
---|
| 756 | if (FAILED(hrc))
|
---|
[34417] | 757 | cMillies = 3 * VBOXCLIENT_DEFAULT_INTERVAL;
|
---|
| 758 | else
|
---|
[35135] | 759 | {
|
---|
[94926] | 760 | LogRel(("VirtualBoxClient: detected working VBoxSVC (hrc=%Rhrc)\n", hrc));
|
---|
[35135] | 761 | {
|
---|
| 762 | AutoWriteLock alock(pThis COMMA_LOCKVAL_SRC_POS);
|
---|
| 763 | /* Update the VirtualBox reference, there's a working
|
---|
| 764 | * VBoxSVC again from now on. */
|
---|
[50174] | 765 | pThis->mData.m_pVirtualBox = pVirtualBox;
|
---|
[66902] | 766 | pThis->mData.m_pToken = pToken;
|
---|
[90828] | 767 | #ifdef VBOX_WITH_MAIN_NLS
|
---|
| 768 | /* update language using new instance of IVirtualBox in case the language settings was changed */
|
---|
| 769 | pThis->i_reloadApiLanguage();
|
---|
| 770 | pThis->i_registerEventListener();
|
---|
| 771 | #endif
|
---|
[35135] | 772 | }
|
---|
[85300] | 773 | ::FireVBoxSVCAvailabilityChangedEvent(pThis->mData.m_pEventSource, TRUE);
|
---|
[34417] | 774 | cMillies = VBOXCLIENT_DEFAULT_INTERVAL;
|
---|
[35135] | 775 | }
|
---|
[34417] | 776 | }
|
---|
| 777 | }
|
---|
| 778 | vrc = RTSemEventWait(sem, cMillies);
|
---|
| 779 | }
|
---|
| 780 | return 0;
|
---|
[1] | 781 | }
|
---|
| 782 |
|
---|
[90828] | 783 | #ifdef VBOX_WITH_MAIN_NLS
|
---|
| 784 |
|
---|
| 785 | HRESULT VirtualBoxClient::i_reloadApiLanguage()
|
---|
| 786 | {
|
---|
| 787 | if (mData.m_pVBoxTranslator == NULL)
|
---|
| 788 | return S_OK;
|
---|
| 789 |
|
---|
[94926] | 790 | HRESULT hrc = mData.m_pVBoxTranslator->loadLanguage(mData.m_pVirtualBox);
|
---|
| 791 | if (FAILED(hrc))
|
---|
| 792 | setError(hrc, tr("Failed to load user language instance"));
|
---|
| 793 | return hrc;
|
---|
[90828] | 794 | }
|
---|
| 795 |
|
---|
| 796 | HRESULT VirtualBoxClient::i_registerEventListener()
|
---|
| 797 | {
|
---|
[94926] | 798 | HRESULT hrc = mData.m_pVirtualBox->COMGETTER(EventSource)(mData.m_pVBoxEventSource.asOutParam());
|
---|
| 799 | if (SUCCEEDED(hrc))
|
---|
[90828] | 800 | {
|
---|
[90879] | 801 | ComObjPtr<VBoxEventListenerImpl> pVBoxListener;
|
---|
| 802 | pVBoxListener.createObject();
|
---|
| 803 | pVBoxListener->init(new VBoxEventListener());
|
---|
| 804 | mData.m_pVBoxEventListener = pVBoxListener;
|
---|
[90828] | 805 | com::SafeArray<VBoxEventType_T> eventTypes;
|
---|
| 806 | eventTypes.push_back(VBoxEventType_OnLanguageChanged);
|
---|
[94926] | 807 | hrc = mData.m_pVBoxEventSource->RegisterListener(pVBoxListener, ComSafeArrayAsInParam(eventTypes), true);
|
---|
| 808 | if (FAILED(hrc))
|
---|
[90879] | 809 | {
|
---|
[94926] | 810 | hrc = setError(hrc, tr("Failed to register listener"));
|
---|
[90879] | 811 | mData.m_pVBoxEventListener.setNull();
|
---|
| 812 | mData.m_pVBoxEventSource.setNull();
|
---|
| 813 | }
|
---|
[90828] | 814 | }
|
---|
| 815 | else
|
---|
[94926] | 816 | hrc = setError(hrc, tr("Failed to get event source from VirtualBox"));
|
---|
| 817 | return hrc;
|
---|
[90828] | 818 | }
|
---|
| 819 |
|
---|
[90879] | 820 | void VirtualBoxClient::i_unregisterEventListener()
|
---|
| 821 | {
|
---|
| 822 | if (mData.m_pVBoxEventListener.isNotNull())
|
---|
| 823 | {
|
---|
| 824 | if (mData.m_pVBoxEventSource.isNotNull())
|
---|
| 825 | mData.m_pVBoxEventSource->UnregisterListener(mData.m_pVBoxEventListener);
|
---|
| 826 | mData.m_pVBoxEventListener.setNull();
|
---|
| 827 | }
|
---|
| 828 | mData.m_pVBoxEventSource.setNull();
|
---|
| 829 | }
|
---|
| 830 |
|
---|
[90828] | 831 | #endif /* VBOX_WITH_MAIN_NLS */
|
---|
| 832 |
|
---|
[14772] | 833 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|