VirtualBox

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

Last change on this file was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1/* $Id: USBUninstall.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VBox host drivers - USB drivers - Filter & driver uninstallation.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
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 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/win/windows.h>
42#include <iprt/win/setupapi.h>
43#include <newdev.h>
44
45#include <iprt/assert.h>
46#include <iprt/errcore.h>
47#include <iprt/initterm.h>
48#include <iprt/message.h>
49#include <iprt/param.h>
50#include <iprt/path.h>
51#include <iprt/string.h>
52#include <VBox/VBoxDrvCfg-win.h>
53
54
55/*********************************************************************************************************************************
56* Defined Constants And Macros *
57*********************************************************************************************************************************/
58/** The support service name. */
59#define SERVICE_NAME "VBoxUSBMon"
60/** Win32 Device name. */
61#define DEVICE_NAME "\\\\.\\VBoxUSBMon"
62/** NT Device name. */
63#define DEVICE_NAME_NT L"\\Device\\VBoxUSBMon"
64/** Win32 Symlink name. */
65#define DEVICE_NAME_DOS L"\\DosDevices\\VBoxUSBMon"
66
67
68/*********************************************************************************************************************************
69* Internal Functions *
70*********************************************************************************************************************************/
71static int usblibOsStopService(void);
72static int usblibOsDeleteService(void);
73
74
75static DECLCALLBACK(void) vboxUsbLog(VBOXDRVCFG_LOG_SEVERITY_T enmSeverity, char *pszMsg, void *pvContext)
76{
77 RT_NOREF1(pvContext);
78 switch (enmSeverity)
79 {
80 case VBOXDRVCFG_LOG_SEVERITY_FLOW:
81 case VBOXDRVCFG_LOG_SEVERITY_REGULAR:
82 break;
83 case VBOXDRVCFG_LOG_SEVERITY_REL:
84 RTMsgInfo("%s", pszMsg);
85 break;
86 default:
87 break;
88 }
89}
90
91static DECLCALLBACK(void) vboxUsbPanic(void *pvPanic)
92{
93 RT_NOREF1(pvPanic);
94#ifndef DEBUG_bird
95 AssertFailed();
96#endif
97}
98
99
100int __cdecl main(int argc, char **argv)
101{
102 RTR3InitExeNoArguments(0);
103 if (argc != 1)
104 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "This utility takes no arguments\n");
105 NOREF(argv);
106 RTMsgInfo("USB uninstallation\n");
107
108 VBoxDrvCfgLoggerSet(vboxUsbLog, NULL);
109 VBoxDrvCfgPanicSet(vboxUsbPanic, NULL);
110
111 usblibOsStopService();
112 usblibOsDeleteService();
113
114 HRESULT hr = VBoxDrvCfgInfUninstallAllF(L"USB", L"USB\\VID_80EE&PID_CAFE", SUOI_FORCEDELETE);
115 if (hr != S_OK)
116 return RTMsgErrorExitFailure("SetupUninstallOEMInf failed: %Rhrc\n", hr);
117
118 RTMsgInfo("USB uninstallation succeeded!");
119 return 0;
120}
121
122
123/**
124 * Stops a possibly running service.
125 *
126 * @returns 0 on success.
127 * @returns -1 on failure.
128 */
129static int usblibOsStopService(void)
130{
131 /*
132 * Assume it didn't exist, so we'll create the service.
133 */
134 int rc = -1;
135 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_STOP | SERVICE_QUERY_STATUS);
136 AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", GetLastError()));
137 if (hSMgr)
138 {
139 SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, SERVICE_STOP | SERVICE_QUERY_STATUS);
140 if (hService)
141 {
142 /*
143 * Stop the service.
144 */
145 SERVICE_STATUS Status;
146 QueryServiceStatus(hService, &Status);
147 if (Status.dwCurrentState == SERVICE_STOPPED)
148 rc = 0;
149 else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
150 {
151 /*
152 * Wait for finish about 1 minute.
153 * It should be enough for work with driver verifier
154 */
155 int iWait = 600;
156 while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
157 {
158 Sleep(100);
159 QueryServiceStatus(hService, &Status);
160 }
161 if (Status.dwCurrentState == SERVICE_STOPPED)
162 rc = 0;
163 else
164 AssertMsgFailed(("Failed to stop service. status=%d\n", Status.dwCurrentState));
165 }
166 else
167 AssertMsgFailed(("ControlService failed with LastError=%Rwa. status=%d\n", GetLastError(), Status.dwCurrentState));
168 CloseServiceHandle(hService);
169 }
170 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
171 rc = 0;
172 else
173 AssertMsgFailed(("OpenService failed LastError=%Rwa\n", GetLastError()));
174 CloseServiceHandle(hSMgr);
175 }
176 return rc;
177}
178
179
180/**
181 * Deletes the service.
182 *
183 * @returns 0 on success.
184 * @returns -1 on failure.
185 */
186static int usblibOsDeleteService(void)
187{
188 /*
189 * Assume it didn't exist, so we'll create the service.
190 */
191 int rc = -1;
192 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
193 AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", GetLastError()));
194 if (hSMgr)
195 {
196 SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, DELETE);
197 if (hService)
198 {
199 /*
200 * Delete the service.
201 */
202 if (DeleteService(hService))
203 rc = 0;
204 else
205 AssertMsgFailed(("DeleteService failed LastError=%Rwa\n", GetLastError()));
206 CloseServiceHandle(hService);
207 }
208 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
209 rc = 0;
210 else
211 AssertMsgFailed(("OpenService failed LastError=%Rwa\n", GetLastError()));
212 CloseServiceHandle(hSMgr);
213 }
214 return rc;
215}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use