static BOOLEAN CreateEnergyList(HWND hwnd) { BOOLEAN retval; POWER_POLICY pp; SYSTEM_POWER_CAPABILITIES spc; hList = hwnd; if (!GetActivePwrScheme(&aps)) return FALSE; if (!ReadGlobalPwrPolicy(&gGPP)) return FALSE; if (!ReadPwrScheme(aps,&pp)) return FALSE; if (!ValidatePowerPolicies(&gGPP,0)) return FALSE; /* if (!SetActivePwrScheme(aps,&gGPP,&pp)) return FALSE; */ if (!GetPwrCapabilities(&spc)) return FALSE; if (CanUserWritePwrScheme()) { // TODO: // Enable write / delete powerscheme button } Pos_InitPage(GetParent(hwnd)); if (!GetActivePwrScheme(&aps)) return FALSE; retval = EnumPwrSchemes(callback_EnumPwrScheme, aps); if(SendMessage(hwnd, CB_GETCOUNT, 0, 0) > 0) { EnableWindow(GetDlgItem(hwndDialog, IDC_DELETE_BTN),TRUE); EnableWindow(GetDlgItem(hwndDialog, IDC_SAVEAS_BTN),TRUE); } return retval; }
//------------------------------------------------------------------ // create new profile from current one // // return codes: // -1: active profile detection failed // -2: reading active profile policy failed // -3: writing our own profile failed // -4: reading active cpu policy failed // -5: writing our own cpu policy failed // -6: new profile activation failed // 0: profile creation successful //------------------------------------------------------------------ int createProfile() { // get the # of the currently active profile UINT activeProfile; if( !GetActivePwrScheme(&activeProfile) ) return -1; // read the profile policy settings if( !ReadPwrScheme(activeProfile,&toBeCopied) ) return -2; // copy this to the one we use later internalPolicy = toBeCopied; // create the # of our own profile lastChecked++; // disable throttling in this new profile internalPolicy.user.ForcedThrottleAc = 100; internalPolicy.user.ForcedThrottleDc = 100; // write our own profile policy settings to the registry if( !WritePwrScheme(&lastChecked,L"Speedswitch Control",L"\0\0",&internalPolicy) ) return -3; // this is our profile # internalProfile = lastChecked; // read the CPU policy from the active profile if( !ReadProcessorPwrScheme(activeProfile,&mach) ) return -4; // use this CPU policy for our own profile if( !WriteProcessorPwrScheme(internalProfile,&mach) ) return -5; if( !SetActivePwrScheme(internalProfile,NULL,NULL) ) return -6; updateStates(); return 0; }
HRESULT WindowsPowerLW::GetCurrentPowerScheme() { DEBUG_XCOVER_MARK_LINE; memset(&pwrPolicy,0,sizeof(POWER_POLICY)); //得到当前正在使用的Power Scheme的索引 if (!GetActivePwrScheme(&nIndex)) { return HRESULT_FROM_WIN32(::GetLastError()); } DEBUG_XCOVER_MARK_LINE; if (!ReadPwrScheme(nIndex,&pwrPolicy)) { return HRESULT_FROM_WIN32(::GetLastError()); } //得到电源情况 DEBUG_XCOVER_MARK_LINE; if (!GetSystemPowerStatus(&SystemPowerStatus)) { return HRESULT_FROM_WIN32(GetLastError()); } DEBUG_XCOVER_MARK_LINE; return S_OK; }
BOOL PowerSchemes::ApplyPowerSchemeXP(UINT & uiID) { BOOL bResult = FALSE; if(GetActivePwrScheme(&uiID)) { //printf("GetActivePwrScheme OK\n"); //PROCESSOR_POWER_POLICY ppp; MACHINE_PROCESSOR_POWER_POLICY mppp; /* ReadProcessorPwrScheme is available for use in the operating systems specified in the Requirements section. Requirements: Windows XP [desktop apps only] | Windows Server 2003 [desktop apps only] */ if(ReadProcessorPwrScheme(uiID, &mppp)) { printf("ReadProcessorPwrScheme OK\n"); mppp.ProcessorPolicyAc.Policy[0].AllowPromotion = 1; // C1 mppp.ProcessorPolicyAc.Policy[1].AllowPromotion = 1; // C2 mppp.ProcessorPolicyAc.Policy[2].AllowPromotion = 1; // C3 if (WriteProcessorPwrScheme(uiID, &mppp)) SetActivePwrScheme(uiID, 0, 0); bResult = TRUE; } else { //printf("ReadProcessorPwrScheme FAIL\n"); bResult = FALSE; } } else { printf("GetActivePwrScheme FAIL\n"); bResult = FALSE;; } return bResult; }
static BOOLEAN DelScheme(HWND hwnd) { INT iCurSel; HWND hList; TCHAR szBuf[1024], szBufT[1024]; UINT DelScheme; hList = GetDlgItem(hwnd, IDC_ENERGYLIST); iCurSel = SendMessage(hList, CB_GETCURSEL, 0, 0); if (iCurSel == CB_ERR) return FALSE; SendMessage(hList, CB_SETCURSEL, iCurSel, 0); DelScheme = SendMessage(hList, CB_GETITEMDATA, (WPARAM)iCurSel, 0); if (DelScheme == (UINT)CB_ERR) return FALSE; LoadString(hApplet, IDS_DEL_SCHEME_TITLE, szBufT, sizeof(szBufT) / sizeof(TCHAR)); LoadString(hApplet, IDS_DEL_SCHEME, szBuf, sizeof(szBuf) / sizeof(TCHAR)); if (MessageBox(hwnd, (LPCTSTR)szBuf, (LPCTSTR)szBufT, MB_OKCANCEL | MB_ICONQUESTION) == IDOK) { UINT Current; if (GetActivePwrScheme(&Current)) { SendMessage(hList, CB_SETCURSEL, (WPARAM)0, 0); SendMessage(hList, CB_DELETESTRING, (WPARAM)iCurSel, 0); if (Current == DelScheme) Pos_SaveData(hwnd); } if (DeletePwrScheme(DelScheme) != 0) return TRUE; } return FALSE; }
static VOID ShowPowerSchemesPopupMenu( CSysTray *pSysTray) { PWRSCHEMECONTEXT PowerSchemeContext = {NULL, 0, 0}; UINT uiActiveScheme; DWORD id; POINT pt; PowerSchemeContext.hPopup = CreatePopupMenu(); EnumPwrSchemes(PowerSchemesEnumProc, (LPARAM)&PowerSchemeContext); if (GetActivePwrScheme(&uiActiveScheme)) { CheckMenuRadioItem(PowerSchemeContext.hPopup, PowerSchemeContext.uiFirst, PowerSchemeContext.uiLast, uiActiveScheme + 1, MF_BYCOMMAND); } SetForegroundWindow(pSysTray->GetHWnd()); GetCursorPos(&pt); id = TrackPopupMenuEx(PowerSchemeContext.hPopup, TPM_RETURNCMD | TPM_NONOTIFY | TPM_RIGHTALIGN | TPM_BOTTOMALIGN, pt.x, pt.y, pSysTray->GetHWnd(), NULL); DestroyMenu(PowerSchemeContext.hPopup); if (id != 0) SetActivePwrScheme(id - 1, NULL, NULL); }
//------------------------------------------------------------------ // check power scheme integrity // // check: 0=no consistency check // 1=check power scheme only // 2=check processor scheme only // 3=check both power and processor scheme //------------------------------------------------------------------ BOOL checkProfile( int check ) { log( _T("Checking power scheme integrity ...") ); CString err; err.LoadStringW( IDS_MAINERR2 ); POWER_POLICY policyTemp; MACHINE_PROCESSOR_POWER_POLICY machTemp; static TCHAR msg[1024]; // für diverse Fehlertexte BOOL forceActivate = FALSE; if( !ReadPwrScheme(internalProfile,&policyTemp) ) { log( _T("Recreating power scheme ...") ); UINT x = internalProfile; if( !WritePwrScheme(&x,L"Speedswitch Control",L"\0\0",&internalPolicy) ) { CString s1; s1.LoadStringW( IDS_SW1 ); wsprintf( msg, s1, GetLastError() ); MessageBox( NULL, msg, err, MB_ICONEXCLAMATION|MB_OK ); return FALSE; } forceActivate = TRUE; } else if( check&1 ) { if( !comparePowerProfile(policyTemp,internalPolicy) ) { log( _T("Restoring power scheme data ...") ); UINT x = internalProfile; if( !WritePwrScheme(&x,L"Speedswitch Control",L"\0\0",&internalPolicy) ) { CString s1; s1.LoadStringW( IDS_SW2 ); wsprintf( msg, s1, GetLastError() ); MessageBox( NULL, msg, err, MB_ICONEXCLAMATION|MB_OK ); return FALSE; } forceActivate = TRUE; } } if( !ReadProcessorPwrScheme(internalProfile,&machTemp) ) { log( _T("Recreating processor scheme ...") ); if( !WriteProcessorPwrScheme(internalProfile,&mach) ) { CString s1; s1.LoadStringW( IDS_SW3 ); wsprintf( msg, s1, GetLastError() ); MessageBox( NULL, msg, err, MB_ICONEXCLAMATION|MB_OK ); return FALSE; } forceActivate = TRUE; } else if( check&2 ) { if( !compareCPUScheme(machTemp,mach) ) { log( _T("Restoring processor scheme data ...") ); if( !WriteProcessorPwrScheme(internalProfile,&mach) ) { CString s1; s1.LoadStringW( IDS_SW4 ); wsprintf( msg, s1, GetLastError() ); MessageBox( NULL, msg, err, MB_ICONEXCLAMATION|MB_OK ); return FALSE; } forceActivate = TRUE; } } UINT profile; if( !GetActivePwrScheme(&profile) ) { CString s1; s1.LoadStringW( IDS_SW5 ); wsprintf( msg, s1, GetLastError() ); MessageBox( NULL, msg, err, MB_OK|MB_ICONEXCLAMATION ); return FALSE; } if( forceActivate || profile!=internalProfile ) { log( _T("Reactivating power scheme (%d, %d) ..."), profile, internalProfile ); if( !SetActivePwrScheme(internalProfile,NULL,NULL) ) { CString s1; s1.LoadStringW( IDS_SW6 ); wsprintf( msg, s1, GetLastError() ); MessageBox( NULL, msg, err, MB_ICONEXCLAMATION|MB_OK ); return FALSE; } } return TRUE; }