VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxUSB/win/Install/USBUninstall.cpp@ 61556

Last change on this file since 61556 was 57934, checked in by vboxsync, 9 years ago

HosDrivers/USBMon: fixed BSOD under driver verifier. Increased timeout in USBMon Uninstaller.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
1/** @file
2 *
3 * VBox host drivers - USB drivers - Filter & driver uninstallation
4 *
5 * Installation code
6 *
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <windows.h>
23#include <setupapi.h>
24#include <newdev.h>
25
26#include <iprt/assert.h>
27#include <iprt/err.h>
28#include <iprt/param.h>
29#include <iprt/path.h>
30#include <iprt/string.h>
31#include <VBox/err.h>
32#include <VBox/VBoxDrvCfg-win.h>
33#include <stdio.h>
34
35
36int usblibOsStopService(void);
37int usblibOsDeleteService(void);
38
39static DECLCALLBACK(void) vboxUsbLog(VBOXDRVCFG_LOG_SEVERITY enmSeverity, char * msg, void * pvContext)
40{
41 switch (enmSeverity)
42 {
43 case VBOXDRVCFG_LOG_SEVERITY_FLOW:
44 case VBOXDRVCFG_LOG_SEVERITY_REGULAR:
45 break;
46 case VBOXDRVCFG_LOG_SEVERITY_REL:
47 printf("%s", msg);
48 break;
49 default:
50 break;
51 }
52}
53
54static DECLCALLBACK(void) vboxUsbPanic(void * pvPanic)
55{
56#ifndef DEBUG_bird
57 AssertFailed();
58#endif
59}
60
61
62int __cdecl main(int argc, char **argv)
63{
64 printf("USB uninstallation\n");
65
66 VBoxDrvCfgLoggerSet(vboxUsbLog, NULL);
67 VBoxDrvCfgPanicSet(vboxUsbPanic, NULL);
68
69 usblibOsStopService();
70 usblibOsDeleteService();
71
72 HRESULT hr = VBoxDrvCfgInfUninstallAllF(L"USB", L"USB\\VID_80EE&PID_CAFE", SUOI_FORCEDELETE);
73 if (hr != S_OK)
74 {
75 printf("SetupUninstallOEMInf failed with hr=0x%x\n", hr);
76 return 1;
77 }
78
79 printf("USB uninstallation succeeded!\n");
80
81 return 0;
82}
83
84/** The support service name. */
85#define SERVICE_NAME "VBoxUSBMon"
86/** Win32 Device name. */
87#define DEVICE_NAME "\\\\.\\VBoxUSBMon"
88/** NT Device name. */
89#define DEVICE_NAME_NT L"\\Device\\VBoxUSBMon"
90/** Win32 Symlink name. */
91#define DEVICE_NAME_DOS L"\\DosDevices\\VBoxUSBMon"
92
93/**
94 * Stops a possibly running service.
95 *
96 * @returns 0 on success.
97 * @returns -1 on failure.
98 */
99int usblibOsStopService(void)
100{
101 /*
102 * Assume it didn't exist, so we'll create the service.
103 */
104 int rc = -1;
105 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_STOP | SERVICE_QUERY_STATUS);
106 DWORD LastError = GetLastError(); NOREF(LastError);
107 AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
108 if (hSMgr)
109 {
110 SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, SERVICE_STOP | SERVICE_QUERY_STATUS);
111 if (hService)
112 {
113 /*
114 * Stop the service.
115 */
116 SERVICE_STATUS Status;
117 QueryServiceStatus(hService, &Status);
118 if (Status.dwCurrentState == SERVICE_STOPPED)
119 rc = 0;
120 else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
121 {
122 /*
123 * Wait for finish about 1 minute.
124 * It should be enough for work with driver verifier
125 */
126 int iWait = 600;
127 while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
128 {
129 Sleep(100);
130 QueryServiceStatus(hService, &Status);
131 }
132 if (Status.dwCurrentState == SERVICE_STOPPED)
133 rc = 0;
134 else
135 AssertMsgFailed(("Failed to stop service. status=%d\n", Status.dwCurrentState));
136 }
137 else
138 {
139 DWORD LastError = GetLastError(); NOREF(LastError);
140 AssertMsgFailed(("ControlService failed with LastError=%Rwa. status=%d\n", LastError, Status.dwCurrentState));
141 }
142 CloseServiceHandle(hService);
143 }
144 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
145 rc = 0;
146 else
147 {
148 DWORD LastError = GetLastError(); NOREF(LastError);
149 AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
150 }
151 CloseServiceHandle(hSMgr);
152 }
153 return rc;
154}
155
156
157/**
158 * Deletes the service.
159 *
160 * @returns 0 on success.
161 * @returns -1 on failure.
162 */
163int usblibOsDeleteService(void)
164{
165 /*
166 * Assume it didn't exist, so we'll create the service.
167 */
168 int rc = -1;
169 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
170 DWORD LastError = GetLastError(); NOREF(LastError);
171 AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
172 if (hSMgr)
173 {
174 SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, DELETE);
175 if (hService)
176 {
177 /*
178 * Delete the service.
179 */
180 if (DeleteService(hService))
181 rc = 0;
182 else
183 {
184 DWORD LastError = GetLastError(); NOREF(LastError);
185 AssertMsgFailed(("DeleteService failed LastError=%Rwa\n", LastError));
186 }
187 CloseServiceHandle(hService);
188 }
189 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
190 rc = 0;
191 else
192 {
193 DWORD LastError = GetLastError(); NOREF(LastError);
194 AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
195 }
196 CloseServiceHandle(hSMgr);
197 }
198 return rc;
199}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette