setProcessInformation 函数 (processthreadsapi.h)
设置指定进程的信息。
语法
BOOL SetProcessInformation(
[in] HANDLE hProcess,
[in] PROCESS_INFORMATION_CLASS ProcessInformationClass,
LPVOID ProcessInformation,
[in] DWORD ProcessInformationSize
);
参数
[in] hProcess
进程的句柄。 此句柄必须具有 PROCESS_SET_INFORMATION 访问权限。 有关详细信息,请参阅 进程安全性和访问权限。
[in] ProcessInformationClass
PROCESS_INFORMATION_CLASS枚举的成员,指定要设置的信息类型。
ProcessInformation
指向对象的指针,该对象包含 ProcessInformationClass 参数指定的信息类型。
如果 ProcessInformationClass 参数为 ProcessMemoryPriority,则此参数必须指向 MEMORY_PRIORITY_INFORMATION结构MEMORY_PRIORITY_INFORMATION 结构。
如果 ProcessInformationClass 参数为 ProcessPowerThrottling,则此参数必须指向 PROCESS_POWER_THROTTLING_STATE结构。
如果 ProcessInformationClass 参数为 ProcessLeapSecondInfo,则此参数必须指向 PROCESS_LEAP_SECOND_INFO结构。
如果 ProcessInformationClass 参数为 ProcessOverrideSubsequentPrefetchParameter,则此参数必须指向 OVERRIDE_PREFETCH_PARAMETER结构。
[in] ProcessInformationSize
ProcessInformation 参数指定的结构的大小(以字节为单位)。
如果 ProcessInformationClass 参数为 ProcessMemoryPriority,则此参数必须为 sizeof(MEMORY_PRIORITY_INFORMATION)
。
如果 ProcessInformationClass 参数为 ProcessPowerThrottling,则此参数必须为 sizeof(PROCESS_POWER_THROTTLING_STATE)
。
如果 ProcessInformationClass 参数为 ProcessLeapSecondInfo,则此参数必须为 sizeof(PROCESS_LEAP_SECOND_INFO)
。
返回值
如果该函数成功,则返回值为非零值。
如果函数失败,则返回值为零。 要获得更多的错误信息,请调用 GetLastError。
注解
为了帮助提高系统性能,应用程序应将 SetProcessInformation 函数与 ProcessMemoryPriority 结合使用,以降低执行后台操作或访问预期不会很快再次访问的文件和数据的线程的默认内存优先级。 例如,文件索引应用程序可能会为执行索引任务的进程设置较低的默认优先级。
内存优先级 有助于确定页面在剪裁之前在进程 的工作集中 保留多长时间。 进程的内存优先级确定由进程线程添加到进程工作集的物理页的默认优先级。 当内存管理器剪裁工作集时,它会在优先级较高的页面之前剪裁优先级较低的页面。 这可以提高整体系统性能,因为优先级较高的页面不太可能从工作集中进行剪裁,并在再次访问时触发页面错误。
ProcessPowerThrottling 在进程上启用限制策略,这些策略可用于在不需要最佳性能的情况下平衡性能和电源效率。
当进程选择启用 PROCESS_POWER_THROTTLING_EXECUTION_SPEED
时,进程将归类为 EcoQoS。 系统将尝试通过降低 CPU 频率或使用更节能的内核等策略来提高电源效率。 当工作不影响前台用户体验时,应使用 EcoQoS,这样可以延长电池使用时间,减少热量和风扇噪音。 不应将 EcoQoS 用于性能关键或前台用户体验。 (在Windows 11之前,EcoQoS 级别不存在,并且该过程被标记为 LowQoS) 。 如果应用程序未显式启用 PROCESS_POWER_THROTTLING_EXECUTION_SPEED
,系统将使用其自己的启发法自动推断服务质量级别。 有关详细信息,请参阅服务质量。
当进程选择启用 PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION
时,将忽略进程发出的任何当前计时器解析请求。 属于进程的计时器不再保证在计时器分辨率较高的情况下过期,这可以提高电源效率。 显式禁用 PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION
后,系统会记住并遵循进程之前的任何计时器解析请求。 默认情况下,在 Windows 11如果拥有窗口的进程完全被遮挡、最小化或对最终用户不可见,并且无法听到声音,Windows 可能会自动忽略计时器解析请求,因此不能保证比默认系统分辨率更高的分辨率。
示例
以下示例演示如何使用 ProcessMemoryPriority 调用 SetProcessInformation,以将低内存优先级设置为调用进程的默认值。
DWORD ErrorCode;
BOOL Success;
MEMORY_PRIORITY_INFORMATION MemPrio;
//
// Set low memory priority on the current process.
//
ZeroMemory(&MemPrio, sizeof(MemPrio));
MemPrio.MemoryPriority = MEMORY_PRIORITY_LOW;
Success = SetProcessInformation(GetCurrentProcess(),
ProcessMemoryPriority,
&MemPrio,
sizeof(MemPrio));
if (!Success) {
ErrorCode = GetLastError();
fprintf(stderr, "Set process memory priority failed: %d\n", ErrorCode);
goto cleanup;
}
以下示例演示如何使用 ProcessPowerThrottling 调用 SetProcessInformation 来控制进程的服务质量。
PROCESS_POWER_THROTTLING_STATE PowerThrottling;
RtlZeroMemory(&PowerThrottling, sizeof(PowerThrottling));
PowerThrottling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION;
//
// EcoQoS
// Turn EXECUTION_SPEED throttling on.
// ControlMask selects the mechanism and StateMask declares which mechanism should be on or off.
//
PowerThrottling.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
PowerThrottling.StateMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
SetProcessInformation(GetCurrentProcess(),
ProcessPowerThrottling,
&PowerThrottling,
sizeof(PowerThrottling));
//
// HighQoS
// Turn EXECUTION_SPEED throttling off.
// ControlMask selects the mechanism and StateMask is set to zero as mechanisms should be turned off.
//
PowerThrottling.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
PowerThrottling.StateMask = 0;
SetProcessInformation(GetCurrentProcess(),
ProcessPowerThrottling,
&PowerThrottling,
sizeof(PowerThrottling));
以下示例演示如何使用 ProcessPowerThrottling 调用 SetProcessInformation 来控制进程的计时器解析。
PROCESS_POWER_THROTTLING_STATE PowerThrottling;
RtlZeroMemory(&PowerThrottling, sizeof(PowerThrottling));
PowerThrottling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION;
//
// Ignore Timer Resolution Requests.
// Turn IGNORE_TIMER_RESOLUTION throttling on.
// ControlMask selects the mechanism and StateMask declares which mechanism should be on or off.
//
PowerThrottling.ControlMask = PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION;
PowerThrottling.StateMask = PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION;
SetProcessInformation(GetCurrentProcess(),
ProcessPowerThrottling,
&PowerThrottling,
sizeof(PowerThrottling));
//
// Always honor Timer Resolution Requests.
// Turn IGNORE_TIMER_RESOLUTION throttling off.
// ControlMask selects the mechanism and StateMask is set to zero as mechanisms should be turned off.
//
PowerThrottling.ControlMask = PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION;
PowerThrottling.StateMask = 0;
SetProcessInformation(GetCurrentProcess(),
ProcessPowerThrottling,
&PowerThrottling,
sizeof(PowerThrottling));
以下示例演示如何使用 ProcessPowerThrottling 调用 SetProcessInformation 以重置为默认的系统托管行为。
PROCESS_POWER_THROTTLING_STATE PowerThrottling;
RtlZeroMemory(&PowerThrottling, sizeof(PowerThrottling));
PowerThrottling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION;
//
// Let system manage all power throttling. ControlMask is set to 0 as we don't want
// to control any mechanisms.
//
PowerThrottling.ControlMask = 0;
PowerThrottling.StateMask = 0;
SetProcessInformation(GetCurrentProcess(),
ProcessPowerThrottling,
&PowerThrottling,
sizeof(PowerThrottling));
要求
要求 | 值 |
---|---|
最低受支持的客户端 | Windows 8 [桌面应用 |UWP 应用] |
最低受支持的服务器 | Windows Server 2012 [桌面应用 |UWP 应用] |
目标平台 | Windows |
标头 | processthreadsapi.h (包括 Windows.h) |
Library | Kernel32.lib |
DLL | Kernel32.dll |
另请参阅
GetProcessInformation 函数、SetThreadInformation 函数、MEMORY_PRIORITY_INFORMATION 结构、SetProcessInformation 函数、PROCESS_INFORMATION_CLASS 枚举OVERRIDE_PREFETCH_PARAMETER 结构
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈