#include <bitset>
#include <windows.h>
BOOL IsDirectoryWrite(TCHAR* filePath)
{
SECURITY_INFORMATION securityInfo = DACL_SECURITY_INFORMATION;
SECURITY_DESCRIPTOR* pSecurityDes;
DWORD needBuffer;
//获取需要的内存大小
GetFileSecurity(filePath, securityInfo, NULL, 0, &needBuffer);
pSecurityDes = new SECURITY_DESCRIPTOR[needBuffer];
GetFileSecurity(filePath, securityInfo, pSecurityDes, needBuffer, &needBuffer);
//ACL变量,安全信息链表指针
PACL pACL = NULL;
BOOL DaclPresent;
BOOL DaclDefault;
GetSecurityDescriptorDacl(pSecurityDes, &DaclPresent, &pACL, &DaclDefault);
//获取ACl所有信息
ACL_SIZE_INFORMATION AclInfo;
AclInfo.AceCount = 0;
AclInfo.AclBytesFree = 0;
AclInfo.AclBytesInUse = sizeof(ACL);
GetAclInformation(pACL, &AclInfo, sizeof(ACL_SIZE_INFORMATION), AclSizeInformation);
//每个ACE信息
LPVOID pTempAce = NULL;
BYTE nAceType;
if(DaclPresent && AclInfo.AceCount)
{
for (int i = 0; i < AclInfo.AceCount; i++)
{
GetAce(pACL, i, &pTempAce);
nAceType = ((ACCESS_ALLOWED_ACE*)pTempAce)->Header.AceType;
//表示当前安全权限里有拒绝的
if (nAceType == ACCESS_DENIED_ACE_TYPE)
{
bitset<32> bitAccessMask (((ACCESS_ALLOWED_ACE*)pTempAce)->Mask);
//表示当前文件夹不能写,也就是创建文件什么的
//"FILE_WRITE_DATA" "FILE_APPEND_DATA""FILE_WRITE_EA""FILE_WRITE_ATTRIBUTES"
if (bitAccessMask[1] == TRUE && bitAccessMask[2] == TRUE &&
bitAccessMask[4] == TRUE && bitAccessMask[8] == TRUE)
{
if (pSecurityDes)
{
delete [] pSecurityDes;
}
return FALSE;
}
}
}
if (pSecurityDes)
{
delete [] pSecurityDes;
}
return TRUE;
}
}