private static List <SafeHeapHandle <Guid> > GetPowerSchemeGuids() { List <SafeHeapHandle <Guid> > result = new List <SafeHeapHandle <Guid> >(); IntPtr powerSchemeGuidBuffer; int guidSize = Marshal.SizeOf(typeof(Guid)); int index = 0; int returnCode = 0; while (returnCode == 0) { powerSchemeGuidBuffer = Marshal.AllocHGlobal(guidSize); returnCode = PowrProf.PowerEnumerate(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, PowrProf.PowerDataAccessor.ACCESS_SCHEME, index, powerSchemeGuidBuffer, ref guidSize); if (returnCode == 259) { break; } if (returnCode != 0) { throw new COMException("Error occurred while enumerating power schemes. Win32 error code: " + returnCode); } result.Add(new SafeHeapHandle <Guid>(powerSchemeGuidBuffer)); index++; } return(result); }
private static string GetSchemeName(SafeHeapHandle <Guid> guid) { IntPtr schemeNameGuidPtr = guid.DangerousGetHandle(); int buffSize = 0; if (PowrProf.PowerReadFriendlyName(IntPtr.Zero, schemeNameGuidPtr, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref buffSize) != ReturnCodes.ERROR_SUCCESS) { return(string.Empty); } if (buffSize == 0) { return(string.Empty); } IntPtr schemeNamePtr = Marshal.AllocHGlobal(buffSize); if (PowrProf.PowerReadFriendlyName(IntPtr.Zero, schemeNameGuidPtr, IntPtr.Zero, IntPtr.Zero, schemeNamePtr, ref buffSize) != ReturnCodes.ERROR_SUCCESS) { Marshal.FreeHGlobal(schemeNamePtr); return(string.Empty); } string schemeName = Marshal.PtrToStringUni(schemeNamePtr); Marshal.FreeHGlobal(schemeNamePtr); return(schemeName); }
/// <summary> /// コンピューターを強制的にシャットダウン・再起動・ログオフ・スタンバイ・休止状態にします。 /// </summary> /// <param name="shutDownSwitch"></param> public static void ShutDown(ShutDownSwitch shutDownSwitch) { if (Environment.OSVersion.Platform == PlatformID.Win32NT) { adjustToken(); } EWX ewx = EWX.FORCE; switch (shutDownSwitch) { case ShutDownSwitch.PowerOff: ewx |= EWX.POWEROFF; break; case ShutDownSwitch.Reboot: ewx |= EWX.REBOOT; break; case ShutDownSwitch.LogOff: ewx |= EWX.LOGOFF; break; case ShutDownSwitch.StandBy: case ShutDownSwitch.Hibernate: bool hibernate = (shutDownSwitch == ShutDownSwitch.Hibernate); PowrProf.SetSuspendState(hibernate, true, true); return; default: throw new ArgumentException(); } User32.ExitWindowsEx(ewx, 0); }
private static bool Suspend(bool hibernate, bool wakeupEventsDisabled) { try { using (new PrivilegedCodeBlock(SystemPrivilege.Shutdown)) { if (hibernate) { return(PowrProf.IsPwrHibernateAllowed() ? PowrProf.SetSuspendState(true, false, wakeupEventsDisabled) : false); } else { return(PowrProf.IsPwrSuspendAllowed() ? PowrProf.SetSuspendState(false, false, wakeupEventsDisabled) : false); } } } catch { return(false); } }