VirtualBox

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

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

VBoxGuestNt3.sys: Use the regular driver for NT3 too.

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