SHGetValue returns 2 查询UAC值时

SHGetValue returns 2 when querying UAC value

我想检查 windows 的 UAC 配置设置。从而在注册表项中恢复 UAC 的参数。

我使用了windowsSHGetValue功能,但是状态总是returns我2没有任何信息。

我使用 C++11、MinGW 和 windows。

我的代码是:

DWORD dwStatus;
  LPCSTR pszSubKey= "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System";
  LPCSTR pszValue="";
  DWORD  pdwType=REG_SZ;
  PVOID  pvData[63];
  DWORD  pcbData;
  pcbData=sizeof(pvData);

  dwStatus=SHGetValueA(HKEY_LOCAL_MACHINE, pszSubKey, pszValue, &pdwType, pvData, &pcbData);

  //Here dwStatus = 2
  // pvData = 0x11fd0b2
  // pcbData = 504

您要读取的具体密钥是什么?我不是 win32 专家 API 所以我不知道是否有办法一次读取一组键 (编辑: 我认为有RegEnumValue/RegEnumValueA 的功能)。这是一个示例,显示了如何从该路径读取 "EnableLUA" 或任何其他键:

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


bool ReadUACRegistryKey(char* key, DWORD &keyValue)
{
    LPCTSTR pszSubKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System";
    LPCTSTR pszValue = key;

    //  don't care
    DWORD dwType = 0;
    DWORD dwValue = 0;

    //  
    DWORD dwValueSize = sizeof(dwValue);

    int retval = SHGetValue( HKEY_LOCAL_MACHINE, pszSubKey, key, &dwType, &dwValue, &dwValueSize);
    if ( retval != ERROR_SUCCESS)
    {
        return false;
    }

    keyValue = dwValue;
    return true;
}

int main()
{
    DWORD keyValue;
    char* key = "EnableLUA";  //  "EnableSecureUIAPaths" etc..;
    if (ReadUACRegistryKey(key, keyValue))
    {
        std::cout << "Successfully readed key " << key << ", value:" << keyValue << std::endl;
    }
    else
    {
        std::cout << "Unable to read value of key " << key << std::endl;
    }


    return 0;
}

另请记住,读取键值的值存储在值参数中,而不是函数的 return 值中。

编辑:操作者评论的回答"I want use FilterAdministratorToken but is disable by default how give it back enable .?"。请记住,您的进程需要具有管理员权限才能执行这些操作。

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


bool ReadUACRegistryKey(char* key, DWORD &keyValue)
{
    LPCTSTR pszSubKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System";
    LPCTSTR pszValue = key;

    //  don't care
    DWORD dwType = 0;
    DWORD dwValue = 0;

    //  
    DWORD dwValueSize = sizeof(dwValue);

    int retval = SHGetValue( HKEY_LOCAL_MACHINE, pszSubKey, key, &dwType, &dwValue, &dwValueSize);
    if ( retval != ERROR_SUCCESS)
    {
        return false;
    }

    keyValue = dwValue;
    return true;
}

bool EnableFilterAdministratorToken()
{
    //  first check if its already enabled or not
    DWORD val;
    if (ReadUACRegistryKey("FilterAdministratorToken", val))
    {
        if (val == 1)
        {
            std::cout << "FilterAdministratorToken is already enabled" << std::endl;
            return true;
        }
    }
    else
    {
        std::cout << "Unable to read key" << std::endl;
        return false;
    }


    //  its not enabled, we need to enable it manually
    //  obtain a handle to reg key
    HKEY hKey;
    int retval = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", 0, KEY_SET_VALUE, &hKey);
    if (retval != ERROR_SUCCESS)
    {
        //  we are unable to obtain a handle to reg key
        std::cout << "Unable to obtain handle to reg key" << std::endl;
        return false;
    }


    DWORD enabledValue = 1;
    retval = RegSetValueExA(hKey, "FilterAdministratorToken", 0, REG_DWORD, (BYTE*) &enabledValue, sizeof(DWORD));
    if (retval != ERROR_SUCCESS)
    {
        //  some error occured
        std::cout << "Some error occured during setting the key value" << std::endl;
        RegCloseKey(hKey);
        return false;
    }

    std::cout << "Successfully changed key value" << std::endl;
    RegCloseKey(hKey);
    return true;
}

int main()
{
    if (EnableFilterAdministratorToken())
    {
        std::cout << "OK" << std::endl;
    }
    else
    {
        std::cout << "FAIL" << std::endl;
    }


    return 0;
}