Write a GUI in Powershell – Part 6

6a. This is the last C# class. This class will call function in Kernel.dll and PowrProf.dll to get power info.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace PowerManagerNS
{

//public Guid GUID_SLEEP_SUBGROUP = new Guid("238C9FA8-0AAD-41ED-83F4-97BE242C8F20");
public static class PowerManager
{

public const uint WM_POWERBROADCAST         = 0x0218; // 536
public const uint PBT_APMQUERYSUSPEND       = 0x0000;
public const uint PBT_APMQUERYSTANDBY       = 0x0001;
public const uint PBT_APMQUERYSUSPENDFAILED = 0x0002;
public const uint PBT_APMQUERYSTANDBYFAILED = 0x0003;
public const uint PBT_APMSUSPEND            = 0x0004;
public const uint PBT_APMSTANDBY            = 0x0005;
public const uint PBT_APMRESUMECRITICAL     = 0x0006;
public const uint PBT_APMRESUMESUSPEND      = 0x0007;
public const uint PBT_APMRESUMESTANDBY      = 0x0008;
public const uint PBT_APMBATTERYLOW         = 0x0009;
public const uint PBT_APMPOWERSTATUSCHANGE  = 0x000A;
public const uint PBT_APMOEMEVENT           = 0x000B;
public const uint PBT_APMRESUMEAUTOMATIC    = 0x0012;
public const uint PBTF_APMRESUMEFROMFAILURE = 0x00000001;
public const uint ES_DISPLAY_REQUIRED       = 0x00000002; // zero the display''s idle timer
public const uint ES_SYSTEM_REQUIRED        = 0x00000001; // zero the system''s idle timer
public const uint ES_CONTINUOUS             = 0x80000000; // keep the display or system on (doesn''t work?)
public const uint BROADCAST_QUERY_DENY      = 0x424D5144;

public const int SystemPowerInformation = 12;
public const uint STATUS_SUCCESS = 0;

public static Guid GUID_SLEEP_SUBGROUP = new Guid("238C9FA8-0AAD-41ED-83F4-97BE242C8F20");

[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_POWER_INFORMATION
{
public uint MaxIdlenessAllowed;
public uint Idleness;
public uint TimeRemaining;
public Byte CoolingMode;
}

public static long size
{
get
{
return System.Runtime.InteropServices.Marshal.SizeOf(typeof(SYSTEM_POWER_INFORMATION));
}
}

[DllImport("PowrProf.dll", CharSet = CharSet.Auto,SetLastError = true)]
public static extern uint CallNtPowerInformation(int InformationLevel, IntPtr lpInputBuffer, long nInputBufferSize,
IntPtr lpOutputBuffer, long nOutputBufferSize);

[DllImport("PowrProf.dll", CharSet = CharSet.Auto,SetLastError = true)]
public static extern uint CallNtPowerInformation(int InformationLevel, IntPtr lpInputBuffer, long nInputBufferSize,
out SYSTEM_POWER_INFORMATION lpOutputBuffer, long nOutputBufferSize);

[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)]
public static extern void SetThreadExecutionState(uint esFlags);

[DllImport("PowrProf.dll", CharSet = CharSet.Auto,SetLastError = true)]
public static extern Int32 PowerReadValueMax(
IntPtr RootPowerKey,
ref Guid SubGroupOfPowerSettingsGuid,
ref Guid PowerSettingGuid,
ref Int32 ValueMaximum
);

public static SYSTEM_POWER_INFORMATION GetPowerInformation1()
{
SYSTEM_POWER_INFORMATION spinfo = new SYSTEM_POWER_INFORMATION();
IntPtr pointer = Marshal.AllocHGlobal(Marshal.SizeOf(spinfo));
Marshal.StructureToPtr(spinfo, pointer, true);
CallNtPowerInformation(SystemPowerInformation, (IntPtr)0, 0, pointer, size);
spinfo = (SYSTEM_POWER_INFORMATION)Marshal.PtrToStructure(pointer, typeof(SYSTEM_POWER_INFORMATION));

return spinfo;
}

public static SYSTEM_POWER_INFORMATION GetPowerInformation()
{
SYSTEM_POWER_INFORMATION spinfo;
uint retval = CallNtPowerInformation(SystemPowerInformation, (IntPtr)0, 0, out spinfo, Marshal.SizeOf(typeof(SYSTEM_POWER_INFORMATION)));
if (retval == STATUS_SUCCESS)
return spinfo;
else
{
Console.WriteLine("Error: " + retval);
return new SYSTEM_POWER_INFORMATION();
}
}

public static Int32 GetPowerReadValueMax()
{
Int32 value=0;
Guid g = new Guid("238C9FA8-0AAD-41ED-83F4-97BE242C8F20");
Guid g2 = Guid.Empty;
Int32 retval = PowerReadValueMax((IntPtr)null, ref GUID_SLEEP_SUBGROUP, ref g2, ref value);
if (retval == STATUS_SUCCESS)
return value;
else
return retval;

}
}
}

6b. Add to Powershell

$code=Get-Content -Raw -Path (Join-Path $path "PowerManagerNS.cs")
Add-Type -TypeDefinition $code

Advertisements
This entry was posted in C#, Powershell, XAML. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s