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 [SuppressUnmanagedCodeSecurity ()]
52 public static class Marshal
55 public static readonly int SystemMaxDBCSCharSize = 2; // don't know what this is
56 public static readonly int SystemDefaultCharSize;
60 SystemDefaultCharSize = Environment.OSVersion.Platform == PlatformID.Win32NT ? 2 : 1;
63 [MethodImplAttribute (MethodImplOptions.InternalCall)]
64 private extern static int AddRefInternal (IntPtr pUnk);
66 public static int AddRef (IntPtr pUnk)
68 if (pUnk == IntPtr.Zero)
69 throw new ArgumentException ("Value cannot be null.", "pUnk");
70 return AddRefInternal (pUnk);
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 ();
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)
276 public static Guid GenerateGuidForType (Type type)
282 public static string GenerateProgIdForType (Type type)
284 throw new NotImplementedException ();
288 public static object GetActiveObject (string progID)
290 throw new NotImplementedException ();
293 [MethodImplAttribute (MethodImplOptions.InternalCall)]
294 private extern static IntPtr GetCCW (object o, Type T);
296 private static IntPtr GetComInterfaceForObjectInternal (object o, Type T)
299 return ((__ComObject)o).GetInterface (T);
301 return GetCCW (o, T);
304 public static IntPtr GetComInterfaceForObject (object o, Type T)
306 IntPtr pItf = GetComInterfaceForObjectInternal (o, T);
312 public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
314 throw new NotImplementedException ();
317 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
318 public static object GetComObjectData (object obj, object key)
320 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
323 [MethodImplAttribute(MethodImplOptions.InternalCall)]
324 private extern static int GetComSlotForMethodInfoInternal (MemberInfo m);
326 public static int GetComSlotForMethodInfo (MemberInfo m)
329 throw new ArgumentNullException ("m");
330 if (!(m is MethodInfo))
331 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
332 if (!m.DeclaringType.IsInterface)
333 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
334 return GetComSlotForMethodInfoInternal (m);
338 public static int GetEndComSlot (Type t)
340 throw new NotImplementedException ();
344 public static int GetExceptionCode()
346 throw new NotImplementedException ();
351 public static IntPtr GetExceptionPointers()
353 throw new NotImplementedException ();
356 public static IntPtr GetHINSTANCE (Module m)
359 throw new ArgumentNullException ("m");
361 return m.GetHINSTANCE ();
365 [MonoTODO ("SetErrorInfo")]
366 public static int GetHRForException (Exception e)
372 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
373 public static int GetHRForLastWin32Error()
375 throw new NotImplementedException ();
378 [MethodImplAttribute (MethodImplOptions.InternalCall)]
379 private extern static IntPtr GetIDispatchForObjectInternal (object o);
381 public static IntPtr GetIDispatchForObject (object o)
383 IntPtr pUnk = GetIDispatchForObjectInternal (o);
384 // Internal method does not AddRef
390 public static IntPtr GetIDispatchForObjectInContext (object o)
392 throw new NotImplementedException ();
396 public static IntPtr GetITypeInfoForType (Type t)
398 throw new NotImplementedException ();
401 [MethodImplAttribute (MethodImplOptions.InternalCall)]
402 private extern static IntPtr GetIUnknownForObjectInternal (object o);
404 public static IntPtr GetIUnknownForObject (object o)
406 IntPtr pUnk = GetIUnknownForObjectInternal (o);
407 // Internal method does not AddRef
413 public static IntPtr GetIUnknownForObjectInContext (object o)
415 throw new NotImplementedException ();
419 [Obsolete ("This method has been deprecated")]
420 public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
422 throw new NotImplementedException ();
426 public static MemberInfo GetMethodInfoForComSlot (Type t, int slot, ref ComMemberType memberType)
428 throw new NotImplementedException ();
431 public static void GetNativeVariantForObject (object obj, IntPtr pDstNativeVariant)
433 Variant vt = new Variant();
435 Marshal.StructureToPtr(vt, pDstNativeVariant, false);
438 [MethodImplAttribute (MethodImplOptions.InternalCall)]
439 private static extern object GetObjectForCCW (IntPtr pUnk);
441 public static object GetObjectForIUnknown (IntPtr pUnk)
443 object obj = GetObjectForCCW (pUnk);
446 ComInteropProxy proxy = ComInteropProxy.GetProxy (pUnk, typeof (__ComObject));
447 obj = proxy.GetTransparentProxy ();
452 public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant)
454 Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
455 return vt.GetValue();
458 public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars)
461 throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
462 object[] objects = new object[cVars];
463 for (int i = 0; i < cVars; i++)
464 objects[i] = GetObjectForNativeVariant ((IntPtr)(aSrcNativeVariant.ToInt64 () +
465 i * SizeOf (typeof(Variant))));
470 public static int GetStartComSlot (Type t)
472 throw new NotImplementedException ();
476 [Obsolete ("This method has been deprecated")]
477 public static Thread GetThreadFromFiberCookie (int cookie)
479 throw new NotImplementedException ();
482 public static object GetTypedObjectForIUnknown (IntPtr pUnk, Type t)
484 ComInteropProxy proxy = new ComInteropProxy (pUnk, t);
485 __ComObject co = (__ComObject)proxy.GetTransparentProxy ();
486 foreach (Type itf in t.GetInterfaces ()) {
487 if ((itf.Attributes & TypeAttributes.Import) == TypeAttributes.Import) {
488 if (co.GetInterface (itf) == IntPtr.Zero)
496 public static Type GetTypeForITypeInfo (IntPtr piTypeInfo)
498 throw new NotImplementedException ();
503 public static string GetTypeInfoName (UCOMITypeInfo pTI)
505 throw new NotImplementedException ();
508 public static string GetTypeInfoName (ITypeInfo typeInfo)
510 throw new NotImplementedException ();
515 public static Guid GetTypeLibGuid (UCOMITypeLib pTLB)
517 throw new NotImplementedException ();
521 public static Guid GetTypeLibGuid (ITypeLib typelib)
523 throw new NotImplementedException ();
527 public static Guid GetTypeLibGuidForAssembly (Assembly asm)
529 throw new NotImplementedException ();
534 public static int GetTypeLibLcid (UCOMITypeLib pTLB)
536 throw new NotImplementedException ();
540 public static int GetTypeLibLcid (ITypeLib typelib)
542 throw new NotImplementedException ();
547 public static string GetTypeLibName (UCOMITypeLib pTLB)
549 throw new NotImplementedException ();
553 public static string GetTypeLibName (ITypeLib typelib)
555 throw new NotImplementedException ();
559 public static void GetTypeLibVersionForAssembly (Assembly inputAssembly, out int majorVersion, out int minorVersion)
561 throw new NotImplementedException ();
564 public static object GetUniqueObjectForIUnknown (IntPtr unknown)
566 throw new NotImplementedException ();
570 [Obsolete ("This method has been deprecated")]
571 public static IntPtr GetUnmanagedThunkForManagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
573 throw new NotImplementedException ();
576 [MethodImplAttribute (MethodImplOptions.InternalCall)]
577 public extern static bool IsComObject (object o);
580 public static bool IsTypeVisibleFromCom (Type t)
582 throw new NotImplementedException ();
586 public static int NumParamBytes (MethodInfo m)
588 throw new NotImplementedException ();
592 [MethodImplAttribute(MethodImplOptions.InternalCall)]
593 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
594 public static extern int GetLastWin32Error();
596 [MethodImplAttribute(MethodImplOptions.InternalCall)]
597 public extern static IntPtr OffsetOf (Type t, string fieldName);
599 [MethodImplAttribute(MethodImplOptions.InternalCall)]
600 public extern static void Prelink (MethodInfo m);
602 [MethodImplAttribute(MethodImplOptions.InternalCall)]
603 public extern static void PrelinkAll (Type c);
605 [MethodImplAttribute(MethodImplOptions.InternalCall)]
606 public extern static string PtrToStringAnsi (IntPtr ptr);
608 [MethodImplAttribute(MethodImplOptions.InternalCall)]
609 public extern static string PtrToStringAnsi (IntPtr ptr, int len);
611 public static string PtrToStringAuto (IntPtr ptr)
613 return SystemDefaultCharSize == 2
614 ? PtrToStringUni (ptr) : PtrToStringAnsi (ptr);
617 public static string PtrToStringAuto (IntPtr ptr, int len)
619 return SystemDefaultCharSize == 2
620 ? PtrToStringUni (ptr, len) : PtrToStringAnsi (ptr, len);
623 [MethodImplAttribute(MethodImplOptions.InternalCall)]
624 public extern static string PtrToStringUni (IntPtr ptr);
626 [MethodImplAttribute(MethodImplOptions.InternalCall)]
627 public extern static string PtrToStringUni (IntPtr ptr, int len);
629 [MethodImplAttribute(MethodImplOptions.InternalCall)]
630 public extern static string PtrToStringBSTR (IntPtr ptr);
632 [MethodImplAttribute(MethodImplOptions.InternalCall)]
634 public extern static void PtrToStructure (IntPtr ptr, object structure);
636 [MethodImplAttribute(MethodImplOptions.InternalCall)]
638 public extern static object PtrToStructure (IntPtr ptr, Type structureType);
640 [MethodImplAttribute (MethodImplOptions.InternalCall)]
641 private extern static int QueryInterfaceInternal (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
643 public static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv)
645 if (pUnk == IntPtr.Zero)
646 throw new ArgumentException ("Value cannot be null.", "pUnk");
647 return QueryInterfaceInternal (pUnk, ref iid, out ppv);
650 public static byte ReadByte (IntPtr ptr)
652 return ReadByte (ptr, 0);
655 [MethodImplAttribute(MethodImplOptions.InternalCall)]
656 public extern static byte ReadByte (IntPtr ptr, int ofs);
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 public static short ReadInt16 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
675 throw new NotImplementedException ();
678 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
679 public static int ReadInt32 (IntPtr ptr)
681 return ReadInt32 (ptr, 0);
684 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
685 [MethodImplAttribute(MethodImplOptions.InternalCall)]
686 public extern static int ReadInt32 (IntPtr ptr, int ofs);
688 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
690 public static int ReadInt32 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
692 throw new NotImplementedException ();
695 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
696 public static long ReadInt64 (IntPtr ptr)
698 return ReadInt64 (ptr, 0);
701 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
702 [MethodImplAttribute(MethodImplOptions.InternalCall)]
703 public extern static long ReadInt64 (IntPtr ptr, int ofs);
705 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
707 public static long ReadInt64 ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
709 throw new NotImplementedException ();
712 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
713 public static IntPtr ReadIntPtr (IntPtr ptr)
715 return ReadIntPtr (ptr, 0);
718 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
719 [MethodImplAttribute(MethodImplOptions.InternalCall)]
720 public extern static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
722 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
724 public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
726 throw new NotImplementedException ();
729 [MethodImplAttribute (MethodImplOptions.InternalCall)]
730 public extern static IntPtr ReAllocCoTaskMem (IntPtr pv, int cb);
732 [MethodImplAttribute(MethodImplOptions.InternalCall)]
733 public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
735 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
736 [MethodImplAttribute (MethodImplOptions.InternalCall)]
737 private extern static int ReleaseInternal (IntPtr pUnk);
739 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
740 public static int Release (IntPtr pUnk)
742 if (pUnk == IntPtr.Zero)
743 throw new ArgumentException ("Value cannot be null.", "pUnk");
744 return ReleaseInternal (pUnk);
748 [MethodImplAttribute (MethodImplOptions.InternalCall)]
749 private extern static int ReleaseComObjectInternal (object co);
751 public static int ReleaseComObject (object o)
754 throw new ArgumentException ("Value cannot be null.", "o");
755 if (!IsComObject (o))
756 throw new ArgumentException ("Value must be a Com object.", "o");
757 return ReleaseComObjectInternal (o);
762 public static void ReleaseThreadCache()
764 throw new NotImplementedException ();
767 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
768 public static bool SetComObjectData (object obj, object key, object data)
770 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
775 public static int SizeOf (object structure)
777 return SizeOf (structure.GetType ());
780 [MethodImplAttribute(MethodImplOptions.InternalCall)]
781 public extern static int SizeOf (Type t);
783 [MethodImplAttribute(MethodImplOptions.InternalCall)]
784 public extern static IntPtr StringToBSTR (string s);
787 // I believe this is wrong, because in Mono and in P/Invoke
788 // we treat "Ansi" conversions as UTF-8 conversions, while
789 // this one does not do this
791 public static IntPtr StringToCoTaskMemAnsi (string s)
793 int length = s.Length + 1;
794 IntPtr ctm = AllocCoTaskMem (length);
796 byte[] asBytes = new byte[length];
797 for (int i = 0; i < s.Length; i++)
798 asBytes[i] = (byte)s[i];
799 asBytes[s.Length] = 0;
801 copy_to_unmanaged (asBytes, 0, ctm, length);
805 public static IntPtr StringToCoTaskMemAuto (string s)
807 return SystemDefaultCharSize == 2
808 ? StringToCoTaskMemUni (s) : StringToCoTaskMemAnsi (s);
811 public static IntPtr StringToCoTaskMemUni (string s)
813 int length = s.Length + 1;
814 IntPtr ctm = AllocCoTaskMem (length * 2);
816 char[] asChars = new char[length];
817 s.CopyTo (0, asChars, 0, s.Length);
818 asChars[s.Length] = '\0';
820 copy_to_unmanaged (asChars, 0, ctm, length);
824 [MethodImplAttribute(MethodImplOptions.InternalCall)]
825 public extern static IntPtr StringToHGlobalAnsi (string s);
827 public static IntPtr StringToHGlobalAuto (string s)
829 return SystemDefaultCharSize == 2
830 ? StringToHGlobalUni (s) : StringToHGlobalAnsi (s);
833 [MethodImplAttribute(MethodImplOptions.InternalCall)]
834 public extern static IntPtr StringToHGlobalUni (string s);
837 public static IntPtr SecureStringToBSTR (SecureString s)
840 throw new ArgumentNullException ("s");
842 IntPtr ctm = AllocCoTaskMem ((len+1) * 2 + 4);
843 byte [] buffer = null;
844 WriteInt32 (ctm, 0, len*2);
846 buffer = s.GetBuffer ();
848 for (int i = 0; i < len; i++)
849 WriteInt16 (ctm, 4 + (i * 2), (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
850 WriteInt16 (ctm, 4 + buffer.Length, 0);
853 for (int i = buffer.Length; i > 0; ){
858 return (IntPtr) ((long)ctm + 4);
861 public static IntPtr SecureStringToCoTaskMemAnsi (SecureString s)
864 throw new ArgumentNullException ("s");
866 IntPtr ctm = AllocCoTaskMem (len + 1);
867 byte [] copy = new byte [len+1];
870 byte [] buffer = s.GetBuffer ();
872 for (; i < len; i++, j += 2){
873 copy [i] = buffer [j+1];
878 copy_to_unmanaged (copy, 0, ctm, len+1);
880 // Ensure that we clear the buffer.
881 for (int i = len; i > 0; ){
889 public static IntPtr SecureStringToCoTaskMemUnicode (SecureString s)
892 throw new ArgumentNullException ("s");
894 IntPtr ctm = AllocCoTaskMem (len * 2 + 2);
895 byte [] buffer = null;
897 buffer = s.GetBuffer ();
898 for (int i = 0; i < len; i++)
899 WriteInt16 (ctm, i * 2, (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
900 WriteInt16 (ctm, buffer.Length, 0);
903 for (int i = buffer.Length; i > 0; ){
911 public static IntPtr SecureStringToGlobalAllocAnsi (SecureString s)
914 throw new ArgumentNullException ("s");
915 return SecureStringToCoTaskMemAnsi (s);
918 public static IntPtr SecureStringToGlobalAllocUnicode (SecureString s)
921 throw new ArgumentNullException ("s");
922 return SecureStringToCoTaskMemUnicode (s);
926 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
928 [MethodImplAttribute(MethodImplOptions.InternalCall)]
929 public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
931 public static void ThrowExceptionForHR (int errorCode) {
932 Exception ex = GetExceptionForHR (errorCode);
937 public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo) {
938 Exception ex = GetExceptionForHR (errorCode, errorInfo);
943 [MethodImplAttribute(MethodImplOptions.InternalCall)]
944 public extern static IntPtr UnsafeAddrOfPinnedArrayElement (Array arr, int index);
946 public static void WriteByte (IntPtr ptr, byte val)
948 WriteByte (ptr, 0, val);
951 [MethodImplAttribute(MethodImplOptions.InternalCall)]
952 public extern static void WriteByte (IntPtr ptr, int ofs, byte val);
955 public static void WriteByte ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, byte val)
957 throw new NotImplementedException ();
960 public static void WriteInt16 (IntPtr ptr, short val)
962 WriteInt16 (ptr, 0, val);
965 [MethodImplAttribute(MethodImplOptions.InternalCall)]
966 public extern static void WriteInt16 (IntPtr ptr, int ofs, short val);
969 public static void WriteInt16 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, short val)
971 throw new NotImplementedException ();
974 public static void WriteInt16 (IntPtr ptr, char val)
976 WriteInt16 (ptr, 0, val);
980 [MethodImplAttribute(MethodImplOptions.InternalCall)]
981 public extern static void WriteInt16 (IntPtr ptr, int ofs, char val);
984 public static void WriteInt16([In, Out] object ptr, int ofs, char val)
986 throw new NotImplementedException ();
989 public static void WriteInt32 (IntPtr ptr, int val)
991 WriteInt32 (ptr, 0, val);
994 [MethodImplAttribute(MethodImplOptions.InternalCall)]
995 public extern static void WriteInt32 (IntPtr ptr, int ofs, int val);
998 public static void WriteInt32([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, int val)
1000 throw new NotImplementedException ();
1003 public static void WriteInt64 (IntPtr ptr, long val)
1005 WriteInt64 (ptr, 0, val);
1008 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1009 public extern static void WriteInt64 (IntPtr ptr, int ofs, long val);
1012 public static void WriteInt64 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, long val)
1014 throw new NotImplementedException ();
1017 public static void WriteIntPtr (IntPtr ptr, IntPtr val)
1019 WriteIntPtr (ptr, 0, val);
1022 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1023 public extern static void WriteIntPtr (IntPtr ptr, int ofs, IntPtr val);
1026 public static void WriteIntPtr([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val)
1028 throw new NotImplementedException ();
1031 public static Exception GetExceptionForHR (int errorCode) {
1032 return GetExceptionForHR (errorCode, IntPtr.Zero);
1035 public static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo) {
1037 const int E_OUTOFMEMORY = unchecked ((int)0x8007000EL);
1038 const int E_INVALIDARG = unchecked ((int)0X80070057);
1043 return new OutOfMemoryException ();
1045 return new ArgumentException ();
1048 return new COMException ("", errorCode);
1053 public static int FinalReleaseComObject (object o)
1055 while (ReleaseComObject (o) != 0);
1060 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1061 private static extern Delegate GetDelegateForFunctionPointerInternal (IntPtr ptr, Type t);
1063 public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t)
1066 throw new ArgumentNullException ("t");
1067 if (!t.IsSubclassOf (typeof (MulticastDelegate)) || (t == typeof (MulticastDelegate)))
1068 throw new ArgumentException ("Type is not a delegate", "t");
1069 if (ptr == IntPtr.Zero)
1070 throw new ArgumentNullException ("ptr");
1072 return GetDelegateForFunctionPointerInternal (ptr, t);
1075 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1076 private static extern IntPtr GetFunctionPointerForDelegateInternal (Delegate d);
1078 public static IntPtr GetFunctionPointerForDelegate (Delegate d)
1081 throw new ArgumentNullException ("d");
1083 return GetFunctionPointerForDelegateInternal (d);