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