VirtualBox

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

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

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 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
16
17/*******************************************************************************
18* Header Files *
19*******************************************************************************/
20#include <windows.h>
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <VBox/VBoxGuest.h>
27
28
29//#define TESTMODE
30
31
32
33int installDriver(void)
34{
35 /*
36 * Assume it didn't exist, so we'll create the service.
37 */
38 SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
39 if (!hSMgrCreate)
40 {
41 printf("OpenSCManager(,,create) failed rc=%d\n", GetLastError());
42 return -1;
43 }
44
45 char szDriver[MAX_PATH];
46 GetSystemDirectory(szDriver, sizeof(szDriver));
47 strcat(szDriver, "\\drivers\\VBoxGuestNT.sys");
48
49 SC_HANDLE hService = CreateService(hSMgrCreate,
50 VBOXGUEST_SERVICE_NAME,
51 "VBoxGuest Support Driver",
52 SERVICE_QUERY_STATUS,
53 SERVICE_KERNEL_DRIVER,
54 SERVICE_BOOT_START,
55 SERVICE_ERROR_NORMAL,
56 szDriver,
57 "System",
58 NULL, NULL, NULL, NULL);
59 if (!hService)
60 {
61 printf("CreateService failed! lasterr=%d\n", GetLastError());
62 } else
63 {
64 CloseServiceHandle(hService);
65 }
66 CloseServiceHandle(hSMgrCreate);
67 return hService ? 0 : -1;
68}
69
70int uninstallDriver(void)
71{
72 int rc = -1;
73 SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
74 if (!hSMgr)
75 {
76 printf("OpenSCManager(,,delete) failed rc=%d\n", GetLastError());
77 return -1;
78 }
79 SC_HANDLE hService = OpenService(hSMgr, VBOXGUEST_SERVICE_NAME, DELETE);
80 if (hService)
81 {
82 /*
83 * Delete the service.
84 */
85 if (DeleteService(hService))
86 rc = 0;
87 else
88 printf("DeleteService failed lasterr=%d\n", GetLastError());
89 CloseServiceHandle(hService);
90 }
91 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
92 rc = 0;
93 else
94 printf("OpenService failed lasterr=%d\n", GetLastError());
95 CloseServiceHandle(hSMgr);
96 return rc;
97}
98
99
100HANDLE openDriver(void)
101{
102 HANDLE hDevice;
103
104 hDevice = CreateFile(VBOXGUEST_DEVICE_NAME, // Win2k+: VBOXGUEST_DEVICE_NAME_GLOBAL
105 GENERIC_READ | GENERIC_WRITE,
106 FILE_SHARE_READ | FILE_SHARE_WRITE,
107 NULL,
108 OPEN_EXISTING,
109 FILE_ATTRIBUTE_NORMAL,
110 NULL);
111 if (hDevice == INVALID_HANDLE_VALUE)
112 {
113 printf("CreateFile did not work. GetLastError() 0x%x\n", GetLastError());
114 }
115 return hDevice;
116}
117
118int closeDriver(HANDLE hDevice)
119{
120 CloseHandle(hDevice);
121 return 0;
122}
123
124#ifdef TESTMODE
125typedef struct TESTFOO
126{
127 int values[16];
128} TESTFOO, *PTESTFOO;
129
130int performTest(void)
131{
132 int rc = 0;
133
134 HANDLE hDevice = openDriver();
135
136 if (hDevice != INVALID_HANDLE_VALUE)
137 {
138 DWORD cbReturned;
139
140 closeDriver(hDevice);
141 } else
142 {
143 printf("openDriver failed!\n");
144 }
145
146
147 return rc;
148}
149#endif
150
151void displayHelpAndExit(char *programName)
152{
153 printf("error, syntax: %s [install|uninstall]\n", programName);
154 exit(1);
155}
156
157int main(int argc, char **argv)
158{
159 bool installMode;
160#ifdef TESTMODE
161 bool testMode = false;
162#endif
163
164 if (argc != 2)
165 {
166 displayHelpAndExit(argv[0]);
167 }
168 if (strcmp(argv[1], "install") == 0)
169 {
170 installMode = true;
171 } else
172 if (strcmp(argv[1], "uninstall") == 0)
173 {
174 installMode = false;
175 } else
176#ifdef TESTMODE
177 if (strcmp(argv[1], "test") == 0)
178 {
179 testMode = true;
180 } else
181#endif
182 {
183 displayHelpAndExit(argv[0]);
184 }
185
186 int rc;
187
188#ifdef TESTMODE
189 if (testMode)
190 {
191 rc = performTest();
192 } else
193#endif
194 if (installMode)
195 {
196 rc = installDriver();
197 } else
198 {
199 rc = uninstallDriver();
200 }
201
202 if (rc == 0)
203 {
204 printf("operation completed successfully!\n");
205 } else
206 {
207 printf("error: operation failed with status code %d\n", rc);
208 }
209
210 return rc;
211}
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