有关windows系统IP相关API函数学习(1)——GetAdaptersInfo()

windows系统中有关IP的函数(1):GetAdaptersInfo():

获得电脑中的所有是适配器相关的信息,比如适配器的描述(例如:Intel(R) Centrino(R) Advanced-N 6205)、适配器的名称AdapterName(例如:{50447B9F-5B9B-44BE-80D1-75472FC959D8})、IP地址等等。

该函数的定义在IPTYPES.H文件中,要使用必须加载库文件“IPHLPAPI.LIB”。

有以下几个问题:

1、无法获得适配器的真实名称(例如:本地连接);

2、无法获得网卡的各个掩码;

3、无法获得已经禁用的网卡的信息;

4、在学习过程中发现GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize)获得的整个结构的长度(ulAdapterInfoSize)除以每个适配器信息的长度(sizeof(IP_ADAPTER_INFO)),获得的没有禁用的适配器个数总是比实际小1个;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一段 C 语言代码,可以用于设置 Windows 系统IP 地址、网关和 DNS: ``` #include <windows.h> #include <iphlpapi.h> #include <stdio.h> int main() { DWORD dwRetVal = 0; // Create IP helper API structures ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO); PIP_ADAPTER_INFO pAdapterInfo = malloc(sizeof(IP_ADAPTER_INFO)); if (pAdapterInfo == NULL) { printf("Error allocating memory needed to call GetAdaptersInfo\n"); return 1; } // Call GetAdaptersInfo function to get adapter information if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) { // realloc buffer and call function again free(pAdapterInfo); pAdapterInfo = malloc(ulOutBufLen); if (pAdapterInfo == NULL) { printf("Error allocating memory needed to call GetAdaptersInfo\n"); return 1; } dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen); } else { dwRetVal = ERROR_SUCCESS; } if (dwRetVal != ERROR_SUCCESS) { printf("Error calling GetAdaptersInfo\n"); return 1; } // Loop through the list of adapters and set the IP and DNS information PIP_ADAPTER_INFO pAdapter = pAdapterInfo; while (pAdapter != NULL) { // Set the IP address DWORD dwIPAddress = inet_addr("192.168.0.100"); DWORD dwIPSubnetMask = inet_addr("255.255.255.0"); DWORD dwIPGateway = inet_addr("192.168.0.1"); DWORD dwIPDNS = inet_addr("8.8.8.8"); dwRetVal = SetIpAddrTable((PMIB_IPADDRTABLE)&dwIPAddress, dwIPSubnetMask, dwIPGateway); if (dwRetVal != NO_ERROR) { printf("Error setting IP address: %d\n", dwRetVal); return 1; } // Set the DNS information PIP_ADDR_STRING pDNS = &pAdapter->DnsServerList; dwRetVal = DnsWriteResolverConfiguration(pAdapter->AdapterName, pDNS, NULL, 0, 0); if (dwRetVal != NO_ERROR) { printf("Error setting DNS information: %d\n", dwRetVal); return 1; } // Move to the next adapter in the list pAdapter = pAdapter->Next; } printf("IP address, gateway and DNS settings successfully updated\n"); return 0; } ``` 这段代码使用了 WindowsIP helper API 来获取网络适配器信息,并设置了指定的 IP 地址、网关和 DNS 服务器。请注意,这段代码仅用于演示,实际上在修改系统网络配置时需要谨慎,以免导致网络故障。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值