开发者

createnamedpipe not working in win 7 .how to change security attributes using c#

createnamedpipe not working in win 7 .how to change security attributes using c#

[StructLayout(LayoutKind.Sequential)] private struct SECURITY_ATTRIBUTES { public int nLength; public IntPtr lpSecurityDescriptor; public int bInheritHandle; }

public bool CreatePipe() { // Make a named pipe in message mode

        IntPtr securityDescriptorPtr = IntPtr.Zero;
        int securityDescriptorSize = 0;
        bool result = ConvertStringSecurityDescriptorToSecurityDescriptor(
            LOW_INTEGRITY_SSL_SACL, SDDL_REVISION_1, out securityDescriptorPtr, out securityDescriptorSize);
        if (!result)
            throw new Win32Exception(Marshal.GetLastWin32Error());

        SECURITY_ATTRIBUTES securityAttributes = new SECURITY_ATTRIBUTES();
        securityAttributes.nLength = Marshal.SizeOf(securityAttributes);
        securityAttributes.bInheritHandle = 1;
        securityAttributes.lpSecurityDescriptor = securityDescriptorPtr;

        _handle = CreateNamedPipe(_pipeName,
            PIPE_ACCESS_DUPLEX,
            PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
            PIPE_UNLIMITED_INSTANCES,
            PIPE_SERVER_BUFFER_SIZE,
            PIPE_SERVER_BUFFER_SIZE,
            NMPWAIT_WAIT_FOREVER,
            securityAttributes);
        // Make sure we got a good one
        if (_handle.IsInvalid)
        {
            Debug.WriteLine("Could not create the pipe (开发者_如何学JAVA" + _pipeName + ") - os returned " +
                Marshal.GetLastWin32Error());

            return false;
        }
        return true;
    }

due to security attributes it gives exception.why is so???? i got error in bool result = ConvertStringSecurityDescriptorToSecurityDescriptor( LOW_INTEGRITY_SSL_SACL, SDDL_REVISION_1, out securityDescriptorPtr, out securityDescriptorSize); line which is The specified datatype is invalid(win32 exception)


If you want to stick with native node (and the access issues may be the same in managed code), there is some guidance for Vista here (a long thread but should have everything you need). Also managed code methodology. Not sure if this applies identically to Windows 7 but it might get you started.

In summary:

It seems that there is no way to modify the permissions of a named pipe after it has been created. I tried and it always either simply doesn't work or returns ACCESS_DENIED. So, I had to specify both the DACL and SACL up front when creating the pipe.

The DACL code was pasted above in an earlier thread and the SACL can be created as follows:

  1. Create a new using InitializeAcl()
  2. Initialize a new SID specifying 'SECURITY_MANDATORY_LOW_RID'
  3. Call AddMandatoryAce() to add the SID to the ACL
  4. Call SetSecurityDescriptorSacl() to add the ACL to the security descriptor
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜