VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetFlt/win/tools/VBoxNetFltInstall.cpp@ 86513

Last change on this file since 86513 was 85121, checked in by vboxsync, 5 years ago

iprt/cdefs.h: Refactored the typedef use of DECLCALLBACK as well as DECLCALLBACKMEMBER to wrap the whole expression, similar to the DECLR?CALLBACKMEMBER macros. This allows adding a throw() at the end when compiling with the VC++ compiler to indicate that the callbacks won't throw anything, so we can stop supressing the C5039 warning about passing functions that can potential throw C++ exceptions to extern C code that can't necessarily cope with such (unwind,++). Introduced a few _EX variations that allows specifying different/no calling convention too, as that's handy when dynamically resolving host APIs. Fixed numerous places missing DECLCALLBACK and such. Left two angry @todos regarding use of CreateThread. bugref:9794

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1/* $Id: VBoxNetFltInstall.cpp 85121 2020-07-08 19:33:26Z vboxsync $ */
2/** @file
3 * NetFltInstall - VBoxNetFlt installer command line tool
4 */
5
6/*
7 * Copyright (C) 2008-2020 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#include <VBox/VBoxNetCfg-win.h>
28#include <devguid.h>
29#include <stdio.h>
30
31#define NETFLT_ID L"sun_VBoxNetFlt"
32#define VBOX_NETCFG_APP_NAME L"NetFltInstall"
33#define VBOX_NETFLT_PT_INF L".\\VBoxNetFlt.inf"
34#define VBOX_NETFLT_MP_INF L".\\VBoxNetFltM.inf"
35#define VBOX_NETFLT_RETRIES 10
36
37
38static DECLCALLBACK(void) winNetCfgLogger(const char *pszString)
39{
40 printf("%s", pszString);
41}
42
43/** Wrapper around GetfullPathNameW that will try an alternative INF location.
44 *
45 * The default location is the current directory. If not found there, the
46 * alternative location is the executable directory. If not found there either,
47 * the first alternative is present to the caller.
48 */
49static DWORD MyGetfullPathNameW(LPCWSTR pwszName, size_t cchFull, LPWSTR pwszFull)
50{
51 LPWSTR pwszFilePart;
52 DWORD dwSize = GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, &pwszFilePart);
53 if (dwSize <= 0)
54 return dwSize;
55
56 /* if it doesn't exist, see if the file exists in the same directory as the executable. */
57 if (GetFileAttributesW(pwszFull) == INVALID_FILE_ATTRIBUTES)
58 {
59 WCHAR wsz[512];
60 DWORD cch = GetModuleFileNameW(GetModuleHandle(NULL), &wsz[0], RT_ELEMENTS(wsz));
61 if (cch > 0)
62 {
63 while (cch > 0 && wsz[cch - 1] != '/' && wsz[cch - 1] != '\\' && wsz[cch - 1] != ':')
64 cch--;
65 unsigned i = 0;
66 while (cch < RT_ELEMENTS(wsz))
67 {
68 wsz[cch] = pwszFilePart[i++];
69 if (!wsz[cch])
70 {
71 dwSize = GetFullPathNameW(wsz, (DWORD)cchFull, pwszFull, NULL);
72 if (dwSize > 0 && GetFileAttributesW(pwszFull) != INVALID_FILE_ATTRIBUTES)
73 return dwSize;
74 break;
75 }
76 cch++;
77 }
78 }
79 }
80
81 /* fallback */
82 return GetFullPathNameW(pwszName, (DWORD)cchFull, pwszFull, NULL);
83}
84
85static int VBoxNetFltInstall()
86{
87 WCHAR wszPtInf[MAX_PATH];
88 WCHAR wszMpInf[MAX_PATH];
89 INetCfg *pnc;
90 int rcExit = RTEXITCODE_FAILURE;
91
92 VBoxNetCfgWinSetLogging(winNetCfgLogger);
93
94 HRESULT hr = CoInitialize(NULL);
95 if (hr == S_OK)
96 {
97 for (int i = 0;; i++)
98 {
99 LPWSTR pwszLockedBy = NULL;
100 hr = VBoxNetCfgWinQueryINetCfg(&pnc, TRUE, VBOX_NETCFG_APP_NAME, 10000, &pwszLockedBy);
101 if (hr == S_OK)
102 {
103 DWORD dwSize;
104 dwSize = MyGetfullPathNameW(VBOX_NETFLT_PT_INF, RT_ELEMENTS(wszPtInf), wszPtInf);
105 if (dwSize > 0)
106 {
107 /** @todo add size check for (RT_ELEMENTS(wszPtInf) == dwSize (string length in WCHARs) */
108
109 dwSize = MyGetfullPathNameW(VBOX_NETFLT_MP_INF, RT_ELEMENTS(wszMpInf), wszMpInf);
110 if (dwSize > 0)
111 {
112 /** @todo add size check for (RT_ELEMENTS(wszMpInf) == dwSize (string length in WHCARs) */
113
114 LPCWSTR apwszInfs[] = { wszPtInf, wszMpInf };
115 hr = VBoxNetCfgWinNetFltInstall(pnc, apwszInfs, 2);
116 if (hr == S_OK)
117 {
118 wprintf(L"installed successfully\n");
119 rcExit = RTEXITCODE_SUCCESS;
120 }
121 else
122 wprintf(L"error installing VBoxNetFlt (%#lx)\n", hr);
123 }
124 else
125 {
126 hr = HRESULT_FROM_WIN32(GetLastError());
127 wprintf(L"error getting full inf path for VBoxNetFltM.inf (%#lx)\n", hr);
128 }
129 }
130 else
131 {
132 hr = HRESULT_FROM_WIN32(GetLastError());
133 wprintf(L"error getting full inf path for VBoxNetFlt.inf (%#lx)\n", hr);
134 }
135
136 VBoxNetCfgWinReleaseINetCfg(pnc, TRUE);
137 break;
138 }
139
140 if (hr == NETCFG_E_NO_WRITE_LOCK && pwszLockedBy)
141 {
142 if (i < VBOX_NETFLT_RETRIES && !wcscmp(pwszLockedBy, L"6to4svc.dll"))
143 {
144 wprintf(L"6to4svc.dll is holding the lock, retrying %d out of %d\n", i + 1, VBOX_NETFLT_RETRIES);
145 CoTaskMemFree(pwszLockedBy);
146 }
147 else
148 {
149 wprintf(L"Error: write lock is owned by another application (%s), close the application and retry installing\n", pwszLockedBy);
150 CoTaskMemFree(pwszLockedBy);
151 break;
152 }
153 }
154 else
155 {
156 wprintf(L"Error getting the INetCfg interface (%#lx)\n", hr);
157 break;
158 }
159 }
160
161 CoUninitialize();
162 }
163 else
164 wprintf(L"Error initializing COM (%#lx)\n", hr);
165
166 VBoxNetCfgWinSetLogging(NULL);
167
168 return rcExit;
169}
170
171int __cdecl main(int argc, char **argv)
172{
173 RT_NOREF2(argc, argv);
174 return VBoxNetFltInstall();
175}
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