You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何自动设置Windows应用的默认GPU

强制Windows应用使用独立GPU的自动化方案

完全可以自动化设置应用的GPU偏好,无需手动操作Graphics settings界面。下面提供四种可行的实现方式:

1. 注册表修改(系统级/用户级)

Windows将GPU偏好配置存储在注册表中,直接修改对应项即可生效。

注册表路径

  • 系统级(所有用户,需管理员权限):HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\GPUPreferences
  • 用户级(仅当前用户,无需管理员权限):HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\GPUPreferences

配置规则

每个条目以应用的完整可执行文件路径为名称,值为字符串格式:

  • GpuPreference=2:强制使用高性能(独立)GPU
  • GpuPreference=1:强制使用省电(集成)GPU
  • GpuPreference=0:系统默认

2. PowerShell实现

编写脚本直接操作注册表,需管理员权限(系统级设置):

# 指定你的应用完整路径
$appFullPath = "C:\Projects\YourQtApp\Release\YourApp.exe"
# 系统级注册表路径
$regPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\GPUPreferences"

# 若注册表路径不存在则创建
if (-not (Test-Path $regPath)) {
    New-Item -Path $regPath -Force | Out-Null
}

# 设置高性能GPU偏好
Set-ItemProperty -Path $regPath -Name $appFullPath -Value "GpuPreference=2" -Type String

Write-Host "GPU偏好设置完成"

如果要设置用户级,将$regPath改为"HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\GPUPreferences",无需管理员权限。

3. C++ API实现(集成到Qt应用)

Windows 10 1809及以上版本提供SetProcessGpuPreference API,可在应用启动时直接设置自身的GPU偏好,无需修改系统注册表。

在Qt应用的main.cpp中添加如下代码:

#include <windows.h>
#include <QApplication>
#include <QMainWindow>

// 定义函数指针类型
typedef BOOL(WINAPI* SetProcessGpuPreferenceFunc)(HANDLE hProcess, DWORD GpuPreference);

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // 加载kernel32.dll并获取API地址
    HMODULE hKernel32 = LoadLibrary(L"kernel32.dll");
    if (hKernel32) {
        SetProcessGpuPreferenceFunc pSetProcessGpuPreference =
            reinterpret_cast<SetProcessGpuPreferenceFunc>(GetProcAddress(hKernel32, "SetProcessGpuPreference"));
        
        if (pSetProcessGpuPreference) {
            // 设置高性能GPU(GPU_PREFERENCE_HIGH_PERFORMANCE = 2)
            pSetProcessGpuPreference(GetCurrentProcess(), 2);
        }
        FreeLibrary(hKernel32);
    }

    QMainWindow w;
    w.show();
    return a.exec();
}

注意

  • 该API仅支持Windows 10 1809(版本号10.0.17763)及以上系统,旧系统需回退到注册表方案。
  • 此方法无需管理员权限,仅对当前运行的进程生效,重启应用后仍会自动应用设置。

4. Python实现

方法1:操作注册表

使用winreg模块修改注册表,示例代码:

import winreg

def set_gpu_preference(app_path, user_level=True):
    # 选择注册表根键
    root_key = winreg.HKEY_CURRENT_USER if user_level else winreg.HKEY_LOCAL_MACHINE
    reg_path = r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\GPUPreferences"
    value_data = "GpuPreference=2"

    try:
        # 打开注册表项(64位系统需指定KEY_WOW64_64KEY)
        with winreg.OpenKey(root_key, reg_path, 0, winreg.KEY_WRITE | winreg.KEY_WOW64_64KEY) as key:
            winreg.SetValueEx(key, app_path, 0, winreg.REG_SZ, value_data)
        print(f"已为 {app_path} 设置高性能GPU偏好")
    except Exception as e:
        print(f"设置失败: {e}")
        if not user_level:
            print("系统级设置需要管理员权限,请以管理员身份运行脚本")

if __name__ == "__main__":
    # 替换为你的应用完整路径
    app_path = r"C:\Projects\YourQtApp\Release\YourApp.exe"
    # user_level=True 为当前用户设置,False为所有用户设置(需管理员)
    set_gpu_preference(app_path, user_level=True)

方法2:调用Windows API

使用ctypes调用SetProcessGpuPreference,适合在Python程序中设置自身的GPU偏好:

import ctypes
from ctypes import wintypes

def set_high_performance_gpu():
    kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
    SetProcessGpuPreference = kernel32.SetProcessGpuPreference
    SetProcessGpuPreference.argtypes = [wintypes.HANDLE, wintypes.DWORD]
    SetProcessGpuPreference.restype = wintypes.BOOL

    # 获取当前进程句柄
    current_process = ctypes.windll.kernel32.GetCurrentProcess()
    # 设置高性能GPU(2=高性能)
    success = SetProcessGpuPreference(current_process, 2)
    
    if not success:
        error_code = ctypes.get_last_error()
        print(f"设置GPU偏好失败,错误码: {error_code}")
    else:
        print("已成功设置高性能GPU偏好")

if __name__ == "__main__":
    set_high_performance_gpu()

验证设置是否生效

  1. 打开设置 > 系统 > 显示 > 图形设置,找到你的应用,查看其GPU偏好是否为高性能。
  2. 启动应用后,打开任务管理器,切换到性能标签,查看独立GPU的使用率,确认应用在使用该GPU。

内容的提问来源于stack exchange,提问作者shashashamti2008

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 00:55:06