由于兼容性原因,常常需要建立一个没有权限限制的文件夹
首先,需要管理员权限,不是的话使用ShellExecute或manifest触发UAC提权
设定新对象的NTFS权限,需要按以下步骤操作:
这种方法用于给新文件或新目录指定权限
如果需要修改现有文件或目录的权限,使用SetFileSecurity或SetNamedSecurityInfo
// ConsoleApp1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <aclapi.h>
int main()
{
SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
PSID pEveryoneSID = NULL;
if (!AllocateAndInitializeSid(&SIDAuthWorld, 1,
SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pEveryoneSID)) // 初始化Everyone的SID
{
printf("err\n");
goto cleanup;
}
EXPLICIT_ACCESS ea[1];
memset(&ea, 0, sizeof ea);
ea[0].grfAccessPermissions = GENERIC_ALL; // 所有权限
ea[0].grfAccessMode = SET_ACCESS; // 设置权限
ea[0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT; // 子目录和文件继承权限
ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID; // 使用SID
ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; // 特殊安全对象
ea[0].Trustee.ptstrName = (LPTSTR)pEveryoneSID; // Everyone的SID
PACL pACL = NULL;
DWORD dwRes = SetEntriesInAcl(1, ea, NULL, &pACL);
if (ERROR_SUCCESS != dwRes)
{
printf("err\n");
goto cleanup;
}
PSECURITY_DESCRIPTOR pSD = NULL;
pSD = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
if (NULL == pSD)
{
printf("err\n");
goto cleanup;
}
if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
{
printf("err\n");
goto cleanup;
}
if (!SetSecurityDescriptorDacl(pSD, TRUE, pACL, FALSE))
{
printf("err\n");
goto cleanup;
}
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof sa;
sa.lpSecurityDescriptor = pSD;
sa.bInheritHandle = FALSE;
if (!CreateDirectory(L"任何人可访问", &sa))
{
printf("createdirectory failed\n");
}
cleanup:
if (pEveryoneSID) FreeSid(pEveryoneSID);
if (pACL) LocalFree(pACL);
if (pSD) LocalFree(pSD);
return 0;
}
</aclapi.h></stdio.h></windows.h>