Ejemplo n.º 1
0
        internal static IpcPort Create(String portName, CommonSecurityDescriptor securityDescriptor, bool exclusive)
        {
            if (Environment.OSVersion.Platform != PlatformID.Win32NT) {
                throw new NotSupportedException(CoreChannel.GetResourceString("Remoting_Ipc_Win9x"));
            }
            PipeHandle handle = null;
            // Add the prefix to the portName
            string pipeName = prefix + portName;
            SECURITY_ATTRIBUTES attr = new SECURITY_ATTRIBUTES();
            attr.nLength = (int)Marshal.SizeOf(attr);
            byte[] sd = null;
            // If no securityDescriptor was set by the user use the default
            if (securityDescriptor == null)
            {
                securityDescriptor = s_securityDescriptor;
            }

            sd = new byte[securityDescriptor.BinaryLength];
            // Get the binary form of the descriptor
            securityDescriptor.GetBinaryForm(sd, 0);
               
            GCHandle pinningHandle = GCHandle.Alloc(sd, GCHandleType.Pinned);
            // get the address of the security descriptor
            attr.lpSecurityDescriptor = Marshal.UnsafeAddrOfPinnedArrayElement(sd, 0);

            // Create the named pipe with the appropriate name
            handle = NativePipe.CreateNamedPipe(pipeName, 
                                      NativePipe.PIPE_ACCESS_DUPLEX  | NativePipe.FILE_FLAG_OVERLAPPED 
                                            | (exclusive ? NativePipe.FILE_FLAG_FIRST_PIPE_INSTANCE : 0x0), // Or exclusive flag 
                                      NativePipe.PIPE_TYPE_BYTE | NativePipe.PIPE_READMODE_BYTE | NativePipe.PIPE_WAIT,
                                      NativePipe.PIPE_UNLIMITED_INSTANCES,
                                      8192,
                                      8192,
                                      NativePipe.NMPWAIT_WAIT_FOREVER,
                                      attr); 

            pinningHandle.Free();
            if (handle.Handle.ToInt32() == NativePipe.INVALID_HANDLE_VALUE){
                int error = Marshal.GetLastWin32Error();
                throw new RemotingException(String.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Ipc_CreateIpcFailed"), GetMessage(error)));
            }

             return new IpcPort(portName, handle);   
        }
 public static extern PipeHandle CreateNamedPipe(String lpName,             // pipe name
                                          uint dwOpenMode,            // pipe open mode
                                          uint dwPipeMode,             // pipe-specific modes
                                          uint nMaxInstances,          // maximum number of instances
                                          uint nOutBufferSize,         // output buffer size
                                          uint nInBufferSize,          // input buffer size
                                          uint nDefaultTimeOut,       // time-out interval
                                          SECURITY_ATTRIBUTES pipeSecurityDescriptor	//SecurityAttributes attr					 // SD
                                          );
 public static extern PipeHandle CreateNamedPipe(string lpName, uint dwOpenMode, uint dwPipeMode, uint nMaxInstances, uint nOutBufferSize, uint nInBufferSize, uint nDefaultTimeOut, SECURITY_ATTRIBUTES pipeSecurityDescriptor);
 internal static IpcPort Create(string portName, CommonSecurityDescriptor securityDescriptor, bool exclusive)
 {
     SECURITY_ATTRIBUTES security_attributes;
     if (Environment.OSVersion.Platform != PlatformID.Win32NT)
     {
         throw new NotSupportedException(CoreChannel.GetResourceString("Remoting_Ipc_Win9x"));
     }
     PipeHandle handle = null;
     string lpName = @"\\.\pipe\" + portName;
     security_attributes = new SECURITY_ATTRIBUTES {
         nLength = Marshal.SizeOf(security_attributes)
     };
     byte[] binaryForm = null;
     if (securityDescriptor == null)
     {
         securityDescriptor = s_securityDescriptor;
     }
     binaryForm = new byte[securityDescriptor.BinaryLength];
     securityDescriptor.GetBinaryForm(binaryForm, 0);
     GCHandle handle2 = GCHandle.Alloc(binaryForm, GCHandleType.Pinned);
     security_attributes.lpSecurityDescriptor = Marshal.UnsafeAddrOfPinnedArrayElement(binaryForm, 0);
     handle = NativePipe.CreateNamedPipe(lpName, (uint) (0x40000003 | (exclusive ? 0x80000 : 0)), 0, 0xff, 0x2000, 0x2000, uint.MaxValue, security_attributes);
     handle2.Free();
     if (handle.Handle.ToInt32() == -1)
     {
         int errorCode = Marshal.GetLastWin32Error();
         throw new RemotingException(string.Format(CultureInfo.CurrentCulture, CoreChannel.GetResourceString("Remoting_Ipc_CreateIpcFailed"), new object[] { GetMessage(errorCode) }));
     }
     return new IpcPort(portName, handle);
 }