win7 设置电源管理方案

2019-07-13 21:53发布

    DWORD DisableDisplayPowerWin7(bool bDisable)
    {
        DWORD dwRet = 0;
        static const GUID id = { 0x9fb78f01, 0xc2c4, 0x4cb5, { 0x80, 0x31, 0xdc, 0x63, 0xc8, 0xa3, 0x62, 0x1 } };
        static GUID* OriginalValue = NULL;
        static BOOL bModify = FALSE;
        DWORD (WINAPI *pPowerGetActiveScheme)(HKEY UserRootPowerKey,GUID** ActivePolicyGuid) = NULL;
        DWORD (WINAPI *pPowerReadFriendlyName)(HKEY RootPowerKey,const GUID* SchemeGuid,const GUID* SubGroupOfPowerSettingsGuid,const GUID* PowerSettingGuid,PUCHAR Buffer,LPDWORD BufferSize) = NULL;
        DWORD (WINAPI *pPowerDuplicateScheme)(HKEY RootPowerKey,const GUID* SourceSchemeGuid,GUID** DestinationSchemeGuid) = NULL;
        DWORD (WINAPI *pPowerWriteFriendlyName)(HKEY RootPowerKey,const GUID* SchemeGuid,const GUID* SubGroupOfPowerSettingsGuid,const GUID* PowerSettingGuid,UCHAR* Buffer,DWORD BufferSize) = NULL;
        DWORD (WINAPI *pPowerWriteACValueIndex)(HKEY RootPowerKey,const GUID* SchemeGuid,const GUID* SubGroupOfPowerSettingsGuid,const GUID* PowerSettingGuid,DWORD AcValueIndex) = NULL;
        DWORD (WINAPI *pPowerWriteDCValueIndex)(HKEY RootPowerKey,const GUID* SchemeGuid,const GUID* SubGroupOfPowerSettingsGuid,const GUID* PowerSettingGuid,DWORD DcValueIndex) = NULL;
        DWORD (WINAPI *pPowerSetActiveScheme)(HKEY UserRootPowerKey,const GUID* SchemeGuid) = NULL;
        DWORD (WINAPI *pPowerDeleteScheme)(HKEY RootPowerKey,const GUID* SchemeGuid) = NULL;
        GUID* pid = (GUID*)&id;
        WCHAR szTmp[MAX_PATH] = {0};
        DWORD dwSize = 0;


        HMODULE hLib = ::LoadLibrary(_T("PowrProf.dll"));
        if (NULL == hLib)
        {
            dwRet = GetLastError();
            goto _Exit;
        }


        pPowerGetActiveScheme = (DWORD (WINAPI *)(HKEY,GUID **))::GetProcAddress(hLib, "PowerGetActiveScheme");
        pPowerReadFriendlyName = (DWORD (WINAPI *)(HKEY RootPowerKey,const GUID* SchemeGuid,const GUID* SubGroupOfPowerSettingsGuid,const GUID* PowerSettingGuid,PUCHAR Buffer,LPDWORD BufferSize))::GetProcAddress(hLib, "PowerReadFriendlyName");
        pPowerDuplicateScheme = (DWORD (WINAPI *)(HKEY,const GUID *,GUID **))::GetProcAddress(hLib, "PowerDuplicateScheme");
        pPowerWriteFriendlyName = (DWORD (WINAPI *)(HKEY,const GUID *,const GUID *,const GUID *,UCHAR *,DWORD))::GetProcAddress(hLib, "PowerWriteFriendlyName");
        pPowerWriteACValueIndex = (DWORD (WINAPI *)(HKEY,const GUID *,const GUID *,const GUID *,DWORD))::GetProcAddress(hLib, "PowerWriteACValueIndex");
        pPowerWriteDCValueIndex = (DWORD (WINAPI *)(HKEY,const GUID *,const GUID *,const GUID *,DWORD))::GetProcAddress(hLib, "PowerWriteDCValueIndex");
        pPowerSetActiveScheme = (DWORD (WINAPI *)(HKEY,const GUID *))::GetProcAddress(hLib, "PowerSetActiveScheme");
        pPowerDeleteScheme = (DWORD (WINAPI *)(HKEY,const GUID *))::GetProcAddress(hLib, "PowerDeleteScheme");


        if (pPowerGetActiveScheme == NULL ||
            pPowerReadFriendlyName == NULL ||
            pPowerDuplicateScheme == NULL ||
            pPowerWriteFriendlyName == NULL ||
            pPowerWriteACValueIndex == NULL ||
            pPowerWriteDCValueIndex == NULL ||
            pPowerSetActiveScheme == NULL ||
            pPowerDeleteScheme == NULL)
        {
            dwRet = GetLastError();
            goto _Exit;
        }


        if (bDisable)
        {
            if (!bModify)
            {
                if (NULL != OriginalValue)
                {
                    LocalFree(OriginalValue);
                    OriginalValue = NULL;
                }


                pPowerGetActiveScheme(NULL, &OriginalValue);


                dwSize = MAX_PATH*sizeof(WCHAR);
                dwRet = pPowerReadFriendlyName(NULL, OriginalValue, NULL, NULL, (PUCHAR)szTmp, &dwSize);
                if (0 != lstrcmpiW(szTmp, L"FUCKPOWER"))
                {
                    dwRet = pPowerDuplicateScheme(NULL, OriginalValue, &pid);
                    if ((0 != dwRet) && (183 != dwRet))
                    {
                        LocalFree(OriginalValue);
                        OriginalValue = NULL;
                        goto _Exit;
                    }


                    pPowerWriteFriendlyName(NULL, &id, NULL, NULL, (UCHAR*)L"FUCKPOWER", (lstrlenW(L"FUCKPOWER")+1)*2);
                }


                //显示器
                pPowerWriteACValueIndex(NULL, &id, &GUID_VIDEO_SUBGROUP, &GUID_VIDEO_POWERDOWN_TIMEOUT, 0);
                pPowerWriteDCValueIndex(NULL, &id, &GUID_VIDEO_SUBGROUP, &GUID_VIDEO_POWERDOWN_TIMEOUT, 0);


                //硬盘
                pPowerWriteACValueIndex(NULL, &id, &GUID_DISK_SUBGROUP, &GUID_DISK_POWERDOWN_TIMEOUT, 0);
                pPowerWriteDCValueIndex(NULL, &id, &GUID_DISK_SUBGROUP, &GUID_DISK_POWERDOWN_TIMEOUT, 0);


                //睡眠
                pPowerWriteACValueIndex(NULL, &id, &GUID_SLEEP_SUBGROUP, &GUID_STANDBY_TIMEOUT, 0);
                pPowerWriteDCValueIndex(NULL, &id, &GUID_SLEEP_SUBGROUP, &GUID_STANDBY_TIMEOUT, 0);


                //待机
                pPowerWriteACValueIndex(NULL, &id, &GUID_SLEEP_SUBGROUP, &GUID_HIBERNATE_TIMEOUT, 0);
                pPowerWriteDCValueIndex(NULL, &id, &GUID_SLEEP_SUBGROUP, &GUID_HIBERNATE_TIMEOUT, 0);


                dwRet = pPowerSetActiveScheme(NULL, &id);
                if (0 != dwRet)
                {
                    LocalFree(OriginalValue);
                    OriginalValue = NULL;


                    pPowerDeleteScheme(NULL, &id);
                    goto _Exit;
                }


                bModify = TRUE;
            }
        }
        else
        {
            if (bModify)
            {
                bModify = FALSE;


                if (NULL != OriginalValue)
                {
                    if (*OriginalValue != id)
                    {
                        pPowerSetActiveScheme(NULL, OriginalValue);
                        pPowerDeleteScheme(NULL, &id);
                    }


                    LocalFree(OriginalValue);
                    OriginalValue = NULL;
                }
                else
                {
                    pPowerDeleteScheme(NULL, &id);
                }
            }
        }
_Exit:
        if (NULL != hLib)
        {
            ::FreeLibrary(hLib);
        }


        return dwRet;
    }