// -------------------------------------------------------------------------------------------------------------------- // // This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. // // // Win32 API calls // // -------------------------------------------------------------------------------------------------------------------- namespace HandBrake.ApplicationServices.Utilities { using System; using System.Runtime.InteropServices; using System.Windows.Threading; /// /// Win32 API calls /// public class Win32 { /// /// Used to force UI Thread. /// private static Action executor = action => action(); /// /// Set the Forground Window /// /// /// The h wnd. /// /// /// A Boolean true when complete. /// [DllImport("user32.dll")] public static extern bool SetForegroundWindow(int hWnd); /// /// Lock the workstation /// [DllImport("user32.dll")] public static extern void LockWorkStation(); /// /// Exit Windows /// /// /// The u flags. /// /// /// The dw reason. /// /// /// an integer /// [DllImport("user32.dll")] public static extern int ExitWindowsEx(int uFlags, int dwReason); /// /// Memory Status EX Struct /// public struct MEMORYSTATUSEX { public int dwLength; public int dwMemoryLoad; public ulong ullTotalPhys; public ulong ullAvailPhys; public ulong ullTotalPageFile; public ulong ullAvailPageFile; public ulong ullTotalVirtual; public ulong ullAvailVirtual; public ulong ullAvailExtendedVirtual; } /// /// Get the System Memory information /// /// /// The lp buffer. /// /// /// A boolean. /// [DllImport("kernel32.dll", SetLastError = true)] public static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer); /// /// Generate a Console Ctrl Event /// /// /// The sigevent. /// /// /// The dw process group id. /// /// /// Bool true is sucess /// [DllImport("kernel32.dll", SetLastError = true)] public static extern bool GenerateConsoleCtrlEvent(ConsoleCtrlEvent sigevent, int dwProcessGroupId); /// /// Console Ctrl Event /// public enum ConsoleCtrlEvent { /// /// Ctrl - C /// CTRL_C = 0, /// /// Ctrl - Break /// CTRL_BREAK = 1, /// /// Ctrl - Close /// CTRL_CLOSE = 2, } [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags); /// /// Execution State /// [Flags] public enum EXECUTION_STATE : uint { ES_SYSTEM_REQUIRED = 0x00000001, ES_CONTINUOUS = 0x80000000, ES_AWAYMODE_REQUIRED = 0x00000040 } /// /// Initializes static members of the class. /// static Win32() { InitializeWithDispatcher(); } /// /// Prevent the system from sleeping /// public static void PreventSleep() { executor( () => SetThreadExecutionState( EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_AWAYMODE_REQUIRED)); } /// /// Allow the system to sleep. /// public static void AllowSleep() { executor(() => SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS)); } /// /// Initializes the framework using the current dispatcher. /// public static void InitializeWithDispatcher() { var dispatcher = Dispatcher.CurrentDispatcher; SetUIThreadMarshaller(action => { if (dispatcher.CheckAccess()) action(); else dispatcher.Invoke(action); }); } /// /// Sets a custom UI thread marshaller. /// /// The marshaller. public static void SetUIThreadMarshaller(Action marshaller) { executor = marshaller; } } }