开发者

.NET - setting power options from code

开发者 https://www.devze.com 2023-04-05 08:52 出处:网络
I\'d like to write very simple code from开发者_StackOverflow C# - a simple 2 button app that will allow me to change the action that is taken when the lid is closed.

I'd like to write very simple code from开发者_StackOverflow C# - a simple 2 button app that will allow me to change the action that is taken when the lid is closed. From "sleep" to "no action" and vice versa.

I found that it somehow connected to WMI - but no actual info on how achieving this.

help would be much appreciated.

Thanks!


This is a really old post, but surprisingly, the answer is rather difficult to find elsewhere. Here was my solution to control the power button. Hopefully it helps someone. Although serving a different purpose, this article helped tremendously:

https://www.codeproject.com/Tips/490390/How-to-disable-the-Sleep-button-while-your-code-is

Here are all the required imports. Note that you have a DC and AC value index depending whether the tablet (in my case) is running on battery or not.

[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
static extern UInt32 PowerWriteDCValueIndex(IntPtr RootPowerKey,
    [MarshalAs(UnmanagedType.LPStruct)] Guid SchemeGuid,
    [MarshalAs(UnmanagedType.LPStruct)] Guid SubGroupOfPowerSettingsGuid,
    [MarshalAs(UnmanagedType.LPStruct)] Guid PowerSettingGuid,
    int AcValueIndex);

[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
static extern UInt32 PowerWriteACValueIndex(IntPtr RootPowerKey,
    [MarshalAs(UnmanagedType.LPStruct)] Guid SchemeGuid,
    [MarshalAs(UnmanagedType.LPStruct)] Guid SubGroupOfPowerSettingsGuid,
    [MarshalAs(UnmanagedType.LPStruct)] Guid PowerSettingGuid,
    int AcValueIndex);

[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
static extern UInt32 PowerSetActiveScheme(IntPtr RootPowerKey,
    [MarshalAs(UnmanagedType.LPStruct)] Guid SchemeGuid);

[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
static extern UInt32 PowerGetActiveScheme(IntPtr UserPowerKey, out IntPtr ActivePolicyGuid);

static readonly Guid GUID_SYSTEM_BUTTON_SUBGROUP = new Guid("4f971e89-eebd-4455-a8de-9e59040e7347");
static readonly Guid GUID_POWERBUTTON = new Guid("7648efa3-dd9c-4e3e-b566-50f929386280");
static readonly Guid GUID_SLEEPBUTTON = new Guid("96996bc0-ad50-47ec-923b-6f41874dd9eb ");

And here is how you set it:

IntPtr pActiveSchemeGuid;
var hr = PowerGetActiveScheme(IntPtr.Zero, out pActiveSchemeGuid);
Guid activeSchemeGuid = (Guid)Marshal.PtrToStructure(pActiveSchemeGuid, typeof(Guid));

hr = PowerWriteDCValueIndex(
     IntPtr.Zero,
     activeSchemeGuid,
     GUID_SYSTEM_BUTTON_SUBGROUP,
     GUID_POWERBUTTON,
     0);

PowerSetActiveScheme(IntPtr.Zero, activeSchemeGuid); //This is necessary to apply the current scheme.

where the index is defined here https://msdn.microsoft.com/en-us/library/windows/hardware/mt608287(v=vs.85).aspx

That's it. Some error protection should be incorporated, but it works great.


Take a look at the Windows API Code Pack, which is a wrapper around a lot of the Windows APIs. It includes the Power Management API.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号