/// <summary> /// Creates pipe with full access rights for everyone. /// </summary> /// <exception cref="System.InvalidOperationException">Pipe is already open.</exception> /// <exception cref="System.IO.IOException">Cannot create named pipe.</exception> public void CreateWithFullAccess() { if (SafeHandle != null) { throw new InvalidOperationException("Pipe is already open."); } var sec = new RawSecurityDescriptor(ControlFlags.DiscretionaryAclPresent, null, null, null, null); var sa = new NativeMethods.SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf(typeof(NativeMethods.SECURITY_ATTRIBUTES)), bInheritHandle = true }; var secBinary = new byte[sec.BinaryLength]; sec.GetBinaryForm(secBinary, 0); sa.lpSecurityDescriptor = Marshal.AllocHGlobal(secBinary.Length); Marshal.Copy(secBinary, 0, sa.lpSecurityDescriptor, secBinary.Length); SafeHandle = NativeMethods.CreateNamedPipe(FullPipeName, NativeMethods.PIPE_ACCESS_DUPLEX, NativeMethods.PIPE_TYPE_BYTE | NativeMethods.PIPE_READMODE_BYTE | NativeMethods.PIPE_WAIT, NativeMethods.PIPE_UNLIMITED_INSTANCES, 4096, 4096, NativeMethods.NMPWAIT_USE_DEFAULT_WAIT, ref sa); if (SafeHandle.IsInvalid) { throw new IOException("Cannot create named pipe.", new Win32Exception()); } }
/// <summary> /// Closes named pipe. /// </summary> public void Close() { if (this.SafeHandle != null) { this.SafeHandle.Close(); this.SafeHandle = null; } }
/// <summary> /// Closes named pipe. /// </summary> public void Close() { if (SafeHandle != null) { SafeHandle.Close(); SafeHandle = null; } }
/// <summary> /// Creates pipe. /// </summary> /// <exception cref="System.InvalidOperationException">Pipe is already open.</exception> /// <exception cref="System.IO.IOException">Cannot create named pipe.</exception> public void Create() { if (this.SafeHandle != null) { throw new InvalidOperationException("Pipe is already open."); } this.SafeHandle = NativeMethods.CreateNamedPipe(this.FullPipeName, NativeMethods.PIPE_ACCESS_DUPLEX, NativeMethods.PIPE_TYPE_BYTE | NativeMethods.PIPE_READMODE_BYTE | NativeMethods.PIPE_WAIT, NativeMethods.PIPE_UNLIMITED_INSTANCES, 4096, 4096, NativeMethods.NMPWAIT_USE_DEFAULT_WAIT, IntPtr.Zero); if (this.SafeHandle.IsInvalid) { throw new IOException("Cannot create named pipe.", new Win32Exception()); } }
/// <summary> /// Opens existing named pipe. /// </summary> /// <exception cref="System.InvalidOperationException">Pipe is already open.</exception> /// <exception cref="System.IO.IOException">Cannot find open named pipe. -or- Cannot open named pipe.</exception> public void Open() { if (this.SafeHandle != null) { throw new InvalidOperationException("Pipe is already open."); } if (NativeMethods.WaitNamedPipe(this.FullPipeName, NativeMethods.NMPWAIT_USE_DEFAULT_WAIT) == false) { throw new IOException("Cannot find open named pipe.", new Win32Exception()); } this.SafeHandle = NativeMethods.CreateFile(this.FullPipeName, NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, 0, System.IntPtr.Zero, NativeMethods.OPEN_EXISTING, NativeMethods.FILE_ATTRIBUTE_NORMAL, System.IntPtr.Zero); if (this.SafeHandle.IsInvalid) { throw new IOException("Cannot open named pipe.", new Win32Exception()); } }
public static void Send(Transfer transfer) { NativeMethods.FileSafeHandle handle = null; try { var buffer = Serialize(transfer); //open pipe if (NativeMethods.WaitNamedPipe(NamedPipeName, NativeMethods.NMPWAIT_USE_DEFAULT_WAIT) == false) { throw new Win32Exception(); } handle = NativeMethods.CreateFile(NamedPipeName, NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, 0, System.IntPtr.Zero, NativeMethods.OPEN_EXISTING, NativeMethods.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); if (handle.IsInvalid) { throw new Win32Exception(); } //send bytes uint written = 0; NativeOverlapped overlapped = new NativeOverlapped(); if (NativeMethods.WriteFile(handle, buffer, (uint)buffer.Length, ref written, ref overlapped) == false) { throw new Win32Exception(); } if (written != buffer.Length) { throw new InvalidOperationException("Invalid byte count."); } } finally { if (handle != null) { handle.Close(); } } }
public static bool Attach(bool noAutoExit) { lock (_syncRoot) { NativeMethods.FileSafeHandle handle = null; bool isFirstInstance = false; try { _mtxFirstInstance = new Mutex(true, MutexName, out isFirstInstance); if (isFirstInstance == false) //we need to contact previous instance. { _mtxFirstInstance = null; byte[] buffer; using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); bf.Serialize(ms, new NewInstanceEventArgs(System.Environment.CommandLine, System.Environment.GetCommandLineArgs())); ms.Flush(); buffer = ms.GetBuffer(); } //open pipe if (!NativeMethods.WaitNamedPipe(NamedPipeName, NativeMethods.NMPWAIT_USE_DEFAULT_WAIT)) { throw new System.InvalidOperationException(Resources.ExceptionWaitNamedPipeFailed); } handle = NativeMethods.CreateFile(NamedPipeName, NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, 0, System.IntPtr.Zero, NativeMethods.OPEN_EXISTING, NativeMethods.FILE_ATTRIBUTE_NORMAL, System.IntPtr.Zero); if (handle.IsInvalid) { throw new System.InvalidOperationException(Resources.ExceptionCreateFileFailed); } //send bytes uint written = 0; NativeOverlapped overlapped = new NativeOverlapped(); if (!NativeMethods.WriteFile(handle, buffer, (uint)buffer.Length, ref written, ref overlapped)) { throw new System.InvalidOperationException(Resources.ExceptionWriteFileFailed); } if (written != buffer.Length) { throw new System.InvalidOperationException(Resources.ExceptionWriteFileWroteUnexpectedNumberOfBytes); } } else //there is no application already running. { _thread = new Thread(Run); _thread.Name = "Medo.Application.SingleInstance.0"; _thread.IsBackground = true; _thread.Start(); } } catch (System.Exception ex) { System.Diagnostics.Trace.TraceWarning(ex.Message + " {Medo.Application.SingleInstance}"); } finally { //if (handle != null && (!(handle.IsClosed || handle.IsInvalid))) { // handle.Close(); //} if (handle != null) { handle.Dispose(); } } if ((isFirstInstance == false) && (noAutoExit == false)) { System.Diagnostics.Trace.TraceInformation("Exit(E_ABORT): Another instance is running. {Medo.Application.SingleInstance}"); System.Environment.Exit(unchecked ((int)0x80004004)); //E_ABORT(0x80004004) } return(isFirstInstance); } }
/// <summary> /// Creates pipe with full access rights for everyone. /// </summary> /// <exception cref="System.InvalidOperationException">Pipe is already open.</exception> /// <exception cref="System.IO.IOException">Cannot create named pipe.</exception> public void CreateWithFullAccess() { if (this.SafeHandle != null) { throw new InvalidOperationException("Pipe is already open."); } var sec = new RawSecurityDescriptor(ControlFlags.DiscretionaryAclPresent, null, null, null, null); var sa = new NativeMethods.SECURITY_ATTRIBUTES(); sa.nLength = Marshal.SizeOf(typeof(NativeMethods.SECURITY_ATTRIBUTES)); sa.bInheritHandle = true; byte[] secBinary = new byte[sec.BinaryLength]; sec.GetBinaryForm(secBinary, 0); sa.lpSecurityDescriptor = Marshal.AllocHGlobal(secBinary.Length); Marshal.Copy(secBinary, 0, sa.lpSecurityDescriptor, secBinary.Length); this.SafeHandle = NativeMethods.CreateNamedPipe(this.FullPipeName, NativeMethods.PIPE_ACCESS_DUPLEX, NativeMethods.PIPE_TYPE_BYTE | NativeMethods.PIPE_READMODE_BYTE | NativeMethods.PIPE_WAIT, NativeMethods.PIPE_UNLIMITED_INSTANCES, 4096, 4096, NativeMethods.NMPWAIT_USE_DEFAULT_WAIT, ref sa); if (this.SafeHandle.IsInvalid) { throw new IOException("Cannot create named pipe.", new Win32Exception()); } }