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 = Environment.OSVersion.Platform == PlatformID.Win32NT ? 2 : 1;
57 [MethodImplAttribute (MethodImplOptions.InternalCall)]
58 private extern static int AddRefInternal (IntPtr pUnk);
60 public static int AddRef (IntPtr pUnk)
62 if (pUnk == IntPtr.Zero)
63 throw new ArgumentException ("Value cannot be null.", "pUnk");
64 return AddRefInternal (pUnk);
67 [MethodImplAttribute(MethodImplOptions.InternalCall)]
68 public extern static IntPtr AllocCoTaskMem (int cb);
70 [MethodImplAttribute(MethodImplOptions.InternalCall)]
71 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
72 public extern static IntPtr AllocHGlobal (IntPtr cb);
74 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
75 public static IntPtr AllocHGlobal (int cb)
77 return AllocHGlobal ((IntPtr)cb);
81 public static object BindToMoniker (string monikerName)
83 throw new NotImplementedException ();
87 public static void ChangeWrapperHandleStrength (object otp, bool fIsWeak)
89 throw new NotImplementedException ();
92 [MethodImplAttribute(MethodImplOptions.InternalCall)]
93 internal extern static void copy_to_unmanaged (Array source, int startIndex,
94 IntPtr destination, int length);
96 [MethodImplAttribute(MethodImplOptions.InternalCall)]
97 internal extern static void copy_from_unmanaged (IntPtr source, int startIndex,
98 Array destination, int length);
100 public static void Copy (byte[] source, int startIndex, IntPtr destination, int length)
102 copy_to_unmanaged (source, startIndex, destination, length);
105 public static void Copy (char[] source, int startIndex, IntPtr destination, int length)
107 copy_to_unmanaged (source, startIndex, destination, length);
110 public static void Copy (short[] source, int startIndex, IntPtr destination, int length)
112 copy_to_unmanaged (source, startIndex, destination, length);
115 public static void Copy (int[] source, int startIndex, IntPtr destination, int length)
117 copy_to_unmanaged (source, startIndex, destination, length);
120 public static void Copy (long[] source, int startIndex, IntPtr destination, int length)
122 copy_to_unmanaged (source, startIndex, destination, length);
125 public static void Copy (float[] source, int startIndex, IntPtr destination, int length)
127 copy_to_unmanaged (source, startIndex, destination, length);
130 public static void Copy (double[] source, int startIndex, IntPtr destination, int length)
132 copy_to_unmanaged (source, startIndex, destination, length);
135 public static void Copy (IntPtr[] source, int startIndex, IntPtr destination, int length)
137 copy_to_unmanaged (source, startIndex, destination, length);
140 public static void Copy (IntPtr source, byte[] destination, int startIndex, int length)
142 copy_from_unmanaged (source, startIndex, destination, length);
145 public static void Copy (IntPtr source, char[] destination, int startIndex, int length)
147 copy_from_unmanaged (source, startIndex, destination, length);
150 public static void Copy (IntPtr source, short[] destination, int startIndex, int length)
152 copy_from_unmanaged (source, startIndex, destination, length);
155 public static void Copy (IntPtr source, int[] destination, int startIndex, int length)
157 copy_from_unmanaged (source, startIndex, destination, length);
160 public static void Copy (IntPtr source, long[] destination, int startIndex, int length)
162 copy_from_unmanaged (source, startIndex, destination, length);
165 public static void Copy (IntPtr source, float[] destination, int startIndex, int length)
167 copy_from_unmanaged (source, startIndex, destination, length);
170 public static void Copy (IntPtr source, double[] destination, int startIndex, int length)
172 copy_from_unmanaged (source, startIndex, destination, length);
175 public static void Copy (IntPtr source, IntPtr[] destination, int startIndex, int length)
177 copy_from_unmanaged (source, startIndex, destination, length);
180 public static IntPtr CreateAggregatedObject (IntPtr pOuter,
183 throw new NotImplementedException ();
187 public static object CreateWrapperOfType (object o, Type t)
189 __ComObject co = o as __ComObject;
191 throw new ArgumentException ("o must derive from __ComObject", "o");
193 throw new ArgumentNullException ("t");
195 Type[] itfs = o.GetType ().GetInterfaces ();
196 foreach (Type itf in itfs) {
197 if (itf.IsImport && co.GetInterface (itf) == IntPtr.Zero)
198 throw new InvalidCastException ();
201 return ComInteropProxy.GetProxy (co.IUnknown, t).GetTransparentProxy ();
205 [MethodImplAttribute(MethodImplOptions.InternalCall)]
207 public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
209 [MethodImplAttribute(MethodImplOptions.InternalCall)]
210 public extern static void FreeBSTR (IntPtr ptr);
212 [MethodImplAttribute(MethodImplOptions.InternalCall)]
213 public extern static void FreeCoTaskMem (IntPtr ptr);
215 [MethodImplAttribute(MethodImplOptions.InternalCall)]
216 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
217 public extern static void FreeHGlobal (IntPtr hglobal);
219 static void ClearBSTR (IntPtr ptr)
221 int len = ReadInt32 (ptr, -4);
223 for (int i = 0; i < len; i++)
224 WriteByte (ptr, i, 0);
227 public static void ZeroFreeBSTR (IntPtr s)
233 static void ClearAnsi (IntPtr ptr)
235 for (int i = 0; ReadByte (ptr, i) != 0; i++)
236 WriteByte (ptr, i, 0);
239 static void ClearUnicode (IntPtr ptr)
241 for (int i = 0; ReadInt16 (ptr, i) != 0; i += 2)
242 WriteInt16 (ptr, i, 0);
245 public static void ZeroFreeCoTaskMemAnsi (IntPtr s)
251 public static void ZeroFreeCoTaskMemUnicode (IntPtr s)
257 public static void ZeroFreeGlobalAllocAnsi (IntPtr s)
263 public static void ZeroFreeGlobalAllocUnicode (IntPtr s)
270 public static Guid GenerateGuidForType (Type type)
276 public static string GenerateProgIdForType (Type type)
278 throw new NotImplementedException ();
282 public static object GetActiveObject (string progID)
284 throw new NotImplementedException ();
287 [MethodImplAttribute (MethodImplOptions.InternalCall)]
288 private extern static IntPtr GetCCW (object o, Type T);
290 private static IntPtr GetComInterfaceForObjectInternal (object o, Type T)
293 return ((__ComObject)o).GetInterface (T);
295 return GetCCW (o, T);
298 public static IntPtr GetComInterfaceForObject (object o, Type T)
300 IntPtr pItf = GetComInterfaceForObjectInternal (o, T);
306 public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
308 throw new NotImplementedException ();
311 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
312 public static object GetComObjectData (object obj, object key)
314 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
317 [MethodImplAttribute(MethodImplOptions.InternalCall)]
318 private extern static int GetComSlotForMethodInfoInternal (MemberInfo m);
320 public static int GetComSlotForMethodInfo (MemberInfo m)
323 throw new ArgumentNullException ("m");
324 if (!(m is MethodInfo))
325 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
326 if (!m.DeclaringType.IsInterface)
327 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
328 return GetComSlotForMethodInfoInternal (m);
332 public static int GetEndComSlot (Type t)
334 throw new NotImplementedException ();
338 public static int GetExceptionCode()
340 throw new NotImplementedException ();
345 public static IntPtr GetExceptionPointers()
347 throw new NotImplementedException ();
350 public static IntPtr GetHINSTANCE (Module m)
353 throw new ArgumentNullException ("m");
355 return m.GetHINSTANCE ();
359 [MonoTODO ("SetErrorInfo")]
360 public static int GetHRForException (Exception e)
366 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
367 public static int GetHRForLastWin32Error()
369 throw new NotImplementedException ();
372 [MethodImplAttribute (MethodImplOptions.InternalCall)]
373 private extern static IntPtr GetIDispatchForObjectInternal (object o);
375 public static IntPtr GetIDispatchForObject (object o)
377 IntPtr pUnk = GetIDispatchForObjectInternal (o);
378 // Internal method does not AddRef
384 public static IntPtr GetIDispatchForObjectInContext (object o)
386 throw new NotImplementedException ();
390 public static IntPtr GetITypeInfoForType (Type t)
392 throw new NotImplementedException ();
395 [MethodImplAttribute (MethodImplOptions.InternalCall)]
396 private extern static IntPtr GetIUnknownForObjectInternal (object o);
398 public static IntPtr GetIUnknownForObject (object o)
400 IntPtr pUnk = GetIUnknownForObjectInternal (o);
401 // Internal method does not AddRef
407 public static IntPtr GetIUnknownForObjectInContext (object o)
409 throw new NotImplementedException ();
413 [Obsolete ("This method has been deprecated")]
414 public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
416 throw new NotImplementedException ();
420 public static MemberInfo GetMethodInfoForComSlot (Type t, int slot, ref ComMemberType memberType)
422 throw new NotImplementedException ();
425 public static void GetNativeVariantForObject (object obj, IntPtr pDstNativeVariant)
427 Variant vt = new Variant();
429 Marshal.StructureToPtr(vt, pDstNativeVariant, false);
432 [MethodImplAttribute (MethodImplOptions.InternalCall)]
433 private static extern object GetObjectForCCW (IntPtr pUnk);
435 public static object GetObjectForIUnknown (IntPtr pUnk)
437 object obj = GetObjectForCCW (pUnk);
440 ComInteropProxy proxy = ComInteropProxy.GetProxy (pUnk, typeof (__ComObject));
441 obj = proxy.GetTransparentProxy ();
446 public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant)
448 Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
449 return vt.GetValue();
452 public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars)
455 throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
456 object[] objects = new object[cVars];
457 for (int i = 0; i < cVars; i++)
458 objects[i] = GetObjectForNativeVariant ((IntPtr)(aSrcNativeVariant.ToInt64 () +
459 i * SizeOf (typeof(Variant))));
464 public static int GetStartComSlot (Type t)
466 throw new NotImplementedException ();
470 [Obsolete ("This method has been deprecated")]
471 public static Thread GetThreadFromFiberCookie (int cookie)
473 throw new NotImplementedException ();
476 public static object GetTypedObjectForIUnknown (IntPtr pUnk, Type t)
478 ComInteropProxy proxy = new ComInteropProxy (pUnk, t);
479 __ComObject co = (__ComObject)proxy.GetTransparentProxy ();
480 foreach (Type itf in t.GetInterfaces ()) {
481 if ((itf.Attributes & TypeAttributes.Import) == TypeAttributes.Import) {
482 if (co.GetInterface (itf) == IntPtr.Zero)
490 public static Type GetTypeForITypeInfo (IntPtr piTypeInfo)
492 throw new NotImplementedException ();
497 public static string GetTypeInfoName (UCOMITypeInfo pTI)
499 throw new NotImplementedException ();
502 public static string GetTypeInfoName (ITypeInfo typeInfo)
504 throw new NotImplementedException ();
509 public static Guid GetTypeLibGuid (UCOMITypeLib pTLB)
511 throw new NotImplementedException ();
515 public static Guid GetTypeLibGuid (ITypeLib typelib)
517 throw new NotImplementedException ();
521 public static Guid GetTypeLibGuidForAssembly (Assembly asm)
523 throw new NotImplementedException ();
528 public static int GetTypeLibLcid (UCOMITypeLib pTLB)
530 throw new NotImplementedException ();
534 public static int GetTypeLibLcid (ITypeLib typelib)
536 throw new NotImplementedException ();
541 public static string GetTypeLibName (UCOMITypeLib pTLB)
543 throw new NotImplementedException ();
547 public static string GetTypeLibName (ITypeLib typelib)
549 throw new NotImplementedException ();
553 public static void GetTypeLibVersionForAssembly (Assembly inputAssembly, out int majorVersion, out int minorVersion)
555 throw new NotImplementedException ();
558 public static object GetUniqueObjectForIUnknown (IntPtr unknown)
560 throw new NotImplementedException ();
564 [Obsolete ("This method has been deprecated")]
565 public static IntPtr GetUnmanagedThunkForManagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
567 throw new NotImplementedException ();
570 [MethodImplAttribute (MethodImplOptions.InternalCall)]
571 public extern static bool IsComObject (object o);
574 public static bool IsTypeVisibleFromCom (Type t)
576 throw new NotImplementedException ();
580 public static int NumParamBytes (MethodInfo m)
582 throw new NotImplementedException ();
586 [MethodImplAttribute(MethodImplOptions.InternalCall)]
587 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
588 public static extern int GetLastWin32Error();
590 [MethodImplAttribute(MethodImplOptions.InternalCall)]
591 public extern static IntPtr OffsetOf (Type t, string fieldName);
593 [MethodImplAttribute(MethodImplOptions.InternalCall)]
594 public extern static void Prelink (MethodInfo m);
596 [MethodImplAttribute(MethodImplOptions.InternalCall)]
597 public extern static void PrelinkAll (Type c);
599 [MethodImplAttribute(MethodImplOptions.InternalCall)]
600 public extern static string PtrToStringAnsi (IntPtr ptr);
602 [MethodImplAttribute(MethodImplOptions.InternalCall)]
603 public extern static string PtrToStringAnsi (IntPtr ptr, int len);
605 public static string PtrToStringAuto (IntPtr ptr)
607 return SystemDefaultCharSize == 2
608 ? PtrToStringUni (ptr) : PtrToStringAnsi (ptr);
611 public static string PtrToStringAuto (IntPtr ptr, int len)
613 return SystemDefaultCharSize == 2
614 ? PtrToStringUni (ptr, len) : PtrToStringAnsi (ptr, len);
617 [MethodImplAttribute(MethodImplOptions.InternalCall)]
618 public extern static string PtrToStringUni (IntPtr ptr);
620 [MethodImplAttribute(MethodImplOptions.InternalCall)]
621 public extern static string PtrToStringUni (IntPtr ptr, int len);
623 [MethodImplAttribute(MethodImplOptions.InternalCall)]
624 public extern static string PtrToStringBSTR (IntPtr ptr);
626 [MethodImplAttribute(MethodImplOptions.InternalCall)]
628 public extern static void PtrToStructure (IntPtr ptr, object structure);
630 [MethodImplAttribute(MethodImplOptions.InternalCall)]
632 public extern static object PtrToStructure (IntPtr ptr, Type structureType);
634 [MethodImplAttribute (MethodImplOptions.InternalCall)]
635 private extern static int QueryInterfaceInternal (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
637 public static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv)
639 if (pUnk == IntPtr.Zero)
640 throw new ArgumentException ("Value cannot be null.", "pUnk");
641 return QueryInterfaceInternal (pUnk, ref iid, out ppv);
644 public static byte ReadByte (IntPtr ptr)
646 return ReadByte (ptr, 0);
649 [MethodImplAttribute(MethodImplOptions.InternalCall)]
650 public extern static byte ReadByte (IntPtr ptr, int ofs);
653 [SuppressUnmanagedCodeSecurity]
654 public static byte ReadByte ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
656 throw new NotImplementedException ();
659 public static short ReadInt16 (IntPtr ptr)
661 return ReadInt16 (ptr, 0);
664 [MethodImplAttribute(MethodImplOptions.InternalCall)]
665 public extern static short ReadInt16 (IntPtr ptr, int ofs);
668 [SuppressUnmanagedCodeSecurity]
669 public static short ReadInt16 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
671 throw new NotImplementedException ();
674 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
675 public static int ReadInt32 (IntPtr ptr)
677 return ReadInt32 (ptr, 0);
680 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
681 [MethodImplAttribute(MethodImplOptions.InternalCall)]
682 public extern static int ReadInt32 (IntPtr ptr, int ofs);
684 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
686 [SuppressUnmanagedCodeSecurity]
687 public static int ReadInt32 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
689 throw new NotImplementedException ();
692 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
693 public static long ReadInt64 (IntPtr ptr)
695 return ReadInt64 (ptr, 0);
698 [MethodImplAttribute(MethodImplOptions.InternalCall)]
699 public extern static long ReadInt64 (IntPtr ptr, int ofs);
701 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
703 [SuppressUnmanagedCodeSecurity]
704 public static long ReadInt64 ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
706 throw new NotImplementedException ();
709 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
710 public static IntPtr ReadIntPtr (IntPtr ptr)
712 return ReadIntPtr (ptr, 0);
715 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
716 [MethodImplAttribute(MethodImplOptions.InternalCall)]
717 public extern static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
719 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
721 public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
723 throw new NotImplementedException ();
726 [MethodImplAttribute (MethodImplOptions.InternalCall)]
727 public extern static IntPtr ReAllocCoTaskMem (IntPtr pv, int cb);
729 [MethodImplAttribute(MethodImplOptions.InternalCall)]
730 public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
732 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
733 [MethodImplAttribute (MethodImplOptions.InternalCall)]
734 private extern static int ReleaseInternal (IntPtr pUnk);
736 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
737 public static int Release (IntPtr pUnk)
739 if (pUnk == IntPtr.Zero)
740 throw new ArgumentException ("Value cannot be null.", "pUnk");
741 return ReleaseInternal (pUnk);
745 [MethodImplAttribute (MethodImplOptions.InternalCall)]
746 private extern static int ReleaseComObjectInternal (object co);
748 public static int ReleaseComObject (object o)
751 throw new ArgumentException ("Value cannot be null.", "o");
752 if (!IsComObject (o))
753 throw new ArgumentException ("Value must be a Com object.", "o");
754 return ReleaseComObjectInternal (o);
759 public static void ReleaseThreadCache()
761 throw new NotImplementedException ();
764 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
765 public static bool SetComObjectData (object obj, object key, object data)
767 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
772 public static int SizeOf (object structure)
774 return SizeOf (structure.GetType ());
777 [MethodImplAttribute(MethodImplOptions.InternalCall)]
778 public extern static int SizeOf (Type t);
780 [MethodImplAttribute(MethodImplOptions.InternalCall)]
781 public extern static IntPtr StringToBSTR (string s);
784 // I believe this is wrong, because in Mono and in P/Invoke
785 // we treat "Ansi" conversions as UTF-8 conversions, while
786 // this one does not do this
788 public static IntPtr StringToCoTaskMemAnsi (string s)
790 int length = s.Length + 1;
791 IntPtr ctm = AllocCoTaskMem (length);
793 byte[] asBytes = new byte[length];
794 for (int i = 0; i < s.Length; i++)
795 asBytes[i] = (byte)s[i];
796 asBytes[s.Length] = 0;
798 copy_to_unmanaged (asBytes, 0, ctm, length);
802 public static IntPtr StringToCoTaskMemAuto (string s)
804 return SystemDefaultCharSize == 2
805 ? StringToCoTaskMemUni (s) : StringToCoTaskMemAnsi (s);
808 public static IntPtr StringToCoTaskMemUni (string s)
810 int length = s.Length + 1;
811 IntPtr ctm = AllocCoTaskMem (length * 2);
813 char[] asChars = new char[length];
814 s.CopyTo (0, asChars, 0, s.Length);
815 asChars[s.Length] = '\0';
817 copy_to_unmanaged (asChars, 0, ctm, length);
821 [MethodImplAttribute(MethodImplOptions.InternalCall)]
822 public extern static IntPtr StringToHGlobalAnsi (string s);
824 public static IntPtr StringToHGlobalAuto (string s)
826 return SystemDefaultCharSize == 2
827 ? StringToHGlobalUni (s) : StringToHGlobalAnsi (s);
830 [MethodImplAttribute(MethodImplOptions.InternalCall)]
831 public extern static IntPtr StringToHGlobalUni (string s);
834 public static IntPtr SecureStringToBSTR (SecureString s)
837 throw new ArgumentNullException ("s");
839 IntPtr ctm = AllocCoTaskMem ((len+1) * 2 + 4);
840 byte [] buffer = null;
841 WriteInt32 (ctm, 0, len*2);
843 buffer = s.GetBuffer ();
845 for (int i = 0; i < len; i++)
846 WriteInt16 (ctm, 4 + (i * 2), (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
847 WriteInt16 (ctm, 4 + buffer.Length, 0);
850 for (int i = buffer.Length; i > 0; ){
855 return (IntPtr) ((long)ctm + 4);
858 public static IntPtr SecureStringToCoTaskMemAnsi (SecureString s)
861 throw new ArgumentNullException ("s");
863 IntPtr ctm = AllocCoTaskMem (len + 1);
864 byte [] copy = new byte [len+1];
867 byte [] buffer = s.GetBuffer ();
869 for (; i < len; i++, j += 2){
870 copy [i] = buffer [j+1];
875 copy_to_unmanaged (copy, 0, ctm, len+1);
877 // Ensure that we clear the buffer.
878 for (int i = len; i > 0; ){
886 public static IntPtr SecureStringToCoTaskMemUnicode (SecureString s)
889 throw new ArgumentNullException ("s");
891 IntPtr ctm = AllocCoTaskMem (len * 2 + 2);
892 byte [] buffer = null;
894 buffer = s.GetBuffer ();
895 for (int i = 0; i < len; i++)
896 WriteInt16 (ctm, i * 2, (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
897 WriteInt16 (ctm, buffer.Length, 0);
900 for (int i = buffer.Length; i > 0; ){
908 public static IntPtr SecureStringToGlobalAllocAnsi (SecureString s)
911 throw new ArgumentNullException ("s");
912 return SecureStringToCoTaskMemAnsi (s);
915 public static IntPtr SecureStringToGlobalAllocUnicode (SecureString s)
918 throw new ArgumentNullException ("s");
919 return SecureStringToCoTaskMemUnicode (s);
923 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
925 [MethodImplAttribute(MethodImplOptions.InternalCall)]
926 public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
928 public static void ThrowExceptionForHR (int errorCode) {
929 Exception ex = GetExceptionForHR (errorCode);
934 public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo) {
935 Exception ex = GetExceptionForHR (errorCode, errorInfo);
940 [MethodImplAttribute(MethodImplOptions.InternalCall)]
941 public extern static IntPtr UnsafeAddrOfPinnedArrayElement (Array arr, int index);
943 public static void WriteByte (IntPtr ptr, byte val)
945 WriteByte (ptr, 0, val);
948 [MethodImplAttribute(MethodImplOptions.InternalCall)]
949 public extern static void WriteByte (IntPtr ptr, int ofs, byte val);
952 [SuppressUnmanagedCodeSecurity]
953 public static void WriteByte ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, byte val)
955 throw new NotImplementedException ();
958 public static void WriteInt16 (IntPtr ptr, short val)
960 WriteInt16 (ptr, 0, val);
963 [MethodImplAttribute(MethodImplOptions.InternalCall)]
964 public extern static void WriteInt16 (IntPtr ptr, int ofs, short val);
967 [SuppressUnmanagedCodeSecurity]
968 public static void WriteInt16 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, short val)
970 throw new NotImplementedException ();
973 public static void WriteInt16 (IntPtr ptr, char val)
975 WriteInt16 (ptr, 0, val);
979 [MethodImplAttribute(MethodImplOptions.InternalCall)]
980 public extern static void WriteInt16 (IntPtr ptr, int ofs, char val);
983 public static void WriteInt16([In, Out] object ptr, int ofs, char val)
985 throw new NotImplementedException ();
988 public static void WriteInt32 (IntPtr ptr, int val)
990 WriteInt32 (ptr, 0, val);
993 [MethodImplAttribute(MethodImplOptions.InternalCall)]
994 public extern static void WriteInt32 (IntPtr ptr, int ofs, int val);
997 [SuppressUnmanagedCodeSecurity]
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 [SuppressUnmanagedCodeSecurity]
1013 public static void WriteInt64 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, long val)
1015 throw new NotImplementedException ();
1018 public static void WriteIntPtr (IntPtr ptr, IntPtr val)
1020 WriteIntPtr (ptr, 0, val);
1023 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1024 public extern static void WriteIntPtr (IntPtr ptr, int ofs, IntPtr val);
1027 public static void WriteIntPtr([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val)
1029 throw new NotImplementedException ();
1032 public static Exception GetExceptionForHR (int errorCode) {
1033 return GetExceptionForHR (errorCode, IntPtr.Zero);
1036 public static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo) {
1038 const int E_OUTOFMEMORY = unchecked ((int)0x8007000EL);
1039 const int E_INVALIDARG = unchecked ((int)0X80070057);
1044 return new OutOfMemoryException ();
1046 return new ArgumentException ();
1049 return new COMException ("", errorCode);
1054 public static int FinalReleaseComObject (object o)
1056 while (ReleaseComObject (o) != 0);
1061 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1062 private static extern Delegate GetDelegateForFunctionPointerInternal (IntPtr ptr, Type t);
1064 public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t)
1067 throw new ArgumentNullException ("t");
1068 if (!t.IsSubclassOf (typeof (MulticastDelegate)) || (t == typeof (MulticastDelegate)))
1069 throw new ArgumentException ("Type is not a delegate", "t");
1070 if (t.IsGenericType)
1071 throw new ArgumentException ("The specified Type must not be a generic type definition.");
1072 if (ptr == IntPtr.Zero)
1073 throw new ArgumentNullException ("ptr");
1075 return GetDelegateForFunctionPointerInternal (ptr, t);
1078 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1079 private static extern IntPtr GetFunctionPointerForDelegateInternal (Delegate d);
1081 public static IntPtr GetFunctionPointerForDelegate (Delegate d)
1084 throw new ArgumentNullException ("d");
1086 return GetFunctionPointerForDelegateInternal (d);