public SecurityAttributes(String accountName) { const int ACL_REVISION = 2; //const int ACL_REVISION_DS = 4; unchecked { const int GENERIC_READ = (int)0x80000000; } //const int GENERIC_WRITE = 0x40000000; //const int GENERIC_EXECUTE = 0x20000000; const int GENERIC_ALL = 0x10000000; IntPtr token = GetToken(); //byte[] sd1 = GetSidByAccountName("ASPNET"); IntPtr sd1 = GetSidByAccountName("Administrator"); IntPtr sd2 = GetSelfSid(token); //Get SecurityAttributes size nLength = Marshal.SizeOf(typeof(SecurityAttributes)); //Inherit handle bInheritHandle = 1; IntPtr pacl = Marshal.AllocCoTaskMem(1024); bool ret = InitializeAcl(ref pacl, 1024, ACL_REVISION); ret = AddAccessAllowedAce(ref pacl, ACL_REVISION, GENERIC_ALL, sd1); ret = AddAccessAllowedAce(ref pacl, ACL_REVISION, GENERIC_ALL, sd2); //lpSecurityDescriptor = Marshal.AllocCoTaskMem(dest.Length); //Marshal.Copy(dest, 0, lpSecurityDescriptor, dest.Length); //Struct to Ptr //bool ret = InitializeSecurityDescriptor(lpSecurityDescriptor, 1); //ret = SetSecurityDescriptorDacl(lpSecurityDescriptor, true, IntPtr.Zero, false); SECURITY_DESCRIPTOR sd = new SECURITY_DESCRIPTOR(); //Alloc memory for security descriptor lpSecurityDescriptor = Marshal.AllocCoTaskMem(Marshal.SizeOf(sd)); //Struct to Ptr Marshal.StructureToPtr(sd, lpSecurityDescriptor, false); ret = InitializeSecurityDescriptor(lpSecurityDescriptor, 1); ret = SetSecurityDescriptorDacl(lpSecurityDescriptor, true, pacl, true); }
public SecurityAttributes() { //Get SecurityAttributes size nLength = Marshal.SizeOf(typeof(SecurityAttributes)); //Inherit handle bInheritHandle = 1; //Create a NULL DACL SECURITY_DESCRIPTOR sd = new SECURITY_DESCRIPTOR(); //Alloc memory for security descriptor lpSecurityDescriptor = Marshal.AllocCoTaskMem(Marshal.SizeOf(sd)); //Struct to Ptr Marshal.StructureToPtr(sd, lpSecurityDescriptor, false); InitializeSecurityDescriptor(lpSecurityDescriptor, 1); SetSecurityDescriptorDacl(lpSecurityDescriptor, true, IntPtr.Zero, false); }