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 @@ - - +