From 5878929430089ba8efc852746eb65a50fdfb5dae Mon Sep 17 00:00:00 2001 From: chao wan <1013448513@qq.com> Date: Fri, 5 Apr 2024 14:46:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=BE=E5=BC=83=E4=BD=BF=E7=94=A8Vanara.PInv?= =?UTF-8?q?oke.SetupAPI=E7=9A=84=E6=96=B9=E5=BC=8F=E6=B8=85=E9=99=A4?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E9=A9=B1=E5=8A=A8=EF=BC=8C=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E4=BD=BF=E7=94=A8pnputil=EF=BC=8C=E5=B9=B6=E4=BC=A0=E5=85=A5in?= =?UTF-8?q?stance=20ID=E5=8F=82=E6=95=B0=E6=B8=85=E9=99=A4sing-tun?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E6=AE=8B=E7=95=99=E4=B8=8B=E7=9A=84wintun?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E9=A9=B1=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v2rayN/v2rayN/Common/Utils.cs | 28 ++-------------------------- v2rayN/v2rayN/Handler/CoreHandler.cs | 2 +- v2rayN/v2rayN/v2rayN.csproj | 3 +-- 3 files changed, 4 insertions(+), 29 deletions(-) diff --git a/v2rayN/v2rayN/Common/Utils.cs b/v2rayN/v2rayN/Common/Utils.cs index 38f41583..9390c7eb 100644 --- a/v2rayN/v2rayN/Common/Utils.cs +++ b/v2rayN/v2rayN/Common/Utils.cs @@ -22,7 +22,6 @@ using ZXing; using ZXing.Common; using ZXing.QrCode; using ZXing.Windows.Compatibility; -using Vanara.PInvoke; namespace v2rayN { @@ -1142,12 +1141,12 @@ namespace v2rayN taskService.RootFolder.RegisterTaskDefinition(TaskName, task); } - public static void RemoveTunDevice() + public static void RemoveDeviceForPnputil(string removeArg) { try { string pnputilPath = @"C:\Windows\System32\pnputil.exe"; - string arg = $" /remove-device /deviceid \"wintun\""; + string arg = $" /remove-device " + removeArg; // Try to remove the device 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() }; - 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) { string deviceInstanceId = string.Empty; diff --git a/v2rayN/v2rayN/Handler/CoreHandler.cs b/v2rayN/v2rayN/Handler/CoreHandler.cs index 4f8137a4..f12cd880 100644 --- a/v2rayN/v2rayN/Handler/CoreHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreHandler.cs @@ -54,7 +54,7 @@ namespace v2rayN.Handler { ShowMsg(true, $"{node!.GetSummary()}"); CoreStop(); - Utils.RemoveDeviceByDevIstId(Utils.GetSingboxTunDeviceInstanceId(Global.DefaultSingboxTunDeviceName)); + Utils.RemoveDeviceForPnputil(Utils.GetSingboxTunDeviceInstanceId(Global.DefaultSingboxTunDeviceName)); CoreStart(node); } executingResetEvents.ForEach(result => result.Set()); diff --git a/v2rayN/v2rayN/v2rayN.csproj b/v2rayN/v2rayN/v2rayN.csproj index 503ddc41..2610148f 100644 --- a/v2rayN/v2rayN/v2rayN.csproj +++ b/v2rayN/v2rayN/v2rayN.csproj @@ -20,8 +20,7 @@ - - +