我正在尝试以编程方式在 Windows 7 上创建和销毁网桥。
从技术上讲,我希望留在 .Net 4 领域(PInvokes 很好,ofc),但使用 C++ 是一种选择。< br/>
到目前为止,我的研究表明对于配置,netsh
-commands 是 the route to go .然而,似乎没有办法真正与他们建立新的桥梁。
我目前正在调查 this使用 INetCfg
API 的程序,但该程序或更具体地说,API 似乎无法(再次)构建新桥。
如果有人可以为解决问题做出贡献,我们将不胜感激任何形式的帮助。
[更新:] 似乎 newtork 网桥是使用一个驱动程序实现的,该驱动程序然后绑定(bind)到两个设备。我还不能充分利用这些信息,所以仍然感谢您的帮助。
最佳答案
我找到了适用于网桥服务 和网桥适配器 驱动程序的解决方案。我不像 devcon 那样使用 UpdateDriverForPlugAndPlayDevices
,而是使用 DiInstallDevice
。
但是,无法以非交互模式(没有用户交互)首次安装驱动程序。这是因为内置桥.inf 文件没有相应的.cat 文件。 UpdateDriverForPlugAndPlayDevices
、DiInstallDevice
和 DiInstallDriver
均不适用于 .inf 文件已包含在 中的手动驱动程序安装>%SystemRoot%\inf 但尚未在 %SystemRoot%\System32\DriverStore 中。
The files should be on the distribution media or in a vendor-created directory, not in a system location such as %SystemRoot%\inf
所有提到的安装方法都将创建 .inf 文件的 OEM 副本并将其安装到驱动程序商店。因为这个 OEM 副本最初不是驱动程序商店的一部分,Windows 将显示一个提示对话框并要求用户交互,要么强制安装驱动程序,要么取消。顺便说一句,无需任何用户交互即可安装后续驱动程序。还可以在非交互模式下安装预安装的驱动程序(请参阅 pnputil -a)。
所以这是我的解决方案:
- 首先在 HKLM\System\CurrentControlSet\Enum\Root 中创建一个设备条目,在
SetupDiCreateDeviceInfo
- 硬件 ID 由
SetupDiSetDeviceRegistryProperty
分配 - 驱动程序列表是在
SetupDiSetDeviceInstallParams
的帮助下由给定的单个 .inf 文件构建的
- 使用
SetupDiSetSelectedDriver
枚举和预选驱动程序 - 使用
SetupDiCallClassInstaller(DIF_REGISTERDEVICE...)
注册设备 - 使用
DiInstallDevice
安装
这是完整的代码:
HRESULT InstallDriver(const wchar_t* DriverInfFile, const wchar_t* HardwareId) {
HRESULT Hr = S_OK;
GUID ClassGUID;
wchar_t ClassName[MAX_CLASS_NAME_LEN] = {0};
if (SetupDiGetINFClass(DriverInfFile, &ClassGUID, ClassName, sizeof(ClassName) / sizeof(wchar_t), nullptr) == FALSE) {
Hr = HRESULT_FROM_SETUPAPI(GetLastError());
return Hr;
}
HDEVINFO DeviceInfoSet = SetupDiCreateDeviceInfoList(&ClassGUID, nullptr);
if (DeviceInfoSet == INVALID_HANDLE_VALUE) {
Hr = HRESULT_FROM_SETUPAPI(GetLastError());
return Hr;
}
SP_DEVINFO_DATA DeviceInfoData = {
sizeof(SP_DEVINFO_DATA), 0
};
if (SetupDiCreateDeviceInfo(DeviceInfoSet, HardwareId, &ClassGUID, nullptr, nullptr, DICD_GENERATE_ID, &DeviceInfoData) == FALSE) {
Hr = HRESULT_FROM_SETUPAPI(GetLastError());
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
return Hr;
}
if (SetupDiSetDeviceRegistryProperty(DeviceInfoSet, &DeviceInfoData, SPDRP_HARDWAREID, (LPBYTE) HardwareId, (DWORD) (wcslen(HardwareId) + 1) * sizeof(wchar_t)) == FALSE) {
Hr = HRESULT_FROM_SETUPAPI(GetLastError());
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
return Hr;
}
SP_DEVINSTALL_PARAMS InstallParams = {sizeof(SP_DEVINSTALL_PARAMS), 0};
InstallParams.FlagsEx = DI_FLAGSEX_ALLOWEXCLUDEDDRVS | DI_FLAGSEX_ALWAYSWRITEIDS;
InstallParams.Flags = DI_QUIETINSTALL | DI_ENUMSINGLEINF;
wcscpy_s(InstallParams.DriverPath, DriverInfFile);
if (SetupDiSetDeviceInstallParams(DeviceInfoSet, &DeviceInfoData, &InstallParams) == FALSE) {
Hr = HRESULT_FROM_SETUPAPI(GetLastError());
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
return Hr;
}
SP_DRVINFO_DATA DriverInfoData = {sizeof(SP_DRVINFO_DATA), 0};
if (SetupDiBuildDriverInfoList(DeviceInfoSet, &DeviceInfoData, SPDIT_COMPATDRIVER) == FALSE) {
Hr = HRESULT_FROM_SETUPAPI(GetLastError());
SetupDiDestroyDriverInfoList(DeviceInfoSet, &DeviceInfoData, SPDIT_COMPATDRIVER);
}
// Use first best driver (since specified by inf file)
if (SetupDiEnumDriverInfo(DeviceInfoSet, &DeviceInfoData, SPDIT_COMPATDRIVER, 0, &DriverInfoData)) {
SetupDiSetSelectedDriver(DeviceInfoSet, &DeviceInfoData, &DriverInfoData);
}
if (SetupDiCallClassInstaller(DIF_REGISTERDEVICE, DeviceInfoSet, &DeviceInfoData) == FALSE) {
Hr = HRESULT_FROM_SETUPAPI(GetLastError());
}
// TODO: Allow non interactive mode for drivers already contained in %SystemRoot%\inf directory
//BOOL PreviousMode = SetupSetNonInteractiveMode(TRUE);
if (Hr == S_OK) {
if (DiInstallDevice(nullptr, DeviceInfoSet, &DeviceInfoData, &DriverInfoData, 0, nullptr) == FALSE) {
Hr = HRESULT_FROM_SETUPAPI(GetLastError());
// Ensure that the device entry in \ROOT\ENUM\ will be removed...
SetupDiRemoveDevice(DeviceInfoSet, &DeviceInfoData);
}
}
//SetupSetNonInteractiveMode(PreviousMode);
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
return Hr;
}
待办事项:找到一种从 %SystemRoot%\inf 中安装此桥接驱动程序的方法,无需创建 OEM 副本且无需任何用户交互。
您可以在 Sourceforge 获得对 Subversion 存储库的读/写访问权限
如有任何其他信息或改进建议,我们将不胜感激!请大家随时检查/修改代码。
基本命令:
- bridgeutil.exe/install
- bridgeutil.exe/uninstall
- bridgeutil.exe/attach
- bridgeutil.exe/detach
示例:
bridgeutil.exe /attach "PCI\VEN_10EC&DEV_8169" /attach {5d624f94-8850-40c3-a3fa-a4fd2080baf3}\vwifimp
将每个 Realtek 8169 网络接口(interface)卡和 Microsoft 虚拟 Wifi 适配器附加到桥接。如果尚未安装网桥,则会先安装它。
bridgeutil.exe /detach 1
从桥上分离 ID 为 1 的适配器。
要查看可桥接适配器的列表,只需调用不带任何参数的 bridgeutil.exe。
关于c# - 在 Windows 7 上使用 .NET 以编程方式创建/销毁网桥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17588957/