VirtualBox

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

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

VBoxGuest: NT 3.1 support.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 KB
Line 
1/* $Id: VBoxGuestInst.cpp 70342 2017-12-26 14:42:45Z 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 uint32_t const uMajorNtVer = GetVersion() & 0xff;
57 const char *pszSlashName = uMajorNtVer < 4 ? "\\VBoxGuestNT3.sys" : "\\VBoxGuest.sys";
58 char szDriver[MAX_PATH * 2];
59 GetCurrentDirectory(MAX_PATH, szDriver);
60 strcat(szDriver, pszSlashName);
61 if (GetFileAttributesA(szDriver) == INVALID_FILE_ATTRIBUTES)
62 {
63 GetSystemDirectory(szDriver, sizeof(szDriver));
64 strcat(strcat(szDriver, "\\drivers"), pszSlashName);
65
66 /* Try FAT name abbreviation. */
67 if ( GetFileAttributesA(szDriver) == INVALID_FILE_ATTRIBUTES
68 && uMajorNtVer < 4)
69 {
70 pszSlashName = "\\VBoxGst3.sys";
71 GetCurrentDirectory(MAX_PATH, szDriver);
72 strcat(szDriver, pszSlashName);
73 if (GetFileAttributesA(szDriver) == INVALID_FILE_ATTRIBUTES)
74 {
75 GetSystemDirectory(szDriver, sizeof(szDriver));
76 strcat(strcat(szDriver, "\\drivers"), pszSlashName);
77
78 }
79 }
80 }
81
82 SC_HANDLE hService = CreateService(hSMgrCreate,
83 VBOXGUEST_SERVICE_NAME,
84 "VBoxGuest Support Driver",
85 SERVICE_QUERY_STATUS | (fStartIt ? SERVICE_START : 0),
86 SERVICE_KERNEL_DRIVER,
87 SERVICE_BOOT_START,
88 SERVICE_ERROR_NORMAL,
89 szDriver,
90 "System",
91 NULL, NULL, NULL, NULL);
92 if (hService)
93 {
94 printf("Successfully created service '%s' for driver '%s'.\n", VBOXGUEST_SERVICE_NAME, szDriver);
95 if (fStartIt)
96 {
97 if (StartService(hService, 0, NULL))
98 printf("successfully started driver '%s'\n", szDriver);
99 else
100 printf("StartService failed: %d\n", GetLastError(), szDriver);
101 }
102 CloseServiceHandle(hService);
103 }
104 else
105 printf("CreateService failed! lasterr=%d (szDriver=%s)\n", GetLastError(), szDriver);
106 CloseServiceHandle(hSMgrCreate);
107 return hService ? 0 : -1;
108}
109
110static int uninstallDriver(void)
111{
112 int rc = -1;
113 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
114 if (!hSMgr)
115 {
116 printf("OpenSCManager(,,delete) failed rc=%d\n", GetLastError());
117 return -1;
118 }
119 SC_HANDLE hService = OpenService(hSMgr, VBOXGUEST_SERVICE_NAME, SERVICE_STOP | SERVICE_QUERY_STATUS | DELETE);
120 if (hService)
121 {
122 /*
123 * Try stop it if it's running.
124 */
125 SERVICE_STATUS Status = { 0, 0, 0, 0, 0, 0, 0 };
126 QueryServiceStatus(hService, &Status);
127 if (Status.dwCurrentState == SERVICE_STOPPED)
128 rc = VINF_SUCCESS;
129 else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
130 {
131 int iWait = 100;
132 while (Status.dwCurrentState == SERVICE_STOP_PENDING && iWait-- > 0)
133 {
134 Sleep(100);
135 QueryServiceStatus(hService, &Status);
136 }
137 if (Status.dwCurrentState == SERVICE_STOPPED)
138 rc = VINF_SUCCESS;
139 else
140 {
141 printf("Failed to stop service. status=%d (%#x)\n", Status.dwCurrentState, Status.dwCurrentState);
142 rc = VERR_GENERAL_FAILURE;
143 }
144 }
145 else
146 {
147 DWORD dwErr = GetLastError();
148 if ( Status.dwCurrentState == SERVICE_STOP_PENDING
149 && dwErr == ERROR_SERVICE_CANNOT_ACCEPT_CTRL)
150 rc = VERR_RESOURCE_BUSY; /* better than VERR_GENERAL_FAILURE */
151 else
152 {
153 printf("ControlService failed with dwErr=%u. status=%d (%#x)\n",
154 dwErr, Status.dwCurrentState, Status.dwCurrentState);
155 rc = -1;
156 }
157 }
158
159 /*
160 * Delete the service.
161 */
162 if (RT_SUCCESS(rc))
163 {
164 if (DeleteService(hService))
165 rc = 0;
166 else
167 {
168 printf("DeleteService failed lasterr=%d\n", GetLastError());
169 rc = -1;
170 }
171 }
172 CloseServiceHandle(hService);
173 }
174 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
175 rc = 0;
176 else
177 printf("OpenService failed lasterr=%d\n", GetLastError());
178 CloseServiceHandle(hSMgr);
179 return rc;
180}
181
182#ifdef TESTMODE
183
184static HANDLE openDriver(void)
185{
186 HANDLE hDevice;
187
188 hDevice = CreateFile(VBOXGUEST_DEVICE_NAME, // Win2k+: VBOXGUEST_DEVICE_NAME_GLOBAL
189 GENERIC_READ | GENERIC_WRITE,
190 FILE_SHARE_READ | FILE_SHARE_WRITE,
191 NULL,
192 OPEN_EXISTING,
193 FILE_ATTRIBUTE_NORMAL,
194 NULL);
195 if (hDevice == INVALID_HANDLE_VALUE)
196 {
197 printf("CreateFile did not work. GetLastError() 0x%x\n", GetLastError());
198 }
199 return hDevice;
200}
201
202static int closeDriver(HANDLE hDevice)
203{
204 CloseHandle(hDevice);
205 return 0;
206}
207
208static int performTest(void)
209{
210 int rc = 0;
211
212 HANDLE hDevice = openDriver();
213
214 if (hDevice != INVALID_HANDLE_VALUE)
215 closeDriver(hDevice);
216 else
217 printf("openDriver failed!\n");
218
219 return rc;
220}
221
222#endif /* TESTMODE */
223
224static int usage(char *programName)
225{
226 printf("error, syntax: %s [install|uninstall]\n", programName);
227 return 1;
228}
229
230int main(int argc, char **argv)
231{
232 bool installMode;
233#ifdef TESTMODE
234 bool testMode = false;
235#endif
236
237 if (argc != 2)
238 return usage(argv[0]);
239
240 if (strcmp(argv[1], "install") == 0)
241 installMode = true;
242 else if (strcmp(argv[1], "uninstall") == 0)
243 installMode = false;
244#ifdef TESTMODE
245 else if (strcmp(argv[1], "test") == 0)
246 testMode = true;
247#endif
248 else
249 return usage(argv[0]);
250
251
252 int rc;
253#ifdef TESTMODE
254 if (testMode)
255 rc = performTest();
256 else
257#endif
258 if (installMode)
259 rc = installDriver(true);
260 else
261 rc = uninstallDriver();
262
263 if (rc == 0)
264 printf("operation completed successfully!\n");
265 else
266 printf("error: operation failed with status code %d\n", rc);
267
268 return rc;
269}
270
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