VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxUSB/win/Install/USBInstall.cpp@ 49098

Last change on this file since 49098 was 46264, checked in by vboxsync, 12 years ago

these are driving me crazy.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1/** @file
2 *
3 * VBox host drivers - USB drivers - Filter & driver installation
4 *
5 * Installation code
6 *
7 * Copyright (C) 2006-2011 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#include <setupapi.h>
24#include <newdev.h>
25#include <iprt/assert.h>
26#include <iprt/err.h>
27#include <iprt/initterm.h>
28#include <iprt/param.h>
29#include <iprt/path.h>
30#include <iprt/stream.h>
31#include <iprt/string.h>
32#include <VBox/err.h>
33#include <stdio.h>
34
35#include <VBox/VBoxDrvCfg-win.h>
36
37static DECLCALLBACK(void) vboxUsbLog(VBOXDRVCFG_LOG_SEVERITY enmSeverity, char * msg, void * pvContext)
38{
39 switch (enmSeverity)
40 {
41 case VBOXDRVCFG_LOG_SEVERITY_FLOW:
42 case VBOXDRVCFG_LOG_SEVERITY_REGULAR:
43 break;
44 case VBOXDRVCFG_LOG_SEVERITY_REL:
45 RTPrintf("%s", msg);
46 break;
47 default:
48 break;
49 }
50}
51
52static DECLCALLBACK(void) vboxUsbPanic(void * pvPanic)
53{
54#ifndef DEBUG_bird
55 AssertFailed();
56#endif
57}
58
59int usblibOsCreateService(void);
60
61int __cdecl main(int argc, char **argv)
62{
63 if (RTR3InitExe(argc, &argv, 0) != VINF_SUCCESS)
64 {
65 printf("Could not init IPRT!\n");
66 return 1;
67 }
68
69 VBoxDrvCfgLoggerSet(vboxUsbLog, NULL);
70 VBoxDrvCfgPanicSet(vboxUsbPanic, NULL);
71
72 RTPrintf("USB installation\n");
73
74 int rc = usblibOsCreateService();
75
76 if (RT_SUCCESS(rc))
77 {
78 LPWSTR lpszFilePart;
79 WCHAR szFullPath[MAX_PATH];
80 DWORD len;
81
82 len = GetFullPathNameW(L".\\VBoxUSB.inf", RT_ELEMENTS(szFullPath), szFullPath, &lpszFilePart);
83 Assert(len);
84
85 HRESULT hr = VBoxDrvCfgInfInstall(szFullPath);
86 if (hr == S_OK)
87 {
88 RTPrintf("Installation successful.\n");
89 }
90 else
91 {
92 rc = -1;
93 }
94 }
95
96 if (RT_SUCCESS(rc))
97 rc = 0;
98
99 /** @todo RTR3Term(); */
100 return rc;
101}
102
103/** The support service name. */
104#define SERVICE_NAME "VBoxUSBMon"
105/** Win32 Device name. */
106#define DEVICE_NAME "\\\\.\\VBoxUSBMon"
107/** NT Device name. */
108#define DEVICE_NAME_NT L"\\Device\\VBoxUSBMon"
109/** Win32 Symlink name. */
110#define DEVICE_NAME_DOS L"\\DosDevices\\VBoxUSBMon"
111
112
113/**
114 * Changes the USB driver service to specified driver path.
115 *
116 * @returns 0 on success.
117 * @returns < 0 on failure.
118 */
119int usblibOsChangeService(const char *pszDriverPath)
120{
121 SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
122 DWORD dwLastError = GetLastError();
123 int rc = RTErrConvertFromWin32(dwLastError);
124 AssertPtr(pszDriverPath);
125 AssertMsg(hSMgrCreate, ("OpenSCManager(,,create) failed rc=%d\n", dwLastError));
126 if (hSMgrCreate)
127 {
128 SC_HANDLE hService = OpenService(hSMgrCreate,
129 SERVICE_NAME,
130 GENERIC_ALL);
131 DWORD dwLastError = GetLastError();
132 if (hService == NULL)
133 {
134 AssertMsg(hService, ("OpenService failed! LastError=%Rwa, pszDriver=%s\n", dwLastError, pszDriverPath));
135 rc = RTErrConvertFromWin32(dwLastError);
136 }
137 else
138 {
139 /* We only gonna change the driver image path, the rest remains like it already is */
140 if (ChangeServiceConfig(hService,
141 SERVICE_NO_CHANGE,
142 SERVICE_NO_CHANGE,
143 SERVICE_NO_CHANGE,
144 pszDriverPath,
145 NULL,
146 NULL,
147 NULL,
148 NULL,
149 NULL,
150 NULL))
151 {
152 RTPrintf("Changed service config to new driver path: %s\n", pszDriverPath);
153 }
154 else
155 {
156 AssertMsg(hService, ("ChangeServiceConfig failed! LastError=%Rwa, pszDriver=%s\n", dwLastError, pszDriverPath));
157 rc = RTErrConvertFromWin32(dwLastError);
158 }
159 if (hService != NULL)
160 CloseServiceHandle(hService);
161 }
162
163 CloseServiceHandle(hSMgrCreate);
164 }
165 return rc;
166}
167
168
169/**
170 * Creates the service.
171 *
172 * @returns 0 on success.
173 * @returns < 0 on failure.
174 */
175int usblibOsCreateService(void)
176{
177 /*
178 * Assume it didn't exist, so we'll create the service.
179 */
180 SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
181 DWORD dwLastError = GetLastError();
182 int rc = RTErrConvertFromWin32(dwLastError);
183 AssertMsg(hSMgrCreate, ("OpenSCManager(,,create) failed rc=%d\n", dwLastError));
184 if (hSMgrCreate)
185 {
186 char szDriver[RTPATH_MAX];
187 int rc = RTPathExecDir(szDriver, sizeof(szDriver) - sizeof("\\VBoxUSBMon.sys"));
188 if (RT_SUCCESS(rc))
189 {
190 strcat(szDriver, "\\VBoxUSBMon.sys");
191 RTPrintf("Creating USB monitor driver service with path %s ...\n", szDriver);
192 SC_HANDLE hService = CreateService(hSMgrCreate,
193 SERVICE_NAME,
194 "VBox USB Monitor Driver",
195 SERVICE_QUERY_STATUS,
196 SERVICE_KERNEL_DRIVER,
197 SERVICE_DEMAND_START,
198 SERVICE_ERROR_NORMAL,
199 szDriver,
200 NULL, NULL, NULL, NULL, NULL);
201 DWORD dwLastError = GetLastError();
202 if (dwLastError == ERROR_SERVICE_EXISTS)
203 {
204 RTPrintf("USB monitor driver service already exists, skipping creation.\n");
205 rc = usblibOsChangeService(szDriver);
206 }
207 else
208 {
209 AssertMsg(hService, ("CreateService failed! LastError=%Rwa, szDriver=%s\n", dwLastError, szDriver));
210 rc = RTErrConvertFromWin32(dwLastError);
211 if (hService != NULL)
212 CloseServiceHandle(hService);
213 }
214 }
215 CloseServiceHandle(hSMgrCreate);
216 }
217 return rc;
218}
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