CreateDirectory()后赋予全部权限
时间: 2024-01-19 21:03:11 浏览: 114
可以使用Windows API函数SetSecurityInfo()来将一个目录的安全描述符中的所有者、组和DACL(访问控制列表)设为指定的值,从而实现授权。代码示例如下:
```c++
#include <windows.h>
#include <aclapi.h>
int main()
{
LPCWSTR dirPath = L"C:\\test";
PSECURITY_DESCRIPTOR pSD = NULL;
// 获取默认的安全描述符
if (GetNamedSecurityInfoW(dirPath, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, NULL, NULL, &pSD) != ERROR_SUCCESS)
{
return -1;
}
// 初始化一个ACCESS_ALLOWED_ACE结构体,授予所有权限
EXPLICIT_ACCESSW ea;
ZeroMemory(&ea, sizeof(EXPLICIT_ACCESSW));
ea.grfAccessMode = SET_ACCESS;
ea.grfAccessPermissions = GENERIC_ALL;
ea.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
ea.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
ea.Trustee.TrusteeType = TRUSTEE_IS_USER;
ea.Trustee.ptstrName = L"Everyone"; // 给所有用户授权
// 将ACE添加到DACL中
PACL pNewDACL = NULL;
if (SetEntriesInAclW(1, &ea, pSD->Dacl, &pNewDACL) != ERROR_SUCCESS)
{
LocalFree(pSD);
return -1;
}
// 将新的DACL设置到安全描述符中
if (SetNamedSecurityInfoW(dirPath, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, pNewDACL, NULL) != ERROR_SUCCESS)
{
LocalFree(pNewDACL);
LocalFree(pSD);
return -1;
}
// 释放内存
LocalFree(pNewDACL);
LocalFree(pSD);
return 0;
}
```
此代码将C:\test目录的所有者、组和DACL设为“Everyone”用户,授予所有权限。
阅读全文