marshal.cs

来自「没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没」· CS 代码 · 共 1,057 行 · 第 1/2 页

CS
1,057
字号
/* * Marshal.cs - Implementation of the *			"System.Runtime.InteropServices.Marshal" class. * * Copyright (C) 2002  Southern Storm Software, Pty Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */namespace System.Runtime.InteropServices{#if CONFIG_RUNTIME_INFRAusing System;using System.Runtime.CompilerServices;using System.Reflection;using System.Threading;using System.Security;// This class is not ECMA-compatible, strictly speaking.  But it is// usually necessary for any application that uses PInvoke or C.#if CONFIG_PERMISSIONS && !ECMA_COMPAT[SuppressUnmanagedCodeSecurity]#endifpublic sealed class Marshal{	// This class cannot be instantiated.	private Marshal() {}	// Character size information.	public static readonly int SystemDefaultCharSize = 1;	public static readonly int SystemMaxDBCSCharSize = 6;	// Allocate memory from the global (malloc) heap.	[MethodImpl(MethodImplOptions.InternalCall)]	extern public static IntPtr AllocHGlobal(IntPtr cb);	public static IntPtr AllocHGlobal(int cb)			{				return AllocHGlobal(new IntPtr(cb));			}	// Internal version of "Copy" from managed to unmanaged.	[MethodImpl(MethodImplOptions.InternalCall)]	extern private static void CopyMU(Array source, int startOffset,					           		  IntPtr destination, int numBytes);	// Copy data from a managed array to an unmanaged memory pointer.	public static void Copy(byte[] source, int startIndex,					        IntPtr destination, int length)			{				if(source == null)				{					throw new ArgumentNullException("source");				}				if(destination == IntPtr.Zero)				{					throw new ArgumentNullException("destination");				}				if(startIndex < 0 || startIndex > source.Length)				{					throw new ArgumentOutOfRangeException						("startIndex", _("ArgRange_Array"));				}				if(length < 0 || (source.Length - startIndex) < length)				{					throw new ArgumentOutOfRangeException						("length", _("ArgRange_Array"));				}				CopyMU(source, startIndex, destination, length);			}	public static void Copy(char[] source, int startIndex,					        IntPtr destination, int length)			{				if(source == null)				{					throw new ArgumentNullException("source");				}				if(destination == IntPtr.Zero)				{					throw new ArgumentNullException("destination");				}				if(startIndex < 0 || startIndex > source.Length)				{					throw new ArgumentOutOfRangeException						("startIndex", _("ArgRange_Array"));				}				if(length < 0 || (source.Length - startIndex) < length)				{					throw new ArgumentOutOfRangeException						("length", _("ArgRange_Array"));				}				CopyMU(source, startIndex * 2, destination, length * 2);			}	public static void Copy(double[] source, int startIndex,					        IntPtr destination, int length)			{				if(source == null)				{					throw new ArgumentNullException("source");				}				if(destination == IntPtr.Zero)				{					throw new ArgumentNullException("destination");				}				if(startIndex < 0 || startIndex > source.Length)				{					throw new ArgumentOutOfRangeException						("startIndex", _("ArgRange_Array"));				}				if(length < 0 || (source.Length - startIndex) < length)				{					throw new ArgumentOutOfRangeException						("length", _("ArgRange_Array"));				}				CopyMU(source, startIndex * 8, destination, length * 8);			}	public static void Copy(float[] source, int startIndex,					        IntPtr destination, int length)			{				if(source == null)				{					throw new ArgumentNullException("source");				}				if(destination == IntPtr.Zero)				{					throw new ArgumentNullException("destination");				}				if(startIndex < 0 || startIndex > source.Length)				{					throw new ArgumentOutOfRangeException						("startIndex", _("ArgRange_Array"));				}				if(length < 0 || (source.Length - startIndex) < length)				{					throw new ArgumentOutOfRangeException						("length", _("ArgRange_Array"));				}				CopyMU(source, startIndex * 4, destination, length * 4);			}	public static void Copy(int[] source, int startIndex,					        IntPtr destination, int length)			{				if(source == null)				{					throw new ArgumentNullException("source");				}				if(destination == IntPtr.Zero)				{					throw new ArgumentNullException("destination");				}				if(startIndex < 0 || startIndex > source.Length)				{					throw new ArgumentOutOfRangeException						("startIndex", _("ArgRange_Array"));				}				if(length < 0 || (source.Length - startIndex) < length)				{					throw new ArgumentOutOfRangeException						("length", _("ArgRange_Array"));				}				CopyMU(source, startIndex * 4, destination, length * 4);			}	public static void Copy(long[] source, int startIndex,					        IntPtr destination, int length)			{				if(source == null)				{					throw new ArgumentNullException("source");				}				if(destination == IntPtr.Zero)				{					throw new ArgumentNullException("destination");				}				if(startIndex < 0 || startIndex > source.Length)				{					throw new ArgumentOutOfRangeException						("startIndex", _("ArgRange_Array"));				}				if(length < 0 || (source.Length - startIndex) < length)				{					throw new ArgumentOutOfRangeException						("length", _("ArgRange_Array"));				}				CopyMU(source, startIndex * 8, destination, length * 8);			}	public static void Copy(short[] source, int startIndex,					        IntPtr destination, int length)			{				if(source == null)				{					throw new ArgumentNullException("source");				}				if(destination == IntPtr.Zero)				{					throw new ArgumentNullException("destination");				}				if(startIndex < 0 || startIndex > source.Length)				{					throw new ArgumentOutOfRangeException						("startIndex", _("ArgRange_Array"));				}				if(length < 0 || (source.Length - startIndex) < length)				{					throw new ArgumentOutOfRangeException						("length", _("ArgRange_Array"));				}				CopyMU(source, startIndex * 2, destination, length * 2);			}	// Internal version of "Copy" from unmanaged to managed.	[MethodImpl(MethodImplOptions.InternalCall)]	extern private static void CopyUM(IntPtr source, Array destination,							   		  int startOffset, int numBytes);	// Copy data from an unmanaged pointer to a managed array.	public static void Copy(IntPtr source, byte[] destination,							int startIndex, int length)			{				if(source == IntPtr.Zero)				{					throw new ArgumentNullException("source");				}				if(destination == null)				{					throw new ArgumentNullException("destination");				}				if(startIndex < 0 || startIndex > destination.Length)				{					throw new ArgumentOutOfRangeException						("startIndex", _("ArgRange_Array"));				}				if(length < 0 || (destination.Length - startIndex) < length)				{					throw new ArgumentOutOfRangeException						("length", _("ArgRange_Array"));				}				CopyUM(source, destination, startIndex, length);			}	public static void Copy(IntPtr source, char[] destination,							int startIndex, int length)			{				if(source == IntPtr.Zero)				{					throw new ArgumentNullException("source");				}				if(destination == null)				{					throw new ArgumentNullException("destination");				}				if(startIndex < 0 || startIndex > destination.Length)				{					throw new ArgumentOutOfRangeException						("startIndex", _("ArgRange_Array"));				}				if(length < 0 || (destination.Length - startIndex) < length)				{					throw new ArgumentOutOfRangeException						("length", _("ArgRange_Array"));				}				CopyUM(source, destination, startIndex * 2, length * 2);			}	public static void Copy(IntPtr source, double[] destination,							int startIndex, int length)			{				if(source == IntPtr.Zero)				{					throw new ArgumentNullException("source");				}				if(destination == null)				{					throw new ArgumentNullException("destination");				}				if(startIndex < 0 || startIndex > destination.Length)				{					throw new ArgumentOutOfRangeException						("startIndex", _("ArgRange_Array"));				}				if(length < 0 || (destination.Length - startIndex) < length)				{					throw new ArgumentOutOfRangeException						("length", _("ArgRange_Array"));				}				CopyUM(source, destination, startIndex * 8, length * 8);			}	public static void Copy(IntPtr source, float[] destination,							int startIndex, int length)			{				if(source == IntPtr.Zero)				{					throw new ArgumentNullException("source");				}				if(destination == null)				{					throw new ArgumentNullException("destination");				}				if(startIndex < 0 || startIndex > destination.Length)				{					throw new ArgumentOutOfRangeException						("startIndex", _("ArgRange_Array"));				}				if(length < 0 || (destination.Length - startIndex) < length)				{					throw new ArgumentOutOfRangeException						("length", _("ArgRange_Array"));				}				CopyUM(source, destination, startIndex * 4, length * 4);			}	public static void Copy(IntPtr source, int[] destination,							int startIndex, int length)			{				if(source == IntPtr.Zero)				{					throw new ArgumentNullException("source");				}				if(destination == null)				{					throw new ArgumentNullException("destination");				}				if(startIndex < 0 || startIndex > destination.Length)				{					throw new ArgumentOutOfRangeException						("startIndex", _("ArgRange_Array"));				}				if(length < 0 || (destination.Length - startIndex) < length)				{					throw new ArgumentOutOfRangeException						("length", _("ArgRange_Array"));				}				CopyUM(source, destination, startIndex * 4, length * 4);			}	public static void Copy(IntPtr source, long[] destination,							int startIndex, int length)			{				if(source == IntPtr.Zero)				{					throw new ArgumentNullException("source");				}				if(destination == null)				{					throw new ArgumentNullException("destination");				}				if(startIndex < 0 || startIndex > destination.Length)				{					throw new ArgumentOutOfRangeException						("startIndex", _("ArgRange_Array"));				}				if(length < 0 || (destination.Length - startIndex) < length)				{					throw new ArgumentOutOfRangeException						("length", _("ArgRange_Array"));				}				CopyUM(source, destination, startIndex * 8, length * 8);			}	public static void Copy(IntPtr source, short[] destination,							int startIndex, int length)			{				if(source == IntPtr.Zero)				{					throw new ArgumentNullException("source");				}				if(destination == null)				{					throw new ArgumentNullException("destination");				}				if(startIndex < 0 || startIndex > destination.Length)				{					throw new ArgumentOutOfRangeException						("startIndex", _("ArgRange_Array"));				}				if(length < 0 || (destination.Length - startIndex) < length)				{					throw new ArgumentOutOfRangeException						("length", _("ArgRange_Array"));				}				CopyUM(source, destination, startIndex * 2, length * 2);			}	// Free memory that was allocated with AllocHGlobal.	[MethodImpl(MethodImplOptions.InternalCall)]	extern public static void FreeHGlobal(IntPtr hglobal);	// Get the offset of a field within a class.	[MethodImpl(MethodImplOptions.InternalCall)]	extern private static IntPtr OffsetOfInternal(Type t, String fieldName);	public static IntPtr OffsetOf(Type t, String fieldName)			{				if(t == null)				{					throw new ArgumentNullException("t");				}				else if(!(t is ClrType))				{					throw new ArgumentException(_("Arg_MustBeType"), "t");				}				if(fieldName == null)				{					throw new ArgumentNullException("fieldName");				}				IntPtr offset = OffsetOfInternal(t, fieldName);				if(offset == new IntPtr(-1))				{					throw new ArgumentException						(_("Reflection_UnknownField"), "fieldName");				}				return offset;			}	// Convert a pointer to an ANSI string into a string object.	[MethodImpl(MethodImplOptions.InternalCall)]	extern private static String PtrToStringAnsiInternal(IntPtr ptr, int len);	public static String PtrToStringAnsi(IntPtr ptr)			{				if(ptr == IntPtr.Zero)				{					throw new ArgumentNullException("ptr");				}				else				{					return PtrToStringAnsiInternal(ptr, -1);				}			}	public static String PtrToStringAnsi(IntPtr ptr, int len)			{				if(ptr == IntPtr.Zero)				{					throw new ArgumentNullException("ptr");				}				else if(len < 0)				{					throw new ArgumentException(_("ArgRange_NonNegative"));				}				else				{					return PtrToStringAnsiInternal(ptr, len);				}			}	// Convert a pointer to an Auto string into a string object.	// In this implementation, "Auto" is UTF-8.	[MethodImpl(MethodImplOptions.InternalCall)]	extern private static String PtrToStringAutoInternal(IntPtr ptr, int len);	public static String PtrToStringAuto(IntPtr ptr)			{				if(ptr == IntPtr.Zero)				{					throw new ArgumentNullException("ptr");				}				else				{					return PtrToStringAutoInternal(ptr, -1);				}			}	public static String PtrToStringAuto(IntPtr ptr, int len)			{				if(ptr == IntPtr.Zero)				{					throw new ArgumentNullException("ptr");				}				else if(len < 0)				{					throw new ArgumentException(_("ArgRange_NonNegative"));				}				else				{					return PtrToStringAutoInternal(ptr, len);				}			}	// Convert a pointer to a Unicode string into a string object.	[MethodImpl(MethodImplOptions.InternalCall)]	extern private static String PtrToStringUniInternal(IntPtr ptr, int len);	public static String PtrToStringUni(IntPtr ptr)			{				if(ptr == IntPtr.Zero)				{					throw new ArgumentNullException("ptr");				}				else				{					return PtrToStringUniInternal(ptr, -1);				}			}	public static String PtrToStringUni(IntPtr ptr, int len)			{				if(ptr == IntPtr.Zero)				{					throw new ArgumentNullException("ptr");				}				else if(len < 0)				{					throw new ArgumentException(_("ArgRange_NonNegative"));				}				else				{					return PtrToStringUniInternal(ptr, len);				}			}	// Convert the data at an unmanaged pointer location into	// an object by marshalling its fields one by one.	[MethodImpl(MethodImplOptions.InternalCall)]	extern private static bool PtrToStructureInternal				(IntPtr ptr, Object structure, bool allowValueTypes);	public static void PtrToStructure(IntPtr ptr, Object structure)			{				if(ptr == IntPtr.Zero)				{					throw new ArgumentNullException("ptr");				}				else if(structure == null)				{					throw new ArgumentNullException("structure");				}				if(!PtrToStructureInternal(ptr, structure, false))

⌨️ 快捷键说明

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