首页 > 解决方案 > 如何恢复注册表项的有效权限?C++

问题描述

我想检索给定注册表项的有效权限作为参数。我在 Windows 下使用现有的注册表项进行测试。为此,我使用 CreateFile、GetSecurityInfo 和 GetEffectiveRightsFromAclA 方法。我想知道这是否是正确的方法,因为 CreateFile 方法有一个错误,它返回 INVALID_HANDLE_VALUE。此外,对于 GetEffectiveRightsFromAclA 方法,我不明白我必须在 TRUSTEE_A 中输入哪个参数?

LPCWSTR lpwRegistryKey = L"HKEY_CLASSES_ROOT\\.acc\\OpenWithProgids\\WMP11.AssocFile.ADTS";
HANDLE handleKey;
handleKey = CreateFile(lpwRegistryKey, GENERIC_READ, FILE_SHARED_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);

if(handleKey == INVALID_HANDLE_VALUE)
{
   //qDebug()<<"error";
}

//Here is an user SID
PSID pUserSid;
QString sSid("S-4-5-12");
BOOL resultSidConvert = ConvertStringSidToSidA(sSid.toStdString().c_str(), &pUserSid);
//Here success
if(resultSidConvert != 0)
{
    PACL pDacl;
    PSECURITY_DESCRIPTOR pSecurityDescriptor;

    DWORD dwResultSecurity = GetSecurityInfo(handleKey, SE_REGISTRY_KEY, DACL_SECURITY_INFORMATION, nullptr, &pUserSid, &pDacl, nullptr, &pSecurityDescritptor);
    if(dwResultSecurity == ERROR_SUCCESS)
    {
       ACCESS_MASK pAccessMask;
       TRUSTEE_A pTrustee; //How should I initialize pTrustee?

       DWORD dwEffectiveRights = (GetEffectiveRightsFromAclA(pDacl, &pTrustee, &pAccessMask);
       if(dwEffectiveRights == ERROR_SUCCESS)
       {
           if((pAccessMask & DELETE) == DELETE)
           {
               qDebug()<<"Delete";
           }
           if((pAccessMask & GENERIC_READ) == GENERIC_READ)
           {
              qDebug()<<"READ";
           }
         //etc ..........
       }
    }
}

标签: c++windowsqtcreatefile

解决方案


注册表项不是文件!

不要使用 CreateFile。请改用 RegOpenKeyEx。首先,您必须以管理员身份运行应用程序。

然后,您可以成功打开密钥。之后,看看RegGetKeySecurity功能。


推荐阅读