VirtualBox

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

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

export the VBoxUSB host driver to OSE

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 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 * Oracle Corporation confidential
10 * All rights reserved
11 */
12
13
14/*******************************************************************************
15* Header Files *
16*******************************************************************************/
17#include <windows.h>
18#include <setupapi.h>
19#include <newdev.h>
20#include <iprt/assert.h>
21#include <iprt/err.h>
22#include <iprt/param.h>
23#include <iprt/path.h>
24#include <iprt/string.h>
25#include <VBox/err.h>
26#include <stdio.h>
27
28
29int usblibOsStopService(void);
30int usblibOsDeleteService(void);
31
32
33int __cdecl main(int argc, char **argv)
34{
35 BOOL rc;
36 TCHAR szFullPath[MAX_PATH];
37 CHAR *lpszFilePart;
38 int len;
39
40 printf("USB uninstallation\n");
41
42 usblibOsStopService();
43 usblibOsDeleteService();
44
45 len = GetFullPathName(".\\VBoxUSB.inf", sizeof(szFullPath), szFullPath, &lpszFilePart);
46 Assert(len);
47
48 /* Remove the inf plus all associated files. */
49 rc = SetupUninstallOEMInf(szFullPath, SUOI_FORCEDELETE, NULL);
50 if (rc == FALSE)
51 {
52 printf("SetupUninstallOEMInf failed with rc=%x\n", GetLastError());
53 return 1;
54 }
55 return 0;
56}
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 * Stops a possibly running service.
69 *
70 * @returns 0 on success.
71 * @returns -1 on failure.
72 */
73int usblibOsStopService(void)
74{
75 /*
76 * Assume it didn't exist, so we'll create the service.
77 */
78 int rc = -1;
79 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_STOP | SERVICE_QUERY_STATUS);
80 DWORD LastError = GetLastError(); NOREF(LastError);
81 AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
82 if (hSMgr)
83 {
84 SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, SERVICE_STOP | SERVICE_QUERY_STATUS);
85 if (hService)
86 {
87 /*
88 * Stop the service.
89 */
90 SERVICE_STATUS Status;
91 QueryServiceStatus(hService, &Status);
92 if (Status.dwCurrentState == SERVICE_STOPPED)
93 rc = 0;
94 else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
95 {
96 int iWait = 100;
97 while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
98 {
99 Sleep(100);
100 QueryServiceStatus(hService, &Status);
101 }
102 if (Status.dwCurrentState == SERVICE_STOPPED)
103 rc = 0;
104 else
105 AssertMsgFailed(("Failed to stop service. status=%d\n", Status.dwCurrentState));
106 }
107 else
108 {
109 DWORD LastError = GetLastError(); NOREF(LastError);
110 AssertMsgFailed(("ControlService failed with LastError=%Rwa. status=%d\n", LastError, Status.dwCurrentState));
111 }
112 CloseServiceHandle(hService);
113 }
114 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
115 rc = 0;
116 else
117 {
118 DWORD LastError = GetLastError(); NOREF(LastError);
119 AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
120 }
121 CloseServiceHandle(hSMgr);
122 }
123 return rc;
124}
125
126
127/**
128 * Deletes the service.
129 *
130 * @returns 0 on success.
131 * @returns -1 on failure.
132 */
133int usblibOsDeleteService(void)
134{
135 /*
136 * Assume it didn't exist, so we'll create the service.
137 */
138 int rc = -1;
139 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
140 DWORD LastError = GetLastError(); NOREF(LastError);
141 AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
142 if (hSMgr)
143 {
144 SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, DELETE);
145 if (hService)
146 {
147 /*
148 * Delete the service.
149 */
150 if (DeleteService(hService))
151 rc = 0;
152 else
153 {
154 DWORD LastError = GetLastError(); NOREF(LastError);
155 AssertMsgFailed(("DeleteService failed LastError=%Rwa\n", LastError));
156 }
157 CloseServiceHandle(hService);
158 }
159 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
160 rc = 0;
161 else
162 {
163 DWORD LastError = GetLastError(); NOREF(LastError);
164 AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
165 }
166 CloseServiceHandle(hSMgr);
167 }
168 return rc;
169}
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