以编程方式获取 Windows 设置 |账户设置 |需要登录
Programmatically get Windows Setting | Account Setting | Require Sign In
我在尝试以编程方式获取 Windows 设置时遇到问题。我想检测用户是否配置了当 PC 从睡眠中唤醒时需要登录。我只能从屏幕保护程序设置中获取信息,但在这种情况下还不够。
我谷歌搜索了很多但找不到任何解决方案,这可能吗?
谢谢
Screen Capture Require Sign In option
好的,经过长时间的搜索,在 PowerGetActiveScheme ()
和 PowerReadDCValueIndex ()
的帮助下,我找到了您需要的答案
代码示例:(C#)
根据您的需要,您可以轻松转换为C++。
using System;
using System.Runtime.InteropServices;
namespace PowrprofTest
{
class Program
{
private static readonly Guid CONSOLELOCK = new Guid("0e796bdb-100d-47d6-a2d5-f7d2daa51f51");
private static readonly Guid SUB_NONE = new Guid("fea3413e-7e05-4911-9a71-700331f1c294");
[StructLayout(LayoutKind.Sequential)]
public class GuidClass
{
public Guid Value;
}
[DllImport("powrprof.dll")]
public static extern UInt32 PowerGetActiveScheme(
IntPtr UserRootPowerKey,
ref IntPtr ActivePolicyGuid
);
[DllImport("powrprof.dll", CharSet = CharSet.Unicode)]
public static extern UInt32 PowerReadACValueIndex(
IntPtr RootPowerKey,
ref Guid SchemeGuid,
ref Guid SubGroupOfPowerSettingsGuid,
ref Guid PowerSettingGuid,
ref UInt32 AcValueIndex
);
[DllImport("powrprof.dll", CharSet = CharSet.Unicode)]
public static extern UInt32 PowerReadDCValueIndex(
IntPtr RootPowerKey, ref Guid SchemeGuid,
ref Guid SubGroupOfPowerSettingsGuid,
ref Guid PowerSettingGuid,
ref UInt32 AcValueIndex
);
static void Main(string[] args)
{
Guid scheme = GetActiveSchemeGuid();
bool sleepLockEnabledAC = (GetACValue(scheme, SUB_NONE, CONSOLELOCK) == 1) ? true : false;
bool sleepLockEnabledDC = (GetDCValue(scheme, SUB_NONE, CONSOLELOCK) == 1) ? true : false;
Console.WriteLine("Sleep lock enabled on AC : " + sleepLockEnabledAC);
Console.WriteLine("Sleep lock enabled on DC : " + sleepLockEnabledDC);
Console.ReadLine();
}
static Guid GetActiveSchemeGuid()
{
IntPtr activeSchemePtr = IntPtr.Zero;
uint res = PowerGetActiveScheme(IntPtr.Zero, ref activeSchemePtr);
GuidClass temp = new GuidClass();
Marshal.PtrToStructure(activeSchemePtr, temp);
Guid scheme = temp.Value;
return scheme;
}
/// <summary>
/// Get setting for Plugged in
/// </summary>
static UInt32 GetACValue(Guid scheme, Guid subgroup, Guid setting)
{
UInt32 value = 0;
PowerReadACValueIndex(IntPtr.Zero, ref scheme, ref subgroup, ref setting, ref value);
return value;
}
/// <summary>
/// Get setting for On battery
/// </summary>
static UInt32 GetDCValue(Guid scheme, Guid subgroup, Guid setting)
{
UInt32 value = 0;
PowerReadDCValueIndex(IntPtr.Zero, ref scheme, ref subgroup, ref setting, ref value);
return value;
}
}
}
调试:
已在空调上启用睡眠锁:正确
在空调上启用睡眠锁:假
我在尝试以编程方式获取 Windows 设置时遇到问题。我想检测用户是否配置了当 PC 从睡眠中唤醒时需要登录。我只能从屏幕保护程序设置中获取信息,但在这种情况下还不够。 我谷歌搜索了很多但找不到任何解决方案,这可能吗? 谢谢 Screen Capture Require Sign In option
好的,经过长时间的搜索,在 PowerGetActiveScheme ()
和 PowerReadDCValueIndex ()
代码示例:(C#)
根据您的需要,您可以轻松转换为C++。
using System;
using System.Runtime.InteropServices;
namespace PowrprofTest
{
class Program
{
private static readonly Guid CONSOLELOCK = new Guid("0e796bdb-100d-47d6-a2d5-f7d2daa51f51");
private static readonly Guid SUB_NONE = new Guid("fea3413e-7e05-4911-9a71-700331f1c294");
[StructLayout(LayoutKind.Sequential)]
public class GuidClass
{
public Guid Value;
}
[DllImport("powrprof.dll")]
public static extern UInt32 PowerGetActiveScheme(
IntPtr UserRootPowerKey,
ref IntPtr ActivePolicyGuid
);
[DllImport("powrprof.dll", CharSet = CharSet.Unicode)]
public static extern UInt32 PowerReadACValueIndex(
IntPtr RootPowerKey,
ref Guid SchemeGuid,
ref Guid SubGroupOfPowerSettingsGuid,
ref Guid PowerSettingGuid,
ref UInt32 AcValueIndex
);
[DllImport("powrprof.dll", CharSet = CharSet.Unicode)]
public static extern UInt32 PowerReadDCValueIndex(
IntPtr RootPowerKey, ref Guid SchemeGuid,
ref Guid SubGroupOfPowerSettingsGuid,
ref Guid PowerSettingGuid,
ref UInt32 AcValueIndex
);
static void Main(string[] args)
{
Guid scheme = GetActiveSchemeGuid();
bool sleepLockEnabledAC = (GetACValue(scheme, SUB_NONE, CONSOLELOCK) == 1) ? true : false;
bool sleepLockEnabledDC = (GetDCValue(scheme, SUB_NONE, CONSOLELOCK) == 1) ? true : false;
Console.WriteLine("Sleep lock enabled on AC : " + sleepLockEnabledAC);
Console.WriteLine("Sleep lock enabled on DC : " + sleepLockEnabledDC);
Console.ReadLine();
}
static Guid GetActiveSchemeGuid()
{
IntPtr activeSchemePtr = IntPtr.Zero;
uint res = PowerGetActiveScheme(IntPtr.Zero, ref activeSchemePtr);
GuidClass temp = new GuidClass();
Marshal.PtrToStructure(activeSchemePtr, temp);
Guid scheme = temp.Value;
return scheme;
}
/// <summary>
/// Get setting for Plugged in
/// </summary>
static UInt32 GetACValue(Guid scheme, Guid subgroup, Guid setting)
{
UInt32 value = 0;
PowerReadACValueIndex(IntPtr.Zero, ref scheme, ref subgroup, ref setting, ref value);
return value;
}
/// <summary>
/// Get setting for On battery
/// </summary>
static UInt32 GetDCValue(Guid scheme, Guid subgroup, Guid setting)
{
UInt32 value = 0;
PowerReadDCValueIndex(IntPtr.Zero, ref scheme, ref subgroup, ref setting, ref value);
return value;
}
}
}
调试:
已在空调上启用睡眠锁:正确
在空调上启用睡眠锁:假