1.重启windows网卡命令
rem 禁用网卡
netsh interface set interface 本地连接 disabled
rem 启用网卡
netsh interface set interface 本地连接 enabled
2.根据相关信息作出如下修改
rem 禁用网卡
netsh interface set interface 以太网 disabled
rem 启用网卡
netsh interface set interface 以太网 enabled
三、以下是网上找到的C++代码,
调试OK,需要管理员模式,requireAdministrator (/level=‘requireAdministrator‘),不知道原文地址了,着时尴尬
方法一,VS2015 ANSI
[cpp] view plain copy
- // lan2.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <windows.h>
- #include <Setupapi.h>
- #include <objbase.h>
- #pragma comment(lib, "Setupapi.lib")
- void EnumDevices(HDEVINFO hDevInfo)
- {
- SP_DEVINFO_DATA DeviceInfoData = { sizeof(SP_DEVINFO_DATA) };
- LPOLESTR guid;
- char devName[128];
- char instanceId[128];
- for (int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
- {
- StringFromCLSID(DeviceInfoData.ClassGuid, &guid);
- SetupDiClassNameFromGuid(&DeviceInfoData.ClassGuid, devName, 128, NULL);
- //printf("%s\n", devName);
- ; if (!strcmp(devName, "Net"))
- {
- SetupDiGetDeviceInstanceId(hDevInfo, &DeviceInfoData, instanceId, 128, NULL);
- //printf("instanceId1:%s\n", instanceId);
- if (!strncmp(instanceId, "PCI", 3))
- {
- //wprintf(L"guid:%s\n", guid);
- // printf("devName:%s\n", devName);
- // printf("instanceId2:%s\n", instanceId);
- SP_PROPCHANGE_PARAMS params = { sizeof(SP_CLASSINSTALL_HEADER) };
- params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
- params.Scope = DICS_FLAG_CONFIGSPECIFIC;
- <span style="color:#ff0000;"> params.StateChange = DICS_DISABLE;
- </span><span style="color:#33ff33;"> //params.StateChange = DICS_ENABLE;
- </span> params.HwProfile = 0;
- SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER*)¶ms, sizeof(SP_PROPCHANGE_PARAMS));
- SetupDiChangeState(hDevInfo, &DeviceInfoData);
- }
- }
- CoTaskMemFree(guid);
- }
- }
- int main(int argc, char* argv[])
- {
- HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
- hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
- if (INVALID_HANDLE_VALUE == hDevInfo)
- return GetLastError();
- EnumDevices(hDevInfo);
- SetupDiDestroyDeviceInfoList(hDevInfo);
- return 0;
- }
UNICODE mode
[cpp] view plain copy
- // lan2.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <windows.h>
- #include <Setupapi.h>
- #include <objbase.h>
- #pragma comment(lib, "Setupapi.lib")
- int main(int argc, char* argv[])
- {
- HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
- hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
- if (INVALID_HANDLE_VALUE == hDevInfo)
- return GetLastError();
- //EnumDevices(hDevInfo);
- SP_DEVINFO_DATA DeviceInfoData = { sizeof(SP_DEVINFO_DATA) };
- LPOLESTR guid;
- wchar_t devName[128];
- wchar_t instanceId[128];
- for (int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
- {
- StringFromCLSID(DeviceInfoData.ClassGuid, &guid);
- SetupDiClassNameFromGuid(&DeviceInfoData.ClassGuid, devName, 128, NULL);
- // printf("%s\n", devName);
- if (!wcscmp(devName, _T("Net")))
- {
- SetupDiGetDeviceInstanceId(hDevInfo, &DeviceInfoData, instanceId, 128, NULL);
- //printf("instanceId1:%s\n", instanceId);
- if (!wcsncmp(instanceId, _T("PCI"), 3))
- {
- //wprintf(L"guid:%s\n", guid);
- // printf("devName:%s\n", devName);
- // printf("instanceId2:%s\n", instanceId);
- SP_PROPCHANGE_PARAMS params = { sizeof(SP_CLASSINSTALL_HEADER) };
- params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
- params.Scope = DICS_FLAG_CONFIGSPECIFIC;
- <span style="color:#ff0000;"> // params.StateChange = DICS_DISABLE;
- </span><span style="color:#009900;"> params.StateChange = DICS_ENABLE;
- </span> params.HwProfile = 0;
- SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER*)¶ms, sizeof(SP_PROPCHANGE_PARAMS));
- SetupDiChangeState(hDevInfo, &DeviceInfoData);
- }
- }
- CoTaskMemFree(guid);
- }
- SetupDiDestroyDeviceInfoList(hDevInfo);
- return 0;
- }
方法二.VS2015 UNICODE
SetupDiCallClassInstaller函数需要编译64位程序运行64位系统
[cpp] view plain copy
- // LAN.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <windows.h>
- #include <SetupAPI.h>
- #include <stdio.h>
- #include <tchar.h>
- #define ERROR_IN_WOW64 (APPLICATION_ERROR_MASK|ERROR_SEVERITY_ERROR|0x235)
- #pragma comment(lib,"setupapi.lib")
- DWORD CtrlNetcard(BOOL bStatus);
- void _tmain(int arg, TCHAR** argv)
- {
- BOOL b = FALSE;
- DWORD err = 0;
- if (2 == arg)
- {
- argv++;
- if (!_tcsicmp(TEXT("enable"), *argv))
- {
- b = TRUE;
- err = CtrlNetcard(TRUE);
- }
- else if (!_tcsicmp(TEXT("disable"), *argv))
- {
- b = FALSE;
- err = CtrlNetcard(FALSE);
- }
- else
- {
- goto main_end;
- }
- _tprintf(_T("\n%s NetAdapter %s !\n"),
- b ? TEXT("Enable") : TEXT("Disable"),
- err ? TEXT("fail") : TEXT("success"));
- return;
- }
- main_end:
- _tprintf(TEXT("\nenable NetCard: enable"));
- _tprintf(TEXT("\ndisable NetCard: disable\n"));
- }
- DWORD CtrlNetcard(BOOL bStatus)
- {
- LPCTSTR HardwareId = TEXT("{4D36E972-E325-11CE-BFC1-08002BE10318}");
- GUID g = { 0X4D36E972, 0XE325, 0X11CE,{ 0XBF, 0XC1, 0X08, 0X00, 0X2B, 0XE1, 0X03, 0X18 } };
- DWORD NewState = 0;
- //调用ddk函数,来禁用网卡
- DWORD i = 0, err = 0;
- HDEVINFO hDevInfo = NULL;
- SP_DEVINFO_DATA spDevInfoData = { 0 };
- NewState = bStatus ? DICS_ENABLE : DICS_DISABLE;
- //访问系统的硬件库
- hDevInfo = SetupDiGetClassDevs(NULL, TEXT("PCI"), NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
- if (INVALID_HANDLE_VALUE == hDevInfo)
- {
- _tprintf(TEXT("访问系统硬件出错!"));
- return GetLastError();
- }
- //枚举硬件,获得需要的接口
- spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
- memcmp(&(spDevInfoData.ClassGuid), &g, sizeof(GUID));
- for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &spDevInfoData); i++)
- {
- PBYTE PropertyBuffer = NULL;
- DWORD PropertyBufferSize = 0;
- //获得硬件的属性值
- while (!SetupDiGetDeviceRegistryProperty(
- hDevInfo,
- &spDevInfoData,
- SPDRP_CLASSGUID,
- NULL,
- PropertyBuffer,
- PropertyBufferSize,
- &PropertyBufferSize))
- {
- if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
- {
- if (PropertyBuffer)
- {
- free(PropertyBuffer);
- }
- PropertyBuffer = (PBYTE)malloc(PropertyBufferSize);
- }
- else
- {
- break;
- }
- }
- if (!PropertyBuffer)
- {
- continue;
- }
- //MessageBox(NULL, (LPCTSTR)PropertyBuffer, L"提示", MB_OK);
- if (!_tcsicmp(HardwareId, (LPTSTR)PropertyBuffer))
- {
- SP_PROPCHANGE_PARAMS spPropChangeParams;
- //printf("PropertyBuffer ok\n");
- free(PropertyBuffer);
- spPropChangeParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
- spPropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
- spPropChangeParams.Scope = DICS_FLAG_GLOBAL;
- spPropChangeParams.StateChange = NewState;
- if (!SetupDiSetClassInstallParams(hDevInfo, &spDevInfoData, (SP_CLASSINSTALL_HEADER*)&spPropChangeParams, sizeof(spPropChangeParams)))
- {
- err = GetLastError();
- printf("SetupDiSetClassInstallParams:%d\n", GetLastError());
- goto cleanup_DeviceInfo;
- }
- if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &spDevInfoData))
- {
- err = GetLastError();
- printf("SetupDiCallClassInstaller:%x\n", GetLastError());
- goto cleanup_DeviceInfo;
- }
- }
- else
- {
- //printf("PropertyBuffer error\n");
- free(PropertyBuffer);
- }
- }
- cleanup_DeviceInfo:
- SetupDiDestroyDeviceInfoList(hDevInfo);
- return err;
- }
方法三.网上相关:http://blog.csdn.net/buyicn/article/details/6438635
此前,提到为了实现修改IP(一网卡对应多个IP,不重启电脑让修改立即生效),我尝试在VC6.0上调试采用WMI实现的修改IP的程序,编译不过的情况屡屡出现,在网上搜索了很久就是没有找到彻底的解决方法,最后只好放弃通过WMI来设置IP了!
那么,另一个思路(修改注册表中IP项,然后重启网卡)是否能行呢?于是,继续寻求重启网卡的方法,没想到居然找到了一些不错的资料。现整理如下,供日后参考。
(1)《vc++ 修改IP地址DNS 附带源码》实例代码,来自csdn下载频道,我下载下来调试过了,貌似和我上面提及的思路一致:通过重启网卡来让设置的IP立即生效。
(2)《程序禁用启用网络/网卡》是一个封装好的函数,我复制到本地并适当修改之后,就调试通过了!正好是我想要的:)关于函数中的配置文件ConfigInfo.ini,其内容貌似如下所示:
[cpp] view plain copy
- [NETCARD]
- TYPE=PCI
- NAME=NVIDIA nForce Networking Controller
(3)《实战DeviceIoControl 之五:列举已安装的存储设备》这篇文章详细地介绍了如何访问设备,有代码有注释(作者另外的几篇《实战DeviceIoControl 》也很不错),是很好的入门教材!
(4)《Windows下不重启机器程序修改IP的三种方法》一篇总结性的文章,对几种实现方法做了简介,让我们有一个全局的认识。
网上应该还有好资料,等以后遇到了再整理!
http://blog.csdn.net/greless/article/details/70236969