VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/win/VBoxGuestInst.cpp@ 75547

Last change on this file since 75547 was 72829, checked in by vboxsync, 7 years ago

VBoxGuestInst.cpp: drop unused printf argument.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1/* $Id: VBoxGuestInst.cpp 72829 2018-07-03 16:10:21Z vboxsync $ */
2/** @file
3 * Small tool to (un)install the VBoxGuest device driver.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/win/windows.h>
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36
37#include <VBox/VBoxGuest.h> /* for VBOXGUEST_SERVICE_NAME */
38
39
40//#define TESTMODE
41
42
43
44static int installDriver(bool fStartIt)
45{
46 /*
47 * Assume it didn't exist, so we'll create the service.
48 */
49 SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
50 if (!hSMgrCreate)
51 {
52 printf("OpenSCManager(,,create) failed rc=%d\n", GetLastError());
53 return -1;
54 }
55
56 const char *pszSlashName = "\\VBoxGuest.sys";
57 char szDriver[MAX_PATH * 2];
58 GetCurrentDirectory(MAX_PATH, szDriver);
59 strcat(szDriver, pszSlashName);
60 if (GetFileAttributesA(szDriver) == INVALID_FILE_ATTRIBUTES)
61 {
62 GetSystemDirectory(szDriver, sizeof(szDriver));
63 strcat(strcat(szDriver, "\\drivers"), pszSlashName);
64
65 /* Try FAT name abbreviation. */
66 if (GetFileAttributesA(szDriver) == INVALID_FILE_ATTRIBUTES)
67 {
68 pszSlashName = "\\VBoxGst.sys";
69 GetCurrentDirectory(MAX_PATH, szDriver);
70 strcat(szDriver, pszSlashName);
71 if (GetFileAttributesA(szDriver) == INVALID_FILE_ATTRIBUTES)
72 {
73 GetSystemDirectory(szDriver, sizeof(szDriver));
74 strcat(strcat(szDriver, "\\drivers"), pszSlashName);
75
76 }
77 }
78 }
79
80 SC_HANDLE hService = CreateService(hSMgrCreate,
81 VBOXGUEST_SERVICE_NAME,
82 "VBoxGuest Support Driver",
83 SERVICE_QUERY_STATUS | (fStartIt ? SERVICE_START : 0),
84 SERVICE_KERNEL_DRIVER,
85 SERVICE_BOOT_START,
86 SERVICE_ERROR_NORMAL,
87 szDriver,
88 "System",
89 NULL, NULL, NULL, NULL);
90 if (hService)
91 {
92 printf("Successfully created service '%s' for driver '%s'.\n", VBOXGUEST_SERVICE_NAME, szDriver);
93 if (fStartIt)
94 {
95 if (StartService(hService, 0, NULL))
96 printf("successfully started driver '%s'\n", szDriver);
97 else
98 printf("StartService failed: %d\n", GetLastError());
99 }
100 CloseServiceHandle(hService);
101 }
102 else
103 printf("CreateService failed! lasterr=%d (szDriver=%s)\n", GetLastError(), szDriver);
104 CloseServiceHandle(hSMgrCreate);
105 return hService ? 0 : -1;
106}
107
108static int uninstallDriver(void)
109{
110 int rc = -1;
111 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
112 if (!hSMgr)
113 {
114 printf("OpenSCManager(,,delete) failed rc=%d\n", GetLastError());
115 return -1;
116 }
117 SC_HANDLE hService = OpenService(hSMgr, VBOXGUEST_SERVICE_NAME, SERVICE_STOP | SERVICE_QUERY_STATUS | DELETE);
118 if (hService)
119 {
120 /*
121 * Try stop it if it's running.
122 */
123 SERVICE_STATUS Status = { 0, 0, 0, 0, 0, 0, 0 };
124 QueryServiceStatus(hService, &Status);
125 if (Status.dwCurrentState == SERVICE_STOPPED)
126 rc = VINF_SUCCESS;
127 else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
128 {
129 int iWait = 100;
130 while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
131 {
132 Sleep(100);
133 QueryServiceStatus(hService, &Status);
134 }
135 if (Status.dwCurrentState == SERVICE_STOPPED)
136 rc = VINF_SUCCESS;
137 else
138 {
139 printf("Failed to stop service. status=%d (%#x)\n", Status.dwCurrentState, Status.dwCurrentState);
140 rc = VERR_GENERAL_FAILURE;
141 }
142 }
143 else
144 {
145 DWORD dwErr = GetLastError();
146 if ( Status.dwCurrentState == SERVICE_STOP_PENDING
147 && dwErr == ERROR_SERVICE_CANNOT_ACCEPT_CTRL)
148 rc = VERR_RESOURCE_BUSY; /* better than VERR_GENERAL_FAILURE */
149 else
150 {
151 printf("ControlService failed with dwErr=%u. status=%d (%#x)\n",
152 dwErr, Status.dwCurrentState, Status.dwCurrentState);
153 rc = -1;
154 }
155 }
156
157 /*
158 * Delete the service.
159 */
160 if (RT_SUCCESS(rc))
161 {
162 if (DeleteService(hService))
163 rc = 0;
164 else
165 {
166 printf("DeleteService failed lasterr=%d\n", GetLastError());
167 rc = -1;
168 }
169 }
170 CloseServiceHandle(hService);
171 }
172 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
173 rc = 0;
174 else
175 printf("OpenService failed lasterr=%d\n", GetLastError());
176 CloseServiceHandle(hSMgr);
177 return rc;
178}
179
180#ifdef TESTMODE
181
182static HANDLE openDriver(void)
183{
184 HANDLE hDevice;
185
186 hDevice = CreateFile(VBOXGUEST_DEVICE_NAME, // Win2k+: VBOXGUEST_DEVICE_NAME_GLOBAL
187 GENERIC_READ | GENERIC_WRITE,
188 FILE_SHARE_READ | FILE_SHARE_WRITE,
189 NULL,
190 OPEN_EXISTING,
191 FILE_ATTRIBUTE_NORMAL,
192 NULL);
193 if (hDevice == INVALID_HANDLE_VALUE)
194 {
195 printf("CreateFile did not work. GetLastError() 0x%x\n", GetLastError());
196 }
197 return hDevice;
198}
199
200static int closeDriver(HANDLE hDevice)
201{
202 CloseHandle(hDevice);
203 return 0;
204}
205
206static int performTest(void)
207{
208 int rc = 0;
209
210 HANDLE hDevice = openDriver();
211
212 if (hDevice != INVALID_HANDLE_VALUE)
213 closeDriver(hDevice);
214 else
215 printf("openDriver failed!\n");
216
217 return rc;
218}
219
220#endif /* TESTMODE */
221
222static int usage(char *programName)
223{
224 printf("error, syntax: %s [install|uninstall]\n", programName);
225 return 1;
226}
227
228int main(int argc, char **argv)
229{
230 bool installMode;
231#ifdef TESTMODE
232 bool testMode = false;
233#endif
234
235 if (argc != 2)
236 return usage(argv[0]);
237
238 if (strcmp(argv[1], "install") == 0)
239 installMode = true;
240 else if (strcmp(argv[1], "uninstall") == 0)
241 installMode = false;
242#ifdef TESTMODE
243 else if (strcmp(argv[1], "test") == 0)
244 testMode = true;
245#endif
246 else
247 return usage(argv[0]);
248
249
250 int rc;
251#ifdef TESTMODE
252 if (testMode)
253 rc = performTest();
254 else
255#endif
256 if (installMode)
257 rc = installDriver(true);
258 else
259 rc = uninstallDriver();
260
261 if (rc == 0)
262 printf("operation completed successfully!\n");
263 else
264 printf("error: operation failed with status code %d\n", rc);
265
266 return rc;
267}
268
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