VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/VBoxGuest/VBoxGuestInst.cpp@ 2981

Last change on this file since 2981 was 2981, checked in by vboxsync, 17 years ago

InnoTek -> innotek: all the headers and comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/** @file
2 *
3 * Small tool to (un)install the VBoxGuest device driver
4 *
5 * Copyright (C) 2006-2007 innotek GmbH
6 *
7 * This file is part of VirtualBox Open Source Edition (OSE), as
8 * available from http://www.virtualbox.org. This file is free software;
9 * you can redistribute it and/or modify it under the terms of the GNU
10 * General Public License as published by the Free Software Foundation,
11 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
12 * distribution. VirtualBox OSE is distributed in the hope that it will
13 * be useful, but WITHOUT ANY WARRANTY of any kind.
14 *
15 * If you received this file as part of a commercial VirtualBox
16 * distribution, then only the terms of your commercial VirtualBox
17 * license agreement apply instead of the previous paragraph.
18 *
19 */
20
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#include <windows.h>
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30
31#include <VBox/VBoxGuest.h>
32
33
34//#define TESTMODE
35
36
37
38int installDriver(void)
39{
40 /*
41 * Assume it didn't exist, so we'll create the service.
42 */
43 SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
44 if (!hSMgrCreate)
45 {
46 printf("OpenSCManager(,,create) failed rc=%d\n", GetLastError());
47 return -1;
48 }
49
50 char szDriver[MAX_PATH];
51 GetSystemDirectory(szDriver, sizeof(szDriver));
52 strcat(szDriver, "\\drivers\\VBoxGuestNT.sys");
53
54 SC_HANDLE hService = CreateService(hSMgrCreate,
55 VBOXGUEST_SERVICE_NAME,
56 "VBoxGuest Support Driver",
57 SERVICE_QUERY_STATUS,
58 SERVICE_KERNEL_DRIVER,
59 SERVICE_BOOT_START,
60 SERVICE_ERROR_NORMAL,
61 szDriver,
62 "System",
63 NULL, NULL, NULL, NULL);
64 if (!hService)
65 {
66 printf("CreateService failed! lasterr=%d\n", GetLastError());
67 } else
68 {
69 CloseServiceHandle(hService);
70 }
71 CloseServiceHandle(hSMgrCreate);
72 return hService ? 0 : -1;
73}
74
75int uninstallDriver(void)
76{
77 int rc = -1;
78 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
79 if (!hSMgr)
80 {
81 printf("OpenSCManager(,,delete) failed rc=%d\n", GetLastError());
82 return -1;
83 }
84 SC_HANDLE hService = OpenService(hSMgr, VBOXGUEST_SERVICE_NAME, DELETE);
85 if (hService)
86 {
87 /*
88 * Delete the service.
89 */
90 if (DeleteService(hService))
91 rc = 0;
92 else
93 printf("DeleteService failed lasterr=%d\n", GetLastError());
94 CloseServiceHandle(hService);
95 }
96 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
97 rc = 0;
98 else
99 printf("OpenService failed lasterr=%d\n", GetLastError());
100 CloseServiceHandle(hSMgr);
101 return rc;
102}
103
104
105HANDLE openDriver(void)
106{
107 HANDLE hDevice;
108
109 hDevice = CreateFile(VBOXGUEST_DEVICE_NAME, // Win2k+: VBOXGUEST_DEVICE_NAME_GLOBAL
110 GENERIC_READ | GENERIC_WRITE,
111 FILE_SHARE_READ | FILE_SHARE_WRITE,
112 NULL,
113 OPEN_EXISTING,
114 FILE_ATTRIBUTE_NORMAL,
115 NULL);
116 if (hDevice == INVALID_HANDLE_VALUE)
117 {
118 printf("CreateFile did not work. GetLastError() 0x%x\n", GetLastError());
119 }
120 return hDevice;
121}
122
123int closeDriver(HANDLE hDevice)
124{
125 CloseHandle(hDevice);
126 return 0;
127}
128
129#ifdef TESTMODE
130typedef struct TESTFOO
131{
132 int values[16];
133} TESTFOO, *PTESTFOO;
134
135int performTest(void)
136{
137 int rc = 0;
138
139 HANDLE hDevice = openDriver();
140
141 if (hDevice != INVALID_HANDLE_VALUE)
142 {
143 DWORD cbReturned;
144
145 closeDriver(hDevice);
146 } else
147 {
148 printf("openDriver failed!\n");
149 }
150
151
152 return rc;
153}
154#endif
155
156void displayHelpAndExit(char *programName)
157{
158 printf("error, syntax: %s [install|uninstall]\n", programName);
159 exit(1);
160}
161
162int main(int argc, char **argv)
163{
164 bool installMode;
165#ifdef TESTMODE
166 bool testMode = false;
167#endif
168
169 if (argc != 2)
170 {
171 displayHelpAndExit(argv[0]);
172 }
173 if (strcmp(argv[1], "install") == 0)
174 {
175 installMode = true;
176 } else
177 if (strcmp(argv[1], "uninstall") == 0)
178 {
179 installMode = false;
180 } else
181#ifdef TESTMODE
182 if (strcmp(argv[1], "test") == 0)
183 {
184 testMode = true;
185 } else
186#endif
187 {
188 displayHelpAndExit(argv[0]);
189 }
190
191 int rc;
192
193#ifdef TESTMODE
194 if (testMode)
195 {
196 rc = performTest();
197 } else
198#endif
199 if (installMode)
200 {
201 rc = installDriver();
202 } else
203 {
204 rc = uninstallDriver();
205 }
206
207 if (rc == 0)
208 {
209 printf("operation completed successfully!\n");
210 } else
211 {
212 printf("error: operation failed with status code %d\n", rc);
213 }
214
215 return rc;
216}
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