当前位置:编程学习 > VB >>

vb怎样修改文件夹acl权限

我想用api修改目录权限可以实现吗
我不想用cacls或ADsSecurity ,谢谢 请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。
shell cacls.exe 可以这样,但是若遇到那种有提示的需要按y。
而且cacls最终也是调用的api吧。所以我怎么喜欢调用cacls Handle可以给文件或者目录的句柄,用CreateFile得到

Sub SetAccess(ByVal Handle As Long, UserName As String, ByVal AccessMask As Long, ByVal AllowAccess As Boolean)
If Handle = 0 Then Exit Sub
Dim SecurityDescriptor As Long, Acl As Long, Status As Long, Sid As Long, SidLength As Long, DomainName As Long, DomainNameLength As Long, UserType As Integer
DomainName = Allocate(512)
DomainNameLength = 512
SidLength = 0
LookupAccountNameW 0, StrPtr(UserName), ByVal 0, SidLength, DomainName, DomainNameLength, UserType
Sid = Allocate(SidLength)
LookupAccountNameW 0, StrPtr(UserName), ByVal Sid, SidLength, DomainName, DomainNameLength, UserType
Deallocate DomainName
If RtlValidSid(ByVal Sid) Then
SecurityDescriptor = Allocate(4096)
If SecurityDescriptor Then Status = RtlCreateSecurityDescriptor(ByVal SecurityDescriptor, 1)
If Status = 0 Then
Acl = Allocate(4096)
Status = RtlCreateAcl(ByVal Acl, 4096, 2)
If Status = 0 Then
If AllowAccess Then RtlAddAccessAllowedAce ByVal Acl, 2, AccessMask, ByVal Sid Else RtlAddAccessDeniedAce ByVal Acl, 2, AccessMask, ByVal Sid
RtlSetDaclSecurityDescriptor ByVal SecurityDescriptor, True, ByVal Acl, True
NtSetSecurityObject Handle, 4, ByVal SecurityDescriptor
End If
Deallocate Acl
End If
Deallocate SecurityDescriptor
End If
Deallocate Sid
End Sub
这个只能设置1个dacl,不过懂了后你可以自己改 是1个ace
补充:VB ,  API
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,