VirtualBox

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

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

these are driving me crazy.

  • 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#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 int iWait = 100;
123 while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
124 {
125 Sleep(100);
126 QueryServiceStatus(hService, &Status);
127 }
128 if (Status.dwCurrentState == SERVICE_STOPPED)
129 rc = 0;
130 else
131 AssertMsgFailed(("Failed to stop service. status=%d\n", Status.dwCurrentState));
132 }
133 else
134 {
135 DWORD LastError = GetLastError(); NOREF(LastError);
136 AssertMsgFailed(("ControlService failed with LastError=%Rwa. status=%d\n", LastError, Status.dwCurrentState));
137 }
138 CloseServiceHandle(hService);
139 }
140 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
141 rc = 0;
142 else
143 {
144 DWORD LastError = GetLastError(); NOREF(LastError);
145 AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
146 }
147 CloseServiceHandle(hSMgr);
148 }
149 return rc;
150}
151
152
153/**
154 * Deletes the service.
155 *
156 * @returns 0 on success.
157 * @returns -1 on failure.
158 */
159int usblibOsDeleteService(void)
160{
161 /*
162 * Assume it didn't exist, so we'll create the service.
163 */
164 int rc = -1;
165 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
166 DWORD LastError = GetLastError(); NOREF(LastError);
167 AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
168 if (hSMgr)
169 {
170 SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, DELETE);
171 if (hService)
172 {
173 /*
174 * Delete the service.
175 */
176 if (DeleteService(hService))
177 rc = 0;
178 else
179 {
180 DWORD LastError = GetLastError(); NOREF(LastError);
181 AssertMsgFailed(("DeleteService failed LastError=%Rwa\n", LastError));
182 }
183 CloseServiceHandle(hService);
184 }
185 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
186 rc = 0;
187 else
188 {
189 DWORD LastError = GetLastError(); NOREF(LastError);
190 AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
191 }
192 CloseServiceHandle(hSMgr);
193 }
194 return rc;
195}
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