void ControlNIC(wchar_t* InterfaceName,BOOL bEnable)
{
HANDLE hNdisPwr;
NDISPWR_SAVEPOWERSTATE SavePowerState;
TCHAR szName[MAX_PATH];
int nChars;
TCHAR* pszAdapter;
TCHAR multiSz[257];
DWORD cbBuffer = sizeof(multiSz);
hNdisPwr = CreateFile((PTCHAR)NDISPWR_DEVICE_NAME,
0x00,0x00,NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_OVERLAPPED,
(HANDLE)INVALID_HANDLE_VALUE);
if (hNdisPwr != INVALID_HANDLE_VALUE)
{
SavePowerState.pwcAdapterName = InterfaceName;
if (bEnable)
{
SavePowerState.CePowerState = PwrDeviceUnspecified;
}
else
{
SavePowerState.CePowerState = D4;
}
BOOL ret = DeviceIoControl(hNdisPwr,
IOCTL_NPW_SAVE_POWER_STATE,
&SavePowerState,
sizeof(NDISPWR_SAVEPOWERSTATE),
NULL,0x00,NULL,NULL);
// TODO: take care of the return value 'ret '
CloseHandle(hNdisPwr);
// Just composing a Class-Qualified Device Name, to pass on to SetDevicePower
pszAdapter = new TCHAR[wcslen(InterfaceName)+1];
wcscpy_s(pszAdapter,wcslen(InterfaceName)+1,InterfaceName);
nChars = _sntprintf_s(szName,
MAX_PATH-1,
MAX_PATH-1,
_T("%s\\%s"),
PMCLASS_NDIS_MINIPORT,
pszAdapter);
szName[MAX_PATH-1]=0;
DWORD res = 0;
if(bEnable)
{
res = SetDevicePower(szName,
POWER_NAME,
PwrDeviceUnspecified);
}
else
{
res = SetDevicePower(szName,
POWER_NAME,
D4);
}
// TODO: take care of the return value 'res'
StringCchCopy(multiSz, (cbBuffer / sizeof(TCHAR))-1, pszAdapter);
multiSz[::_tcslen(multiSz)+1] = _T('\0'); // Multi sz needs an extra null
BOOL bRes;
if (bEnable)
{
bRes = DoNdisIOControl(IOCTL_NDIS_BIND_ADAPTER,
multiSz,
(_tcslen(multiSz)+2) * sizeof(TCHAR),
NULL,NULL);
}
else
{
bRes = DoNdisIOControl(IOCTL_NDIS_UNBIND_ADAPTER,
multiSz,
(_tcslen(multiSz)+2) * sizeof(TCHAR),
NULL,NULL);
}
// TODO: take care of the return value 'bRes '
}
}