VirtualBox

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

Last change on this file since 44528 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/** @file
2 *
3 * VBox host drivers - USB drivers - Filter & driver uninstallation
4 *
5 * Installation code
6 *
7 * Copyright (C) 2006-2011 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 AssertFailed();
57}
58
59
60int __cdecl main(int argc, char **argv)
61{
62 printf("USB uninstallation\n");
63
64 VBoxDrvCfgLoggerSet(vboxUsbLog, NULL);
65 VBoxDrvCfgPanicSet(vboxUsbPanic, NULL);
66
67 usblibOsStopService();
68 usblibOsDeleteService();
69
70 HRESULT hr = VBoxDrvCfgInfUninstallAllF(L"USB", L"USB\\VID_80EE&PID_CAFE", SUOI_FORCEDELETE);
71 if (hr != S_OK)
72 {
73 printf("SetupUninstallOEMInf failed with hr=0x%x\n", hr);
74 return 1;
75 }
76
77 printf("USB uninstallation succeeded!\n");
78
79 return 0;
80}
81
82/** The support service name. */
83#define SERVICE_NAME "VBoxUSBMon"
84/** Win32 Device name. */
85#define DEVICE_NAME "\\\\.\\VBoxUSBMon"
86/** NT Device name. */
87#define DEVICE_NAME_NT L"\\Device\\VBoxUSBMon"
88/** Win32 Symlink name. */
89#define DEVICE_NAME_DOS L"\\DosDevices\\VBoxUSBMon"
90
91/**
92 * Stops a possibly running service.
93 *
94 * @returns 0 on success.
95 * @returns -1 on failure.
96 */
97int usblibOsStopService(void)
98{
99 /*
100 * Assume it didn't exist, so we'll create the service.
101 */
102 int rc = -1;
103 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_STOP | SERVICE_QUERY_STATUS);
104 DWORD LastError = GetLastError(); NOREF(LastError);
105 AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
106 if (hSMgr)
107 {
108 SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, SERVICE_STOP | SERVICE_QUERY_STATUS);
109 if (hService)
110 {
111 /*
112 * Stop the service.
113 */
114 SERVICE_STATUS Status;
115 QueryServiceStatus(hService, &Status);
116 if (Status.dwCurrentState == SERVICE_STOPPED)
117 rc = 0;
118 else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
119 {
120 int iWait = 100;
121 while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
122 {
123 Sleep(100);
124 QueryServiceStatus(hService, &Status);
125 }
126 if (Status.dwCurrentState == SERVICE_STOPPED)
127 rc = 0;
128 else
129 AssertMsgFailed(("Failed to stop service. status=%d\n", Status.dwCurrentState));
130 }
131 else
132 {
133 DWORD LastError = GetLastError(); NOREF(LastError);
134 AssertMsgFailed(("ControlService failed with LastError=%Rwa. status=%d\n", LastError, Status.dwCurrentState));
135 }
136 CloseServiceHandle(hService);
137 }
138 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
139 rc = 0;
140 else
141 {
142 DWORD LastError = GetLastError(); NOREF(LastError);
143 AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
144 }
145 CloseServiceHandle(hSMgr);
146 }
147 return rc;
148}
149
150
151/**
152 * Deletes the service.
153 *
154 * @returns 0 on success.
155 * @returns -1 on failure.
156 */
157int usblibOsDeleteService(void)
158{
159 /*
160 * Assume it didn't exist, so we'll create the service.
161 */
162 int rc = -1;
163 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
164 DWORD LastError = GetLastError(); NOREF(LastError);
165 AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
166 if (hSMgr)
167 {
168 SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, DELETE);
169 if (hService)
170 {
171 /*
172 * Delete the service.
173 */
174 if (DeleteService(hService))
175 rc = 0;
176 else
177 {
178 DWORD LastError = GetLastError(); NOREF(LastError);
179 AssertMsgFailed(("DeleteService failed LastError=%Rwa\n", LastError));
180 }
181 CloseServiceHandle(hService);
182 }
183 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
184 rc = 0;
185 else
186 {
187 DWORD LastError = GetLastError(); NOREF(LastError);
188 AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
189 }
190 CloseServiceHandle(hSMgr);
191 }
192 return rc;
193}
Note: See TracBrowser for help on using the repository browser.

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