// 第一种方法:
// 直接从注册表取值的方式
HKEY hKey;TCHAR szPath[MAX_PATH] = TEXT("\\System\\CurrentControlSet\\Control\\Power\\State");
if(ERROR_SUCCESS != ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, szPath, 0, 0, &hKey))
{
debug_level((DBG_ERROR, "ERROR: CWLAN::GetPowerState >> RegOpenKeyEx"));
return false;
}
TCHAR valueName[MAX_PATH];
memset(valueName, 0, sizeof(valueName));
wsprintf(valueName, TEXT("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\\%s"), adapterName);
DWORD dwState = 0;
DWORD dwType = 0;
DWORD dwSize = MAX_PATH; // 这是要注意,不能初值为"0"
if(ERROR_SUCCESS != RegQueryValueEx(hKey, valueName, NULL, &dwType, (LPBYTE)&dwState, &dwSize))
{
debug_level((DBG_ERROR, "ERROR: CWLAN::GetPowerState >> RegQueryValueEx"));
::RegCloseKey(hKey);
return false;
}
::RegCloseKey(hKey);
powerState = dwState == 1 ? true : false;
/////////////////////////////////////////////////////////////////////////
//第二种方法:
// GetDevicePower API 方式
TCHAR device[MAX_PATH];
memset(device, 0, sizeof(device));
wsprintf(device, TEXT("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\\%s"), adapterName);
CEDEVICE_POWER_STATE state = D4;
if(ERROR_SUCCESS != ::GetDevicePower(device, POWER_NAME, &state))
{
debug_level((DBG_ERROR, "ERROR: CWLAN::GetPowerState >> GetDevicePower"));
return false;
}
powerState = state == D0 ? true : false;