⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 namedpipenative.cs

📁 通过NamePipe与其他进程通信的代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Security;
using System.Runtime.InteropServices;

namespace AppModule.NamedPipes {
	#region Comments
	/// <summary>
	/// This utility class exposes kernel32.dll methods for named pipes communication.
	/// </summary>
	/// <remarks>
	/// Use the following links for complete information about the exposed methods:
	/// <list type="bullet">
	/// <item>
	/// <description><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ipc/base/pipe_functions.asp" target="_blank">Named Pipe Functions</a></description>
	/// </item>
	/// <item>
	/// <description><a href="http://msdn.microsoft.com/library/en-us/fileio/base/file_management_functions.asp" target="_blank">File Management Functions</a></description>
	/// </item>
	/// <item>
	/// <description><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/handle_and_object_functions.asp" target="_blank">Handle and Object Functions</a></description>
	/// </item>
	/// <item>
	/// <description><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/system_error_codes.asp" target="_blank">System Error Codes</a></description>
	/// </item>
	/// </list>
	/// </remarks>
	#endregion
	[SuppressUnmanagedCodeSecurity]
	public sealed class NamedPipeNative {
		#region Comments
		/// <summary>
		/// Outbound pipe access.
		/// </summary>
		#endregion
		public const uint PIPE_ACCESS_OUTBOUND = 0x00000002;
		#region Comments
		/// <summary>
		/// Duplex pipe access.
		/// </summary>
		#endregion
		public const uint PIPE_ACCESS_DUPLEX = 0x00000003;
		#region Comments
		/// <summary>
		/// Inbound pipe access.
		/// </summary>
		#endregion
		public const uint PIPE_ACCESS_INBOUND = 0x00000001;
		#region Comments
		/// <summary>
		/// Pipe blocking mode.
		/// </summary>
		#endregion
		public const uint PIPE_WAIT = 0x00000000;
		#region Comments
		/// <summary>
		/// Pipe non-blocking mode.
		/// </summary>
		#endregion
		public const uint PIPE_NOWAIT = 0x00000001;
		#region Comments
		/// <summary>
		/// Pipe read mode of type Byte.
		/// </summary>
		#endregion
		public const uint PIPE_READMODE_BYTE = 0x00000000;
		#region Comments
		/// <summary>
		/// Pipe read mode of type Message.
		/// </summary>
		#endregion
		public const uint PIPE_READMODE_MESSAGE = 0x00000002;
		#region Comments
		/// <summary>
		/// Byte pipe type.
		/// </summary>
		#endregion
		public const uint PIPE_TYPE_BYTE = 0x00000000;
		#region Comments
		/// <summary>
		/// Message pipe type.
		/// </summary>
		#endregion
		public const uint PIPE_TYPE_MESSAGE = 0x00000004;
		#region Comments
		/// <summary>
		/// Pipe client end.
		/// </summary>
		#endregion
		public const uint PIPE_CLIENT_END = 0x00000000;
		#region Comments
		/// <summary>
		/// Pipe server end.
		/// </summary>
		#endregion
		public const uint PIPE_SERVER_END = 0x00000001;
		#region Comments
		/// <summary>
		/// Unlimited server pipe instances.
		/// </summary>
		#endregion
		public const uint PIPE_UNLIMITED_INSTANCES = 255;
		#region Comments
		/// <summary>
		/// Waits indefinitely when connecting to a pipe.
		/// </summary>
		#endregion
		public const uint NMPWAIT_WAIT_FOREVER = 0xffffffff;
		#region Comments
		/// <summary>
		/// Does not wait for the named pipe.
		/// </summary>
		#endregion
		public const uint NMPWAIT_NOWAIT = 0x00000001;
		#region Comments
		/// <summary>
		/// Uses the default time-out specified in a call to the CreateNamedPipe method.
		/// </summary>
		#endregion
		public const uint NMPWAIT_USE_DEFAULT_WAIT = 0x00000000;
		#region Comments
		/// <summary>
		/// 
		/// </summary>
		#endregion
		public const uint GENERIC_READ = (0x80000000);
		#region Comments
		/// <summary>
		/// Generic write access to the pipe.
		/// </summary>
		#endregion
		public const uint GENERIC_WRITE = (0x40000000);
		#region Comments
		/// <summary>
		/// Generic execute access to the pipe.
		/// </summary>
		#endregion
		public const uint GENERIC_EXECUTE = (0x20000000);
		#region Comments
		/// <summary>
		/// Read, write, and execute access.
		/// </summary>
		#endregion
		public const uint GENERIC_ALL = (0x10000000);
		#region Comments
		/// <summary>
		/// Create new file. Fails if the file exists.
		/// </summary>
		#endregion
		public const uint CREATE_NEW        = 1;
		#region Comments
		/// <summary>
		/// Create new file. Overrides an existing file.
		/// </summary>
		#endregion
		public const uint CREATE_ALWAYS     = 2;
		#region Comments
		/// <summary>
		/// Open existing file.
		/// </summary>
		#endregion
		public const uint OPEN_EXISTING     = 3;
		#region Comments
		/// <summary>
		/// Open existing file. If the file does not exist, creates it.
		/// </summary>
		#endregion
		public const uint OPEN_ALWAYS       = 4;
		#region Comments
		/// <summary>
		/// Opens the file and truncates it so that its size is zero bytes.
		/// </summary>
		#endregion
		public const uint TRUNCATE_EXISTING = 5;
		#region Comments
		/// <summary>
		/// Invalid operating system handle.
		/// </summary>
		#endregion
		public const int INVALID_HANDLE_VALUE = -1;
		#region Comments
		/// <summary>
		/// The operation completed successfully.
		/// </summary>
		#endregion
		public const ulong ERROR_SUCCESS = 0;
		#region Comments
		/// <summary>
		/// The system cannot find the file specified.
		/// </summary>
		#endregion
		public const ulong ERROR_CANNOT_CONNECT_TO_PIPE = 2;
		#region Comments
		/// <summary>
		/// All pipe instances are busy.
		/// </summary>
		#endregion
		public const ulong ERROR_PIPE_BUSY = 231;
		#region Comments
		/// <summary>
		/// The pipe is being closed.
		/// </summary>
		#endregion
		public const ulong ERROR_NO_DATA = 232;
		#region Comments
		/// <summary>
		/// No process is on the other end of the pipe.
		/// </summary>
		#endregion
		public const ulong ERROR_PIPE_NOT_CONNECTED = 233;
		#region Comments
		/// <summary>
		/// More data is available.
		/// </summary>
		#endregion
		public const ulong ERROR_MORE_DATA = 234;
		#region Comments
		/// <summary>
		/// There is a process on other end of the pipe.
		/// </summary>
		#endregion
		public const ulong ERROR_PIPE_CONNECTED = 535;
		#region Comments
		/// <summary>
		/// Waiting for a process to open the other end of the pipe.
		/// </summary>
		#endregion
		public const ulong ERROR_PIPE_LISTENING = 536;
		#region Comments
		/// <summary>
		/// Creates an instance of a named pipe and returns a handle for 
		/// subsequent pipe operations.
		/// </summary>
		/// <param name="lpName">Pointer to the null-terminated string that 
		/// uniquely identifies the pipe.</param>
		/// <param name="dwOpenMode">Pipe access mode, the overlapped mode, 
		/// the write-through mode, and the security access mode of the pipe handle.</param>
		/// <param name="dwPipeMode">Type, read, and wait modes of the pipe handle.</param>
		/// <param name="nMaxInstances">Maximum number of instances that can be 
		/// created for this pipe.</param>
		/// <param name="nOutBufferSize">Number of bytes to reserve for the output buffer.</param>
		/// <param name="nInBufferSize">Number of bytes to reserve for the input buffer.</param>
		/// <param name="nDefaultTimeOut">Default time-out value, in milliseconds.</param>
		/// <param name="pipeSecurityDescriptor">Pointer to a 
		/// <see cref="AppModule.NamedPipes.SECURITY_ATTRIBUTES">SECURITY_ATTRIBUTES</see> 
		/// object that specifies a security descriptor for the new named pipe.</param>
		/// <returns>If the function succeeds, the return value is a handle 
		/// to the server end of a named pipe instance.</returns>
		#endregion
		[DllImport("kernel32.dll", SetLastError=true)]
		public static extern IntPtr 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
			IntPtr pipeSecurityDescriptor		// SD
			);
		#region Comments
		/// <summary>
		/// Enables a named pipe server process to wait for a client 
		/// process to connect to an instance of a named pipe.
		/// </summary>
		/// <param name="hHandle">Handle to the server end of a named pipe instance.</param>
		/// <param name="lpOverlapped">Pointer to an 
		/// <see cref="AppModule.NamedPipes.Overlapped">Overlapped</see> object.</param>
		/// <returns>If the function succeeds, the return value is nonzero.</returns>
		#endregion
		[DllImport("kernel32.dll", SetLastError=true)]
		public static extern bool ConnectNamedPipe(
			IntPtr hHandle,									// handle to named pipe
			Overlapped lpOverlapped					// overlapped structure
			);
		#region Comments
		/// <summary>
		/// Connects to a message-type pipe (and waits if an instance of the 
		/// pipe is not available), writes to and reads from the pipe, and then closes the pipe.
		/// </summary>
		/// <param name="lpNamedPipeName">Pointer to a null-terminated string 
		/// specifying the pipe name.</param>
		/// <param name="lpInBuffer">Pointer to the buffer containing the data written 
		/// to the pipe.</param>
		/// <param name="nInBufferSize">Size of the write buffer, in bytes.</param>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -