在文件中有类型Security_IDENTIFIER结构的对象.我需要从这个结构获得所有者SID.为了做到这一点,我调用GetSecurityDescriptorOwner WinAPI函数并创建System.Security.Principal.SecurityIdentifier(它有过载以IntPtr作为参数)
问题是文件中的结构有时会被破坏,因此我从GetSecurityDescriptorOwner获取的指针无效.它不是IntPtr.Zero,它是无效的,所以当我创建SecurityIdentifier类型的对象时,我得到了AccessViolationException,这是不可能用简单的try-catch来捕获.NET 4.
我知道允许捕获此类异常的属性,所以我暂时使用它,但我不喜欢这个解决方案.不建议捕获Corrupted State Exceptions(CSE),但我没有看到任何其他解决方案.这个WinAPI函数返回无效指针,我看不到检查它的有效性.有任何想法吗?
更新
WinAPI的
BOOL WINAPI GetSecurityDescriptorOwner( _In_ PSecurity_DESCRIPTOR pSecurityDescriptor,_Out_ PSID *pOwner,_Out_ LPBOOL lpbOwnerDefaulted );
外部定义
[DllImport("Advapi32.dll")] static extern bool GetSecurityDescriptorOwner( IntPtr pSecurityDescriptor,out IntPtr owner,out bool defaulted);
更新
private static SecurityIdentifier GetSecurityIdentifier() { // Allocate managed buffer for invalid security descriptor structure (20 bytes) int[] b = new int[5] {1,1,1}; // Allocate unmanaged memory for security descriptor IntPtr descriptorPtr = Marshal.AllocHGlobal(b.Length); // copy invalid security descriptor structure to the unmanaged buffer Marshal.copy(b,descriptorPtr,b.Length); IntPtr ownerSid; bool defaulted; if (GetSecurityDescriptorGroup(descriptorPtr,out ownerSid,out defaulted)) { // GetSecurityDescriptorGroup returns true,but `ownerSid` is `1` // Marshal.GetLastWin32Error returns 0 here return new SecurityIdentifier(ownerSid); } return null; }
此代码抛出有时会从SecurityIdentifier构造函数中抛出Corrupted State Exceptions.有解决方案吗
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。