如何用程序禁用与启用网卡?

如何用程序禁用与启用网卡?

BOOL Disable_NetCard(bool bStatus)
{
GUID guid;
UuidFromString((unsigned char *)"4D36E972-E325-11CE-BFC1-08002BE10318", &guid);

DWORD NewState;

if(bStatus)
{
NewState=DICS_DISABLE;//禁用
}
else
{
NewState=DICS_ENABLE;//启用
}

//调用ddk函数,来禁用网卡

DWORD i;
BOOL Found=false;

HDEVINFO hDevInfo;
SP_DEVINFO_DATA spDevInfoData;

//访问系统的硬件库
hDevInfo=SetupDiGetClassDevs( &guid, NULL, NULL, DIGCF_PRESENT );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
AfxMessageBox("访问系统硬件出错!");
return false;
}

//枚举硬件,获得需要的接口
memset( &spDevInfoData, 0, sizeof(SP_DEVINFO_DATA));
spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&spDevInfoData);i++)
{
LPTSTR buffer = NULL;
DWORD buffersize = 0;

if (!SetupDiEnumDeviceInfo(hDevInfo, i, &spDevInfoData))
{
AfxMessageBox("SetupDiEnumDeviceInfo出错!");
return false;
}

//禁用该设备

SP_PROPCHANGE_PARAMS spPropChangeParams;

spPropChangeParams.ClassInstallHeader.cbSize=sizeof(SP_CLASSINSTALL_HEADER);
spPropChangeParams.ClassInstallHeader.InstallFunction=DIF_PROPERTYCHANGE;
spPropChangeParams.Scope=DICS_FLAG_GLOBAL;
spPropChangeParams.StateChange=NewState;//禁用:DICS_DISABLE,DICS_ENABLE启用

if(!SetupDiSetClassInstallParams(hDevInfo,&spDevInfoData,(SP_CLASSINSTALL_HEADER *)&spPropChangeParams,sizeof(spPropChangeParams)))
{
DWORD errorcode=GetLastError();
}

if(!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,hDevInfo,&spDevInfoData))
{
DWORD errorcode=GetLastError();
}

}

//退出时,清理工作环境
SetupDiDestroyDeviceInfoList(hDevInfo);
return true;
}