放弃使用Vanara.PInvoke.SetupAPI的方式清除设备驱动,恢复使用pnputil,并传入instance ID参数清除sing-tun可能残留下的wintun设备驱动

This commit is contained in:
chao wan 2024-04-05 14:46:34 +08:00
parent f5d359fded
commit 5878929430
3 changed files with 4 additions and 29 deletions

View file

@ -22,7 +22,6 @@ using ZXing;
using ZXing.Common; using ZXing.Common;
using ZXing.QrCode; using ZXing.QrCode;
using ZXing.Windows.Compatibility; using ZXing.Windows.Compatibility;
using Vanara.PInvoke;
namespace v2rayN namespace v2rayN
{ {
@ -1142,12 +1141,12 @@ namespace v2rayN
taskService.RootFolder.RegisterTaskDefinition(TaskName, task); taskService.RootFolder.RegisterTaskDefinition(TaskName, task);
} }
public static void RemoveTunDevice() public static void RemoveDeviceForPnputil(string removeArg)
{ {
try try
{ {
string pnputilPath = @"C:\Windows\System32\pnputil.exe"; string pnputilPath = @"C:\Windows\System32\pnputil.exe";
string arg = $" /remove-device /deviceid \"wintun\""; string arg = $" /remove-device " + removeArg;
// Try to remove the device // Try to remove the device
Process proc = new() Process proc = new()
@ -1171,29 +1170,6 @@ namespace v2rayN
} }
} }
public static bool RemoveDeviceByDevIstId(string deviceInstanceId)
{
var isRemoved = false;
var devs = SetupAPI.SetupDiGetClassDevs(SetupAPI.GUID_DEVCLASS_NET, null, HWND.NULL, SetupAPI.DIGCF.DIGCF_PROFILE);
var instanceIdSize = deviceInstanceId.Length + 1;
var devInfo = new SetupAPI.SP_DEVINFO_DATA() { cbSize = (uint)Marshal.SizeOf<SetupAPI.SP_DEVINFO_DATA>() };
var instanceId = new StringBuilder(instanceIdSize);
uint index = 0;
while (SetupAPI.SetupDiEnumDeviceInfo(devs, index++, ref devInfo))
{
var success = SetupAPI.SetupDiGetDeviceInstanceId(devs, devInfo, instanceId, (uint)instanceIdSize, out uint size);
if (success && size == instanceIdSize && instanceId.ToString() == deviceInstanceId)
{
isRemoved = SetupAPI.SetupDiRemoveDevice(devs, ref devInfo);
break;
}
}
SetupAPI.SetupDiDestroyDeviceInfoList(devs);
return isRemoved;
}
public static string GetSingboxTunDeviceInstanceId(string deviceName) public static string GetSingboxTunDeviceInstanceId(string deviceName)
{ {
string deviceInstanceId = string.Empty; string deviceInstanceId = string.Empty;

View file

@ -54,7 +54,7 @@ namespace v2rayN.Handler
{ {
ShowMsg(true, $"{node!.GetSummary()}"); ShowMsg(true, $"{node!.GetSummary()}");
CoreStop(); CoreStop();
Utils.RemoveDeviceByDevIstId(Utils.GetSingboxTunDeviceInstanceId(Global.DefaultSingboxTunDeviceName)); Utils.RemoveDeviceForPnputil(Utils.GetSingboxTunDeviceInstanceId(Global.DefaultSingboxTunDeviceName));
CoreStart(node); CoreStart(node);
} }
executingResetEvents.ForEach(result => result.Set()); executingResetEvents.ForEach(result => result.Set());

View file

@ -20,8 +20,7 @@
<PackageReference Include="H.NotifyIcon.Wpf" Version="2.0.124" /> <PackageReference Include="H.NotifyIcon.Wpf" Version="2.0.124" />
<PackageReference Include="QRCoder.Xaml" Version="1.4.3" /> <PackageReference Include="QRCoder.Xaml" Version="1.4.3" />
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" /> <PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
<PackageReference Include="TaskScheduler" Version="2.10.1" /> <PackageReference Include="TaskScheduler" Version="2.10.1" />
<PackageReference Include="Vanara.PInvoke.SetupAPI" Version="4.0.0" />
<PackageReference Include="ZXing.Net.Bindings.Windows.Compatibility" Version="0.16.12" /> <PackageReference Include="ZXing.Net.Bindings.Windows.Compatibility" Version="0.16.12" />
<PackageReference Include="ReactiveUI.Fody" Version="19.5.41" /> <PackageReference Include="ReactiveUI.Fody" Version="19.5.41" />
<PackageReference Include="ReactiveUI.Validation" Version="3.1.7" /> <PackageReference Include="ReactiveUI.Validation" Version="3.1.7" />