Read “OS install date” value from registry with RegQueryValueExA() api failed

狂风中的少年 提交于 2020-02-29 09:21:19

问题


I wanna read Windows Install Date value from registry using Windows API as bellow :

HKEY hKey = { 0 };
LONG lResult = ERROR_SUCCESS;

// Open a registry key
lResult = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
                        "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
                        0, KEY_READ, &hKey);

// If registry key opened
if (lResult == ERROR_SUCCESS)
{
    // Get OS install date/time
    DWORD dwInstallDate = 0;
    dwBufferSize = sizeof(DWORD);
    if (RegQueryValueExA(hKey, "InstallDate", 0, NULL, (LPBYTE)&dwInstallDate, &dwBufferSize) == ERROR_SUCCESS)
    {
        printf("OS Install Date is : %lu", dwInstallDate);
    }
    else
    {
        printf("The specific key not found!");
    }

    // Finally we should close the key when we finished with it
    RegCloseKey(hKey);
}

But the output is always 0, while "InstallDate" value is 1520291827:

OS Installe Date is : 0

Also, I'm used of "%ld" and "d" formats, but it has the same output...
The "InstallDate" value in registry, contained a REG_DWORD type.
By the way, when i create a new REG_DWORD value, my code cannot read it :

For example , I create a value in bellow reg-address as "test" with 123 data :

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion

But dwInstallDate will be 0 instead of 123 :

RegQueryValueExA(hKey, "test", 0,
                 NULL, (LPBYTE)&dwInstallDate,
                 &dwBufferSize);

Any Suggestion? I'm confused about this action...


回答1:


According Microsoft :

  • KEY_WOW64_32KEY (0x0200) :

    Indicates that an application on 64-bit Windows should operate on the 32-bit registry view. This flag is ignored by 32-bit Windows. For more information, see Accessing an Alternate Registry View. This flag must be combined using the OR operator with the other flags in this table that either query or access registry values. Windows 2000: This flag is not supported.

  • KEY_WOW64_64KEY (0x0100) :

    Indicates that an application on 64-bit Windows should operate on the 64-bit registry view. This flag is ignored by 32-bit Windows. For more information, see Accessing an Alternate Registry View. This flag must be combined using the OR operator with the other flags in this table that either query or access registry values. Windows 2000: This flag is not supported.

I used of KEY_WOW64_64KEY | KEY_READ either way x86 and x64 :

// Open a registry key
lResult = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
                        "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
                        0, KEY_WOW64_64KEY | KEY_READ, &hKey);

It does work.



来源:https://stackoverflow.com/questions/52740356/read-os-install-date-value-from-registry-with-regqueryvalueexa-api-failed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!