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

📄 pipehandle.cs

📁 通过NamePipe与其他进程通信的代码
💻 CS
字号:
using System;
using System.Runtime.InteropServices;
using AppModule.InterProcessComm;

namespace AppModule.NamedPipes {
	#region Comments
	/// <summary>
	/// Holds the operating system native handle and the current state of the pipe connection.
	/// </summary>
	#endregion
	public sealed class PipeHandle {
		#region Comments
		/// <summary>
		/// The operating system native handle.
		/// </summary>
		#endregion
		public IntPtr Handle;
		#region Comments
		/// <summary>
		/// The current state of the pipe connection.
		/// </summary>
		#endregion
		public InterProcessConnectionState State;
		#region Comments
		/// <summary>
		/// Creates a PipeHandle instance using the passed native handle.
		/// </summary>
		/// <param name="hnd">The native handle.</param>
		#endregion
		public PipeHandle (int hnd) {
			this.Handle = new IntPtr(hnd);
			this.State = InterProcessConnectionState.NotSet;
		}
		#region Comments
		/// <summary>
		/// Creates a PipeHandle instance using the provided native handle and state.
		/// </summary>
		/// <param name="hnd">The native handle.</param>
		/// <param name="state">The state of the pipe connection.</param>
		#endregion
		public PipeHandle (int hnd, InterProcessConnectionState state) {
			this.Handle = new IntPtr(hnd);
			this.State = state;
		}
		#region Comments
		/// <summary>
		/// Creates a PipeHandle instance with an invalid native handle.
		/// </summary>
		#endregion
		public PipeHandle () {
			this.Handle = new IntPtr(NamedPipeNative.INVALID_HANDLE_VALUE);
			this.State = InterProcessConnectionState.NotSet;
		}
	}
	[Serializable]
	public struct RPIPEMessage
	{
		public UInt32   Size;
		public byte Kind;
		public UInt32   Count;
		public char[] Data;   //8095长度
		private RPIPEMessage(string str)
		{
			Size=0;
			Kind=1;
			Count=0;
			Data=new char[8095];
		}
		public RPIPEMessage(byte msgKind,string str)
		{
			this.Size=0;
			this.Kind=msgKind;
			byte[] bytes=System.Text.Encoding.UTF8.GetBytes(str);
			if (bytes.Length>8095) 
			{
				byte[] bts=new byte[8096];
				Array.Copy(bytes,0,bts,0,8096);
				bytes=bts;
			}
			this.Data =System.Text.Encoding.UTF8.GetChars(bytes);
			this.Count =( UInt32)bytes.Length;
			this.Size =(UInt32)Marshal.SizeOf(this.Kind)+(UInt32)Marshal.SizeOf(this.Size)+(UInt32)Marshal.SizeOf(this.Count)+this.Count+3;	
		}
		public static RPIPEMessage GetMessage(byte[] bytes)
		{
			RPIPEMessage msg=new RPIPEMessage("");
			if (bytes.Length<9) return msg;
			msg.Size =BitConverter.ToUInt32(bytes,0);
			msg.Count =BitConverter.ToUInt32(bytes,4);
			msg.Kind =bytes[8];
			
			string str=System.Text.Encoding.UTF8.GetString(bytes,9,(int)msg.Count);
			msg.Data =System.Text.Encoding.UTF8.GetChars(System.Text.Encoding.UTF8.GetBytes(str));

            return msg;			
		}

		public byte[] GetBytes()
		{
			byte[] bytes=new byte[this.Size];
			BitConverter.GetBytes(this.Size).CopyTo(bytes,0);
			BitConverter.GetBytes(this.Count).CopyTo(bytes,4);
			BitConverter.GetBytes(this.Kind).CopyTo(bytes,8);
			byte[] bts=System.Text.Encoding.UTF8.GetBytes(this.Data);
			bts.CopyTo(bytes,9);
			return bytes;
		}

	}
}

⌨️ 快捷键说明

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