1 // System.Runtime.InteropServices.Marshal.cs
3 // Sean MacIsaac (macisaac@ximian.com)
4 // Paolo Molaro (lupus@ximian.com)
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Jonathan Chambers (joncham@gmail.com)
8 // (C) 2001-2002 Ximian, Inc.
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.Runtime.CompilerServices;
37 using System.Security;
38 using System.Reflection;
39 using System.Threading;
41 using System.Runtime.ConstrainedExecution;
43 using System.Runtime.InteropServices.ComTypes;
47 namespace System.Runtime.InteropServices
49 public static class Marshal
52 public static readonly int SystemMaxDBCSCharSize = 2; // don't know what this is
53 public static readonly int SystemDefaultCharSize = Environment.IsRunningOnWindows ? 2 : 1;
56 [MethodImplAttribute (MethodImplOptions.InternalCall)]
57 private extern static int AddRefInternal (IntPtr pUnk);
60 public static int AddRef (IntPtr pUnk)
63 if (pUnk == IntPtr.Zero)
64 throw new ArgumentException ("Value cannot be null.", "pUnk");
65 return AddRefInternal (pUnk);
67 throw new NotImplementedException ();
71 [MethodImplAttribute(MethodImplOptions.InternalCall)]
72 public extern static IntPtr AllocCoTaskMem (int cb);
74 [MethodImplAttribute(MethodImplOptions.InternalCall)]
75 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
76 public extern static IntPtr AllocHGlobal (IntPtr cb);
78 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
79 public static IntPtr AllocHGlobal (int cb)
81 return AllocHGlobal ((IntPtr)cb);
85 public static object BindToMoniker (string monikerName)
87 throw new NotImplementedException ();
91 public static void ChangeWrapperHandleStrength (object otp, bool fIsWeak)
93 throw new NotImplementedException ();
96 [MethodImplAttribute(MethodImplOptions.InternalCall)]
97 internal extern static void copy_to_unmanaged (Array source, int startIndex,
98 IntPtr destination, int length);
100 [MethodImplAttribute(MethodImplOptions.InternalCall)]
101 internal extern static void copy_from_unmanaged (IntPtr source, int startIndex,
102 Array destination, int length);
104 public static void Copy (byte[] source, int startIndex, IntPtr destination, int length)
106 copy_to_unmanaged (source, startIndex, destination, length);
109 public static void Copy (char[] source, int startIndex, IntPtr destination, int length)
111 copy_to_unmanaged (source, startIndex, destination, length);
114 public static void Copy (short[] source, int startIndex, IntPtr destination, int length)
116 copy_to_unmanaged (source, startIndex, destination, length);
119 public static void Copy (int[] source, int startIndex, IntPtr destination, int length)
121 copy_to_unmanaged (source, startIndex, destination, length);
124 public static void Copy (long[] source, int startIndex, IntPtr destination, int length)
126 copy_to_unmanaged (source, startIndex, destination, length);
129 public static void Copy (float[] source, int startIndex, IntPtr destination, int length)
131 copy_to_unmanaged (source, startIndex, destination, length);
134 public static void Copy (double[] source, int startIndex, IntPtr destination, int length)
136 copy_to_unmanaged (source, startIndex, destination, length);
139 public static void Copy (IntPtr[] source, int startIndex, IntPtr destination, int length)
141 copy_to_unmanaged (source, startIndex, destination, length);
144 public static void Copy (IntPtr source, byte[] destination, int startIndex, int length)
146 copy_from_unmanaged (source, startIndex, destination, length);
149 public static void Copy (IntPtr source, char[] destination, int startIndex, int length)
151 copy_from_unmanaged (source, startIndex, destination, length);
154 public static void Copy (IntPtr source, short[] destination, int startIndex, int length)
156 copy_from_unmanaged (source, startIndex, destination, length);
159 public static void Copy (IntPtr source, int[] destination, int startIndex, int length)
161 copy_from_unmanaged (source, startIndex, destination, length);
164 public static void Copy (IntPtr source, long[] destination, int startIndex, int length)
166 copy_from_unmanaged (source, startIndex, destination, length);
169 public static void Copy (IntPtr source, float[] destination, int startIndex, int length)
171 copy_from_unmanaged (source, startIndex, destination, length);
174 public static void Copy (IntPtr source, double[] destination, int startIndex, int length)
176 copy_from_unmanaged (source, startIndex, destination, length);
179 public static void Copy (IntPtr source, IntPtr[] destination, int startIndex, int length)
181 copy_from_unmanaged (source, startIndex, destination, length);
184 public static IntPtr CreateAggregatedObject (IntPtr pOuter,
187 throw new NotImplementedException ();
191 public static IntPtr CreateAggregatedObject<T> (IntPtr pOuter, T o) {
192 return CreateAggregatedObject (pOuter, (object)o);
196 #if !FULL_AOT_RUNTIME
197 public static object CreateWrapperOfType (object o, Type t)
199 __ComObject co = o as __ComObject;
201 throw new ArgumentException ("o must derive from __ComObject", "o");
203 throw new ArgumentNullException ("t");
205 Type[] itfs = o.GetType ().GetInterfaces ();
206 foreach (Type itf in itfs) {
207 if (itf.IsImport && co.GetInterface (itf) == IntPtr.Zero)
208 throw new InvalidCastException ();
211 return ComInteropProxy.GetProxy (co.IUnknown, t).GetTransparentProxy ();
215 public static TWrapper CreateWrapperOfType<T, TWrapper> (T o) {
216 return (TWrapper)CreateWrapperOfType ((object)o, typeof (TWrapper));
221 [MethodImplAttribute(MethodImplOptions.InternalCall)]
223 public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
226 public static void DestroyStructure<T> (IntPtr ptr) {
227 DestroyStructure (ptr, typeof (T));
231 [MethodImplAttribute(MethodImplOptions.InternalCall)]
232 public extern static void FreeBSTR (IntPtr ptr);
234 [MethodImplAttribute(MethodImplOptions.InternalCall)]
235 public extern static void FreeCoTaskMem (IntPtr ptr);
237 [MethodImplAttribute(MethodImplOptions.InternalCall)]
238 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
239 public extern static void FreeHGlobal (IntPtr hglobal);
241 static void ClearBSTR (IntPtr ptr)
243 int len = ReadInt32 (ptr, -4);
245 for (int i = 0; i < len; i++)
246 WriteByte (ptr, i, 0);
249 public static void ZeroFreeBSTR (IntPtr s)
255 static void ClearAnsi (IntPtr ptr)
257 for (int i = 0; ReadByte (ptr, i) != 0; i++)
258 WriteByte (ptr, i, 0);
261 static void ClearUnicode (IntPtr ptr)
263 for (int i = 0; ReadInt16 (ptr, i) != 0; i += 2)
264 WriteInt16 (ptr, i, 0);
267 public static void ZeroFreeCoTaskMemAnsi (IntPtr s)
273 public static void ZeroFreeCoTaskMemUnicode (IntPtr s)
279 public static void ZeroFreeGlobalAllocAnsi (IntPtr s)
285 public static void ZeroFreeGlobalAllocUnicode (IntPtr s)
291 #if !FULL_AOT_RUNTIME
292 public static Guid GenerateGuidForType (Type type)
297 public static string GenerateProgIdForType (Type type)
299 IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes (type);
301 foreach (var a in attrs)
303 var dt = a.Constructor.DeclaringType;
304 string name = dt.Name;
305 if (name == "ProgIdAttribute")
307 var args = a.ConstructorArguments;
308 string text = a.ConstructorArguments[0].Value as string;
317 return type.FullName;
321 public static object GetActiveObject (string progID)
323 throw new NotImplementedException ();
327 [MethodImplAttribute (MethodImplOptions.InternalCall)]
328 private extern static IntPtr GetCCW (object o, Type T);
330 private static IntPtr GetComInterfaceForObjectInternal (object o, Type T)
333 return ((__ComObject)o).GetInterface (T);
335 return GetCCW (o, T);
339 public static IntPtr GetComInterfaceForObject (object o, Type T)
342 IntPtr pItf = GetComInterfaceForObjectInternal (o, T);
346 throw new NotImplementedException ();
351 public static IntPtr GetComInterfaceForObject<T, TInterface> (T o) {
352 return GetComInterfaceForObject ((object)o, typeof (T));
357 public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
359 throw new NotImplementedException ();
362 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
363 public static object GetComObjectData (object obj, object key)
365 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
369 [MethodImplAttribute(MethodImplOptions.InternalCall)]
370 private extern static int GetComSlotForMethodInfoInternal (MemberInfo m);
373 public static int GetComSlotForMethodInfo (MemberInfo m)
377 throw new ArgumentNullException ("m");
378 if (!(m is MethodInfo))
379 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
380 if (!m.DeclaringType.IsInterface)
381 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
382 return GetComSlotForMethodInfoInternal (m);
384 throw new NotImplementedException ();
389 public static int GetEndComSlot (Type t)
391 throw new NotImplementedException ();
395 public static int GetExceptionCode()
397 throw new NotImplementedException ();
402 public static IntPtr GetExceptionPointers()
404 throw new NotImplementedException ();
407 public static IntPtr GetHINSTANCE (Module m)
410 throw new ArgumentNullException ("m");
412 return m.GetHINSTANCE ();
414 #endif // !FULL_AOT_RUNTIME
416 #if !FULL_AOT_RUNTIME
417 [MonoTODO ("SetErrorInfo")]
418 public static int GetHRForException (Exception e)
424 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
425 public static int GetHRForLastWin32Error()
427 throw new NotImplementedException ();
430 [MethodImplAttribute (MethodImplOptions.InternalCall)]
431 private extern static IntPtr GetIDispatchForObjectInternal (object o);
433 public static IntPtr GetIDispatchForObject (object o)
435 IntPtr pUnk = GetIDispatchForObjectInternal (o);
436 // Internal method does not AddRef
442 public static IntPtr GetIDispatchForObjectInContext (object o)
444 throw new NotImplementedException ();
448 public static IntPtr GetITypeInfoForType (Type t)
450 throw new NotImplementedException ();
453 [MethodImplAttribute (MethodImplOptions.InternalCall)]
454 private extern static IntPtr GetIUnknownForObjectInternal (object o);
456 public static IntPtr GetIUnknownForObject (object o)
458 IntPtr pUnk = GetIUnknownForObjectInternal (o);
459 // Internal method does not AddRef
465 public static IntPtr GetIUnknownForObjectInContext (object o)
467 throw new NotImplementedException ();
471 [Obsolete ("This method has been deprecated")]
472 public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
474 throw new NotImplementedException ();
478 public static MemberInfo GetMethodInfoForComSlot (Type t, int slot, ref ComMemberType memberType)
480 throw new NotImplementedException ();
483 public static void GetNativeVariantForObject (object obj, IntPtr pDstNativeVariant)
485 Variant vt = new Variant();
487 Marshal.StructureToPtr(vt, pDstNativeVariant, false);
491 public static void GetNativeVariantForObject<T> (T obj, IntPtr pDstNativeVariant) {
492 GetNativeVariantForObject ((object)obj, pDstNativeVariant);
497 [MethodImplAttribute (MethodImplOptions.InternalCall)]
498 private static extern object GetObjectForCCW (IntPtr pUnk);
501 public static object GetObjectForIUnknown (IntPtr pUnk)
504 object obj = GetObjectForCCW (pUnk);
507 ComInteropProxy proxy = ComInteropProxy.GetProxy (pUnk, typeof (__ComObject));
508 obj = proxy.GetTransparentProxy ();
512 throw new NotImplementedException ();
516 public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant)
518 Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
519 return vt.GetValue();
523 public static T GetObjectForNativeVariant<T> (IntPtr pSrcNativeVariant) {
524 Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
525 return (T)vt.GetValue();
529 public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars)
532 throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
533 object[] objects = new object[cVars];
534 for (int i = 0; i < cVars; i++)
535 objects[i] = GetObjectForNativeVariant ((IntPtr)(aSrcNativeVariant.ToInt64 () +
536 i * SizeOf (typeof(Variant))));
541 public static T[] GetObjectsForNativeVariants<T> (IntPtr aSrcNativeVariant, int cVars) {
543 throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
544 T[] objects = new T[cVars];
545 for (int i = 0; i < cVars; i++)
546 objects[i] = GetObjectForNativeVariant<T> ((IntPtr)(aSrcNativeVariant.ToInt64 () +
547 i * SizeOf (typeof(Variant))));
553 public static int GetStartComSlot (Type t)
555 throw new NotImplementedException ();
559 [Obsolete ("This method has been deprecated")]
560 public static Thread GetThreadFromFiberCookie (int cookie)
562 throw new NotImplementedException ();
565 public static object GetTypedObjectForIUnknown (IntPtr pUnk, Type t)
567 ComInteropProxy proxy = new ComInteropProxy (pUnk, t);
568 __ComObject co = (__ComObject)proxy.GetTransparentProxy ();
569 foreach (Type itf in t.GetInterfaces ()) {
570 if ((itf.Attributes & TypeAttributes.Import) == TypeAttributes.Import) {
571 if (co.GetInterface (itf) == IntPtr.Zero)
579 public static Type GetTypeForITypeInfo (IntPtr piTypeInfo)
581 throw new NotImplementedException ();
584 #if !FULL_AOT_RUNTIME
587 public static string GetTypeInfoName (UCOMITypeInfo pTI)
589 throw new NotImplementedException ();
592 public static string GetTypeInfoName (ITypeInfo typeInfo)
594 throw new NotImplementedException ();
599 public static Guid GetTypeLibGuid (UCOMITypeLib pTLB)
601 throw new NotImplementedException ();
605 public static Guid GetTypeLibGuid (ITypeLib typelib)
607 throw new NotImplementedException ();
611 public static Guid GetTypeLibGuidForAssembly (Assembly asm)
613 throw new NotImplementedException ();
618 public static int GetTypeLibLcid (UCOMITypeLib pTLB)
620 throw new NotImplementedException ();
624 public static int GetTypeLibLcid (ITypeLib typelib)
626 throw new NotImplementedException ();
631 public static string GetTypeLibName (UCOMITypeLib pTLB)
633 throw new NotImplementedException ();
637 public static string GetTypeLibName (ITypeLib typelib)
639 throw new NotImplementedException ();
643 public static void GetTypeLibVersionForAssembly (Assembly inputAssembly, out int majorVersion, out int minorVersion)
645 throw new NotImplementedException ();
648 public static object GetUniqueObjectForIUnknown (IntPtr unknown)
650 throw new NotImplementedException ();
655 [Obsolete ("This method has been deprecated")]
656 public static IntPtr GetUnmanagedThunkForManagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
658 throw new NotImplementedException ();
662 [MethodImplAttribute (MethodImplOptions.InternalCall)]
663 public extern static bool IsComObject (object o);
665 public static bool IsComObject (object o)
667 throw new NotImplementedException ();
672 public static bool IsTypeVisibleFromCom (Type t)
674 throw new NotImplementedException ();
678 public static int NumParamBytes (MethodInfo m)
680 throw new NotImplementedException ();
684 [MethodImplAttribute(MethodImplOptions.InternalCall)]
685 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
686 public static extern int GetLastWin32Error();
688 [MethodImplAttribute(MethodImplOptions.InternalCall)]
689 public extern static IntPtr OffsetOf (Type t, string fieldName);
692 public static IntPtr OffsetOf<T> (string fieldName) {
693 return OffsetOf (typeof (T), fieldName);
697 [MethodImplAttribute(MethodImplOptions.InternalCall)]
698 public extern static void Prelink (MethodInfo m);
700 [MethodImplAttribute(MethodImplOptions.InternalCall)]
701 public extern static void PrelinkAll (Type c);
703 [MethodImplAttribute(MethodImplOptions.InternalCall)]
704 public extern static string PtrToStringAnsi (IntPtr ptr);
706 [MethodImplAttribute(MethodImplOptions.InternalCall)]
707 public extern static string PtrToStringAnsi (IntPtr ptr, int len);
709 public static string PtrToStringAuto (IntPtr ptr)
711 return SystemDefaultCharSize == 2
712 ? PtrToStringUni (ptr) : PtrToStringAnsi (ptr);
715 public static string PtrToStringAuto (IntPtr ptr, int len)
717 return SystemDefaultCharSize == 2
718 ? PtrToStringUni (ptr, len) : PtrToStringAnsi (ptr, len);
721 [MethodImplAttribute(MethodImplOptions.InternalCall)]
722 public extern static string PtrToStringUni (IntPtr ptr);
724 [MethodImplAttribute(MethodImplOptions.InternalCall)]
725 public extern static string PtrToStringUni (IntPtr ptr, int len);
728 [MethodImplAttribute(MethodImplOptions.InternalCall)]
729 public extern static string PtrToStringBSTR (IntPtr ptr);
731 public static string PtrToStringBSTR (IntPtr ptr)
733 throw new NotImplementedException ();
737 [MethodImplAttribute(MethodImplOptions.InternalCall)]
739 public extern static void PtrToStructure (IntPtr ptr, object structure);
741 [MethodImplAttribute(MethodImplOptions.InternalCall)]
743 public extern static object PtrToStructure (IntPtr ptr, Type structureType);
746 public static void PtrToStructure<T> (IntPtr ptr, T structure) {
747 PtrToStructure (ptr, (object)structure);
750 public static object PtrToStructure<T> (IntPtr ptr) {
751 return PtrToStructure (ptr, typeof (T));
756 [MethodImplAttribute (MethodImplOptions.InternalCall)]
757 private extern static int QueryInterfaceInternal (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
760 public static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv)
763 if (pUnk == IntPtr.Zero)
764 throw new ArgumentException ("Value cannot be null.", "pUnk");
765 return QueryInterfaceInternal (pUnk, ref iid, out ppv);
767 throw new NotImplementedException ();
771 public static byte ReadByte (IntPtr ptr)
778 public static byte ReadByte (IntPtr ptr, int ofs) {
780 return *((byte*)ptr + ofs);
785 [SuppressUnmanagedCodeSecurity]
786 public static byte ReadByte ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
788 throw new NotImplementedException ();
791 public unsafe static short ReadInt16 (IntPtr ptr)
793 byte *addr = (byte *) ptr;
795 // The mono JIT can't inline this due to the hight number of calls
796 // return ReadInt16 (ptr, 0);
798 if (((uint)addr & 1) == 0)
799 return *(short*)addr;
802 String.memcpy ((byte*)&s, (byte*)ptr, 2);
806 public unsafe static short ReadInt16 (IntPtr ptr, int ofs)
808 byte *addr = ((byte *) ptr) + ofs;
810 if (((uint) addr & 1) == 0)
811 return *(short*)addr;
814 String.memcpy ((byte*)&s, addr, 2);
819 [SuppressUnmanagedCodeSecurity]
820 public static short ReadInt16 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
822 throw new NotImplementedException ();
825 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
826 public unsafe static int ReadInt32 (IntPtr ptr)
828 byte *addr = (byte *) ptr;
830 if (((uint)addr & 3) == 0)
834 String.memcpy ((byte*)&s, addr, 4);
838 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
839 public unsafe static int ReadInt32 (IntPtr ptr, int ofs)
841 byte *addr = ((byte *) ptr) + ofs;
843 if ((((int) addr) & 3) == 0)
847 String.memcpy ((byte*)&s, addr, 4);
852 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
854 [SuppressUnmanagedCodeSecurity]
855 public static int ReadInt32 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
857 throw new NotImplementedException ();
860 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
861 public unsafe static long ReadInt64 (IntPtr ptr)
863 byte *addr = (byte *) ptr;
865 // The real alignment might be 4 on some platforms, but this is just an optimization,
866 // so it doesn't matter.
867 if (((uint) addr & 7) == 0)
871 String.memcpy ((byte*)&s, addr, 8);
875 public unsafe static long ReadInt64 (IntPtr ptr, int ofs)
877 byte *addr = ((byte *) ptr) + ofs;
879 if (((uint) addr & 7) == 0)
883 String.memcpy ((byte*)&s, addr, 8);
887 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
889 [SuppressUnmanagedCodeSecurity]
890 public static long ReadInt64 ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
892 throw new NotImplementedException ();
895 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
896 public static IntPtr ReadIntPtr (IntPtr ptr)
898 if (IntPtr.Size == 4)
899 return (IntPtr)ReadInt32 (ptr);
901 return (IntPtr)ReadInt64 (ptr);
904 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
905 public static IntPtr ReadIntPtr (IntPtr ptr, int ofs)
907 if (IntPtr.Size == 4)
908 return (IntPtr)ReadInt32 (ptr, ofs);
910 return (IntPtr)ReadInt64 (ptr, ofs);
913 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
915 public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
917 throw new NotImplementedException ();
920 [MethodImplAttribute (MethodImplOptions.InternalCall)]
921 public extern static IntPtr ReAllocCoTaskMem (IntPtr pv, int cb);
923 [MethodImplAttribute(MethodImplOptions.InternalCall)]
924 public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
927 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
928 [MethodImplAttribute (MethodImplOptions.InternalCall)]
929 private extern static int ReleaseInternal (IntPtr pUnk);
932 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
933 public static int Release (IntPtr pUnk)
936 if (pUnk == IntPtr.Zero)
937 throw new ArgumentException ("Value cannot be null.", "pUnk");
939 return ReleaseInternal (pUnk);
941 throw new NotImplementedException ();
945 #if !FULL_AOT_RUNTIME
946 [MethodImplAttribute (MethodImplOptions.InternalCall)]
947 private extern static int ReleaseComObjectInternal (object co);
949 public static int ReleaseComObject (object o)
952 throw new ArgumentException ("Value cannot be null.", "o");
953 if (!IsComObject (o))
954 throw new ArgumentException ("Value must be a Com object.", "o");
955 return ReleaseComObjectInternal (o);
960 public static void ReleaseThreadCache()
962 throw new NotImplementedException ();
965 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
966 public static bool SetComObjectData (object obj, object key, object data)
968 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
973 public static int SizeOf (object structure)
975 return SizeOf (structure.GetType ());
978 [MethodImplAttribute(MethodImplOptions.InternalCall)]
979 public extern static int SizeOf (Type t);
982 public static int SizeOf<T> () {
983 return SizeOf (typeof (T));
986 public static int SizeOf<T> (T structure) {
987 return SizeOf (structure.GetType ());
991 [MethodImplAttribute(MethodImplOptions.InternalCall)]
992 public extern static IntPtr StringToBSTR (string s);
995 // I believe this is wrong, because in Mono and in P/Invoke
996 // we treat "Ansi" conversions as UTF-8 conversions, while
997 // this one does not do this
999 public static IntPtr StringToCoTaskMemAnsi (string s)
1001 int length = s.Length + 1;
1002 IntPtr ctm = AllocCoTaskMem (length);
1004 byte[] asBytes = new byte[length];
1005 for (int i = 0; i < s.Length; i++)
1006 asBytes[i] = (byte)s[i];
1007 asBytes[s.Length] = 0;
1009 copy_to_unmanaged (asBytes, 0, ctm, length);
1013 public static IntPtr StringToCoTaskMemAuto (string s)
1015 return SystemDefaultCharSize == 2
1016 ? StringToCoTaskMemUni (s) : StringToCoTaskMemAnsi (s);
1019 public static IntPtr StringToCoTaskMemUni (string s)
1021 int length = s.Length + 1;
1022 IntPtr ctm = AllocCoTaskMem (length * 2);
1024 char[] asChars = new char[length];
1025 s.CopyTo (0, asChars, 0, s.Length);
1026 asChars[s.Length] = '\0';
1028 copy_to_unmanaged (asChars, 0, ctm, length);
1032 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1033 public extern static IntPtr StringToHGlobalAnsi (string s);
1035 public static IntPtr StringToHGlobalAuto (string s)
1037 return SystemDefaultCharSize == 2
1038 ? StringToHGlobalUni (s) : StringToHGlobalAnsi (s);
1041 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1042 public extern static IntPtr StringToHGlobalUni (string s);
1044 public static IntPtr SecureStringToBSTR (SecureString s)
1047 throw new ArgumentNullException ("s");
1049 IntPtr ctm = AllocCoTaskMem ((len+1) * 2 + 4);
1050 byte [] buffer = null;
1051 WriteInt32 (ctm, 0, len*2);
1053 buffer = s.GetBuffer ();
1055 for (int i = 0; i < len; i++)
1056 WriteInt16 (ctm, 4 + (i * 2), (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
1057 WriteInt16 (ctm, 4 + buffer.Length, 0);
1060 for (int i = buffer.Length; i > 0; ){
1065 return (IntPtr) ((long)ctm + 4);
1068 public static IntPtr SecureStringToCoTaskMemAnsi (SecureString s)
1071 throw new ArgumentNullException ("s");
1073 IntPtr ctm = AllocCoTaskMem (len + 1);
1074 byte [] copy = new byte [len+1];
1077 byte [] buffer = s.GetBuffer ();
1079 for (; i < len; i++, j += 2){
1080 copy [i] = buffer [j+1];
1085 copy_to_unmanaged (copy, 0, ctm, len+1);
1087 // Ensure that we clear the buffer.
1088 for (int i = len; i > 0; ){
1096 public static IntPtr SecureStringToCoTaskMemUnicode (SecureString s)
1099 throw new ArgumentNullException ("s");
1101 IntPtr ctm = AllocCoTaskMem (len * 2 + 2);
1102 byte [] buffer = null;
1104 buffer = s.GetBuffer ();
1105 for (int i = 0; i < len; i++)
1106 WriteInt16 (ctm, i * 2, (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
1107 WriteInt16 (ctm, buffer.Length, 0);
1110 for (int i = buffer.Length; i > 0; ){
1118 public static IntPtr SecureStringToGlobalAllocAnsi (SecureString s)
1121 throw new ArgumentNullException ("s");
1122 return SecureStringToCoTaskMemAnsi (s);
1125 public static IntPtr SecureStringToGlobalAllocUnicode (SecureString s)
1128 throw new ArgumentNullException ("s");
1129 return SecureStringToCoTaskMemUnicode (s);
1132 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1134 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1135 public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
1138 public static void StructureToPtr<T> (T structure, IntPtr ptr, bool fDeleteOld) {
1139 StructureToPtr ((object)structure, ptr, fDeleteOld);
1143 public static void ThrowExceptionForHR (int errorCode) {
1144 Exception ex = GetExceptionForHR (errorCode);
1149 public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo) {
1150 Exception ex = GetExceptionForHR (errorCode, errorInfo);
1155 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1156 public extern static IntPtr UnsafeAddrOfPinnedArrayElement (Array arr, int index);
1159 public static IntPtr UnsafeAddrOfPinnedArrayElement<T> (T[] arr, int index) {
1160 return UnsafeAddrOfPinnedArrayElement ((Array)arr, index);
1164 public static void WriteByte (IntPtr ptr, byte val)
1171 public static void WriteByte (IntPtr ptr, int ofs, byte val) {
1173 *(byte*)(IntPtr.Add (ptr, ofs)) = val;
1178 [SuppressUnmanagedCodeSecurity]
1179 public static void WriteByte ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, byte val)
1181 throw new NotImplementedException ();
1184 public static unsafe void WriteInt16 (IntPtr ptr, short val)
1186 byte *addr = (byte *) ptr;
1188 if (((uint)addr & 1) == 0)
1189 *(short*)addr = val;
1191 String.memcpy (addr, (byte*)&val, 2);
1194 public static unsafe void WriteInt16 (IntPtr ptr, int ofs, short val)
1196 byte *addr = ((byte *) ptr) + ofs;
1198 if (((uint)addr & 1) == 0)
1199 *(short*)addr = val;
1201 String.memcpy (addr, (byte*)&val, 2);
1206 [SuppressUnmanagedCodeSecurity]
1207 public static void WriteInt16 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, short val)
1209 throw new NotImplementedException ();
1212 public static void WriteInt16 (IntPtr ptr, char val)
1214 WriteInt16 (ptr, 0, (short)val);
1217 public static void WriteInt16 (IntPtr ptr, int ofs, char val)
1219 WriteInt16 (ptr, ofs, (short)val);
1223 public static void WriteInt16([In, Out] object ptr, int ofs, char val)
1225 throw new NotImplementedException ();
1228 public static unsafe void WriteInt32 (IntPtr ptr, int val)
1230 byte *addr = (byte *) ptr;
1232 if (((uint)addr & 3) == 0)
1235 String.memcpy (addr, (byte*)&val, 4);
1239 public unsafe static void WriteInt32 (IntPtr ptr, int ofs, int val)
1241 byte *addr = ((byte *) ptr) + ofs;
1243 if (((uint)addr & 3) == 0)
1246 String.memcpy (addr, (byte*)&val, 4);
1251 [SuppressUnmanagedCodeSecurity]
1252 public static void WriteInt32([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, int val)
1254 throw new NotImplementedException ();
1257 public static unsafe void WriteInt64 (IntPtr ptr, long val)
1259 byte *addr = (byte *) ptr;
1261 // The real alignment might be 4 on some platforms, but this is just an optimization,
1262 // so it doesn't matter.
1263 if (((uint)addr & 7) == 0)
1266 String.memcpy (addr, (byte*)&val, 8);
1269 public static unsafe void WriteInt64 (IntPtr ptr, int ofs, long val)
1271 byte *addr = ((byte *) ptr) + ofs;
1273 // The real alignment might be 4 on some platforms, but this is just an optimization,
1274 // so it doesn't matter.
1275 if (((uint)addr & 7) == 0)
1278 String.memcpy (addr, (byte*)&val, 8);
1282 [SuppressUnmanagedCodeSecurity]
1283 public static void WriteInt64 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, long val)
1285 throw new NotImplementedException ();
1288 public static void WriteIntPtr (IntPtr ptr, IntPtr val)
1290 if (IntPtr.Size == 4)
1291 WriteInt32 (ptr, (int)val);
1293 WriteInt64 (ptr, (long)val);
1296 public static void WriteIntPtr (IntPtr ptr, int ofs, IntPtr val)
1298 if (IntPtr.Size == 4)
1299 WriteInt32 (ptr, ofs, (int)val);
1301 WriteInt64 (ptr, ofs, (long)val);
1305 public static void WriteIntPtr([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val)
1307 throw new NotImplementedException ();
1310 public static Exception GetExceptionForHR (int errorCode) {
1311 return GetExceptionForHR (errorCode, IntPtr.Zero);
1314 public static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo) {
1316 const int E_OUTOFMEMORY = unchecked ((int)0x8007000EL);
1317 const int E_INVALIDARG = unchecked ((int)0X80070057);
1322 return new OutOfMemoryException ();
1324 return new ArgumentException ();
1327 return new COMException ("", errorCode);
1331 #if !FULL_AOT_RUNTIME
1332 public static int FinalReleaseComObject (object o)
1334 while (ReleaseComObject (o) != 0);
1339 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1340 private static extern Delegate GetDelegateForFunctionPointerInternal (IntPtr ptr, Type t);
1342 public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t)
1345 throw new ArgumentNullException ("t");
1346 if (!t.IsSubclassOf (typeof (MulticastDelegate)) || (t == typeof (MulticastDelegate)))
1347 throw new ArgumentException ("Type is not a delegate", "t");
1348 if (t.IsGenericType)
1349 throw new ArgumentException ("The specified Type must not be a generic type definition.");
1350 if (ptr == IntPtr.Zero)
1351 throw new ArgumentNullException ("ptr");
1353 return GetDelegateForFunctionPointerInternal (ptr, t);
1357 public static Delegate GetDelegateForFunctionPointer<T> (IntPtr ptr) {
1358 return GetDelegateForFunctionPointer (ptr, typeof (T));
1362 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1363 private static extern IntPtr GetFunctionPointerForDelegateInternal (Delegate d);
1365 public static IntPtr GetFunctionPointerForDelegate (Delegate d)
1368 throw new ArgumentNullException ("d");
1370 return GetFunctionPointerForDelegateInternal (d);
1374 public static IntPtr GetFunctionPointerForDelegate<TDelegate> (TDelegate d) {
1376 throw new ArgumentNullException ("d");
1378 return GetFunctionPointerForDelegateInternal ((Delegate)(object)d);