You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET/C#实现特定网卡上网络服务/过滤器的禁用与启用

编程禁用/启用网络接口指定服务的可行方案

一、修复COM互操作方案(解决0x8004A024错误)

错误0x8004A024核心原因是未正确锁定网络配置或操作逻辑不符合系统要求,修正后的实现需包含以下关键步骤:

  • 必须以管理员身份运行程序
  • 先锁定INetCfg配置,防止其他进程干扰
  • 通过绑定路径控制启用/禁用,而非直接调用UnbindFrom

修正后的C#代码片段:

// 初始化并锁定网络配置
INetCfg netCfg = new NetCfgClass();
INetCfgLock lockCfg = netCfg.QueryInterface<INetCfgLock>();
uint lockTimeout = 30000; // 30秒超时
int lockResult = lockCfg.Lock(null, ref lockTimeout);
if (lockResult != 0)
{
    // 处理锁定失败,比如提示关闭网络设置窗口
    return;
}

try
{
    netCfg.Initialize(null);
    INetCfgComponent? devComponent = FindComponent(netCfg, deviceName); // 目标网络接口组件
    if (devComponent == null) return;

    foreach (string serv in servicesToDisable)
    {
        INetCfgComponent? serviceComponent = FindServiceComponent(netCfg, serv); // 目标服务组件
        if (serviceComponent == null) continue;

        INetCfgComponentBindings serviceBindings = serviceComponent.QueryInterface<INetCfgComponentBindings>();
        if (serviceBindings.IsBoundTo(devComponent) == 0) // 0表示已绑定
        {
            // 获取绑定路径并禁用
            INetCfgBindingPath? bindingPath = null;
            serviceBindings.EnumBindingPaths(out bindingPath);
            bindingPath?.SetEnabled(false);
        }
    }

    netCfg.Apply(); // 提交更改
}
finally
{
    // 释放资源与解锁
    netCfg.Uninitialize();
    lockCfg.Unlock();
    Marshal.ReleaseComObject(netCfg);
    Marshal.ReleaseComObject(lockCfg);
}

二、P/Invoke调用SetupAPI方案

通过Windows底层SetupAPI直接操作设备注册表,修改服务绑定状态:

[DllImport("setupapi.dll", SetLastError = true)]
private static extern bool SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, uint Property, out uint PropertyRegDataType, IntPtr PropertyBuffer, uint PropertyBufferSize, out uint RequiredSize);

[DllImport("setupapi.dll", SetLastError = true)]
private static extern bool SetupDiSetDeviceRegistryProperty(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, uint Property, IntPtr PropertyBuffer, uint PropertyBufferSize);

private const uint SPDRP_CLASSGUID = 0x00000002;
private const uint SPDRP_SERVICE = 0x00000004;

public void DisableServiceOnDevice(string deviceInstanceId)
{
    IntPtr deviceInfoSet = SetupDiGetClassDevs(ref GUID_DEVCLASS_NET, deviceInstanceId, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    if (deviceInfoSet == IntPtr.Zero) return;

    SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA();
    devInfoData.cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
    if (!SetupDiEnumDeviceInfo(deviceInfoSet, 0, ref devInfoData)) return;

    IntPtr hKey = SetupDiOpenDevRegKey(deviceInfoSet, ref devInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ | KEY_WRITE);
    if (hKey == IntPtr.Zero) return;

    try
    {
        // 读取设备Bindings注册表子键,找到目标服务项并设置Enabled值为0
        // 需根据实际服务的注册表路径调整具体逻辑
    }
    finally
    {
        RegCloseKey(hKey);
        SetupDiDestroyDeviceInfoList(deviceInfoSet);
    }
}

注:需先通过GetAdaptersAddresses等API获取目标网络设备的实例ID。

三、CLI命令方案(PowerShell)

利用PowerShell内置命令快速实现,无需复杂编程:

# 禁用指定网卡上的Npcap服务
Disable-NetAdapterBinding -Name "以太网" -DisplayName "Npcap Packet Driver (NPCAP)"

# 启用指定网卡上的Npcap服务
Enable-NetAdapterBinding -Name "以太网" -DisplayName "Npcap Packet Driver (NPCAP)"

若需在C#中调用,可通过进程启动执行:

string adapterName = "以太网";
string serviceDisplayName = "Npcap Packet Driver (NPCAP)";

ProcessStartInfo psi = new ProcessStartInfo
{
    FileName = "powershell.exe",
    Arguments = $"-Command Disable-NetAdapterBinding -Name '{adapterName}' -DisplayName '{serviceDisplayName}'",
    Verb = "runas",
    WindowStyle = ProcessWindowStyle.Hidden
};

Process.Start(psi).WaitForExit();

内容的提问来源于stack exchange,提问作者Ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 12:22:06