C++启用禁用网卡的代码

时间: 2023-11-01 11:07:41 浏览: 73
以下是使用C++实现启用禁用网卡的示例代码: ```c++ #include <WinSock2.h> #include <Iphlpapi.h> #include <iostream> #pragma comment(lib, "iphlpapi.lib") int main() { // 获取所有网卡信息 DWORD dwSize = 0; DWORD dwRetVal = 0; PIP_ADAPTER_INFO pAdapterInfo = NULL; GetAdaptersInfo(NULL, &dwSize); pAdapterInfo = (PIP_ADAPTER_INFO)malloc(dwSize); if (pAdapterInfo == NULL) { std::cout << "Failed to allocate memory for adapter info" << std::endl; return 1; } if (GetAdaptersInfo(pAdapterInfo, &dwSize) != ERROR_SUCCESS) { std::cout << "Failed to get adapter info" << std::endl; free(pAdapterInfo); return 1; } // 输出所有网卡信息并让用户选择 std::cout << "Available network adapters:" << std::endl; PIP_ADAPTER_INFO currentAdapter = pAdapterInfo; int adapterIndex = 0; while (currentAdapter) { std::cout << adapterIndex << ". " << currentAdapter->AdapterName << " - " << currentAdapter->Description << std::endl; currentAdapter = currentAdapter->Next; adapterIndex++; } std::cout << "Enter the index of the adapter to enable/disable (0-" << adapterIndex - 1 << "): "; int chosenIndex; std::cin >> chosenIndex; if (chosenIndex < 0 || chosenIndex >= adapterIndex) { std::cout << "Invalid adapter index" << std::endl; free(pAdapterInfo); return 1; } currentAdapter = pAdapterInfo; for (int i = 0; i < chosenIndex; i++) { currentAdapter = currentAdapter->Next; } // 获取网卡状态并启用/禁用 MIB_IFROW ifRow; memset(&ifRow, 0, sizeof(ifRow)); ifRow.dwIndex = currentAdapter->Index; dwRetVal = GetIfEntry(&ifRow); if (dwRetVal != NO_ERROR) { std::cout << "Failed to get interface info" << std::endl; free(pAdapterInfo); return 1; } if (ifRow.dwAdminStatus == IF_ADMIN_STATUS_UP) { std::cout << "Disabling adapter..." << std::endl; ifRow.dwAdminStatus = IF_ADMIN_STATUS_DOWN; } else { std::cout << "Enabling adapter..." << std::endl; ifRow.dwAdminStatus = IF_ADMIN_STATUS_UP; } dwRetVal = SetIfEntry(&ifRow); if (dwRetVal != NO_ERROR) { std::cout << "Failed to set interface info" << std::endl; free(pAdapterInfo); return 1; } std::cout << "Success!" << std::endl; free(pAdapterInfo); return 0; } ``` 上述代码使用Winsock API和IP Helper API获取网卡信息,并允许用户选择要启用/禁用的网卡。然后,它使用SetIfEntry函数启用/禁用选定的网卡。注意,此方法仅适用于Windows操作系统。

相关推荐

最新推荐

recommend-type

在C++程序中开启和禁用Windows设备的无线网卡的方法

主要介绍了在C++程序中开启和禁用Windows设备的无线网卡的方法,包括一些常见错误的分析与解决,需要的朋友可以参考下
recommend-type

C++获得本机所有网卡的IP和MAC地址信息的实现方法

下面小编就为大家带来一篇C++获得本机所有网卡的IP和MAC地址信息的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

c++读取excel的代码详解

c++如何读取excel?C++ ODBC操作excel全过程 想要通过ODBC直接读、写Excel表格文件,首先,应确保ODBC中已安装有Excel表格文件的驱动”MICROSOFT EXCEL DRIVER (*.XLS)”。然后,可根据下面步骤进行: 1. 在StdAfx.h...
recommend-type

使用C++调用Python代码的方法详解

主要介绍了使用C++调用Python代码并给大家介绍了.py和.pyc的区别,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

C++递归算法实例代码

主要介绍了C++递归算法实例代码,还是比较不错的,运用了递归算法解决相关问题,这里分享给大家,需要的朋友可以参考下。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。