GetPwrCapabilities 在某些计算机上给我错误的睡眠状态结果.我怎样才能得到更准确的结果? [英] GetPwrCapabilities is giving me wrong sleep state results on some computers. How can I get a more accurate result?

查看:25
本文介绍了GetPwrCapabilities 在某些计算机上给我错误的睡眠状态结果.我怎样才能得到更准确的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台 64 位 Win7 Ultimate 机器,我正在调用 GetPwrCapabilities.但是它告诉我睡眠状态 4(休眠)不可用并且没有休眠文件.我可以使机器休眠,并且有一个休眠文件.难道我做错了什么?还有什么我可以打电话来获得准确支持的睡眠状态

I have a 64-bit Win7 Ultimate machine that I'm calling GetPwrCapabilities on. However it tells me that sleep state 4 (hibernation) is not available and that there is no hibernation file. I can hibernate the machine and there is a hibernation file. Am i doing something wrong? Is there anything else I can call to get accurate supported sleep states

谢谢

编辑有趣的是 powercfg -AVAILABLESTATES 提供了正确的信息.有谁知道它调用什么 API 或者为什么存在差异

EDIT It's interesting that powercfg -AVAILABLESTATES provides the correct information. Does anyone know what API it calls or why there is a discrepency

添加代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;

class Program
{
    [DllImport("PowrProf.dll")]
    public static extern bool GetPwrCapabilities(out SYSTEM_POWER_CAPABILITIES lpSystemPowerCapabilities);

    static void Main(string[] args)
    {
        SYSTEM_POWER_CAPABILITIES spc;
        bool well = GetPwrCapabilities(out spc);

        Stream stdOut = System.Console.OpenStandardOutput();
        XmlSerializer x = new XmlSerializer(typeof(SYSTEM_POWER_CAPABILITIES));
        x.Serialize(stdOut, spc);
    }
}

[Serializable]
public struct SYSTEM_POWER_CAPABILITIES
{
    public bool PowerButtonPresent;
    public bool SleepButtonPresent;
    public bool LidPresent;
    public bool SystemS1;
    public bool SystemS2;
    public bool SystemS3;
    public bool SystemS4;
    public bool SystemS5;
    public bool HiberFilePresent;
    public bool FullWake;
    public bool VideoDimPresent;
    public bool ApmPresent;
    public bool UpsPresent;
    public bool ThermalControl;
    public bool ProcessorThrottle;
    public byte ProcessorMinThrottle;
    public byte ProcessorMaxThrottle;
    public bool FastSystemS4;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
    public byte[] spare2;
    public bool DiskSpinDown;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
    public byte[] spare3;
    public bool SystemBatteriesPresent;
    public bool BatteriesAreShortTerm;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
    public BATTERY_REPORTING_SCALE[] BatteryScale;
    public SYSTEM_POWER_STATE AcOnLineWake;
    public SYSTEM_POWER_STATE SoftLidWake;
    public SYSTEM_POWER_STATE RtcWake;
    public SYSTEM_POWER_STATE MinDeviceWakeState;
    public SYSTEM_POWER_STATE DefaultLowLatencyWake;
}
public struct BATTERY_REPORTING_SCALE
{
    public UInt32 Granularity;
    public UInt32 Capacity;
}
public enum SYSTEM_POWER_STATE
{
    PowerSystemUnspecified = 0,
    PowerSystemWorking = 1,
    PowerSystemSleeping1 = 2,
    PowerSystemSleeping2 = 3,
    PowerSystemSleeping3 = 4,
    PowerSystemHibernate = 5,
    PowerSystemShutdown = 6,
    PowerSystemMaximum = 7
}

推荐答案

bool 被编组为 4 字节.这与 Win32 BOOL 匹配.但是 Win32 BOOLEAN 是一个字节,因此您需要使用 [MarshalAs(UnmanagedType.I1)] 标记所有 SYSTEM_POWER_CAPABILITIES bool 成员.

bool is marshalled as 4 byte. This matches Win32 BOOL. But Win32 BOOLEAN is one byte, so you need to flag all your SYSTEM_POWER_CAPABILITIES bool members with [MarshalAs(UnmanagedType.I1)].

这篇关于GetPwrCapabilities 在某些计算机上给我错误的睡眠状态结果.我怎样才能得到更准确的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆