查询UAC值时,SHGetValue返回2

时间:2019-04-17 16:17:59

标签: c++ windows winapi mingw uac

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

我使用了Windows SHGetValue功能,但状态始终返回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

1 个答案:

答案 0 :(得分:4)

您要读取什么特定键?我不是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;
}

还请记住,读取键值的值存储在value参数中,而不存储在函数的返回值中。

编辑:操作员评论的答案“我要使用FilterAdministratorToken,但默认情况下处于禁用状态,如何将其恢复启用。?” 。请记住,您的进程需要具有管理员权限才能执行这些操作。

#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;
}