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;
44 using System.Runtime.InteropServices.ComTypes;
49 namespace System.Runtime.InteropServices
51 public static class Marshal
54 public static readonly int SystemMaxDBCSCharSize = 2; // don't know what this is
55 public static readonly int SystemDefaultCharSize = Environment.OSVersion.Platform == PlatformID.Win32NT ? 2 : 1;
58 [MethodImplAttribute (MethodImplOptions.InternalCall)]
59 private extern static int AddRefInternal (IntPtr pUnk);
62 public static int AddRef (IntPtr pUnk)
65 if (pUnk == IntPtr.Zero)
66 throw new ArgumentException ("Value cannot be null.", "pUnk");
67 return AddRefInternal (pUnk);
69 throw new NotImplementedException ();
73 [MethodImplAttribute(MethodImplOptions.InternalCall)]
74 public extern static IntPtr AllocCoTaskMem (int cb);
76 [MethodImplAttribute(MethodImplOptions.InternalCall)]
77 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
78 public extern static IntPtr AllocHGlobal (IntPtr cb);
80 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
81 public static IntPtr AllocHGlobal (int cb)
83 return AllocHGlobal ((IntPtr)cb);
87 public static object BindToMoniker (string monikerName)
89 throw new NotImplementedException ();
93 public static void ChangeWrapperHandleStrength (object otp, bool fIsWeak)
95 throw new NotImplementedException ();
98 [MethodImplAttribute(MethodImplOptions.InternalCall)]
99 internal extern static void copy_to_unmanaged (Array source, int startIndex,
100 IntPtr destination, int length);
102 [MethodImplAttribute(MethodImplOptions.InternalCall)]
103 internal extern static void copy_from_unmanaged (IntPtr source, int startIndex,
104 Array destination, int length);
106 public static void Copy (byte[] source, int startIndex, IntPtr destination, int length)
108 copy_to_unmanaged (source, startIndex, destination, length);
111 public static void Copy (char[] source, int startIndex, IntPtr destination, int length)
113 copy_to_unmanaged (source, startIndex, destination, length);
116 public static void Copy (short[] source, int startIndex, IntPtr destination, int length)
118 copy_to_unmanaged (source, startIndex, destination, length);
121 public static void Copy (int[] source, int startIndex, IntPtr destination, int length)
123 copy_to_unmanaged (source, startIndex, destination, length);
126 public static void Copy (long[] source, int startIndex, IntPtr destination, int length)
128 copy_to_unmanaged (source, startIndex, destination, length);
131 public static void Copy (float[] source, int startIndex, IntPtr destination, int length)
133 copy_to_unmanaged (source, startIndex, destination, length);
136 public static void Copy (double[] source, int startIndex, IntPtr destination, int length)
138 copy_to_unmanaged (source, startIndex, destination, length);
141 public static void Copy (IntPtr[] source, int startIndex, IntPtr destination, int length)
143 copy_to_unmanaged (source, startIndex, destination, length);
146 public static void Copy (IntPtr source, byte[] destination, int startIndex, int length)
148 copy_from_unmanaged (source, startIndex, destination, length);
151 public static void Copy (IntPtr source, char[] destination, int startIndex, int length)
153 copy_from_unmanaged (source, startIndex, destination, length);
156 public static void Copy (IntPtr source, short[] destination, int startIndex, int length)
158 copy_from_unmanaged (source, startIndex, destination, length);
161 public static void Copy (IntPtr source, int[] destination, int startIndex, int length)
163 copy_from_unmanaged (source, startIndex, destination, length);
166 public static void Copy (IntPtr source, long[] destination, int startIndex, int length)
168 copy_from_unmanaged (source, startIndex, destination, length);
171 public static void Copy (IntPtr source, float[] destination, int startIndex, int length)
173 copy_from_unmanaged (source, startIndex, destination, length);
176 public static void Copy (IntPtr source, double[] destination, int startIndex, int length)
178 copy_from_unmanaged (source, startIndex, destination, length);
181 public static void Copy (IntPtr source, IntPtr[] destination, int startIndex, int length)
183 copy_from_unmanaged (source, startIndex, destination, length);
186 public static IntPtr CreateAggregatedObject (IntPtr pOuter,
189 throw new NotImplementedException ();
192 #if !FULL_AOT_RUNTIME && !MOONLIGHT
193 public static object CreateWrapperOfType (object o, Type t)
195 __ComObject co = o as __ComObject;
197 throw new ArgumentException ("o must derive from __ComObject", "o");
199 throw new ArgumentNullException ("t");
201 Type[] itfs = o.GetType ().GetInterfaces ();
202 foreach (Type itf in itfs) {
203 if (itf.IsImport && co.GetInterface (itf) == IntPtr.Zero)
204 throw new InvalidCastException ();
207 return ComInteropProxy.GetProxy (co.IUnknown, t).GetTransparentProxy ();
211 [MethodImplAttribute(MethodImplOptions.InternalCall)]
213 public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
215 [MethodImplAttribute(MethodImplOptions.InternalCall)]
216 public extern static void FreeBSTR (IntPtr ptr);
218 [MethodImplAttribute(MethodImplOptions.InternalCall)]
219 public extern static void FreeCoTaskMem (IntPtr ptr);
221 [MethodImplAttribute(MethodImplOptions.InternalCall)]
222 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
223 public extern static void FreeHGlobal (IntPtr hglobal);
225 static void ClearBSTR (IntPtr ptr)
227 int len = ReadInt32 (ptr, -4);
229 for (int i = 0; i < len; i++)
230 WriteByte (ptr, i, 0);
233 public static void ZeroFreeBSTR (IntPtr s)
239 static void ClearAnsi (IntPtr ptr)
241 for (int i = 0; ReadByte (ptr, i) != 0; i++)
242 WriteByte (ptr, i, 0);
245 static void ClearUnicode (IntPtr ptr)
247 for (int i = 0; ReadInt16 (ptr, i) != 0; i += 2)
248 WriteInt16 (ptr, i, 0);
251 public static void ZeroFreeCoTaskMemAnsi (IntPtr s)
257 public static void ZeroFreeCoTaskMemUnicode (IntPtr s)
263 public static void ZeroFreeGlobalAllocAnsi (IntPtr s)
269 public static void ZeroFreeGlobalAllocUnicode (IntPtr s)
275 #if !FULL_AOT_RUNTIME && !MOONLIGHT
276 public static Guid GenerateGuidForType (Type type)
281 public static string GenerateProgIdForType (Type type)
283 IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes (type);
285 foreach (var a in attrs)
287 var dt = a.Constructor.DeclaringType;
288 string name = dt.Name;
289 if (name == "ProgIdAttribute")
291 var args = a.ConstructorArguments;
292 string text = a.ConstructorArguments[0].Value as string;
301 return type.FullName;
305 public static object GetActiveObject (string progID)
307 throw new NotImplementedException ();
311 [MethodImplAttribute (MethodImplOptions.InternalCall)]
312 private extern static IntPtr GetCCW (object o, Type T);
314 private static IntPtr GetComInterfaceForObjectInternal (object o, Type T)
317 return ((__ComObject)o).GetInterface (T);
319 return GetCCW (o, T);
323 public static IntPtr GetComInterfaceForObject (object o, Type T)
326 IntPtr pItf = GetComInterfaceForObjectInternal (o, T);
330 throw new NotImplementedException ();
335 public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
337 throw new NotImplementedException ();
340 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
341 public static object GetComObjectData (object obj, object key)
343 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
347 [MethodImplAttribute(MethodImplOptions.InternalCall)]
348 private extern static int GetComSlotForMethodInfoInternal (MemberInfo m);
351 public static int GetComSlotForMethodInfo (MemberInfo m)
355 throw new ArgumentNullException ("m");
356 if (!(m is MethodInfo))
357 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
358 if (!m.DeclaringType.IsInterface)
359 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
360 return GetComSlotForMethodInfoInternal (m);
362 throw new NotImplementedException ();
367 public static int GetEndComSlot (Type t)
369 throw new NotImplementedException ();
373 public static int GetExceptionCode()
375 throw new NotImplementedException ();
380 public static IntPtr GetExceptionPointers()
382 throw new NotImplementedException ();
385 public static IntPtr GetHINSTANCE (Module m)
388 throw new ArgumentNullException ("m");
390 return m.GetHINSTANCE ();
394 #if !FULL_AOT_RUNTIME
395 [MonoTODO ("SetErrorInfo")]
396 public static int GetHRForException (Exception e)
402 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
403 public static int GetHRForLastWin32Error()
405 throw new NotImplementedException ();
408 [MethodImplAttribute (MethodImplOptions.InternalCall)]
409 private extern static IntPtr GetIDispatchForObjectInternal (object o);
411 public static IntPtr GetIDispatchForObject (object o)
413 IntPtr pUnk = GetIDispatchForObjectInternal (o);
414 // Internal method does not AddRef
420 public static IntPtr GetIDispatchForObjectInContext (object o)
422 throw new NotImplementedException ();
426 public static IntPtr GetITypeInfoForType (Type t)
428 throw new NotImplementedException ();
431 [MethodImplAttribute (MethodImplOptions.InternalCall)]
432 private extern static IntPtr GetIUnknownForObjectInternal (object o);
434 public static IntPtr GetIUnknownForObject (object o)
436 IntPtr pUnk = GetIUnknownForObjectInternal (o);
437 // Internal method does not AddRef
443 public static IntPtr GetIUnknownForObjectInContext (object o)
445 throw new NotImplementedException ();
449 [Obsolete ("This method has been deprecated")]
450 public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
452 throw new NotImplementedException ();
456 public static MemberInfo GetMethodInfoForComSlot (Type t, int slot, ref ComMemberType memberType)
458 throw new NotImplementedException ();
461 public static void GetNativeVariantForObject (object obj, IntPtr pDstNativeVariant)
463 Variant vt = new Variant();
465 Marshal.StructureToPtr(vt, pDstNativeVariant, false);
469 [MethodImplAttribute (MethodImplOptions.InternalCall)]
470 private static extern object GetObjectForCCW (IntPtr pUnk);
473 public static object GetObjectForIUnknown (IntPtr pUnk)
476 object obj = GetObjectForCCW (pUnk);
479 ComInteropProxy proxy = ComInteropProxy.GetProxy (pUnk, typeof (__ComObject));
480 obj = proxy.GetTransparentProxy ();
484 throw new NotImplementedException ();
488 public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant)
490 Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
491 return vt.GetValue();
494 public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars)
497 throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
498 object[] objects = new object[cVars];
499 for (int i = 0; i < cVars; i++)
500 objects[i] = GetObjectForNativeVariant ((IntPtr)(aSrcNativeVariant.ToInt64 () +
501 i * SizeOf (typeof(Variant))));
506 public static int GetStartComSlot (Type t)
508 throw new NotImplementedException ();
512 [Obsolete ("This method has been deprecated")]
513 public static Thread GetThreadFromFiberCookie (int cookie)
515 throw new NotImplementedException ();
518 public static object GetTypedObjectForIUnknown (IntPtr pUnk, Type t)
520 ComInteropProxy proxy = new ComInteropProxy (pUnk, t);
521 __ComObject co = (__ComObject)proxy.GetTransparentProxy ();
522 foreach (Type itf in t.GetInterfaces ()) {
523 if ((itf.Attributes & TypeAttributes.Import) == TypeAttributes.Import) {
524 if (co.GetInterface (itf) == IntPtr.Zero)
532 public static Type GetTypeForITypeInfo (IntPtr piTypeInfo)
534 throw new NotImplementedException ();
537 #if !FULL_AOT_RUNTIME
540 public static string GetTypeInfoName (UCOMITypeInfo pTI)
542 throw new NotImplementedException ();
545 public static string GetTypeInfoName (ITypeInfo typeInfo)
547 throw new NotImplementedException ();
552 public static Guid GetTypeLibGuid (UCOMITypeLib pTLB)
554 throw new NotImplementedException ();
558 public static Guid GetTypeLibGuid (ITypeLib typelib)
560 throw new NotImplementedException ();
564 public static Guid GetTypeLibGuidForAssembly (Assembly asm)
566 throw new NotImplementedException ();
571 public static int GetTypeLibLcid (UCOMITypeLib pTLB)
573 throw new NotImplementedException ();
577 public static int GetTypeLibLcid (ITypeLib typelib)
579 throw new NotImplementedException ();
584 public static string GetTypeLibName (UCOMITypeLib pTLB)
586 throw new NotImplementedException ();
590 public static string GetTypeLibName (ITypeLib typelib)
592 throw new NotImplementedException ();
596 public static void GetTypeLibVersionForAssembly (Assembly inputAssembly, out int majorVersion, out int minorVersion)
598 throw new NotImplementedException ();
601 public static object GetUniqueObjectForIUnknown (IntPtr unknown)
603 throw new NotImplementedException ();
608 [Obsolete ("This method has been deprecated")]
609 public static IntPtr GetUnmanagedThunkForManagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
611 throw new NotImplementedException ();
615 [MethodImplAttribute (MethodImplOptions.InternalCall)]
616 public extern static bool IsComObject (object o);
618 public static bool IsComObject (object o)
620 throw new NotImplementedException ();
625 public static bool IsTypeVisibleFromCom (Type t)
627 throw new NotImplementedException ();
631 public static int NumParamBytes (MethodInfo m)
633 throw new NotImplementedException ();
638 [MethodImplAttribute(MethodImplOptions.InternalCall)]
639 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
640 public static extern int GetLastWin32Error();
642 [MethodImplAttribute(MethodImplOptions.InternalCall)]
643 public extern static IntPtr OffsetOf (Type t, string fieldName);
645 [MethodImplAttribute(MethodImplOptions.InternalCall)]
646 public extern static void Prelink (MethodInfo m);
648 [MethodImplAttribute(MethodImplOptions.InternalCall)]
649 public extern static void PrelinkAll (Type c);
651 [MethodImplAttribute(MethodImplOptions.InternalCall)]
652 public extern static string PtrToStringAnsi (IntPtr ptr);
654 [MethodImplAttribute(MethodImplOptions.InternalCall)]
655 public extern static string PtrToStringAnsi (IntPtr ptr, int len);
657 public static string PtrToStringAuto (IntPtr ptr)
659 return SystemDefaultCharSize == 2
660 ? PtrToStringUni (ptr) : PtrToStringAnsi (ptr);
663 public static string PtrToStringAuto (IntPtr ptr, int len)
665 return SystemDefaultCharSize == 2
666 ? PtrToStringUni (ptr, len) : PtrToStringAnsi (ptr, len);
669 [MethodImplAttribute(MethodImplOptions.InternalCall)]
670 public extern static string PtrToStringUni (IntPtr ptr);
672 [MethodImplAttribute(MethodImplOptions.InternalCall)]
673 public extern static string PtrToStringUni (IntPtr ptr, int len);
676 [MethodImplAttribute(MethodImplOptions.InternalCall)]
677 public extern static string PtrToStringBSTR (IntPtr ptr);
679 public static string PtrToStringBSTR (IntPtr ptr)
681 throw new NotImplementedException ();
685 [MethodImplAttribute(MethodImplOptions.InternalCall)]
687 public extern static void PtrToStructure (IntPtr ptr, object structure);
689 [MethodImplAttribute(MethodImplOptions.InternalCall)]
691 public extern static object PtrToStructure (IntPtr ptr, Type structureType);
694 [MethodImplAttribute (MethodImplOptions.InternalCall)]
695 private extern static int QueryInterfaceInternal (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
698 public static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv)
701 if (pUnk == IntPtr.Zero)
702 throw new ArgumentException ("Value cannot be null.", "pUnk");
703 return QueryInterfaceInternal (pUnk, ref iid, out ppv);
705 throw new NotImplementedException ();
709 public static byte ReadByte (IntPtr ptr)
711 return ReadByte (ptr, 0);
714 [MethodImplAttribute(MethodImplOptions.InternalCall)]
715 public extern static byte ReadByte (IntPtr ptr, int ofs);
718 [SuppressUnmanagedCodeSecurity]
719 public static byte ReadByte ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
721 throw new NotImplementedException ();
724 public static short ReadInt16 (IntPtr ptr)
726 return ReadInt16 (ptr, 0);
729 [MethodImplAttribute(MethodImplOptions.InternalCall)]
730 public extern static short ReadInt16 (IntPtr ptr, int ofs);
733 [SuppressUnmanagedCodeSecurity]
734 public static short ReadInt16 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
736 throw new NotImplementedException ();
739 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
740 public static int ReadInt32 (IntPtr ptr)
742 return ReadInt32 (ptr, 0);
745 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
746 [MethodImplAttribute(MethodImplOptions.InternalCall)]
747 public extern static int ReadInt32 (IntPtr ptr, int ofs);
749 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
751 [SuppressUnmanagedCodeSecurity]
752 public static int ReadInt32 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
754 throw new NotImplementedException ();
757 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
758 public static long ReadInt64 (IntPtr ptr)
760 return ReadInt64 (ptr, 0);
763 [MethodImplAttribute(MethodImplOptions.InternalCall)]
764 public extern static long ReadInt64 (IntPtr ptr, int ofs);
766 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
768 [SuppressUnmanagedCodeSecurity]
769 public static long ReadInt64 ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
771 throw new NotImplementedException ();
774 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
775 public static IntPtr ReadIntPtr (IntPtr ptr)
777 return ReadIntPtr (ptr, 0);
780 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
781 [MethodImplAttribute(MethodImplOptions.InternalCall)]
782 public extern static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
784 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
786 public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
788 throw new NotImplementedException ();
791 [MethodImplAttribute (MethodImplOptions.InternalCall)]
792 public extern static IntPtr ReAllocCoTaskMem (IntPtr pv, int cb);
794 [MethodImplAttribute(MethodImplOptions.InternalCall)]
795 public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
798 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
799 [MethodImplAttribute (MethodImplOptions.InternalCall)]
800 private extern static int ReleaseInternal (IntPtr pUnk);
803 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
804 public static int Release (IntPtr pUnk)
807 if (pUnk == IntPtr.Zero)
808 throw new ArgumentException ("Value cannot be null.", "pUnk");
810 return ReleaseInternal (pUnk);
812 throw new NotImplementedException ();
816 #if !FULL_AOT_RUNTIME
818 [MethodImplAttribute (MethodImplOptions.InternalCall)]
819 private extern static int ReleaseComObjectInternal (object co);
821 public static int ReleaseComObject (object o)
824 throw new ArgumentException ("Value cannot be null.", "o");
825 if (!IsComObject (o))
826 throw new ArgumentException ("Value must be a Com object.", "o");
827 return ReleaseComObjectInternal (o);
832 public static void ReleaseThreadCache()
834 throw new NotImplementedException ();
837 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
838 public static bool SetComObjectData (object obj, object key, object data)
840 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
846 public static int SizeOf (object structure)
848 return SizeOf (structure.GetType ());
851 [MethodImplAttribute(MethodImplOptions.InternalCall)]
852 public extern static int SizeOf (Type t);
854 [MethodImplAttribute(MethodImplOptions.InternalCall)]
855 public extern static IntPtr StringToBSTR (string s);
858 // I believe this is wrong, because in Mono and in P/Invoke
859 // we treat "Ansi" conversions as UTF-8 conversions, while
860 // this one does not do this
862 public static IntPtr StringToCoTaskMemAnsi (string s)
864 int length = s.Length + 1;
865 IntPtr ctm = AllocCoTaskMem (length);
867 byte[] asBytes = new byte[length];
868 for (int i = 0; i < s.Length; i++)
869 asBytes[i] = (byte)s[i];
870 asBytes[s.Length] = 0;
872 copy_to_unmanaged (asBytes, 0, ctm, length);
876 public static IntPtr StringToCoTaskMemAuto (string s)
878 return SystemDefaultCharSize == 2
879 ? StringToCoTaskMemUni (s) : StringToCoTaskMemAnsi (s);
882 public static IntPtr StringToCoTaskMemUni (string s)
884 int length = s.Length + 1;
885 IntPtr ctm = AllocCoTaskMem (length * 2);
887 char[] asChars = new char[length];
888 s.CopyTo (0, asChars, 0, s.Length);
889 asChars[s.Length] = '\0';
891 copy_to_unmanaged (asChars, 0, ctm, length);
895 [MethodImplAttribute(MethodImplOptions.InternalCall)]
896 public extern static IntPtr StringToHGlobalAnsi (string s);
898 public static IntPtr StringToHGlobalAuto (string s)
900 return SystemDefaultCharSize == 2
901 ? StringToHGlobalUni (s) : StringToHGlobalAnsi (s);
904 [MethodImplAttribute(MethodImplOptions.InternalCall)]
905 public extern static IntPtr StringToHGlobalUni (string s);
908 public static IntPtr SecureStringToBSTR (SecureString s)
911 throw new ArgumentNullException ("s");
913 IntPtr ctm = AllocCoTaskMem ((len+1) * 2 + 4);
914 byte [] buffer = null;
915 WriteInt32 (ctm, 0, len*2);
917 buffer = s.GetBuffer ();
919 for (int i = 0; i < len; i++)
920 WriteInt16 (ctm, 4 + (i * 2), (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
921 WriteInt16 (ctm, 4 + buffer.Length, 0);
924 for (int i = buffer.Length; i > 0; ){
929 return (IntPtr) ((long)ctm + 4);
932 public static IntPtr SecureStringToCoTaskMemAnsi (SecureString s)
935 throw new ArgumentNullException ("s");
937 IntPtr ctm = AllocCoTaskMem (len + 1);
938 byte [] copy = new byte [len+1];
941 byte [] buffer = s.GetBuffer ();
943 for (; i < len; i++, j += 2){
944 copy [i] = buffer [j+1];
949 copy_to_unmanaged (copy, 0, ctm, len+1);
951 // Ensure that we clear the buffer.
952 for (int i = len; i > 0; ){
960 public static IntPtr SecureStringToCoTaskMemUnicode (SecureString s)
963 throw new ArgumentNullException ("s");
965 IntPtr ctm = AllocCoTaskMem (len * 2 + 2);
966 byte [] buffer = null;
968 buffer = s.GetBuffer ();
969 for (int i = 0; i < len; i++)
970 WriteInt16 (ctm, i * 2, (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
971 WriteInt16 (ctm, buffer.Length, 0);
974 for (int i = buffer.Length; i > 0; ){
982 public static IntPtr SecureStringToGlobalAllocAnsi (SecureString s)
985 throw new ArgumentNullException ("s");
986 return SecureStringToCoTaskMemAnsi (s);
989 public static IntPtr SecureStringToGlobalAllocUnicode (SecureString s)
992 throw new ArgumentNullException ("s");
993 return SecureStringToCoTaskMemUnicode (s);
997 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
999 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1000 public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
1002 public static void ThrowExceptionForHR (int errorCode) {
1003 Exception ex = GetExceptionForHR (errorCode);
1008 public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo) {
1009 Exception ex = GetExceptionForHR (errorCode, errorInfo);
1014 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1015 public extern static IntPtr UnsafeAddrOfPinnedArrayElement (Array arr, int index);
1017 public static void WriteByte (IntPtr ptr, byte val)
1019 WriteByte (ptr, 0, val);
1022 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1023 public extern static void WriteByte (IntPtr ptr, int ofs, byte val);
1026 [SuppressUnmanagedCodeSecurity]
1027 public static void WriteByte ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, byte val)
1029 throw new NotImplementedException ();
1032 public static void WriteInt16 (IntPtr ptr, short val)
1034 WriteInt16 (ptr, 0, val);
1037 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1038 public extern static void WriteInt16 (IntPtr ptr, int ofs, short val);
1041 [SuppressUnmanagedCodeSecurity]
1042 public static void WriteInt16 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, short val)
1044 throw new NotImplementedException ();
1047 public static void WriteInt16 (IntPtr ptr, char val)
1049 WriteInt16 (ptr, 0, val);
1053 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1054 public extern static void WriteInt16 (IntPtr ptr, int ofs, char val);
1057 public static void WriteInt16([In, Out] object ptr, int ofs, char val)
1059 throw new NotImplementedException ();
1062 public static void WriteInt32 (IntPtr ptr, int val)
1064 WriteInt32 (ptr, 0, val);
1067 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1068 public extern static void WriteInt32 (IntPtr ptr, int ofs, int val);
1071 [SuppressUnmanagedCodeSecurity]
1072 public static void WriteInt32([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, int val)
1074 throw new NotImplementedException ();
1077 public static void WriteInt64 (IntPtr ptr, long val)
1079 WriteInt64 (ptr, 0, val);
1082 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1083 public extern static void WriteInt64 (IntPtr ptr, int ofs, long val);
1086 [SuppressUnmanagedCodeSecurity]
1087 public static void WriteInt64 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, long val)
1089 throw new NotImplementedException ();
1092 public static void WriteIntPtr (IntPtr ptr, IntPtr val)
1094 WriteIntPtr (ptr, 0, val);
1097 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1098 public extern static void WriteIntPtr (IntPtr ptr, int ofs, IntPtr val);
1101 public static void WriteIntPtr([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val)
1103 throw new NotImplementedException ();
1106 public static Exception GetExceptionForHR (int errorCode) {
1107 return GetExceptionForHR (errorCode, IntPtr.Zero);
1110 public static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo) {
1112 const int E_OUTOFMEMORY = unchecked ((int)0x8007000EL);
1113 const int E_INVALIDARG = unchecked ((int)0X80070057);
1118 return new OutOfMemoryException ();
1120 return new ArgumentException ();
1123 return new COMException ("", errorCode);
1127 #if !FULL_AOT_RUNTIME
1129 public static int FinalReleaseComObject (object o)
1131 while (ReleaseComObject (o) != 0);
1137 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1138 private static extern Delegate GetDelegateForFunctionPointerInternal (IntPtr ptr, Type t);
1140 public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t)
1143 throw new ArgumentNullException ("t");
1144 if (!t.IsSubclassOf (typeof (MulticastDelegate)) || (t == typeof (MulticastDelegate)))
1145 throw new ArgumentException ("Type is not a delegate", "t");
1146 if (t.IsGenericType)
1147 throw new ArgumentException ("The specified Type must not be a generic type definition.");
1148 if (ptr == IntPtr.Zero)
1149 throw new ArgumentNullException ("ptr");
1151 return GetDelegateForFunctionPointerInternal (ptr, t);
1154 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1155 private static extern IntPtr GetFunctionPointerForDelegateInternal (Delegate d);
1157 public static IntPtr GetFunctionPointerForDelegate (Delegate d)
1160 throw new ArgumentNullException ("d");
1162 return GetFunctionPointerForDelegateInternal (d);