📄 securitystruct.cs
字号:
namespace Imps.Client.Utils
{
using System;
using System.Runtime.InteropServices;
public class SecurityStruct : IDisposable
{
private IntPtr lpSecurityDescriptor;
private IntPtr pSa;
private const int SECURITY_DESCRIPTOR_REVISION = 1;
public IntPtr AnonymousSA()
{
return this.AnonymousSA(false);
}
public IntPtr AnonymousSA(bool bInheritHandle)
{
this.Close();
SECURITY_ATTRIBUTES structure = new SECURITY_ATTRIBUTES();
SECURITY_DESCRIPTOR security_descriptor = new SECURITY_DESCRIPTOR();
this.lpSecurityDescriptor = Marshal.AllocCoTaskMem(Marshal.SizeOf(security_descriptor));
Marshal.StructureToPtr(security_descriptor, this.lpSecurityDescriptor, false);
if (!InitializeSecurityDescriptor(this.lpSecurityDescriptor, 1))
{
throw new ApplicationException("InitializeSecurityDescriptor invoked fail");
}
if (!SetSecurityDescriptorDacl(this.lpSecurityDescriptor, 1, IntPtr.Zero, 0))
{
throw new ApplicationException("SetSecurityDescriptorDacl invoked fail");
}
structure.bInheritHandle = bInheritHandle ? 1 : 0;
structure.nLength = (uint) Marshal.SizeOf(structure);
structure.lpSecurityDescriptor = this.lpSecurityDescriptor;
this.pSa = Marshal.AllocCoTaskMem(Marshal.SizeOf(structure));
Marshal.StructureToPtr(structure, this.pSa, false);
return this.pSa;
}
public void Close()
{
if (this.lpSecurityDescriptor != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(this.lpSecurityDescriptor);
this.lpSecurityDescriptor = IntPtr.Zero;
}
if (this.pSa != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(this.pSa);
this.pSa = IntPtr.Zero;
}
}
public void Dispose()
{
this.Close();
}
[DllImport("advapi32.dll", SetLastError=true)]
private static extern bool InitializeSecurityDescriptor(IntPtr pSecurityDescriptor, uint dwRevision);
[DllImport("advapi32.dll", SetLastError=true)]
private static extern bool SetSecurityDescriptorDacl(IntPtr pSecurityDescriptor, int bDaclPresent, IntPtr pDacl, int bDaclDefaulted);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -