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

📄 marshalex.cs

📁 蓝牙通讯
💻 CS
📖 第 1 页 / 共 3 页
字号:
		{
			byte[] data = new byte[8];
			Marshal.Copy(new IntPtr(ptr.ToInt32() + ofs), data, 0, 8);

			return BitConverter.ToUInt64(data,0);
		}
		#endregion

		#region Bool

		/// <summary>
		/// Reads a bool from an unmanaged pointer.   
		/// </summary>
		/// <param name="ptr">The address in unmanaged memory from which to read. </param>
		/// <param name="ofs">The offset from the ptr where the bool is located.</param>
		/// <returns>The bool read from the ptr parameter. </returns>
		public static bool ReadBool(IntPtr ptr, int ofs)
		{
			bool b = false;

			byte[] data = new byte[4];
			Marshal.Copy(new IntPtr(ptr.ToInt32() + ofs), data, 0, 4);

			b = Convert.ToBoolean(data);

			return b;
		}

		#endregion

		#region Byte

		/// <summary>
		/// Reads a single byte from an unmanaged pointer.   
		/// </summary>
		/// <param name="ptr">The address in unmanaged memory from which to read. </param>
		/// <param name="ofs">The offset from the ptr where the byte is located.</param>
		/// <returns>The byte read from the ptr parameter. </returns>
		public static byte ReadByte(IntPtr ptr, int ofs)
		{
			byte b;

			byte[] data = new byte[1];
			Marshal.Copy(new IntPtr(ptr.ToInt32() + ofs), data, 0, 1);

			b = data[0];

			return b;
		}

		#endregion

		#endregion

		#region Write functions

		#region IntPtr

		/// <summary>
		/// Writes an IntPtr value to unmanaged memory.   
		/// </summary>
		/// <param name="ptr">The address in unmanaged memory from which to write. </param>
		/// <param name="ofs">The offset of the IntPtr from the ptr.</param>
		/// <param name="val">The value to write. </param>
		public static void WriteIntPtr(IntPtr ptr, int ofs, IntPtr val)
		{
			MarshalEx.WriteInt32(ptr, ofs, val.ToInt32());
		}
		
		#endregion

		#region Int32

		/// <summary>
		/// Writes an Int32 value to unmanaged memory.   
		/// </summary>
		/// <param name="ptr">The base address in unmanaged memory from which to write.</param>
		/// <param name="ofs">An additional byte offset, added to the ptr parameter before writing.</param>
		/// <param name="val">The value to write.</param>
		public static void WriteInt32(IntPtr ptr, int ofs, int val)
		{
			byte[] data = BitConverter.GetBytes(val);
			Marshal.Copy(data, 0, new IntPtr(ptr.ToInt32() + ofs), data.Length);
		}

		/// <summary>
		/// Writes a UInt32 value to unmanaged memory.
		/// </summary>
		/// <param name="ptr">The base address in unmanaged memory from which to write.</param>
		/// <param name="ofs">An additional byte offset, added to the ptr parameter before writing.</param>
		/// <param name="val">The value to write.</param>
		public static void WriteUInt32(IntPtr ptr, int ofs, uint val)
		{
			byte[] data = BitConverter.GetBytes(val);
			Marshal.Copy(data, 0, new IntPtr(ptr.ToInt32() + ofs), data.Length);
		}

		#endregion

		#region Int16

		/// <summary>
		/// Writes an Int16 value to unmanaged memory.   
		/// </summary>
		/// <param name="ptr">The address in unmanaged memory from which to write. </param>
		/// <param name="ofs">An additional byte offset, added to the ptr parameter before writing.</param>
		/// <param name="val">The value to write. </param>
		public static void WriteInt16(IntPtr ptr, int ofs, Int16 val)
		{
			byte[] data = BitConverter.GetBytes(val);
			Marshal.Copy(data, 0, new IntPtr(ptr.ToInt32() + ofs), data.Length);
		}

		/// <summary>
		/// Writes a 16-bit unsigned integer value to unmanaged memory.
		/// </summary>
		/// <param name="ptr">The base address in unmanaged memory from which to write.</param>
		/// <param name="ofs">An additional byte offset, added to the ptr parameter before writing.</param>
		/// <param name="val">The value to write.</param>
		public static void WriteUInt16(IntPtr ptr, int ofs, UInt16 val)
		{
			byte[] data = BitConverter.GetBytes(val);
			Marshal.Copy(data, 0, new IntPtr(ptr.ToInt32() + ofs), data.Length);
		}

		#endregion

		#region String

		#region ANSI String

		/// <summary>
		/// Copies the contents of a managed <see cref="System.String"/> into unmanaged memory, converting into ANSI format as it copies.
		/// </summary>
		/// <param name="s">A managed string to be copied. </param>
		/// <returns>The address, in unmanaged memory, to where s was copied, or 0 if a null reference (Nothing in Visual Basic) string was supplied.</returns>
		public static IntPtr StringToHGlobalAnsi(string s)
		{
			if(s == null)
				return IntPtr.Zero;

			int i = s.Length + 1;
			IntPtr ptr = LocalAlloc(LPTR, (uint)i);

			byte[] data = Encoding.ASCII.GetBytes(s);
			Marshal.Copy(data, 0, ptr, data.Length);

			return ptr;
		}

		#endregion

		#region Unicode String		
		/// <summary>
		/// Copies the contents of a managed <see cref="System.String"/> into unmanaged memory.
		/// </summary>
		/// <param name="s">A managed string to be copied.</param>
		/// <returns>The address, in unmanaged memory, to where s was copied, or 0 if a null reference (Nothing in Visual Basic) string was supplied.</returns>
		public static IntPtr StringToHGlobalUni(string s)
		{
			if(s == null)
				return IntPtr.Zero;
	
			int i = (s.Length + 1) * System.Text.UnicodeEncoding.CharSize;
			
			IntPtr ptr = LocalAlloc(LPTR,(uint)i);

			byte[] data = Encoding.Unicode.GetBytes(s);
			Marshal.Copy(data, 0, ptr, data.Length);

			return ptr;
		}

		#endregion

		#endregion

		#region Char

		/// <summary>
		/// Writes a single char value to unmanaged memory.   
		/// </summary>
		/// <param name="ptr">The address in unmanaged memory from which to write. </param>
		/// <param name="ofs">The offset of the char from the ptr.</param>
		/// <param name="val">The value to write. </param>
		public static void WriteChar(IntPtr ptr, int ofs, char val)
		{
			byte[] data = BitConverter.GetBytes(val);
			Marshal.Copy(data, 0, new IntPtr(ptr.ToInt32() + ofs), data.Length);
		}

		#endregion

		#region Byte[]

		/// <summary>
		/// Writes a byte array to unmanaged memory.   
		/// </summary>
		/// <param name="ptr">The address in unmanaged memory from which to write. </param>
		/// <param name="ofs">The offset of the byte array from the ptr.</param>
		/// <param name="val">The value to write. </param>
		public static void WriteByteArray(IntPtr ptr, int ofs, byte[] val)
		{
			Marshal.Copy(val, 0, new IntPtr(ptr.ToInt32() + ofs), val.Length);
		}

		#endregion

		#region Int64

		/// <summary>
		/// Writes an Int64 value to unmanaged memory.   
		/// </summary>
		/// <param name="ptr">The address in unmanaged memory from which to write. </param>
		/// <param name="ofs">An additional byte offset, added to the ptr parameter before writing.</param>
		/// <param name="val">The value to write.</param>
		public static void WriteInt64(IntPtr ptr, int ofs, Int64 val)
		{
			byte[] data = BitConverter.GetBytes(val);
			Marshal.Copy(data, 0, new IntPtr(ptr.ToInt32() + ofs), data.Length);
		}

		/// <summary>
		/// Writes a 64-bit unsigned integer value to unmanaged memory.
		/// </summary>
		/// <param name="ptr">The address in unmanaged memory from which to write.</param>
		/// <param name="ofs">An additional byte offset, added to the ptr parameter before writing.</param>
		/// <param name="val">The value to write.</param>
		public static void WriteUInt64(IntPtr ptr, int ofs, UInt64 val)
		{
			byte[] data = BitConverter.GetBytes(val);
			Marshal.Copy(data, 0, new IntPtr(ptr.ToInt32() + ofs), data.Length);
		}

		#endregion

		#region Bool

		/// <summary>
		/// Writes a bool value to unmanaged memory.   
		/// </summary>
		/// <param name="ptr">The address in unmanaged memory from which to write. </param>
		/// <param name="ofs">The offset of the bool from the ptr.</param>
		/// <param name="val">The value to write. </param>
		public static void WriteBool(IntPtr ptr, int ofs, bool val)
		{
			byte[] data = BitConverter.GetBytes(val);
			Marshal.Copy(data, 0, new IntPtr(ptr.ToInt32() + ofs), data.Length);
		}

		#endregion

		#region Byte

		/// <summary>
		/// Writes a single byte value to unmanaged memory.   
		/// </summary>
		/// <param name="ptr">The address in unmanaged memory from which to write. </param>
		/// <param name="ofs">The offset of the byte from the ptr.</param>
		/// <param name="val">The value to write. </param>
		public static void WriteByte(IntPtr ptr, int ofs, byte val)
		{
			byte[] data = BitConverter.GetBytes(val);
			Marshal.Copy(data, 0, new IntPtr(ptr.ToInt32() + ofs), data.Length);
		}

		#endregion

		#endregion

		#region Copy Functions

		/// <summary>
		/// Copies data from an unmanaged memory pointer to a managed 8-bit unsigned integer array.  
		/// </summary>
		/// <param name="source">The memory pointer to copy from.</param>
		/// <param name="destination">The array to copy to.</param>
		/// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
		/// <param name="length">The number of array elements to copy.</param>
		public static void Copy(IntPtr source, byte[] destination, int startIndex, int length) 
		{
			Marshal.Copy(source, destination, startIndex, length);
		}

		/// <summary>
		/// Copies data from an unmanaged memory pointer to a managed double-precision floating-point number array.  
		/// </summary>
		/// <param name="source">The memory pointer to copy from.</param>
		/// <param name="destination">The array to copy to.</param>
		/// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
		/// <param name="length">The number of array elements to copy.</param>
		public static void Copy(IntPtr source, double[] destination, int startIndex, int length) 
		{
			Marshal.Copy(source, destination, startIndex, length);
		}

		/// <summary>
		/// Copies data from an unmanaged memory pointer to a managed single-precision floating-point number array.  
		/// </summary>
		/// <param name="source">The memory pointer to copy from.</param>
		/// <param name="destination">The array to copy to.</param>
		/// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
		/// <param name="length">The number of array elements to copy.</param>
		public static void Copy(IntPtr source, float[] destination, int startIndex, int length) 
		{
			Marshal.Copy(source, destination, startIndex, length);
		}

		/// <summary>
		/// Copies data from an unmanaged memory pointer to a managed 64-bit signed integer array. 
		/// </summary>
		/// <param name="source">The memory pointer to copy from.</param>
		/// <param name="destination">The array to copy to.</param>
		/// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
		/// <param name="length">The number of array elements to copy.</param>
		public static void Copy(IntPtr source, long[] destination, int startIndex, int length) 
		{
			Marshal.Copy(source, destination, startIndex, length);
		}

⌨️ 快捷键说明

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