ファイルにSECURITY_IDENTIFIER構造のオブジェクトがあります。この構造から所有者SIDを取得する必要があります。これを行うために、GetSecurityDescriptorOwner
WinAPI関数を呼び出して作成しますSystem.Security.Principal.SecurityIdentifier
(IntPtrを引数としてオーバーロードします)
問題は、ファイル内のこの構造が時々壊れているため、GetSecurityDescriptorOwnerから取得したポインターが無効であるということです。これはIntPtr.Zeroではなく、無効であるため、タイプのオブジェクトを作成すると、SecurityIdentifier
が取得されますAccessViolationException
。これは、単純なtry-catchでは.NET4ではキャッチできません。
私はそのような例外を捕らえることができる属性を知っているので、当分の間それを使用しましたが、私はこの解決策が好きではありません。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, 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, 0, 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コンストラクターからCorruptedStateExceptionsをスローすることがあります。解決策はありますか?