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.Runtime.CompilerServices;
36 using System.Security;
37 using System.Reflection;
38 using System.Threading;
40 using System.Runtime.ConstrainedExecution;
42 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;
59 SystemDefaultCharSize = Environment.OSVersion.Platform == PlatformID.Win32NT ? 2 : 1;
62 [MethodImplAttribute (MethodImplOptions.InternalCall)]
63 private extern static int AddRefInternal (IntPtr pUnk);
65 public static int AddRef (IntPtr pUnk)
67 if (pUnk == IntPtr.Zero)
68 throw new ArgumentException ("Value cannot be null.", "pUnk");
69 return AddRefInternal (pUnk);
72 [MethodImplAttribute(MethodImplOptions.InternalCall)]
73 public extern static IntPtr AllocCoTaskMem (int cb);
75 [MethodImplAttribute(MethodImplOptions.InternalCall)]
76 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
77 public extern static IntPtr AllocHGlobal (IntPtr cb);
79 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
80 public static IntPtr AllocHGlobal (int cb)
82 return AllocHGlobal ((IntPtr)cb);
86 public static object BindToMoniker (string monikerName)
88 throw new NotImplementedException ();
92 public static void ChangeWrapperHandleStrength (object otp, bool fIsWeak)
94 throw new NotImplementedException ();
97 [MethodImplAttribute(MethodImplOptions.InternalCall)]
98 internal extern static void copy_to_unmanaged (Array source, int startIndex,
99 IntPtr destination, int length);
101 [MethodImplAttribute(MethodImplOptions.InternalCall)]
102 internal extern static void copy_from_unmanaged (IntPtr source, int startIndex,
103 Array destination, int length);
105 public static void Copy (byte[] source, int startIndex, IntPtr destination, int length)
107 copy_to_unmanaged (source, startIndex, destination, length);
110 public static void Copy (char[] source, int startIndex, IntPtr destination, int length)
112 copy_to_unmanaged (source, startIndex, destination, length);
115 public static void Copy (short[] source, int startIndex, IntPtr destination, int length)
117 copy_to_unmanaged (source, startIndex, destination, length);
120 public static void Copy (int[] source, int startIndex, IntPtr destination, int length)
122 copy_to_unmanaged (source, startIndex, destination, length);
125 public static void Copy (long[] source, int startIndex, IntPtr destination, int length)
127 copy_to_unmanaged (source, startIndex, destination, length);
130 public static void Copy (float[] source, int startIndex, IntPtr destination, int length)
132 copy_to_unmanaged (source, startIndex, destination, length);
135 public static void Copy (double[] source, int startIndex, IntPtr destination, int length)
137 copy_to_unmanaged (source, startIndex, destination, length);
140 public static void Copy (IntPtr[] source, int startIndex, IntPtr destination, int length)
142 copy_to_unmanaged (source, startIndex, destination, length);
145 public static void Copy (IntPtr source, byte[] destination, int startIndex, int length)
147 copy_from_unmanaged (source, startIndex, destination, length);
150 public static void Copy (IntPtr source, char[] destination, int startIndex, int length)
152 copy_from_unmanaged (source, startIndex, destination, length);
155 public static void Copy (IntPtr source, short[] destination, int startIndex, int length)
157 copy_from_unmanaged (source, startIndex, destination, length);
160 public static void Copy (IntPtr source, int[] destination, int startIndex, int length)
162 copy_from_unmanaged (source, startIndex, destination, length);
165 public static void Copy (IntPtr source, long[] destination, int startIndex, int length)
167 copy_from_unmanaged (source, startIndex, destination, length);
170 public static void Copy (IntPtr source, float[] destination, int startIndex, int length)
172 copy_from_unmanaged (source, startIndex, destination, length);
175 public static void Copy (IntPtr source, double[] destination, int startIndex, int length)
177 copy_from_unmanaged (source, startIndex, destination, length);
180 public static void Copy (IntPtr source, IntPtr[] destination, int startIndex, int length)
182 copy_from_unmanaged (source, startIndex, destination, length);
185 public static IntPtr CreateAggregatedObject (IntPtr pOuter,
188 throw new NotImplementedException ();
192 public static object CreateWrapperOfType (object o, Type t)
194 __ComObject co = o as __ComObject;
196 throw new ArgumentException ("o must derive from __ComObject", "o");
198 throw new ArgumentNullException ("t");
200 Type[] itfs = o.GetType ().GetInterfaces ();
201 foreach (Type itf in itfs) {
202 if (itf.IsImport && co.GetInterface (itf) == IntPtr.Zero)
203 throw new InvalidCastException ();
206 return ComInteropProxy.GetProxy (co.IUnknown, t).GetTransparentProxy ();
210 [MethodImplAttribute(MethodImplOptions.InternalCall)]
212 public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
214 [MethodImplAttribute(MethodImplOptions.InternalCall)]
215 public extern static void FreeBSTR (IntPtr ptr);
217 [MethodImplAttribute(MethodImplOptions.InternalCall)]
218 public extern static void FreeCoTaskMem (IntPtr ptr);
220 [MethodImplAttribute(MethodImplOptions.InternalCall)]
221 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
222 public extern static void FreeHGlobal (IntPtr hglobal);
224 static void ClearBSTR (IntPtr ptr)
226 int len = ReadInt32 (ptr, -4);
228 for (int i = 0; i < len; i++)
229 WriteByte (ptr, i, 0);
232 public static void ZeroFreeBSTR (IntPtr s)
238 static void ClearAnsi (IntPtr ptr)
240 for (int i = 0; ReadByte (ptr, i) != 0; i++)
241 WriteByte (ptr, i, 0);
244 static void ClearUnicode (IntPtr ptr)
246 for (int i = 0; ReadInt16 (ptr, i) != 0; i += 2)
247 WriteInt16 (ptr, i, 0);
250 public static void ZeroFreeCoTaskMemAnsi (IntPtr s)
256 public static void ZeroFreeCoTaskMemUnicode (IntPtr s)
262 public static void ZeroFreeGlobalAllocAnsi (IntPtr s)
268 public static void ZeroFreeGlobalAllocUnicode (IntPtr s)
275 public static Guid GenerateGuidForType (Type type)
281 public static string GenerateProgIdForType (Type type)
283 throw new NotImplementedException ();
287 public static object GetActiveObject (string progID)
289 throw new NotImplementedException ();
292 [MethodImplAttribute (MethodImplOptions.InternalCall)]
293 private extern static IntPtr GetCCW (object o, Type T);
295 private static IntPtr GetComInterfaceForObjectInternal (object o, Type T)
298 return ((__ComObject)o).GetInterface (T);
300 return GetCCW (o, T);
303 public static IntPtr GetComInterfaceForObject (object o, Type T)
305 IntPtr pItf = GetComInterfaceForObjectInternal (o, T);
311 public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
313 throw new NotImplementedException ();
316 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
317 public static object GetComObjectData (object obj, object key)
319 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
322 [MethodImplAttribute(MethodImplOptions.InternalCall)]
323 private extern static int GetComSlotForMethodInfoInternal (MemberInfo m);
325 public static int GetComSlotForMethodInfo (MemberInfo m)
328 throw new ArgumentNullException ("m");
329 if (!(m is MethodInfo))
330 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
331 if (!m.DeclaringType.IsInterface)
332 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
333 return GetComSlotForMethodInfoInternal (m);
337 public static int GetEndComSlot (Type t)
339 throw new NotImplementedException ();
343 public static int GetExceptionCode()
345 throw new NotImplementedException ();
350 public static IntPtr GetExceptionPointers()
352 throw new NotImplementedException ();
355 public static IntPtr GetHINSTANCE (Module m)
358 throw new ArgumentNullException ("m");
360 return m.GetHINSTANCE ();
364 [MonoTODO ("SetErrorInfo")]
365 public static int GetHRForException (Exception e)
371 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
372 public static int GetHRForLastWin32Error()
374 throw new NotImplementedException ();
377 [MethodImplAttribute (MethodImplOptions.InternalCall)]
378 private extern static IntPtr GetIDispatchForObjectInternal (object o);
380 public static IntPtr GetIDispatchForObject (object o)
382 IntPtr pUnk = GetIDispatchForObjectInternal (o);
383 // Internal method does not AddRef
389 public static IntPtr GetIDispatchForObjectInContext (object o)
391 throw new NotImplementedException ();
395 public static IntPtr GetITypeInfoForType (Type t)
397 throw new NotImplementedException ();
400 [MethodImplAttribute (MethodImplOptions.InternalCall)]
401 private extern static IntPtr GetIUnknownForObjectInternal (object o);
403 public static IntPtr GetIUnknownForObject (object o)
405 IntPtr pUnk = GetIUnknownForObjectInternal (o);
406 // Internal method does not AddRef
412 public static IntPtr GetIUnknownForObjectInContext (object o)
414 throw new NotImplementedException ();
418 [Obsolete ("This method has been deprecated")]
419 public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
421 throw new NotImplementedException ();
425 public static MemberInfo GetMethodInfoForComSlot (Type t, int slot, ref ComMemberType memberType)
427 throw new NotImplementedException ();
430 public static void GetNativeVariantForObject (object obj, IntPtr pDstNativeVariant)
432 Variant vt = new Variant();
434 Marshal.StructureToPtr(vt, pDstNativeVariant, false);
437 [MethodImplAttribute (MethodImplOptions.InternalCall)]
438 private static extern object GetObjectForCCW (IntPtr pUnk);
440 public static object GetObjectForIUnknown (IntPtr pUnk)
442 object obj = GetObjectForCCW (pUnk);
445 ComInteropProxy proxy = ComInteropProxy.GetProxy (pUnk, typeof (__ComObject));
446 obj = proxy.GetTransparentProxy ();
451 public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant)
453 Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
454 return vt.GetValue();
457 public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars)
460 throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
461 object[] objects = new object[cVars];
462 for (int i = 0; i < cVars; i++)
463 objects[i] = GetObjectForNativeVariant ((IntPtr)(aSrcNativeVariant.ToInt64 () +
464 i * SizeOf (typeof(Variant))));
469 public static int GetStartComSlot (Type t)
471 throw new NotImplementedException ();
475 [Obsolete ("This method has been deprecated")]
476 public static Thread GetThreadFromFiberCookie (int cookie)
478 throw new NotImplementedException ();
481 public static object GetTypedObjectForIUnknown (IntPtr pUnk, Type t)
483 ComInteropProxy proxy = new ComInteropProxy (pUnk, t);
484 __ComObject co = (__ComObject)proxy.GetTransparentProxy ();
485 foreach (Type itf in t.GetInterfaces ()) {
486 if ((itf.Attributes & TypeAttributes.Import) == TypeAttributes.Import) {
487 if (co.GetInterface (itf) == IntPtr.Zero)
495 public static Type GetTypeForITypeInfo (IntPtr piTypeInfo)
497 throw new NotImplementedException ();
502 public static string GetTypeInfoName (UCOMITypeInfo pTI)
504 throw new NotImplementedException ();
507 public static string GetTypeInfoName (ITypeInfo typeInfo)
509 throw new NotImplementedException ();
514 public static Guid GetTypeLibGuid (UCOMITypeLib pTLB)
516 throw new NotImplementedException ();
520 public static Guid GetTypeLibGuid (ITypeLib typelib)
522 throw new NotImplementedException ();
526 public static Guid GetTypeLibGuidForAssembly (Assembly asm)
528 throw new NotImplementedException ();
533 public static int GetTypeLibLcid (UCOMITypeLib pTLB)
535 throw new NotImplementedException ();
539 public static int GetTypeLibLcid (ITypeLib typelib)
541 throw new NotImplementedException ();
546 public static string GetTypeLibName (UCOMITypeLib pTLB)
548 throw new NotImplementedException ();
552 public static string GetTypeLibName (ITypeLib typelib)
554 throw new NotImplementedException ();
558 public static void GetTypeLibVersionForAssembly (Assembly inputAssembly, out int majorVersion, out int minorVersion)
560 throw new NotImplementedException ();
563 public static object GetUniqueObjectForIUnknown (IntPtr unknown)
565 throw new NotImplementedException ();
569 [Obsolete ("This method has been deprecated")]
570 public static IntPtr GetUnmanagedThunkForManagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
572 throw new NotImplementedException ();
575 [MethodImplAttribute (MethodImplOptions.InternalCall)]
576 public extern static bool IsComObject (object o);
579 public static bool IsTypeVisibleFromCom (Type t)
581 throw new NotImplementedException ();
585 public static int NumParamBytes (MethodInfo m)
587 throw new NotImplementedException ();
591 [MethodImplAttribute(MethodImplOptions.InternalCall)]
592 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
593 public static extern int GetLastWin32Error();
595 [MethodImplAttribute(MethodImplOptions.InternalCall)]
596 public extern static IntPtr OffsetOf (Type t, string fieldName);
598 [MethodImplAttribute(MethodImplOptions.InternalCall)]
599 public extern static void Prelink (MethodInfo m);
601 [MethodImplAttribute(MethodImplOptions.InternalCall)]
602 public extern static void PrelinkAll (Type c);
604 [MethodImplAttribute(MethodImplOptions.InternalCall)]
605 public extern static string PtrToStringAnsi (IntPtr ptr);
607 [MethodImplAttribute(MethodImplOptions.InternalCall)]
608 public extern static string PtrToStringAnsi (IntPtr ptr, int len);
610 public static string PtrToStringAuto (IntPtr ptr)
612 return SystemDefaultCharSize == 2
613 ? PtrToStringUni (ptr) : PtrToStringAnsi (ptr);
616 public static string PtrToStringAuto (IntPtr ptr, int len)
618 return SystemDefaultCharSize == 2
619 ? PtrToStringUni (ptr, len) : PtrToStringAnsi (ptr, len);
622 [MethodImplAttribute(MethodImplOptions.InternalCall)]
623 public extern static string PtrToStringUni (IntPtr ptr);
625 [MethodImplAttribute(MethodImplOptions.InternalCall)]
626 public extern static string PtrToStringUni (IntPtr ptr, int len);
628 [MethodImplAttribute(MethodImplOptions.InternalCall)]
629 public extern static string PtrToStringBSTR (IntPtr ptr);
631 [MethodImplAttribute(MethodImplOptions.InternalCall)]
633 public extern static void PtrToStructure (IntPtr ptr, object structure);
635 [MethodImplAttribute(MethodImplOptions.InternalCall)]
637 public extern static object PtrToStructure (IntPtr ptr, Type structureType);
639 [MethodImplAttribute (MethodImplOptions.InternalCall)]
640 private extern static int QueryInterfaceInternal (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
642 public static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv)
644 if (pUnk == IntPtr.Zero)
645 throw new ArgumentException ("Value cannot be null.", "pUnk");
646 return QueryInterfaceInternal (pUnk, ref iid, out ppv);
649 public static byte ReadByte (IntPtr ptr)
651 return ReadByte (ptr, 0);
654 [MethodImplAttribute(MethodImplOptions.InternalCall)]
655 public extern static byte ReadByte (IntPtr ptr, int ofs);
658 [SuppressUnmanagedCodeSecurity]
659 public static byte ReadByte ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
661 throw new NotImplementedException ();
664 public static short ReadInt16 (IntPtr ptr)
666 return ReadInt16 (ptr, 0);
669 [MethodImplAttribute(MethodImplOptions.InternalCall)]
670 public extern static short ReadInt16 (IntPtr ptr, int ofs);
673 [SuppressUnmanagedCodeSecurity]
674 public static short ReadInt16 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
676 throw new NotImplementedException ();
679 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
680 public static int ReadInt32 (IntPtr ptr)
682 return ReadInt32 (ptr, 0);
685 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
686 [MethodImplAttribute(MethodImplOptions.InternalCall)]
687 public extern static int ReadInt32 (IntPtr ptr, int ofs);
689 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
691 [SuppressUnmanagedCodeSecurity]
692 public static int ReadInt32 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
694 throw new NotImplementedException ();
697 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
698 public static long ReadInt64 (IntPtr ptr)
700 return ReadInt64 (ptr, 0);
703 [MethodImplAttribute(MethodImplOptions.InternalCall)]
704 public extern static long ReadInt64 (IntPtr ptr, int ofs);
706 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
708 [SuppressUnmanagedCodeSecurity]
709 public static long ReadInt64 ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
711 throw new NotImplementedException ();
714 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
715 public static IntPtr ReadIntPtr (IntPtr ptr)
717 return ReadIntPtr (ptr, 0);
720 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
721 [MethodImplAttribute(MethodImplOptions.InternalCall)]
722 public extern static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
724 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
726 public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
728 throw new NotImplementedException ();
731 [MethodImplAttribute (MethodImplOptions.InternalCall)]
732 public extern static IntPtr ReAllocCoTaskMem (IntPtr pv, int cb);
734 [MethodImplAttribute(MethodImplOptions.InternalCall)]
735 public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
737 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
738 [MethodImplAttribute (MethodImplOptions.InternalCall)]
739 private extern static int ReleaseInternal (IntPtr pUnk);
741 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
742 public static int Release (IntPtr pUnk)
744 if (pUnk == IntPtr.Zero)
745 throw new ArgumentException ("Value cannot be null.", "pUnk");
746 return ReleaseInternal (pUnk);
750 [MethodImplAttribute (MethodImplOptions.InternalCall)]
751 private extern static int ReleaseComObjectInternal (object co);
753 public static int ReleaseComObject (object o)
756 throw new ArgumentException ("Value cannot be null.", "o");
757 if (!IsComObject (o))
758 throw new ArgumentException ("Value must be a Com object.", "o");
759 return ReleaseComObjectInternal (o);
764 public static void ReleaseThreadCache()
766 throw new NotImplementedException ();
769 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
770 public static bool SetComObjectData (object obj, object key, object data)
772 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
777 public static int SizeOf (object structure)
779 return SizeOf (structure.GetType ());
782 [MethodImplAttribute(MethodImplOptions.InternalCall)]
783 public extern static int SizeOf (Type t);
785 [MethodImplAttribute(MethodImplOptions.InternalCall)]
786 public extern static IntPtr StringToBSTR (string s);
789 // I believe this is wrong, because in Mono and in P/Invoke
790 // we treat "Ansi" conversions as UTF-8 conversions, while
791 // this one does not do this
793 public static IntPtr StringToCoTaskMemAnsi (string s)
795 int length = s.Length + 1;
796 IntPtr ctm = AllocCoTaskMem (length);
798 byte[] asBytes = new byte[length];
799 for (int i = 0; i < s.Length; i++)
800 asBytes[i] = (byte)s[i];
801 asBytes[s.Length] = 0;
803 copy_to_unmanaged (asBytes, 0, ctm, length);
807 public static IntPtr StringToCoTaskMemAuto (string s)
809 return SystemDefaultCharSize == 2
810 ? StringToCoTaskMemUni (s) : StringToCoTaskMemAnsi (s);
813 public static IntPtr StringToCoTaskMemUni (string s)
815 int length = s.Length + 1;
816 IntPtr ctm = AllocCoTaskMem (length * 2);
818 char[] asChars = new char[length];
819 s.CopyTo (0, asChars, 0, s.Length);
820 asChars[s.Length] = '\0';
822 copy_to_unmanaged (asChars, 0, ctm, length);
826 [MethodImplAttribute(MethodImplOptions.InternalCall)]
827 public extern static IntPtr StringToHGlobalAnsi (string s);
829 public static IntPtr StringToHGlobalAuto (string s)
831 return SystemDefaultCharSize == 2
832 ? StringToHGlobalUni (s) : StringToHGlobalAnsi (s);
835 [MethodImplAttribute(MethodImplOptions.InternalCall)]
836 public extern static IntPtr StringToHGlobalUni (string s);
839 public static IntPtr SecureStringToBSTR (SecureString s)
842 throw new ArgumentNullException ("s");
844 IntPtr ctm = AllocCoTaskMem ((len+1) * 2 + 4);
845 byte [] buffer = null;
846 WriteInt32 (ctm, 0, len*2);
848 buffer = s.GetBuffer ();
850 for (int i = 0; i < len; i++)
851 WriteInt16 (ctm, 4 + (i * 2), (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
852 WriteInt16 (ctm, 4 + buffer.Length, 0);
855 for (int i = buffer.Length; i > 0; ){
860 return (IntPtr) ((long)ctm + 4);
863 public static IntPtr SecureStringToCoTaskMemAnsi (SecureString s)
866 throw new ArgumentNullException ("s");
868 IntPtr ctm = AllocCoTaskMem (len + 1);
869 byte [] copy = new byte [len+1];
872 byte [] buffer = s.GetBuffer ();
874 for (; i < len; i++, j += 2){
875 copy [i] = buffer [j+1];
880 copy_to_unmanaged (copy, 0, ctm, len+1);
882 // Ensure that we clear the buffer.
883 for (int i = len; i > 0; ){
891 public static IntPtr SecureStringToCoTaskMemUnicode (SecureString s)
894 throw new ArgumentNullException ("s");
896 IntPtr ctm = AllocCoTaskMem (len * 2 + 2);
897 byte [] buffer = null;
899 buffer = s.GetBuffer ();
900 for (int i = 0; i < len; i++)
901 WriteInt16 (ctm, i * 2, (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
902 WriteInt16 (ctm, buffer.Length, 0);
905 for (int i = buffer.Length; i > 0; ){
913 public static IntPtr SecureStringToGlobalAllocAnsi (SecureString s)
916 throw new ArgumentNullException ("s");
917 return SecureStringToCoTaskMemAnsi (s);
920 public static IntPtr SecureStringToGlobalAllocUnicode (SecureString s)
923 throw new ArgumentNullException ("s");
924 return SecureStringToCoTaskMemUnicode (s);
928 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
930 [MethodImplAttribute(MethodImplOptions.InternalCall)]
931 public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
933 public static void ThrowExceptionForHR (int errorCode) {
934 Exception ex = GetExceptionForHR (errorCode);
939 public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo) {
940 Exception ex = GetExceptionForHR (errorCode, errorInfo);
945 [MethodImplAttribute(MethodImplOptions.InternalCall)]
946 public extern static IntPtr UnsafeAddrOfPinnedArrayElement (Array arr, int index);
948 public static void WriteByte (IntPtr ptr, byte val)
950 WriteByte (ptr, 0, val);
953 [MethodImplAttribute(MethodImplOptions.InternalCall)]
954 public extern static void WriteByte (IntPtr ptr, int ofs, byte val);
957 [SuppressUnmanagedCodeSecurity]
958 public static void WriteByte ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, byte val)
960 throw new NotImplementedException ();
963 public static void WriteInt16 (IntPtr ptr, short val)
965 WriteInt16 (ptr, 0, val);
968 [MethodImplAttribute(MethodImplOptions.InternalCall)]
969 public extern static void WriteInt16 (IntPtr ptr, int ofs, short val);
972 [SuppressUnmanagedCodeSecurity]
973 public static void WriteInt16 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, short val)
975 throw new NotImplementedException ();
978 public static void WriteInt16 (IntPtr ptr, char val)
980 WriteInt16 (ptr, 0, val);
984 [MethodImplAttribute(MethodImplOptions.InternalCall)]
985 public extern static void WriteInt16 (IntPtr ptr, int ofs, char val);
988 public static void WriteInt16([In, Out] object ptr, int ofs, char val)
990 throw new NotImplementedException ();
993 public static void WriteInt32 (IntPtr ptr, int val)
995 WriteInt32 (ptr, 0, val);
998 [MethodImplAttribute(MethodImplOptions.InternalCall)]
999 public extern static void WriteInt32 (IntPtr ptr, int ofs, int val);
1002 [SuppressUnmanagedCodeSecurity]
1003 public static void WriteInt32([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, int val)
1005 throw new NotImplementedException ();
1008 public static void WriteInt64 (IntPtr ptr, long val)
1010 WriteInt64 (ptr, 0, val);
1013 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1014 public extern static void WriteInt64 (IntPtr ptr, int ofs, long val);
1017 [SuppressUnmanagedCodeSecurity]
1018 public static void WriteInt64 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, long val)
1020 throw new NotImplementedException ();
1023 public static void WriteIntPtr (IntPtr ptr, IntPtr val)
1025 WriteIntPtr (ptr, 0, val);
1028 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1029 public extern static void WriteIntPtr (IntPtr ptr, int ofs, IntPtr val);
1032 public static void WriteIntPtr([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val)
1034 throw new NotImplementedException ();
1037 public static Exception GetExceptionForHR (int errorCode) {
1038 return GetExceptionForHR (errorCode, IntPtr.Zero);
1041 public static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo) {
1043 const int E_OUTOFMEMORY = unchecked ((int)0x8007000EL);
1044 const int E_INVALIDARG = unchecked ((int)0X80070057);
1049 return new OutOfMemoryException ();
1051 return new ArgumentException ();
1054 return new COMException ("", errorCode);
1059 public static int FinalReleaseComObject (object o)
1061 while (ReleaseComObject (o) != 0);
1066 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1067 private static extern Delegate GetDelegateForFunctionPointerInternal (IntPtr ptr, Type t);
1069 public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t)
1072 throw new ArgumentNullException ("t");
1073 if (!t.IsSubclassOf (typeof (MulticastDelegate)) || (t == typeof (MulticastDelegate)))
1074 throw new ArgumentException ("Type is not a delegate", "t");
1075 if (t.IsGenericType)
1076 throw new ArgumentException ("The specified Type must not be a generic type definition.");
1077 if (ptr == IntPtr.Zero)
1078 throw new ArgumentNullException ("ptr");
1080 return GetDelegateForFunctionPointerInternal (ptr, t);
1083 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1084 private static extern IntPtr GetFunctionPointerForDelegateInternal (Delegate d);
1086 public static IntPtr GetFunctionPointerForDelegate (Delegate d)
1089 throw new ArgumentNullException ("d");
1091 return GetFunctionPointerForDelegateInternal (d);