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

C++如何从Windows注册表读取REG_QWORD(64位)类型数据?

读取Windows注册表REG_QWORD类型值的实现方法

以下是不同开发/使用场景下的实现方案,目标字段为HardwareInformation.qwMemorySize:


1. PowerShell 实现(无需编译,直接运行)

  • 目标注册表默认路径为 HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinSAT\HardwareInformation
  • 32位PowerShell运行在64位系统时,需将路径替换为 HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion\WinSAT\HardwareInformation 避免注册表重定向问题
# 定位目标注册表路径
$regPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinSAT\HardwareInformation"
# 读取REG_QWORD类型值,PowerShell会自动识别64位类型
$qwMemorySize = (Get-ItemProperty -Path $regPath).qwMemorySize
# 连续两次除以1024转换为MB单位
$vramMB = $qwMemorySize / 1KB / 1KB
Write-Host "显存容量:$vramMB MB"

2. C/C++ 原生实现

使用Windows原生API RegQueryValueExW 读取,需明确指定接收变量为64位无符号整数:

#include <windows.h>
#include <iostream>

int main() {
    HKEY hKey;
    LPCWSTR regPath = L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\WinSAT\\HardwareInformation";
    LPCWSTR valueName = L"qwMemorySize";
    DWORD valueType = 0;
    ULONGLONG qwMemorySize = 0;
    DWORD dataSize = sizeof(qwMemorySize);

    // 打开注册表键
    if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, regPath, 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
        // 读取值并校验类型是否为REG_QWORD
        if (RegQueryValueExW(hKey, valueName, NULL, &valueType, (LPBYTE)&qwMemorySize, &dataSize) == ERROR_SUCCESS) {
            if (valueType == REG_QWORD) {
                DWORD vramMB = (DWORD)(qwMemorySize / 1024 / 1024);
                std::cout << "显存容量:" << vramMB << " MB" << std::endl;
            }
        }
        RegCloseKey(hKey);
    }
    return 0;
}

3. C# 实现

使用.NET内置的Microsoft.Win32.Registry类读取,REG_QWORD类型会自动映射为ulong类型:

using Microsoft.Win32;

class GetVramSize
{
    static void Main()
    {
        // 64位系统下32位程序读取需额外指定RegistryView.Registry64参数
        using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinSAT\HardwareInformation"))
        {
            if (key != null)
            {
                ulong qwMemorySize = (ulong)key.GetValue("qwMemorySize");
                uint vramMB = (uint)(qwMemorySize / 1024 / 1024);
                System.Console.WriteLine($"显存容量:{vramMB} MB");
            }
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 08:57:04