⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pmsysstate.cpp

📁 此代码为WCE5.0下电源管理的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//

//
// This module contains routines for keeping track of devices and 
// managing device power.
//

#include <pmimpl.h>
#include "PmSysReg.h"
// This routine enumerates device power restrictions in the registry
// and adds them to the list of existing restrictions.  It returns a pointer
// to the new list.
PDEVICE_POWER_RESTRICTION
RegReadClassDeviceRestrictions(RegKey * pRegKey, LPCGUID pGuidClass, 
                               PDEVICE_POWER_RESTRICTION pdprCurrent)
{
    TCHAR szDevName[MAX_PATH];
    DWORD dwNameChars, dwType, dwValue, dwValSize, dwIndex, dwStatus;
    DEVICEID devId;
    SETFNAME(_T("RegReadClassDeviceRestrictions"));

    PMLOGMSG(ZONE_REGISTRY, 
        (_T("%s: reading information for class {%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x}\r\n"),
        pszFname,
        pGuidClass->Data1, pGuidClass->Data2, pGuidClass->Data3,
        (pGuidClass->Data4[0] << 8) + pGuidClass->Data4[1], pGuidClass->Data4[2], pGuidClass->Data4[3], 
        pGuidClass->Data4[4], pGuidClass->Data4[5], pGuidClass->Data4[6], pGuidClass->Data4[7]));

    devId.pGuid = pGuidClass;
    dwIndex = 0;
    do {
        dwNameChars = dim(szDevName);
        dwValSize = sizeof(dwValue);
        dwStatus = pRegKey->RegEnumValue( dwIndex, szDevName, &dwNameChars);
        if (dwStatus == ERROR_SUCCESS)
            dwStatus =  pRegKey->RegFindValue(szDevName,(LPBYTE) &dwValue, &dwValSize,&dwType);
        switch(dwStatus) {
        case ERROR_SUCCESS:
            // make sure that the type of this data is a dword and isn't 
            // the reserved word "flags".
            if(dwType != REG_DWORD) {
                PMLOGMSG(ZONE_WARN || ZONE_REGISTRY, 
                    (_T("%s: read wrong type %d\r\n"), pszFname, dwType));
            } else if(_tcsicmp(szDevName, _T("Flags")) == 0) {
                PMLOGMSG(ZONE_REGISTRY, (_T("%s: skipping flags 0x%08x\r\n"),
                    pszFname, dwValue));
            } else if(dwValue < D0 || dwValue > D4) {
                PMLOGMSG(ZONE_WARN || ZONE_REGISTRY, 
                    (_T("%s: invalid device state value %d\r\n"), pszFname,
                    dwValue));
            } else {
                PDEVICE_POWER_RESTRICTION pdprNew;

                // if this is the "default" value it applies to all
                // devices of this class
                if(_tcsicmp(szDevName, _T("default")) == 0) {
                    devId.pszName = NULL;
                } else {
                    devId.pszName = szDevName;
                }
                pdprNew = PowerRestrictionCreate(&devId, 0, (CEDEVICE_POWER_STATE) dwValue, 
                    NULL, 0);
                if(pdprNew != NULL) {
                    PMLOGMSG(ZONE_REGISTRY,  (_T("%s: device '%s' restricted to D%d\r\n"),
                        pszFname, szDevName, dwValue));
                    pdprNew->pNext = pdprCurrent;
                    pdprCurrent = pdprNew;
                }
            }
            break;
        case ERROR_NO_MORE_ITEMS:
            break;
        default:
            // maybe a value of the wrong type or something, treat it as
            // non fatal so that we get all of the device names.
            PMLOGMSG(ZONE_WARN || ZONE_REGISTRY, 
                (_T("%s: RegEnumValue() returned %d\r\n"), pszFname, dwStatus));
            break;
        }
        dwIndex++;
    } while(dwStatus != ERROR_NO_MORE_ITEMS);


    return pdprCurrent;
}

// This routine reads device power restrictions from the registry.  These
// are subkeys of the key describing a system power state.  It builds a
// list of restrictions and returns a pointer to the list.
PDEVICE_POWER_RESTRICTION
RegReadDeviceRestrictions(RegKey * pRegKey)
{
    PDEVICE_POWER_RESTRICTION pdpr = NULL;
    GUID gClass;
    TCHAR szKeyName[MAX_PATH];
    DWORD dwNameChars, dwIndex, dwStatus;
    SETFNAME(_T("RegReadDeviceRestrictions"));

    // build up the list starting with class subkeys (no subkey is allowed
    // for generic devices).
    dwIndex = 0;
    do {
        dwNameChars = dim(szKeyName);
        dwStatus = pRegKey->RegEnumKeyEx(dwIndex, szKeyName, &dwNameChars);
        switch(dwStatus) {
        case ERROR_SUCCESS:
            // convert the key name to a class
            if(ConvertStringToGuid(szKeyName, &gClass)) {
                // open the key
                RegKey * pChildKey =  pRegKey->RegFindKey(szKeyName);
                if(pChildKey == NULL ) {
                    dwStatus = ERROR_NO_MORE_ITEMS;
                    PMLOGMSG(ZONE_WARN || ZONE_REGISTRY,
                        (_T("%s: RegOpenKeyEx('%s') failed %d\r\n"),
                        pszFname, szKeyName));
                } else {
                    // add the class's restriction data to the list
                    pdpr = RegReadClassDeviceRestrictions(pChildKey, 
                        &gClass, pdpr);
                }
            }
            break;
        case ERROR_NO_MORE_ITEMS:
            break;
        default:
            PMLOGMSG(ZONE_WARN || ZONE_REGISTRY, 
                (_T("%s: RegEnumKeyEx() returned %d\r\n"), pszFname, dwStatus));
            break;
        }
        dwIndex++;
    } while(dwStatus != ERROR_NO_MORE_ITEMS);

    // add values from the root key representing generic devices
    pdpr = RegReadClassDeviceRestrictions(pRegKey, &idGenericPMDeviceClass, pdpr);

    return pdpr;
}
EXTERN_C BOOL PmUpdateSystemPowerStatesIfChanged()
{
    return (g_pSysRegistryAccess!=NULL?g_pSysRegistryAccess->UpdateRegistryChange():FALSE);
}
// This routine reads a system state structure from the registry.  It 
// also loads device power restrictions associated with the power state.
// It returns ERROR_SUCCESS if successful or a Win32 error code otherwise.
// Note that malformed device power values won't cause an error.  The new
// system state information and device power restrictions will be passed
// back via pointers.
// If ppsps is NULL, the state keys are read but not passed back.  If ppdpr
// is NULL, individual device power settings are not read or passed back.
DWORD
RegReadSystemPowerState(LPCTSTR pszName, PPSYSTEM_POWER_STATE ppsps, 
                     PPDEVICE_POWER_RESTRICTION ppdpr)
{
    DWORD dwRetStatus = ERROR_SUCCESS;
    DWORD dwStatus, dwLen = MAX_PATH;
    RegKey *keyHandle;
    TCHAR szPath[MAX_PATH];
    SETFNAME(_T("ReadSystemPowerState"));

    PMLOCK();
    if (g_pSysRegistryAccess==NULL) {
        // format the key name
        wsprintf(szPath, _T("%s\\%s"), PWRMGR_REG_KEY, _T("State"));
        dwLen = (dim(szPath) - 1) - _tcslen(szPath);
        g_pSysRegistryAccess = new SystemNotifyRegKey (HKEY_LOCAL_MACHINE,szPath);
        if (g_pSysRegistryAccess && g_pSysRegistryAccess->Init()== FALSE) {
            delete g_pSysRegistryAccess;
            g_pSysRegistryAccess = NULL;
        }
    }
    PMUNLOCK();
    if (g_pSysRegistryAccess== NULL ) { // false to initialize the structure.
        return ERROR_INVALID_PARAMETER;
    }
    g_pSysRegistryAccess->EnterLock();
    __try {
        // copy the name and make sure it's null terminated
        _tcsncpy(szPath, pszName, dwLen);
        szPath[dim(szPath) - 1] = 0;
    }
    __except(EXCEPTION_EXECUTE_HANDLER) {
        PMLOGMSG(ZONE_WARN, (_T("%s: exception copying state name\r\n"), pszFname));
        dwRetStatus = ERROR_INVALID_PARAMETER;
    }
    
    keyHandle = g_pSysRegistryAccess->RegFindKey(szPath);
    if(keyHandle == NULL ) {
        PMLOGMSG(ZONE_WARN | ZONE_REGISTRY, 
            (_T("%s: RegOpenKeyEx('%s') failed\r\n"), pszFname, szPath));
        dwRetStatus = ERROR_NO_MORE_ITEMS ;
    } else {
        DWORD dwDefaultDx, dwFlags, dwSize, dwType;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -