install.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. * QEMU Guest Agent win32 VSS Provider installer
  3. *
  4. * Copyright Hitachi Data Systems Corp. 2013
  5. *
  6. * Authors:
  7. * Tomoki Sekiyama <tomoki.sekiyama@hds.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "vss-common.h"
  14. #include "vss-debug.h"
  15. #ifdef HAVE_VSS_SDK
  16. #include <vscoordint.h>
  17. #else
  18. #include <vsadmin.h>
  19. #endif
  20. #include "install.h"
  21. #include <wbemidl.h>
  22. #include <comdef.h>
  23. #include <comutil.h>
  24. #include <sddl.h>
  25. #include <winsvc.h>
  26. #define BUFFER_SIZE 1024
  27. extern HINSTANCE g_hinstDll;
  28. const GUID CLSID_COMAdminCatalog = { 0xF618C514, 0xDFB8, 0x11d1,
  29. {0xA2, 0xCF, 0x00, 0x80, 0x5F, 0xC7, 0x92, 0x35} };
  30. const GUID IID_ICOMAdminCatalog2 = { 0x790C6E0B, 0x9194, 0x4cc9,
  31. {0x94, 0x26, 0xA4, 0x8A, 0x63, 0x18, 0x56, 0x96} };
  32. const GUID CLSID_WbemLocator = { 0x4590f811, 0x1d3a, 0x11d0,
  33. {0x89, 0x1f, 0x00, 0xaa, 0x00, 0x4b, 0x2e, 0x24} };
  34. const GUID IID_IWbemLocator = { 0xdc12a687, 0x737f, 0x11cf,
  35. {0x88, 0x4d, 0x00, 0xaa, 0x00, 0x4b, 0x2e, 0x24} };
  36. static void errmsg(DWORD err, const char *text)
  37. {
  38. /*
  39. * `text' contains function call statement when errmsg is called via chk().
  40. * To make error message more readable, we cut off the text after '('.
  41. * If text doesn't contains '(', negative precision is given, which is
  42. * treated as though it were missing.
  43. */
  44. char *msg = NULL;
  45. const char *nul = strchr(text, '(');
  46. int len = nul ? nul - text : -1;
  47. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  48. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  49. NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  50. (char *)&msg, 0, NULL);
  51. qga_debug("%.*s. (Error: %lx) %s", len, text, err, msg);
  52. LocalFree(msg);
  53. }
  54. static void errmsg_dialog(DWORD err, const char *text, const char *opt = "")
  55. {
  56. char *msg, buf[512];
  57. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  58. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  59. NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  60. (char *)&msg, 0, NULL);
  61. snprintf(buf, sizeof(buf), "%s%s. (Error: %lx) %s", text, opt, err, msg);
  62. MessageBox(NULL, buf, "Error from " QGA_PROVIDER_NAME, MB_OK|MB_ICONERROR);
  63. LocalFree(msg);
  64. }
  65. #define _chk(hr, status, msg, err_label) \
  66. do { \
  67. hr = (status); \
  68. if (FAILED(hr)) { \
  69. errmsg(hr, msg); \
  70. goto err_label; \
  71. } \
  72. } while (0)
  73. #define chk(status) _chk(hr, status, "Failed to " #status, out)
  74. #if !defined(__MINGW64_VERSION_MAJOR) || !defined(__MINGW64_VERSION_MINOR) || \
  75. __MINGW64_VERSION_MAJOR * 100 + __MINGW64_VERSION_MINOR < 301
  76. void __stdcall _com_issue_error(HRESULT hr)
  77. {
  78. errmsg(hr, "Unexpected error in COM");
  79. }
  80. #endif
  81. template<class T>
  82. HRESULT put_Value(ICatalogObject *pObj, LPCWSTR name, T val)
  83. {
  84. return pObj->put_Value(_bstr_t(name), _variant_t(val));
  85. }
  86. /* Lookup Administrators group name from winmgmt */
  87. static HRESULT GetAdminName(_bstr_t *name)
  88. {
  89. qga_debug_begin;
  90. HRESULT hr;
  91. COMPointer<IWbemLocator> pLoc;
  92. COMPointer<IWbemServices> pSvc;
  93. COMPointer<IEnumWbemClassObject> pEnum;
  94. COMPointer<IWbemClassObject> pWobj;
  95. ULONG returned;
  96. _variant_t var;
  97. chk(CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER,
  98. IID_IWbemLocator, (LPVOID *)pLoc.replace()));
  99. chk(pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, NULL,
  100. 0, 0, 0, pSvc.replace()));
  101. chk(CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE,
  102. NULL, RPC_C_AUTHN_LEVEL_CALL,
  103. RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE));
  104. chk(pSvc->ExecQuery(_bstr_t(L"WQL"),
  105. _bstr_t(L"select * from Win32_Account where "
  106. "SID='S-1-5-32-544' and localAccount=TRUE"),
  107. WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
  108. NULL, pEnum.replace()));
  109. if (!pEnum) {
  110. hr = E_FAIL;
  111. errmsg(hr, "Failed to query for Administrators");
  112. goto out;
  113. }
  114. chk(pEnum->Next(WBEM_INFINITE, 1, pWobj.replace(), &returned));
  115. if (returned == 0) {
  116. hr = E_FAIL;
  117. errmsg(hr, "No Administrators found");
  118. goto out;
  119. }
  120. chk(pWobj->Get(_bstr_t(L"Name"), 0, &var, 0, 0));
  121. try {
  122. *name = var;
  123. } catch(...) {
  124. hr = E_FAIL;
  125. errmsg(hr, "Failed to get name of Administrators");
  126. goto out;
  127. }
  128. out:
  129. qga_debug_end;
  130. return hr;
  131. }
  132. /* Acquire group or user name by SID */
  133. static HRESULT getNameByStringSID(
  134. const wchar_t *sid, LPWSTR buffer, LPDWORD bufferLen)
  135. {
  136. qga_debug_begin;
  137. HRESULT hr = S_OK;
  138. PSID psid = NULL;
  139. SID_NAME_USE groupType;
  140. DWORD domainNameLen = BUFFER_SIZE;
  141. wchar_t domainName[BUFFER_SIZE];
  142. if (!ConvertStringSidToSidW(sid, &psid)) {
  143. hr = HRESULT_FROM_WIN32(GetLastError());
  144. goto out;
  145. }
  146. if (!LookupAccountSidW(NULL, psid, buffer, bufferLen,
  147. domainName, &domainNameLen, &groupType)) {
  148. hr = HRESULT_FROM_WIN32(GetLastError());
  149. /* Fall through and free psid */
  150. }
  151. LocalFree(psid);
  152. out:
  153. qga_debug_end;
  154. return hr;
  155. }
  156. /* Find and iterate QGA VSS provider in COM+ Application Catalog */
  157. static HRESULT QGAProviderFind(
  158. HRESULT (*found)(ICatalogCollection *, int, void *), void *arg)
  159. {
  160. qga_debug_begin;
  161. HRESULT hr;
  162. COMInitializer initializer;
  163. COMPointer<IUnknown> pUnknown;
  164. COMPointer<ICOMAdminCatalog2> pCatalog;
  165. COMPointer<ICatalogCollection> pColl;
  166. COMPointer<ICatalogObject> pObj;
  167. _variant_t var;
  168. long i, n;
  169. chk(CoCreateInstance(CLSID_COMAdminCatalog, NULL, CLSCTX_INPROC_SERVER,
  170. IID_IUnknown, (void **)pUnknown.replace()));
  171. chk(pUnknown->QueryInterface(IID_ICOMAdminCatalog2,
  172. (void **)pCatalog.replace()));
  173. chk(pCatalog->GetCollection(_bstr_t(L"Applications"),
  174. (IDispatch **)pColl.replace()));
  175. chk(pColl->Populate());
  176. chk(pColl->get_Count(&n));
  177. for (i = n - 1; i >= 0; i--) {
  178. chk(pColl->get_Item(i, (IDispatch **)pObj.replace()));
  179. chk(pObj->get_Value(_bstr_t(L"Name"), &var));
  180. if (var == _variant_t(QGA_PROVIDER_LNAME)) {
  181. if (FAILED(found(pColl, i, arg))) {
  182. goto out;
  183. }
  184. }
  185. }
  186. chk(pColl->SaveChanges(&n));
  187. out:
  188. qga_debug_end;
  189. return hr;
  190. }
  191. /* Count QGA VSS provider in COM+ Application Catalog */
  192. static HRESULT QGAProviderCount(ICatalogCollection *coll, int i, void *arg)
  193. {
  194. qga_debug_begin;
  195. (*(int *)arg)++;
  196. qga_debug_end;
  197. return S_OK;
  198. }
  199. /* Remove QGA VSS provider from COM+ Application Catalog Collection */
  200. static HRESULT QGAProviderRemove(ICatalogCollection *coll, int i, void *arg)
  201. {
  202. qga_debug_begin;
  203. HRESULT hr;
  204. qga_debug("Removing COM+ Application: %s", QGA_PROVIDER_NAME);
  205. chk(coll->Remove(i));
  206. out:
  207. qga_debug_end;
  208. return hr;
  209. }
  210. /* Unregister this module from COM+ Applications Catalog */
  211. STDAPI COMUnregister(void);
  212. STDAPI COMUnregister(void)
  213. {
  214. qga_debug_begin;
  215. HRESULT hr;
  216. DllUnregisterServer();
  217. chk(QGAProviderFind(QGAProviderRemove, NULL));
  218. out:
  219. qga_debug_end;
  220. return hr;
  221. }
  222. /* Register this module to COM+ Applications Catalog */
  223. STDAPI COMRegister(void);
  224. STDAPI COMRegister(void)
  225. {
  226. qga_debug_begin;
  227. HRESULT hr;
  228. COMInitializer initializer;
  229. COMPointer<IUnknown> pUnknown;
  230. COMPointer<ICOMAdminCatalog2> pCatalog;
  231. COMPointer<ICatalogCollection> pApps, pRoles, pUsersInRole;
  232. COMPointer<ICatalogObject> pObj;
  233. long n;
  234. _bstr_t name;
  235. _variant_t key;
  236. CHAR dllPath[MAX_PATH], tlbPath[MAX_PATH];
  237. bool unregisterOnFailure = false;
  238. int count = 0;
  239. DWORD bufferLen = BUFFER_SIZE;
  240. wchar_t buffer[BUFFER_SIZE];
  241. const wchar_t *administratorsGroupSID = L"S-1-5-32-544";
  242. const wchar_t *systemUserSID = L"S-1-5-18";
  243. if (!g_hinstDll) {
  244. errmsg(E_FAIL, "Failed to initialize DLL");
  245. qga_debug_end;
  246. return E_FAIL;
  247. }
  248. chk(QGAProviderFind(QGAProviderCount, (void *)&count));
  249. if (count) {
  250. errmsg(E_ABORT, "QGA VSS Provider is already installed");
  251. qga_debug_end;
  252. return E_ABORT;
  253. }
  254. chk(CoCreateInstance(CLSID_COMAdminCatalog, NULL, CLSCTX_INPROC_SERVER,
  255. IID_IUnknown, (void **)pUnknown.replace()));
  256. chk(pUnknown->QueryInterface(IID_ICOMAdminCatalog2,
  257. (void **)pCatalog.replace()));
  258. /* Install COM+ Component */
  259. chk(pCatalog->GetCollection(_bstr_t(L"Applications"),
  260. (IDispatch **)pApps.replace()));
  261. chk(pApps->Populate());
  262. chk(pApps->Add((IDispatch **)&pObj));
  263. chk(put_Value(pObj, L"Name", QGA_PROVIDER_LNAME));
  264. chk(put_Value(pObj, L"Description", QGA_PROVIDER_LNAME));
  265. chk(put_Value(pObj, L"ApplicationAccessChecksEnabled", true));
  266. chk(put_Value(pObj, L"Authentication", short(6)));
  267. chk(put_Value(pObj, L"AuthenticationCapability", short(2)));
  268. chk(put_Value(pObj, L"ImpersonationLevel", short(2)));
  269. chk(pApps->SaveChanges(&n));
  270. /* The app should be deleted if something fails after SaveChanges */
  271. unregisterOnFailure = true;
  272. chk(pObj->get_Key(&key));
  273. if (!GetModuleFileName(g_hinstDll, dllPath, sizeof(dllPath))) {
  274. hr = HRESULT_FROM_WIN32(GetLastError());
  275. errmsg(hr, "GetModuleFileName failed");
  276. goto out;
  277. }
  278. n = strlen(dllPath);
  279. if (n < 3) {
  280. hr = E_FAIL;
  281. errmsg(hr, "Failed to lookup dll");
  282. goto out;
  283. }
  284. strcpy(tlbPath, dllPath);
  285. strcpy(tlbPath+n-3, "tlb");
  286. qga_debug("Registering " QGA_PROVIDER_NAME ": %s %s",
  287. dllPath, tlbPath);
  288. if (!PathFileExists(tlbPath)) {
  289. hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
  290. errmsg(hr, "Failed to lookup tlb");
  291. goto out;
  292. }
  293. chk(pCatalog->CreateServiceForApplication(
  294. _bstr_t(QGA_PROVIDER_LNAME), _bstr_t(QGA_PROVIDER_LNAME),
  295. _bstr_t(L"SERVICE_DEMAND_START"), _bstr_t(L"SERVICE_ERROR_NORMAL"),
  296. _bstr_t(L""), _bstr_t(L".\\localsystem"), _bstr_t(L""), FALSE));
  297. chk(pCatalog->InstallComponent(_bstr_t(QGA_PROVIDER_LNAME),
  298. _bstr_t(dllPath), _bstr_t(tlbPath),
  299. _bstr_t("")));
  300. /* Setup roles of the application */
  301. chk(getNameByStringSID(administratorsGroupSID, buffer, &bufferLen));
  302. chk(pApps->GetCollection(_bstr_t(L"Roles"), key,
  303. (IDispatch **)pRoles.replace()));
  304. chk(pRoles->Populate());
  305. chk(pRoles->Add((IDispatch **)pObj.replace()));
  306. chk(put_Value(pObj, L"Name", buffer));
  307. chk(put_Value(pObj, L"Description", L"Administrators group"));
  308. chk(pRoles->SaveChanges(&n));
  309. chk(pObj->get_Key(&key));
  310. /* Setup users in the role */
  311. chk(pRoles->GetCollection(_bstr_t(L"UsersInRole"), key,
  312. (IDispatch **)pUsersInRole.replace()));
  313. chk(pUsersInRole->Populate());
  314. chk(pUsersInRole->Add((IDispatch **)pObj.replace()));
  315. chk(GetAdminName(&name));
  316. chk(put_Value(pObj, L"User", _bstr_t(".\\") + name));
  317. bufferLen = BUFFER_SIZE;
  318. chk(getNameByStringSID(systemUserSID, buffer, &bufferLen));
  319. chk(pUsersInRole->Add((IDispatch **)pObj.replace()));
  320. chk(put_Value(pObj, L"User", buffer));
  321. chk(pUsersInRole->SaveChanges(&n));
  322. out:
  323. if (unregisterOnFailure && FAILED(hr)) {
  324. COMUnregister();
  325. }
  326. qga_debug_end;
  327. return hr;
  328. }
  329. STDAPI_(void) CALLBACK DLLCOMRegister(HWND, HINSTANCE, LPSTR, int);
  330. STDAPI_(void) CALLBACK DLLCOMRegister(HWND, HINSTANCE, LPSTR, int)
  331. {
  332. COMRegister();
  333. }
  334. STDAPI_(void) CALLBACK DLLCOMUnregister(HWND, HINSTANCE, LPSTR, int);
  335. STDAPI_(void) CALLBACK DLLCOMUnregister(HWND, HINSTANCE, LPSTR, int)
  336. {
  337. COMUnregister();
  338. }
  339. static BOOL CreateRegistryKey(LPCTSTR key, LPCTSTR value, LPCTSTR data)
  340. {
  341. qga_debug_begin;
  342. HKEY hKey;
  343. LONG ret;
  344. DWORD size;
  345. ret = RegCreateKeyEx(HKEY_CLASSES_ROOT, key, 0, NULL,
  346. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
  347. if (ret != ERROR_SUCCESS) {
  348. goto out;
  349. }
  350. if (data != NULL) {
  351. size = strlen(data) + 1;
  352. } else {
  353. size = 0;
  354. }
  355. ret = RegSetValueEx(hKey, value, 0, REG_SZ, (LPBYTE)data, size);
  356. RegCloseKey(hKey);
  357. out:
  358. qga_debug_end;
  359. if (ret != ERROR_SUCCESS) {
  360. /* As we cannot printf within DllRegisterServer(), show a dialog. */
  361. errmsg_dialog(ret, "Cannot add registry", key);
  362. return FALSE;
  363. }
  364. return TRUE;
  365. }
  366. /* Register this dll as a VSS provider */
  367. STDAPI DllRegisterServer(void)
  368. {
  369. qga_debug_begin;
  370. COMInitializer initializer;
  371. COMPointer<IVssAdmin> pVssAdmin;
  372. HRESULT hr = E_FAIL;
  373. char dllPath[MAX_PATH];
  374. char key[256];
  375. if (!g_hinstDll) {
  376. errmsg_dialog(hr, "Module instance is not available");
  377. goto out;
  378. }
  379. /* Add this module to registry */
  380. sprintf(key, "CLSID\\%s", g_szClsid);
  381. if (!CreateRegistryKey(key, NULL, g_szClsid)) {
  382. goto out;
  383. }
  384. if (!GetModuleFileName(g_hinstDll, dllPath, sizeof(dllPath))) {
  385. errmsg_dialog(GetLastError(), "GetModuleFileName failed");
  386. goto out;
  387. }
  388. sprintf(key, "CLSID\\%s\\InprocServer32", g_szClsid);
  389. if (!CreateRegistryKey(key, NULL, dllPath)) {
  390. goto out;
  391. }
  392. if (!CreateRegistryKey(key, "ThreadingModel", "Apartment")) {
  393. goto out;
  394. }
  395. sprintf(key, "CLSID\\%s\\ProgID", g_szClsid);
  396. if (!CreateRegistryKey(key, NULL, g_szProgid)) {
  397. goto out;
  398. }
  399. if (!CreateRegistryKey(g_szProgid, NULL, QGA_PROVIDER_NAME)) {
  400. goto out;
  401. }
  402. sprintf(key, "%s\\CLSID", g_szProgid);
  403. if (!CreateRegistryKey(key, NULL, g_szClsid)) {
  404. goto out;
  405. }
  406. hr = CoCreateInstance(CLSID_VSSCoordinator, NULL, CLSCTX_ALL,
  407. IID_IVssAdmin, (void **)pVssAdmin.replace());
  408. if (FAILED(hr)) {
  409. errmsg_dialog(hr, "CoCreateInstance(VSSCoordinator) failed");
  410. goto out;
  411. }
  412. hr = pVssAdmin->RegisterProvider(g_gProviderId, CLSID_QGAVSSProvider,
  413. const_cast<WCHAR*>(QGA_PROVIDER_LNAME),
  414. VSS_PROV_SOFTWARE,
  415. const_cast<WCHAR*>(QGA_PROVIDER_VERSION),
  416. g_gProviderVersion);
  417. if (hr == (long int) VSS_E_PROVIDER_ALREADY_REGISTERED) {
  418. DllUnregisterServer();
  419. hr = pVssAdmin->RegisterProvider(g_gProviderId, CLSID_QGAVSSProvider,
  420. const_cast<WCHAR * >
  421. (QGA_PROVIDER_LNAME),
  422. VSS_PROV_SOFTWARE,
  423. const_cast<WCHAR * >
  424. (QGA_PROVIDER_VERSION),
  425. g_gProviderVersion);
  426. }
  427. if (FAILED(hr)) {
  428. errmsg_dialog(hr, "RegisterProvider failed");
  429. }
  430. out:
  431. if (FAILED(hr)) {
  432. DllUnregisterServer();
  433. }
  434. qga_debug_end;
  435. return hr;
  436. }
  437. /* Unregister this VSS hardware provider from the system */
  438. STDAPI DllUnregisterServer(void)
  439. {
  440. qga_debug_begin;
  441. TCHAR key[256];
  442. COMInitializer initializer;
  443. COMPointer<IVssAdmin> pVssAdmin;
  444. HRESULT hr = CoCreateInstance(CLSID_VSSCoordinator,
  445. NULL, CLSCTX_ALL, IID_IVssAdmin,
  446. (void **)pVssAdmin.replace());
  447. if (SUCCEEDED(hr)) {
  448. hr = pVssAdmin->UnregisterProvider(g_gProviderId);
  449. } else {
  450. errmsg(hr, "CoCreateInstance(VSSCoordinator) failed");
  451. }
  452. sprintf(key, "CLSID\\%s", g_szClsid);
  453. SHDeleteKey(HKEY_CLASSES_ROOT, key);
  454. SHDeleteKey(HKEY_CLASSES_ROOT, g_szProgid);
  455. qga_debug_end;
  456. return S_OK; /* Uninstall should never fail */
  457. }
  458. /* Support function to convert ASCII string into BSTR (used in _bstr_t) */
  459. namespace _com_util
  460. {
  461. BSTR WINAPI ConvertStringToBSTR(const char *ascii) {
  462. int len = strlen(ascii);
  463. BSTR bstr = SysAllocStringLen(NULL, len);
  464. if (!bstr) {
  465. return NULL;
  466. }
  467. if (mbstowcs(bstr, ascii, len) == (size_t)-1) {
  468. qga_debug("Failed to convert string '%s' into BSTR", ascii);
  469. bstr[0] = 0;
  470. }
  471. return bstr;
  472. }
  473. }
  474. /* Stop QGA VSS provider service using Winsvc API */
  475. STDAPI StopService(void)
  476. {
  477. qga_debug_begin;
  478. HRESULT hr = S_OK;
  479. SC_HANDLE manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  480. SC_HANDLE service = NULL;
  481. if (!manager) {
  482. errmsg(E_FAIL, "Failed to open service manager");
  483. hr = E_FAIL;
  484. goto out;
  485. }
  486. service = OpenService(manager, QGA_PROVIDER_NAME, SC_MANAGER_ALL_ACCESS);
  487. if (!service) {
  488. errmsg(E_FAIL, "Failed to open service");
  489. hr = E_FAIL;
  490. goto out;
  491. }
  492. if (!(ControlService(service, SERVICE_CONTROL_STOP, NULL))) {
  493. errmsg(E_FAIL, "Failed to stop service");
  494. hr = E_FAIL;
  495. }
  496. out:
  497. CloseServiceHandle(service);
  498. CloseServiceHandle(manager);
  499. qga_debug_end;
  500. return hr;
  501. }