VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetFlt/win/tools/VBoxNetAdpInstall.cpp@ 40993

Last change on this file since 40993 was 40993, checked in by vboxsync, 13 years ago

NetCfg/win: disable/enable/update HostOnly

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1/* $Id: VBoxNetAdpInstall.cpp 40993 2012-04-19 14:25:38Z vboxsync $ */
2/** @file
3 * NetAdpInstall - VBoxNetAdp installer command line tool
4 */
5
6/*
7 * Copyright (C) 2009 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#include <VBox/VBoxNetCfg-win.h>
19#include <VBox/VBoxDrvCfg-win.h>
20#include <stdio.h>
21
22#include <devguid.h>
23
24#define VBOX_NETADP_INF L".\\VBoxNetAdp.inf"
25
26static VOID winNetCfgLogger (LPCSTR szString)
27{
28 printf("%s", szString);
29}
30
31static int VBoxNetAdpInstall()
32{
33 int r = 1;
34 VBoxNetCfgWinSetLogging(winNetCfgLogger);
35
36 HRESULT hr = CoInitialize(NULL);
37 if(hr == S_OK)
38 {
39#if 0 //ndef DEBUG_misha
40 printf("not implemented yet, please use device manager for Host-Only net interface installation.. sorry :( \n");
41#else
42 GUID guid;
43 BSTR name, errMsg;
44 printf("adding host-only interface..\n");
45 DWORD WinEr;
46 WCHAR MpInf[MAX_PATH];
47 GetFullPathNameW(VBOX_NETADP_INF, sizeof(MpInf)/sizeof(MpInf[0]), MpInf, NULL);
48 WinEr = GetLastError();
49 if(WinEr == ERROR_SUCCESS)
50 {
51 hr = VBoxNetCfgWinCreateHostOnlyNetworkInterface (MpInf, true, &guid, &name, &errMsg);
52 if(hr == S_OK)
53 {
54 ULONG ip, mask;
55 hr = VBoxNetCfgWinGenHostOnlyNetworkNetworkIp(&ip, &mask);
56 if(hr == S_OK)
57 {
58 /* ip returned by VBoxNetCfgWinGenHostOnlyNetworkNetworkIp is a network ip,
59 * i.e. 192.168.xxx.0, assign 192.168.xxx.1 for the hostonly adapter */
60 ip = ip | (1 << 24);
61 hr = VBoxNetCfgWinEnableStaticIpConfig(&guid, ip, mask);
62 if(hr != S_OK)
63 {
64 printf("VBoxNetCfgWinEnableStaticIpConfig failed: hr = 0x%x\n", hr);
65 }
66 else
67 {
68 r = 0;
69 }
70 }
71 else
72 {
73 printf("VBoxNetCfgWinGenHostOnlyNetworkNetworkIp failed: hr = 0x%x\n", hr);
74 }
75 }
76 else
77 {
78 printf("VBoxNetCfgWinCreateHostOnlyNetworkInterface failed: hr = 0x%x\n", hr);
79 }
80 }
81 else
82 {
83 printf("GetFullPathNameW failed: winEr = %d\n", WinEr);
84 }
85#endif
86
87 CoUninitialize();
88 }
89 else
90 {
91 wprintf(L"Error initializing COM (0x%x)\n", hr);
92 }
93
94 VBoxNetCfgWinSetLogging(NULL);
95
96 return r;
97}
98
99static int VBoxNetAdpUninstall()
100{
101 int r = 1;
102 VBoxNetCfgWinSetLogging(winNetCfgLogger);
103
104 printf("uninstalling all Host-Only interfaces..\n");
105
106 HRESULT hr = CoInitialize(NULL);
107 if(hr == S_OK)
108 {
109 hr = VBoxNetCfgWinRemoveAllNetDevicesOfId(L"sun_VBoxNetAdp");
110 if(hr == S_OK)
111 {
112 hr = VBoxDrvCfgInfUninstallAllSetupDi(&GUID_DEVCLASS_NET, L"Net", L"sun_VBoxNetAdp", 0/* could be SUOI_FORCEDELETE */);
113 if(hr == S_OK)
114 {
115 printf("uninstalled successfully\n");
116 }
117 else
118 {
119 printf("uninstalled successfully, but failed to remove infs\n");
120 }
121 r = 0;
122 }
123 else
124 {
125 printf("uninstall failed, hr = 0x%x\n", hr);
126 }
127
128 CoUninitialize();
129 }
130 else
131 {
132 wprintf(L"Error initializing COM (0x%x)\n", hr);
133 }
134
135 VBoxNetCfgWinSetLogging(NULL);
136
137 return r;
138}
139
140static int VBoxNetAdpUpdate()
141{
142 int r = 1;
143 VBoxNetCfgWinSetLogging(winNetCfgLogger);
144
145 printf("uninstalling all Host-Only interfaces..\n");
146
147 HRESULT hr = CoInitialize(NULL);
148 if(hr == S_OK)
149 {
150 BOOL fRebootRequired = FALSE;
151 hr = VBoxNetCfgWinUpdateHostOnlyNetworkInterface(VBOX_NETADP_INF, &fRebootRequired);
152 if(hr == S_OK)
153 {
154 if (fRebootRequired)
155 printf("!!REBOOT REQUIRED!!\n");
156 printf("updated successfully\n");
157 r = 0;
158 }
159 else
160 {
161 printf("update failed, hr = 0x%x\n", hr);
162 }
163
164 CoUninitialize();
165 }
166 else
167 {
168 wprintf(L"Error initializing COM (0x%x)\n", hr);
169 }
170
171 VBoxNetCfgWinSetLogging(NULL);
172
173 return r;
174}
175
176static int VBoxNetAdpDisable()
177{
178 int r = 1;
179 VBoxNetCfgWinSetLogging(winNetCfgLogger);
180
181 printf("disabling all Host-Only interfaces..\n");
182
183 HRESULT hr = CoInitialize(NULL);
184 if(hr == S_OK)
185 {
186 hr = VBoxNetCfgWinPropChangeAllNetDevicesOfId(L"sun_VBoxNetAdp", VBOXNECTFGWINPROPCHANGE_TYPE_DISABLE);
187 if(hr == S_OK)
188 {
189 printf("disable succeeded!\n");
190 r = 0;
191 }
192 else
193 {
194 printf("disable failed, hr = 0x%x\n", hr);
195 }
196
197 CoUninitialize();
198 }
199 else
200 {
201 wprintf(L"Error initializing COM (0x%x)\n", hr);
202 }
203
204 VBoxNetCfgWinSetLogging(NULL);
205
206 return r;
207}
208
209static int VBoxNetAdpEnable()
210{
211 int r = 1;
212 VBoxNetCfgWinSetLogging(winNetCfgLogger);
213
214 printf("enabling all Host-Only interfaces..\n");
215
216 HRESULT hr = CoInitialize(NULL);
217 if(hr == S_OK)
218 {
219 hr = VBoxNetCfgWinPropChangeAllNetDevicesOfId(L"sun_VBoxNetAdp", VBOXNECTFGWINPROPCHANGE_TYPE_ENABLE);
220 if(hr == S_OK)
221 {
222 printf("disable succeeded!\n");
223 r = 0;
224 }
225 else
226 {
227 printf("disable failed, hr = 0x%x\n", hr);
228 }
229
230 CoUninitialize();
231 }
232 else
233 {
234 wprintf(L"Error initializing COM (0x%x)\n", hr);
235 }
236
237 VBoxNetCfgWinSetLogging(NULL);
238
239 return r;
240}
241
242static void printUsage()
243{
244 printf("Host-Only network adapter configuration tool\n"
245 " Usage: VBoxNetAdpInstall [cmd]\n"
246 " cmd can be one of the following values:\n"
247 " i - install a new host-only interface\n"
248 " u - uninstall all host-only interfaces\n"
249 " a - update the host-only driver\n"
250 " d - disable all host-only interfaces\n"
251 " e - enable all host-only interfaces\n"
252 " h - print this message\n");
253}
254
255int __cdecl main(int argc, char **argv)
256{
257 if (argc < 2)
258 return VBoxNetAdpInstall();
259 if (argc > 2)
260 {
261 printUsage();
262 return 1;
263 }
264
265 __debugbreak();
266
267 if (!strcmp(argv[1], "i"))
268 return VBoxNetAdpInstall();
269 if (!strcmp(argv[1], "u"))
270 return VBoxNetAdpUninstall();
271 if (!strcmp(argv[1], "a"))
272 return VBoxNetAdpUpdate();
273 if (!strcmp(argv[1], "d"))
274 return VBoxNetAdpDisable();
275 if (!strcmp(argv[1], "e"))
276 return VBoxNetAdpEnable();
277
278 printUsage();
279 return !strcmp(argv[1], "h");
280}
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