/* * ThreadPilot - Windows process memory priority native API abstraction. */ namespace ThreadPilot.Platforms.Windows { using System; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; public interface IProcessMemoryPriorityNativeApi { bool IsSupported { get; } SafeProcessHandle OpenProcess(ProcessAccessFlags access, bool inheritHandle, uint processId); bool GetProcessInformation( SafeProcessHandle process, ProcessInformationClass processInformationClass, ref MemoryPriorityInformation processInformation, uint processInformationSize); bool SetProcessInformation( SafeProcessHandle process, ProcessInformationClass processInformationClass, ref MemoryPriorityInformation processInformation, uint processInformationSize); int GetLastWin32Error(); int QueryIoPriority(SafeProcessHandle process, ref int priority, out uint returnLength) { returnLength = 0; return unchecked((int)0xC0000002); } int SetIoPriority(SafeProcessHandle process, ref int priority) => unchecked((int)0xC0000002); bool GetPowerThrottling(SafeProcessHandle process, ref PowerThrottlingState state) => false; bool SetPowerThrottling(SafeProcessHandle process, ref PowerThrottlingState state) => false; IDisposable? AcquirePrivilegeScope(string privilegeName) => null; } public sealed class ProcessMemoryPriorityNativeApi : IProcessMemoryPriorityNativeApi { public static ProcessMemoryPriorityNativeApi Instance { get; } = new(); private ProcessMemoryPriorityNativeApi() { } public bool IsSupported => OperatingSystem.IsWindowsVersionAtLeast(6, 2); public SafeProcessHandle OpenProcess(ProcessAccessFlags access, bool inheritHandle, uint processId) { return ProcessMemoryPriorityNativeMethods.OpenProcess(access, inheritHandle, processId); } public bool GetProcessInformation( SafeProcessHandle process, ProcessInformationClass processInformationClass, ref MemoryPriorityInformation processInformation, uint processInformationSize) { return ProcessMemoryPriorityNativeMethods.GetProcessInformation( process, processInformationClass, ref processInformation, processInformationSize); } public bool SetProcessInformation( SafeProcessHandle process, ProcessInformationClass processInformationClass, ref MemoryPriorityInformation processInformation, uint processInformationSize) { return ProcessMemoryPriorityNativeMethods.SetProcessInformation( process, processInformationClass, ref processInformation, processInformationSize); } public int GetLastWin32Error() { return Marshal.GetLastWin32Error(); } public int QueryIoPriority(SafeProcessHandle process, ref int priority, out uint returnLength) => ProcessMemoryPriorityNativeMethods.NtQueryInformationProcess( process, ProcessMemoryPriorityNativeMethods.ProcessIoPriority, ref priority, sizeof(int), out returnLength); public int SetIoPriority(SafeProcessHandle process, ref int priority) => ProcessMemoryPriorityNativeMethods.NtSetInformationProcess( process, ProcessMemoryPriorityNativeMethods.ProcessIoPriority, ref priority, sizeof(int)); public bool GetPowerThrottling(SafeProcessHandle process, ref PowerThrottlingState state) => ProcessMemoryPriorityNativeMethods.GetProcessPowerThrottling( process, ProcessInformationClass.ProcessPowerThrottling, ref state, (uint)Marshal.SizeOf()); public bool SetPowerThrottling(SafeProcessHandle process, ref PowerThrottlingState state) => ProcessMemoryPriorityNativeMethods.SetProcessPowerThrottling( process, ProcessInformationClass.ProcessPowerThrottling, ref state, (uint)Marshal.SizeOf()); public IDisposable? AcquirePrivilegeScope(string privilegeName) => ProcessMemoryPriorityNativeMethods.AcquirePrivilegeScope(privilegeName); } public enum ProcessInformationClass { ProcessMemoryPriority = 0, ProcessPowerThrottling = 4, } // PROCESS_POWER_THROTTLING_STATE: ControlMask picks the mechanism, StateMask turns it on or off. [StructLayout(LayoutKind.Sequential)] public struct PowerThrottlingState { public const uint CurrentVersion = 1; public const uint ExecutionSpeed = 0x1; public uint Version; public uint ControlMask; public uint StateMask; } [StructLayout(LayoutKind.Sequential)] public struct MemoryPriorityInformation { public uint MemoryPriority; } }