2010-10-30 25 views
1

CreateNamedPipe時在Win 7。如何不工作屬性改變安全屬性使用C#CreateNamedPipe時在Win 7。如何不工作來改變安全使用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 (" + _pipeName + ") - os returned " + 
       Marshal.GetLastWin32Error()); 

      return false; 
     } 
     return true; 
    } 

由於安全屬性它給出例外。爲什麼是這樣? 我得到了 錯誤bool result = ConvertStringSecurityDescriptorToSecurityDescriptor( LOW_INTEGRITY_SSL_SACL,SDDL_REVISION_1,out securityDescriptorPtr,out securityDescriptorSize); 線,是指定的數據類型是無效的(Win32除外)

+2

你應該提供更多的細節,例如拋出什麼異常,爲什麼不使用System.IO.Pipes類的NamedPipeServerStream和NamedPipeClientStream,它們是否工作? – 2010-10-30 11:50:11

回答

1

如果你想與本地節點堅持(和接入問題,可在託管代碼相同),對Vista的here(長螺紋一些指導但應該有你需要的一切)。還有託管代碼方法。不知道這是否適用於Windows 7,但它可能會讓你開始。

總結:

看來,有沒有辦法 修改命名管道 的權限,它已被創建之後。我試過並且 它總是或者根本不工作 或者返回ACCESS_DENIED。所以,我不得不 創建管道時指定DACL和SACL前面 前面。

的DACL代碼上面在 較早螺紋粘貼和SACL可以創建 如下:

  1. 創建一個新的使用InitializeAcl()
  2. 初始化一個新的SID指定「SECURITY_MANDATORY_LOW_RID」
  3. 呼叫AddMandatoryAce()的SID添加到ACL
  4. 呼叫SetSecurityDescriptorSacl()將ACL添加到安全描述符