[Mwinapi-commits] SF.net SVN: mwinapi: [72] trunk/ManagedWinapi
Status: Beta
Brought to you by:
schierlm
|
From: <sch...@us...> - 2008-06-14 19:02:22
|
Revision: 72
http://mwinapi.svn.sourceforge.net/mwinapi/?rev=72&view=rev
Author: schierlm
Date: 2008-06-14 12:02:21 -0700 (Sat, 14 Jun 2008)
Log Message:
-----------
PrivilegedActions: Shutdown and set system time
Modified Paths:
--------------
trunk/ManagedWinapi/CodepointRange.cs
trunk/ManagedWinapi/ManagedWinapi.csproj
Added Paths:
-----------
trunk/ManagedWinapi/PrivilegedActions.cs
Modified: trunk/ManagedWinapi/CodepointRange.cs
===================================================================
--- trunk/ManagedWinapi/CodepointRange.cs 2008-06-14 18:49:09 UTC (rev 71)
+++ trunk/ManagedWinapi/CodepointRange.cs 2008-06-14 19:02:21 UTC (rev 72)
@@ -164,6 +164,8 @@
}
#region Equals and HashCode
+
+ ///
public override bool Equals(object obj)
{
CodepointRange cr = obj as CodepointRange;
@@ -181,6 +183,7 @@
return true;
}
+ ///
public override int GetHashCode()
{
return 3 * codepointCount + 7 * ranges.Length + 9 * FirstCodepoint + 11 * LastCodepoint;
@@ -192,10 +195,10 @@
private static extern uint GetFontUnicodeRanges(IntPtr hdc, IntPtr lpgs);
[DllImport("gdi32.dll")]
- private extern static IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
+ private static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("gdi32.dll")]
- public static extern bool DeleteObject(IntPtr hObject);
+ private static extern bool DeleteObject(IntPtr hObject);
#endregion
}
}
Modified: trunk/ManagedWinapi/ManagedWinapi.csproj
===================================================================
--- trunk/ManagedWinapi/ManagedWinapi.csproj 2008-06-14 18:49:09 UTC (rev 71)
+++ trunk/ManagedWinapi/ManagedWinapi.csproj 2008-06-14 19:02:21 UTC (rev 72)
@@ -66,6 +66,7 @@
<Compile Include="ShortcutBox.Designer.cs">
<DependentUpon>ShortcutBox.cs</DependentUpon>
</Compile>
+ <Compile Include="PrivilegedActions.cs" />
<Compile Include="SystemAccessibleObject.cs" />
<Compile Include="ApiHelper.cs" />
<Compile Include="ClipboardNotifier.cs">
Added: trunk/ManagedWinapi/PrivilegedActions.cs
===================================================================
--- trunk/ManagedWinapi/PrivilegedActions.cs (rev 0)
+++ trunk/ManagedWinapi/PrivilegedActions.cs 2008-06-14 19:02:21 UTC (rev 72)
@@ -0,0 +1,168 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Runtime.InteropServices;
+
+namespace ManagedWinapi
+{
+ /// <summary>
+ /// Collection of miscellaneous actions that cannot be performed as
+ /// a non-administrative user, like shutdown or setting the system time.
+ /// </summary>
+ public static class PrivilegedActions
+ {
+ /// <summary>
+ /// Shutdown the system.
+ /// </summary>
+ public static void ShutDown(ShutdownAction action)
+ {
+ ShutDown(action, ShutdownForceMode.NoForce);
+ }
+
+ /// <summary>
+ /// Shutdown the system.
+ /// </summary>
+ public static void ShutDown(ShutdownAction action, ShutdownForceMode forceMode)
+ {
+ ApiHelper.FailIfZero(ExitWindowsEx((uint)action | (uint)forceMode, SHTDN_REASON_FLAG_PLANNED));
+ }
+
+ /// <summary>
+ /// Get or set the system time in the local timezone.
+ /// </summary>
+ public static DateTime LocalTime
+ {
+ get
+ {
+ SYSTEMTIME st = new SYSTEMTIME();
+ ApiHelper.FailIfZero(GetLocalTime(ref st));
+ return st.ToDateTime();
+ }
+
+ set
+ {
+ SYSTEMTIME st = new SYSTEMTIME(value);
+ // Set it twice due to possible daylight savings change
+ ApiHelper.FailIfZero(SetLocalTime(ref st));
+ ApiHelper.FailIfZero(SetLocalTime(ref st));
+ }
+ }
+
+ /// <summary>
+ /// Get or set the system time, in UTC.
+ /// </summary>
+ public static DateTime SystemTime
+ {
+ get
+ {
+ SYSTEMTIME st = new SYSTEMTIME();
+ ApiHelper.FailIfZero(GetSystemTime(ref st));
+ return st.ToDateTime();
+ }
+
+ set
+ {
+ SYSTEMTIME st = new SYSTEMTIME(value);
+ ApiHelper.FailIfZero(SetLocalTime(ref st));
+ }
+ }
+
+ /// <summary>
+ /// Actions that can be performed at shutdown.
+ /// </summary>
+ public enum ShutdownAction : uint
+ {
+ /// <summary>
+ /// Log off the currently logged-on user.
+ /// </summary>
+ LogOff = 0x00,
+
+ /// <summary>
+ /// Shut down the system.
+ /// </summary>
+ ShutDown = 0x01,
+
+ /// <summary>
+ /// Reboot the system.
+ /// </summary>
+ Reboot = 0x02,
+
+ /// <summary>
+ /// Shut down the system and power it off.
+ /// </summary>
+ PowerOff = 0x08,
+
+ /// <summary>
+ /// Reboot the system and restart applications that are running
+ /// now and support this feature.
+ /// </summary>
+ RestartApps = 0x40,
+ }
+
+ /// <summary>
+ /// Whether shutdown should be forced if an application cancels it
+ /// or is hung.
+ /// </summary>
+ public enum ShutdownForceMode : uint
+ {
+ /// <summary>
+ /// Do not force shutdown, applications can cancel it.
+ /// </summary>
+ NoForce = 0x00,
+
+ /// <summary>
+ /// Force shutdown, even if application cancels it or is hung.
+ /// </summary>
+ Force = 0x04,
+
+ /// <summary>
+ /// Force shutdown if application is hung, but not if it cancels it.
+ /// </summary>
+ ForceIfHung = 0x10
+ }
+
+ #region PInvoke Declarations
+
+ [DllImport("user32.dll", SetLastError = true)]
+ static extern int ExitWindowsEx(uint uFlags, uint dwReason);
+
+ const uint SHTDN_REASON_FLAG_PLANNED = 0x80000000;
+
+ struct SYSTEMTIME
+ {
+ internal ushort wYear, wMonth, wDayOfWeek, wDay,
+ wHour, wMinute, wSecond, wMilliseconds;
+
+ internal SYSTEMTIME(DateTime time)
+ {
+ wYear = (ushort)time.Year;
+ wMonth = (ushort)time.Month;
+ wDayOfWeek = (ushort)time.DayOfWeek;
+ wDay = (ushort)time.Day;
+ wHour = (ushort)time.Hour;
+ wMinute = (ushort)time.Minute;
+ wSecond = (ushort)time.Second;
+ wMilliseconds = (ushort)time.Millisecond;
+ }
+
+ internal DateTime ToDateTime()
+ {
+ return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);
+ }
+ }
+
+ [DllImport("kernel32.dll", SetLastError = true)]
+ static extern int GetSystemTime(ref SYSTEMTIME lpSystemTime);
+
+ [DllImport("kernel32.dll", SetLastError = true)]
+ static extern int SetSystemTime(ref SYSTEMTIME lpSystemTime);
+
+ [DllImport("kernel32.dll", SetLastError = true)]
+ static extern int GetLocalTime(ref SYSTEMTIME lpSystemTime);
+
+ [DllImport("kernel32.dll", SetLastError = true)]
+ static extern int SetLocalTime(ref SYSTEMTIME lpSystemTime);
+
+ #endregion
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|