Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET - setting power options from code

I'd like to write very simple code from 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!

like image 591
Roman Avatar asked Sep 17 '11 18:09

Roman


2 Answers

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.

like image 142
T James Avatar answered Oct 12 '22 23:10

T James


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.

like image 26
Jim Mischel Avatar answered Oct 13 '22 01:10

Jim Mischel