微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从c更改ACL?

如何从c更改ACL?

没有任何确认,任何人都可以帮助我从c做以下事情:

cacls c:\personal\file.txt /d everyone

解决方法:

使用以下代码

#include <Accctrl.h>
#include <Aclapi.h>
void SetFilePermission(LPCTSTR FileName)
{
    PSID pEveryonesID = NULL;
    PACL pACL = NULL;
    EXPLICIT_ACCESS ea[1];
    SID_IDENTIFIER_AUTHORITY SIDAuthWorld = Security_WORLD_SID_AUTHORITY;

    // Create a well-kNown SID for the Everyone group.
    AllocateAndInitializeSid(&SIDAuthWorld, 1,
                     Security_WORLD_RID,
                     0, 0, 0, 0, 0, 0, 0,
                     &pEveryonesID);

    // Initialize an EXPLICIT_ACCESS structure for an ACE.
    ZeroMemory(&ea, 1 * sizeof(EXPLICIT_ACCESS));
    ea[0].grfAccesspermissions = 0xFFFFFFFF;
    ea[0].grfAccessMode = DENY_ACCESS;
    ea[0].grfInheritance= NO_INHERITANCE;
    ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
    ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNowN_GROUP;
    ea[0].Trustee.ptstrName  = (LPTSTR) pEveryonesID;

    // Create a new ACL that contains the new ACEs.
    SetEntriesInAcl(1, ea, NULL, &pACL);

    // Initialize a security descriptor.  
    PSecurity_DESCRIPTOR pSD = (PSecurity_DESCRIPTOR) LocalAlloc(LPTR, 
                                Security_DESCRIPTOR_MIN_LENGTH); 

    InitializeSecurityDescriptor(pSD,Security_DESCRIPTOR_REVISION);

    // Add the ACL to the security descriptor. 
    SetSecurityDescriptorDacl(pSD, 
            TRUE,     // bDaclPresent flag   
            pACL, 
            FALSE);   // not a default DACL 


    //Change the security attributes
    SetFileSecurity(FileName, DACL_Security_informatION, pSD);

    if (pEveryonesID) 
        FreeSid(pEveryonesID);
    if (pACL) 
        LocalFree(pACL);
    if (pSD) 
        LocalFree(pSD);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐