1 | /* $Id: VBoxGuestInst.cpp 44977 2013-03-11 11:36:51Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Small tool to (un)install the VBoxGuest device driver.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <windows.h>
|
---|
23 |
|
---|
24 | #include <stdio.h>
|
---|
25 | #include <stdlib.h>
|
---|
26 | #include <string.h>
|
---|
27 |
|
---|
28 | #include <VBox/VBoxGuest.h>
|
---|
29 |
|
---|
30 |
|
---|
31 | //#define TESTMODE
|
---|
32 |
|
---|
33 |
|
---|
34 |
|
---|
35 | static int installDriver(void)
|
---|
36 | {
|
---|
37 | /*
|
---|
38 | * Assume it didn't exist, so we'll create the service.
|
---|
39 | */
|
---|
40 | SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
|
---|
41 | if (!hSMgrCreate)
|
---|
42 | {
|
---|
43 | printf("OpenSCManager(,,create) failed rc=%d\n", GetLastError());
|
---|
44 | return -1;
|
---|
45 | }
|
---|
46 |
|
---|
47 | char szDriver[MAX_PATH];
|
---|
48 | GetSystemDirectory(szDriver, sizeof(szDriver));
|
---|
49 | strcat(szDriver, "\\drivers\\VBoxGuestNT.sys");
|
---|
50 |
|
---|
51 | SC_HANDLE hService = CreateService(hSMgrCreate,
|
---|
52 | VBOXGUEST_SERVICE_NAME,
|
---|
53 | "VBoxGuest Support Driver",
|
---|
54 | SERVICE_QUERY_STATUS,
|
---|
55 | SERVICE_KERNEL_DRIVER,
|
---|
56 | SERVICE_BOOT_START,
|
---|
57 | SERVICE_ERROR_NORMAL,
|
---|
58 | szDriver,
|
---|
59 | "System",
|
---|
60 | NULL, NULL, NULL, NULL);
|
---|
61 | if (!hService)
|
---|
62 | printf("CreateService failed! lasterr=%d\n", GetLastError());
|
---|
63 | else
|
---|
64 | CloseServiceHandle(hService);
|
---|
65 | CloseServiceHandle(hSMgrCreate);
|
---|
66 | return hService ? 0 : -1;
|
---|
67 | }
|
---|
68 |
|
---|
69 | static int uninstallDriver(void)
|
---|
70 | {
|
---|
71 | int rc = -1;
|
---|
72 | SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
|
---|
73 | if (!hSMgr)
|
---|
74 | {
|
---|
75 | printf("OpenSCManager(,,delete) failed rc=%d\n", GetLastError());
|
---|
76 | return -1;
|
---|
77 | }
|
---|
78 | SC_HANDLE hService = OpenService(hSMgr, VBOXGUEST_SERVICE_NAME, DELETE);
|
---|
79 | if (hService)
|
---|
80 | {
|
---|
81 | /*
|
---|
82 | * Delete the service.
|
---|
83 | */
|
---|
84 | if (DeleteService(hService))
|
---|
85 | rc = 0;
|
---|
86 | else
|
---|
87 | printf("DeleteService failed lasterr=%d\n", GetLastError());
|
---|
88 | CloseServiceHandle(hService);
|
---|
89 | }
|
---|
90 | else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
|
---|
91 | rc = 0;
|
---|
92 | else
|
---|
93 | printf("OpenService failed lasterr=%d\n", GetLastError());
|
---|
94 | CloseServiceHandle(hSMgr);
|
---|
95 | return rc;
|
---|
96 | }
|
---|
97 |
|
---|
98 | #ifdef TESTMODE
|
---|
99 |
|
---|
100 | static HANDLE 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 |
|
---|
118 | static int closeDriver(HANDLE hDevice)
|
---|
119 | {
|
---|
120 | CloseHandle(hDevice);
|
---|
121 | return 0;
|
---|
122 | }
|
---|
123 |
|
---|
124 | static int performTest(void)
|
---|
125 | {
|
---|
126 | int rc = 0;
|
---|
127 |
|
---|
128 | HANDLE hDevice = openDriver();
|
---|
129 |
|
---|
130 | if (hDevice != INVALID_HANDLE_VALUE)
|
---|
131 | closeDriver(hDevice);
|
---|
132 | else
|
---|
133 | printf("openDriver failed!\n");
|
---|
134 |
|
---|
135 | return rc;
|
---|
136 | }
|
---|
137 |
|
---|
138 | #endif /* TESTMODE */
|
---|
139 |
|
---|
140 | static int usage(char *programName)
|
---|
141 | {
|
---|
142 | printf("error, syntax: %s [install|uninstall]\n", programName);
|
---|
143 | return 1;
|
---|
144 | }
|
---|
145 |
|
---|
146 | int main(int argc, char **argv)
|
---|
147 | {
|
---|
148 | bool installMode;
|
---|
149 | #ifdef TESTMODE
|
---|
150 | bool testMode = false;
|
---|
151 | #endif
|
---|
152 |
|
---|
153 | if (argc != 2)
|
---|
154 | return usage(argv[0]);
|
---|
155 |
|
---|
156 | if (strcmp(argv[1], "install") == 0)
|
---|
157 | installMode = true;
|
---|
158 | else if (strcmp(argv[1], "uninstall") == 0)
|
---|
159 | installMode = false;
|
---|
160 | #ifdef TESTMODE
|
---|
161 | else if (strcmp(argv[1], "test") == 0)
|
---|
162 | testMode = true;
|
---|
163 | #endif
|
---|
164 | else
|
---|
165 | return usage(argv[0]);
|
---|
166 |
|
---|
167 |
|
---|
168 | int rc;
|
---|
169 | #ifdef TESTMODE
|
---|
170 | if (testMode)
|
---|
171 | rc = performTest();
|
---|
172 | else
|
---|
173 | #endif
|
---|
174 | if (installMode)
|
---|
175 | rc = installDriver();
|
---|
176 | else
|
---|
177 | rc = uninstallDriver();
|
---|
178 |
|
---|
179 | if (rc == 0)
|
---|
180 | printf("operation completed successfully!\n");
|
---|
181 | else
|
---|
182 | printf("error: operation failed with status code %d\n", rc);
|
---|
183 |
|
---|
184 | return rc;
|
---|
185 | }
|
---|
186 |
|
---|