VirtualBox

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

Last change on this file since 36657 was 31898, checked in by vboxsync, 14 years ago

OSE header fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1/** @file
2 *
3 * VBox host drivers - USB drivers - Filter & driver uninstallation
4 *
5 * Installation code
6 *
7 * Copyright (C) 2006-2007 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#include <iprt/assert.h>
26#include <iprt/err.h>
27#include <iprt/param.h>
28#include <iprt/path.h>
29#include <iprt/string.h>
30#include <VBox/err.h>
31#include <stdio.h>
32
33
34int usblibOsStopService(void);
35int usblibOsDeleteService(void);
36
37
38int __cdecl main(int argc, char **argv)
39{
40 BOOL rc;
41 TCHAR szFullPath[MAX_PATH];
42 CHAR *lpszFilePart;
43 int len;
44
45 printf("USB uninstallation\n");
46
47 usblibOsStopService();
48 usblibOsDeleteService();
49
50 len = GetFullPathName(".\\VBoxUSB.inf", sizeof(szFullPath), szFullPath, &lpszFilePart);
51 Assert(len);
52
53 /* Remove the inf plus all associated files. */
54 rc = SetupUninstallOEMInf(szFullPath, SUOI_FORCEDELETE, NULL);
55 if (rc == FALSE)
56 {
57 printf("SetupUninstallOEMInf failed with rc=%x\n", GetLastError());
58 return 1;
59 }
60 return 0;
61}
62
63/** The support service name. */
64#define SERVICE_NAME "VBoxUSBMon"
65/** Win32 Device name. */
66#define DEVICE_NAME "\\\\.\\VBoxUSBMon"
67/** NT Device name. */
68#define DEVICE_NAME_NT L"\\Device\\VBoxUSBMon"
69/** Win32 Symlink name. */
70#define DEVICE_NAME_DOS L"\\DosDevices\\VBoxUSBMon"
71
72/**
73 * Stops a possibly running service.
74 *
75 * @returns 0 on success.
76 * @returns -1 on failure.
77 */
78int usblibOsStopService(void)
79{
80 /*
81 * Assume it didn't exist, so we'll create the service.
82 */
83 int rc = -1;
84 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_STOP | SERVICE_QUERY_STATUS);
85 DWORD LastError = GetLastError(); NOREF(LastError);
86 AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
87 if (hSMgr)
88 {
89 SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, SERVICE_STOP | SERVICE_QUERY_STATUS);
90 if (hService)
91 {
92 /*
93 * Stop the service.
94 */
95 SERVICE_STATUS Status;
96 QueryServiceStatus(hService, &Status);
97 if (Status.dwCurrentState == SERVICE_STOPPED)
98 rc = 0;
99 else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
100 {
101 int iWait = 100;
102 while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
103 {
104 Sleep(100);
105 QueryServiceStatus(hService, &Status);
106 }
107 if (Status.dwCurrentState == SERVICE_STOPPED)
108 rc = 0;
109 else
110 AssertMsgFailed(("Failed to stop service. status=%d\n", Status.dwCurrentState));
111 }
112 else
113 {
114 DWORD LastError = GetLastError(); NOREF(LastError);
115 AssertMsgFailed(("ControlService failed with LastError=%Rwa. status=%d\n", LastError, Status.dwCurrentState));
116 }
117 CloseServiceHandle(hService);
118 }
119 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
120 rc = 0;
121 else
122 {
123 DWORD LastError = GetLastError(); NOREF(LastError);
124 AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
125 }
126 CloseServiceHandle(hSMgr);
127 }
128 return rc;
129}
130
131
132/**
133 * Deletes the service.
134 *
135 * @returns 0 on success.
136 * @returns -1 on failure.
137 */
138int usblibOsDeleteService(void)
139{
140 /*
141 * Assume it didn't exist, so we'll create the service.
142 */
143 int rc = -1;
144 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
145 DWORD LastError = GetLastError(); NOREF(LastError);
146 AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
147 if (hSMgr)
148 {
149 SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, DELETE);
150 if (hService)
151 {
152 /*
153 * Delete the service.
154 */
155 if (DeleteService(hService))
156 rc = 0;
157 else
158 {
159 DWORD LastError = GetLastError(); NOREF(LastError);
160 AssertMsgFailed(("DeleteService failed LastError=%Rwa\n", LastError));
161 }
162 CloseServiceHandle(hService);
163 }
164 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
165 rc = 0;
166 else
167 {
168 DWORD LastError = GetLastError(); NOREF(LastError);
169 AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
170 }
171 CloseServiceHandle(hSMgr);
172 }
173 return rc;
174}
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