VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetFlt/win/NetFltInstall.cpp@ 28668

Last change on this file since 28668 was 25745, checked in by vboxsync, 15 years ago

NetFltInstall.cpp: Look for files in the executable directory if not found in the current dir.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
1/* $Id: NetFltInstall.cpp 25745 2010-01-12 10:16:41Z vboxsync $ */
2/** @file
3 * NetFltInstall - VBoxNetFlt installer command line tool
4 */
5
6/*
7 * Copyright (C) 2008 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22#include <vbox/WinNetConfig.h>
23#include <devguid.h>
24#include <stdio.h>
25
26#define NETFLT_ID L"sun_VBoxNetFlt"
27#define VBOX_NETCFG_APP_NAME L"NetFltInstall"
28#define VBOX_NETFLT_PT_INF L".\\VBoxNetFlt.inf"
29#define VBOX_NETFLT_MP_INF L".\\VBoxNetFlt_m.inf"
30#define VBOX_NETFLT_RETRIES 10
31
32
33static VOID winNetCfgLogger (LPCWSTR szString)
34{
35 wprintf(L"%s", szString);
36}
37
38/** Wrapper aroung GetfullPathNameW that will try an alternative INF location.
39 *
40 * The default location is the current directory. If not found there, the
41 * alternative locatoin is the executable directory. If not found there either,
42 * the first alternative is present to the caller.
43 */
44static DWORD MyGetfullPathNameW(LPCWSTR pwszName, size_t cchFull, LPWSTR pwszFull)
45{
46 LPWSTR pwszFilePart;
47 DWORD dwSize = GetFullPathNameW(pwszName, cchFull, pwszFull, &pwszFilePart);
48 if(dwSize <= 0)
49 return dwSize;
50
51 /* if it doesn't exist, see if the file exists in the same directory as the executable. */
52 if (GetFileAttributesW(pwszFull) == INVALID_FILE_ATTRIBUTES)
53 {
54 WCHAR wsz[512];
55 DWORD cch = GetModuleFileNameW(GetModuleHandle(NULL), &wsz[0], sizeof(wsz) / sizeof(wsz[0]));
56 if(cch > 0)
57 {
58 while(cch > 0 && wsz[cch - 1] != '/' && wsz[cch - 1] != '\\' && wsz[cch - 1] != ':')
59 cch--;
60 unsigned i = 0;
61 while(cch < sizeof(wsz) / sizeof(wsz[0]))
62 {
63 wsz[cch] = pwszFilePart[i++];
64 if(!wsz[cch])
65 {
66 dwSize = GetFullPathNameW(wsz, cchFull, pwszFull, NULL);
67 if( dwSize > 0
68 && GetFileAttributesW(pwszFull) != INVALID_FILE_ATTRIBUTES)
69 return dwSize;
70 break;
71 }
72 cch++;
73 }
74 }
75 }
76
77 /* fallback */
78 return GetFullPathNameW(pwszName, cchFull, pwszFull, NULL);
79}
80
81static int InstallNetFlt()
82{
83 WCHAR PtInf[MAX_PATH];
84 WCHAR MpInf[MAX_PATH];
85 INetCfg *pnc;
86 LPWSTR lpszLockedBy = NULL;
87 int r = 1;
88
89 VBoxNetCfgWinSetLogging(winNetCfgLogger);
90
91 HRESULT hr = CoInitialize(NULL);
92 if(hr == S_OK)
93 {
94 int i = 0;
95 do
96 {
97 hr = VBoxNetCfgWinQueryINetCfg(TRUE, VBOX_NETCFG_APP_NAME, &pnc, &lpszLockedBy);
98 if(hr == S_OK)
99 {
100 DWORD dwSize;
101 dwSize = MyGetfullPathNameW(VBOX_NETFLT_PT_INF, sizeof(PtInf)/sizeof(PtInf[0]), PtInf);
102 if(dwSize > 0)
103 {
104 /** @todo add size check for (sizeof(PtInf)/sizeof(PtInf[0])) == dwSize (string length in sizeof(PtInf[0])) */
105
106 dwSize = MyGetfullPathNameW(VBOX_NETFLT_MP_INF, sizeof(MpInf)/sizeof(MpInf[0]), MpInf);
107 if(dwSize > 0)
108 {
109 /** @todo add size check for (sizeof(MpInf)/sizeof(MpInf[0])) == dwSize (string length in sizeof(MpInf[0])) */
110
111 LPCWSTR aInfs[] = {PtInf, MpInf};
112 hr = VBoxNetCfgWinNetFltInstall(pnc, aInfs, 2);
113 if(hr == S_OK)
114 {
115 wprintf(L"installed successfully\n");
116 r = 0;
117 }
118 else
119 {
120 wprintf(L"error installing VBoxNetFlt (0x%x)\n", hr);
121 }
122 }
123 else
124 {
125 hr = HRESULT_FROM_WIN32(GetLastError());
126 wprintf(L"error getting full inf path for VBoxNetFlt_m.inf (0x%x)\n", hr);
127 }
128 }
129 else
130 {
131 hr = HRESULT_FROM_WIN32(GetLastError());
132 wprintf(L"error getting full inf path for VBoxNetFlt.inf (0x%x)\n", hr);
133 }
134
135
136 VBoxNetCfgWinReleaseINetCfg(pnc, TRUE);
137 break;
138 }
139 else if(hr == NETCFG_E_NO_WRITE_LOCK && lpszLockedBy)
140 {
141 if(i < VBOX_NETFLT_RETRIES && !wcscmp(lpszLockedBy, L"6to4svc.dll"))
142 {
143 wprintf(L"6to4svc.dll is holding the lock, retrying %d out of %d\n", ++i, VBOX_NETFLT_RETRIES);
144 CoTaskMemFree(lpszLockedBy);
145 }
146 else
147 {
148 wprintf(L"Error: write lock is owned by another application (%s), close the application and retry installing\n", lpszLockedBy);
149 r = 1;
150 CoTaskMemFree(lpszLockedBy);
151 break;
152 }
153 }
154 else
155 {
156 wprintf(L"Error getting the INetCfg interface (0x%x)\n", hr);
157 r = 1;
158 break;
159 }
160 } while(true);
161
162 CoUninitialize();
163 }
164 else
165 {
166 wprintf(L"Error initializing COM (0x%x)\n", hr);
167 r = 1;
168 }
169
170 VBoxNetCfgWinSetLogging(NULL);
171
172 return r;
173}
174
175int __cdecl main(int argc, char **argv)
176{
177 return InstallNetFlt();
178}
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