如何从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);
}

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...