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;
41 using System.Runtime.ConstrainedExecution;
43 using System.Runtime.InteropServices.ComTypes;
51 namespace System.Runtime.InteropServices
53 [SuppressUnmanagedCodeSecurity ()]
63 public static readonly int SystemMaxDBCSCharSize = 2; // don't know what this is
64 public static readonly int SystemDefaultCharSize;
68 SystemDefaultCharSize = Environment.OSVersion.Platform == PlatformID.Win32NT ? 2 : 1;
75 [MethodImplAttribute (MethodImplOptions.InternalCall)]
76 private extern static int AddRefInternal (IntPtr pUnk);
78 public static int AddRef (IntPtr pUnk)
80 if (pUnk == IntPtr.Zero)
81 throw new ArgumentException ("Value cannot be null.", "pUnk");
82 return AddRefInternal (pUnk);
85 [MethodImplAttribute(MethodImplOptions.InternalCall)]
86 public extern static IntPtr AllocCoTaskMem (int cb);
88 [MethodImplAttribute(MethodImplOptions.InternalCall)]
90 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
92 public extern static IntPtr AllocHGlobal (IntPtr cb);
95 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
97 public static IntPtr AllocHGlobal (int cb)
99 return AllocHGlobal ((IntPtr)cb);
103 public static object BindToMoniker (string monikerName)
105 throw new NotImplementedException ();
109 public static void ChangeWrapperHandleStrength (object otp, bool fIsWeak)
111 throw new NotImplementedException ();
114 [MethodImplAttribute(MethodImplOptions.InternalCall)]
115 extern static void copy_to_unmanaged (Array source, int startIndex,
116 IntPtr destination, int length);
118 [MethodImplAttribute(MethodImplOptions.InternalCall)]
119 extern static void copy_from_unmanaged (IntPtr source, int startIndex,
120 Array destination, int length);
122 public static void Copy (byte[] source, int startIndex, IntPtr destination, int length)
124 copy_to_unmanaged (source, startIndex, destination, length);
127 public static void Copy (char[] source, int startIndex, IntPtr destination, int length)
129 copy_to_unmanaged (source, startIndex, destination, length);
132 public static void Copy (short[] source, int startIndex, IntPtr destination, int length)
134 copy_to_unmanaged (source, startIndex, destination, length);
137 public static void Copy (int[] source, int startIndex, IntPtr destination, int length)
139 copy_to_unmanaged (source, startIndex, destination, length);
142 public static void Copy (long[] source, int startIndex, IntPtr destination, int length)
144 copy_to_unmanaged (source, startIndex, destination, length);
147 public static void Copy (float[] source, int startIndex, IntPtr destination, int length)
149 copy_to_unmanaged (source, startIndex, destination, length);
152 public static void Copy (double[] source, int startIndex, IntPtr destination, int length)
154 copy_to_unmanaged (source, startIndex, destination, length);
158 public static void Copy (IntPtr[] source, int startIndex, IntPtr destination, int length)
160 copy_to_unmanaged (source, startIndex, destination, length);
164 public static void Copy (IntPtr source, byte[] destination, int startIndex, int length)
166 copy_from_unmanaged (source, startIndex, destination, length);
169 public static void Copy (IntPtr source, char[] destination, int startIndex, int length)
171 copy_from_unmanaged (source, startIndex, destination, length);
174 public static void Copy (IntPtr source, short[] destination, int startIndex, int length)
176 copy_from_unmanaged (source, startIndex, destination, length);
179 public static void Copy (IntPtr source, int[] destination, int startIndex, int length)
181 copy_from_unmanaged (source, startIndex, destination, length);
184 public static void Copy (IntPtr source, long[] destination, int startIndex, int length)
186 copy_from_unmanaged (source, startIndex, destination, length);
189 public static void Copy (IntPtr source, float[] destination, int startIndex, int length)
191 copy_from_unmanaged (source, startIndex, destination, length);
194 public static void Copy (IntPtr source, double[] destination, int startIndex, int length)
196 copy_from_unmanaged (source, startIndex, destination, length);
200 public static void Copy (IntPtr source, IntPtr[] destination, int startIndex, int length)
202 copy_from_unmanaged (source, startIndex, destination, length);
205 public static IntPtr CreateAggregatedObject (IntPtr pOuter,
208 throw new NotImplementedException ();
213 public static object CreateWrapperOfType (object o, Type t)
215 __ComObject co = o as __ComObject;
217 throw new ArgumentException ("o must derive from __ComObject", "o");
219 throw new ArgumentNullException ("t");
221 Type[] itfs = o.GetType ().GetInterfaces ();
222 foreach (Type itf in itfs) {
223 if (itf.IsImport && co.GetInterface (itf) == IntPtr.Zero)
224 throw new InvalidCastException ();
227 return ComInteropProxy.GetProxy (co.IUnknown, t).GetTransparentProxy ();
231 [MethodImplAttribute(MethodImplOptions.InternalCall)]
235 public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
237 [MethodImplAttribute(MethodImplOptions.InternalCall)]
238 public extern static void FreeBSTR (IntPtr ptr);
240 [MethodImplAttribute(MethodImplOptions.InternalCall)]
241 public extern static void FreeCoTaskMem (IntPtr ptr);
243 [MethodImplAttribute(MethodImplOptions.InternalCall)]
245 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
247 public extern static void FreeHGlobal (IntPtr hglobal);
251 static void ClearBSTR (IntPtr ptr)
253 int len = ReadInt32 (ptr, -4);
255 for (int i = 0; i < len; i++)
256 WriteByte (ptr, i, 0);
259 public static void ZeroFreeBSTR (IntPtr s)
265 static void ClearAnsi (IntPtr ptr)
267 for (int i = 0; ReadByte (ptr, i) != 0; i++)
268 WriteByte (ptr, i, 0);
271 static void ClearUnicode (IntPtr ptr)
273 for (int i = 0; ReadInt16 (ptr, i) != 0; i += 2)
274 WriteInt16 (ptr, i, 0);
277 public static void ZeroFreeCoTaskMemAnsi (IntPtr s)
283 public static void ZeroFreeCoTaskMemUnicode (IntPtr s)
289 public static void ZeroFreeGlobalAllocAnsi (IntPtr s)
295 public static void ZeroFreeGlobalAllocUnicode (IntPtr s)
303 public static Guid GenerateGuidForType (Type type)
309 public static string GenerateProgIdForType (Type type)
311 throw new NotImplementedException ();
315 public static object GetActiveObject (string progID)
317 throw new NotImplementedException ();
320 [MethodImplAttribute (MethodImplOptions.InternalCall)]
321 private extern static IntPtr GetCCW (object o, Type T);
323 private static IntPtr GetComInterfaceForObjectInternal (object o, Type T)
326 return ((__ComObject)o).GetInterface (T);
328 return GetCCW (o, T);
331 public static IntPtr GetComInterfaceForObject (object o, Type T)
333 IntPtr pItf = GetComInterfaceForObjectInternal (o, T);
340 public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
342 throw new NotImplementedException ();
346 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
347 public static object GetComObjectData (object obj, object key)
349 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
352 [MethodImplAttribute(MethodImplOptions.InternalCall)]
353 private extern static int GetComSlotForMethodInfoInternal (MemberInfo m);
355 public static int GetComSlotForMethodInfo (MemberInfo m)
359 throw new ArgumentNullException ("m");
361 throw new ArgumentNullException (null, "Value cannot be null.");
363 if (!(m is MethodInfo))
364 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
365 if (!m.DeclaringType.IsInterface)
366 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
367 return GetComSlotForMethodInfoInternal (m);
371 public static int GetEndComSlot (Type t)
373 throw new NotImplementedException ();
377 public static int GetExceptionCode()
379 throw new NotImplementedException ();
386 public static IntPtr GetExceptionPointers()
388 throw new NotImplementedException ();
391 public static IntPtr GetHINSTANCE (Module m)
394 throw new ArgumentNullException ("m");
396 return m.GetHINSTANCE ();
400 [MonoTODO ("SetErrorInfo")]
401 public static int GetHRForException (Exception e)
408 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
410 public static int GetHRForLastWin32Error()
412 throw new NotImplementedException ();
415 [MethodImplAttribute (MethodImplOptions.InternalCall)]
416 private extern static IntPtr GetIDispatchForObjectInternal (object o);
418 public static IntPtr GetIDispatchForObject (object o)
420 IntPtr pUnk = GetIDispatchForObjectInternal (o);
421 // Internal method does not AddRef
428 public static IntPtr GetIDispatchForObjectInContext (object o)
430 throw new NotImplementedException ();
435 public static IntPtr GetITypeInfoForType (Type t)
437 throw new NotImplementedException ();
440 [MethodImplAttribute (MethodImplOptions.InternalCall)]
441 private extern static IntPtr GetIUnknownForObjectInternal (object o);
443 public static IntPtr GetIUnknownForObject (object o)
445 IntPtr pUnk = GetIUnknownForObjectInternal (o);
446 // Internal method does not AddRef
453 public static IntPtr GetIUnknownForObjectInContext (object o)
455 throw new NotImplementedException ();
461 [Obsolete ("This method has been deprecated")]
463 public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
465 throw new NotImplementedException ();
469 public static MemberInfo GetMethodInfoForComSlot (Type t, int slot, ref ComMemberType memberType)
471 throw new NotImplementedException ();
474 public static void GetNativeVariantForObject (object obj, IntPtr pDstNativeVariant)
476 Variant vt = new Variant();
478 Marshal.StructureToPtr(vt, pDstNativeVariant, false);
481 [MethodImplAttribute (MethodImplOptions.InternalCall)]
482 private static extern object GetObjectForCCW (IntPtr pUnk);
484 public static object GetObjectForIUnknown (IntPtr pUnk)
486 object obj = GetObjectForCCW (pUnk);
489 ComInteropProxy proxy = ComInteropProxy.GetProxy (pUnk, typeof (__ComObject));
490 obj = proxy.GetTransparentProxy ();
495 public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant)
497 Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
498 return vt.GetValue();
501 public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars)
504 throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
505 object[] objects = new object[cVars];
506 for (int i = 0; i < cVars; i++)
507 objects[i] = GetObjectForNativeVariant ((IntPtr)(aSrcNativeVariant.ToInt64 () +
508 i * SizeOf (typeof(Variant))));
513 public static int GetStartComSlot (Type t)
515 throw new NotImplementedException ();
520 [Obsolete ("This method has been deprecated")]
522 public static Thread GetThreadFromFiberCookie (int cookie)
524 throw new NotImplementedException ();
527 public static object GetTypedObjectForIUnknown (IntPtr pUnk, Type t)
529 ComInteropProxy proxy = new ComInteropProxy (pUnk, t);
530 __ComObject co = (__ComObject)proxy.GetTransparentProxy ();
531 foreach (Type itf in t.GetInterfaces ()) {
532 if ((itf.Attributes & TypeAttributes.Import) == TypeAttributes.Import) {
533 if (co.GetInterface (itf) == IntPtr.Zero)
541 public static Type GetTypeForITypeInfo (IntPtr piTypeInfo)
543 throw new NotImplementedException ();
550 public static string GetTypeInfoName (UCOMITypeInfo pTI)
552 throw new NotImplementedException ();
556 public static string GetTypeInfoName (ITypeInfo typeInfo)
558 throw new NotImplementedException ();
566 public static Guid GetTypeLibGuid (UCOMITypeLib pTLB)
568 throw new NotImplementedException ();
573 public static Guid GetTypeLibGuid (ITypeLib typelib)
575 throw new NotImplementedException ();
580 public static Guid GetTypeLibGuidForAssembly (Assembly asm)
582 throw new NotImplementedException ();
589 public static int GetTypeLibLcid (UCOMITypeLib pTLB)
591 throw new NotImplementedException ();
596 public static int GetTypeLibLcid (ITypeLib typelib)
598 throw new NotImplementedException ();
606 public static string GetTypeLibName (UCOMITypeLib pTLB)
608 throw new NotImplementedException ();
613 public static string GetTypeLibName (ITypeLib typelib)
615 throw new NotImplementedException ();
619 public static void GetTypeLibVersionForAssembly (Assembly inputAssembly, out int majorVersion, out int minorVersion)
621 throw new NotImplementedException ();
624 public static object GetUniqueObjectForIUnknown (IntPtr unknown)
626 throw new NotImplementedException ();
632 [Obsolete ("This method has been deprecated")]
634 public static IntPtr GetUnmanagedThunkForManagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
636 throw new NotImplementedException ();
639 [MethodImplAttribute (MethodImplOptions.InternalCall)]
640 public extern static bool IsComObject (object o);
643 public static bool IsTypeVisibleFromCom (Type t)
645 throw new NotImplementedException ();
649 public static int NumParamBytes (MethodInfo m)
651 throw new NotImplementedException ();
655 [MethodImplAttribute(MethodImplOptions.InternalCall)]
657 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
659 public static extern int GetLastWin32Error();
661 [MethodImplAttribute(MethodImplOptions.InternalCall)]
662 public extern static IntPtr OffsetOf (Type t, string fieldName);
664 [MethodImplAttribute(MethodImplOptions.InternalCall)]
665 public extern static void Prelink (MethodInfo m);
667 [MethodImplAttribute(MethodImplOptions.InternalCall)]
668 public extern static void PrelinkAll (Type c);
670 [MethodImplAttribute(MethodImplOptions.InternalCall)]
671 public extern static string PtrToStringAnsi (IntPtr ptr);
673 [MethodImplAttribute(MethodImplOptions.InternalCall)]
674 public extern static string PtrToStringAnsi (IntPtr ptr, int len);
676 public static string PtrToStringAuto (IntPtr ptr)
678 return SystemDefaultCharSize == 2
679 ? PtrToStringUni (ptr) : PtrToStringAnsi (ptr);
682 public static string PtrToStringAuto (IntPtr ptr, int len)
684 return SystemDefaultCharSize == 2
685 ? PtrToStringUni (ptr, len) : PtrToStringAnsi (ptr, len);
688 [MethodImplAttribute(MethodImplOptions.InternalCall)]
689 public extern static string PtrToStringUni (IntPtr ptr);
691 [MethodImplAttribute(MethodImplOptions.InternalCall)]
692 public extern static string PtrToStringUni (IntPtr ptr, int len);
694 [MethodImplAttribute(MethodImplOptions.InternalCall)]
695 public extern static string PtrToStringBSTR (IntPtr ptr);
697 [MethodImplAttribute(MethodImplOptions.InternalCall)]
701 public extern static void PtrToStructure (IntPtr ptr, object structure);
703 [MethodImplAttribute(MethodImplOptions.InternalCall)]
707 public extern static object PtrToStructure (IntPtr ptr, Type structureType);
709 [MethodImplAttribute (MethodImplOptions.InternalCall)]
710 private extern static int QueryInterfaceInternal (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
712 public static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv)
714 if (pUnk == IntPtr.Zero)
715 throw new ArgumentException ("Value cannot be null.", "pUnk");
716 return QueryInterfaceInternal (pUnk, ref iid, out ppv);
719 public static byte ReadByte (IntPtr ptr)
721 return ReadByte (ptr, 0);
724 [MethodImplAttribute(MethodImplOptions.InternalCall)]
725 public extern static byte ReadByte (IntPtr ptr, int ofs);
728 public static byte ReadByte ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
730 throw new NotImplementedException ();
733 public static short ReadInt16 (IntPtr ptr)
735 return ReadInt16 (ptr, 0);
738 [MethodImplAttribute(MethodImplOptions.InternalCall)]
739 public extern static short ReadInt16 (IntPtr ptr, int ofs);
742 public static short ReadInt16 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
744 throw new NotImplementedException ();
748 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
750 public static int ReadInt32 (IntPtr ptr)
752 return ReadInt32 (ptr, 0);
756 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
758 [MethodImplAttribute(MethodImplOptions.InternalCall)]
759 public extern static int ReadInt32 (IntPtr ptr, int ofs);
762 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
765 public static int ReadInt32 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
767 throw new NotImplementedException ();
771 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
773 public static long ReadInt64 (IntPtr ptr)
775 return ReadInt64 (ptr, 0);
779 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
781 [MethodImplAttribute(MethodImplOptions.InternalCall)]
782 public extern static long ReadInt64 (IntPtr ptr, int ofs);
785 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
788 public static long ReadInt64 ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
790 throw new NotImplementedException ();
794 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
796 public static IntPtr ReadIntPtr (IntPtr ptr)
798 return ReadIntPtr (ptr, 0);
802 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
804 [MethodImplAttribute(MethodImplOptions.InternalCall)]
805 public extern static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
808 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
811 public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
813 throw new NotImplementedException ();
816 [MethodImplAttribute (MethodImplOptions.InternalCall)]
817 public extern static IntPtr ReAllocCoTaskMem (IntPtr pv, int cb);
819 [MethodImplAttribute(MethodImplOptions.InternalCall)]
820 public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
823 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
825 [MethodImplAttribute (MethodImplOptions.InternalCall)]
826 private extern static int ReleaseInternal (IntPtr pUnk);
829 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
831 public static int Release (IntPtr pUnk)
833 if (pUnk == IntPtr.Zero)
834 throw new ArgumentException ("Value cannot be null.", "pUnk");
835 return ReleaseInternal (pUnk);
839 [MethodImplAttribute (MethodImplOptions.InternalCall)]
840 private extern static int ReleaseComObjectInternal (object co);
842 public static int ReleaseComObject (object o)
845 throw new ArgumentException ("Value cannot be null.", "o");
846 if (!IsComObject (o))
847 throw new ArgumentException ("Value must be a Com object.", "o");
848 return ReleaseComObjectInternal (o);
855 public static void ReleaseThreadCache()
857 throw new NotImplementedException ();
860 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
861 public static bool SetComObjectData (object obj, object key, object data)
863 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
870 public static int SizeOf (object structure)
872 return SizeOf (structure.GetType ());
875 [MethodImplAttribute(MethodImplOptions.InternalCall)]
876 public extern static int SizeOf (Type t);
878 [MethodImplAttribute(MethodImplOptions.InternalCall)]
879 public extern static IntPtr StringToBSTR (string s);
882 // I believe this is wrong, because in Mono and in P/Invoke
883 // we treat "Ansi" conversions as UTF-8 conversions, while
884 // this one does not do this
886 public static IntPtr StringToCoTaskMemAnsi (string s)
888 int length = s.Length + 1;
889 IntPtr ctm = AllocCoTaskMem (length);
891 byte[] asBytes = new byte[length];
892 for (int i = 0; i < s.Length; i++)
893 asBytes[i] = (byte)s[i];
894 asBytes[s.Length] = 0;
896 copy_to_unmanaged (asBytes, 0, ctm, length);
900 public static IntPtr StringToCoTaskMemAuto (string s)
902 return SystemDefaultCharSize == 2
903 ? StringToCoTaskMemUni (s) : StringToCoTaskMemAnsi (s);
906 public static IntPtr StringToCoTaskMemUni (string s)
908 int length = s.Length + 1;
909 IntPtr ctm = AllocCoTaskMem (length * 2);
911 char[] asChars = new char[length];
912 s.CopyTo (0, asChars, 0, s.Length);
913 asChars[s.Length] = '\0';
915 copy_to_unmanaged (asChars, 0, ctm, length);
919 [MethodImplAttribute(MethodImplOptions.InternalCall)]
920 public extern static IntPtr StringToHGlobalAnsi (string s);
922 public static IntPtr StringToHGlobalAuto (string s)
924 return SystemDefaultCharSize == 2
925 ? StringToHGlobalUni (s) : StringToHGlobalAnsi (s);
928 [MethodImplAttribute(MethodImplOptions.InternalCall)]
929 public extern static IntPtr StringToHGlobalUni (string s);
931 #if NET_2_0 && !NET_2_1
932 public static IntPtr SecureStringToBSTR (SecureString s)
935 throw new ArgumentNullException ("s");
937 IntPtr ctm = AllocCoTaskMem ((len+1) * 2 + 4);
938 byte [] buffer = null;
939 WriteInt32 (ctm, 0, len*2);
941 buffer = s.GetBuffer ();
943 for (int i = 0; i < len; i++)
944 WriteInt16 (ctm, 4 + (i * 2), (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
945 WriteInt16 (ctm, 4 + buffer.Length, 0);
948 for (int i = buffer.Length; i > 0; ){
953 return (IntPtr) ((long)ctm + 4);
956 public static IntPtr SecureStringToCoTaskMemAnsi (SecureString s)
959 throw new ArgumentNullException ("s");
961 IntPtr ctm = AllocCoTaskMem (len + 1);
962 byte [] copy = new byte [len+1];
965 byte [] buffer = s.GetBuffer ();
967 for (; i < len; i++, j += 2){
968 copy [i] = buffer [j+1];
973 copy_to_unmanaged (copy, 0, ctm, len+1);
975 // Ensure that we clear the buffer.
976 for (int i = len; i > 0; ){
984 public static IntPtr SecureStringToCoTaskMemUnicode (SecureString s)
987 throw new ArgumentNullException ("s");
989 IntPtr ctm = AllocCoTaskMem (len * 2 + 2);
990 byte [] buffer = null;
992 buffer = s.GetBuffer ();
993 for (int i = 0; i < len; i++)
994 WriteInt16 (ctm, i * 2, (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
995 WriteInt16 (ctm, buffer.Length, 0);
998 for (int i = buffer.Length; i > 0; ){
1006 public static IntPtr SecureStringToGlobalAllocAnsi (SecureString s)
1009 throw new ArgumentNullException ("s");
1010 return SecureStringToCoTaskMemAnsi (s);
1013 public static IntPtr SecureStringToGlobalAllocUnicode (SecureString s)
1016 throw new ArgumentNullException ("s");
1017 return SecureStringToCoTaskMemUnicode (s);
1022 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1025 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1026 public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
1028 public static void ThrowExceptionForHR (int errorCode) {
1029 Exception ex = GetExceptionForHR (errorCode);
1034 public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo) {
1035 Exception ex = GetExceptionForHR (errorCode, errorInfo);
1040 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1041 public extern static IntPtr UnsafeAddrOfPinnedArrayElement (Array arr, int index);
1043 public static void WriteByte (IntPtr ptr, byte val)
1045 WriteByte (ptr, 0, val);
1048 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1049 public extern static void WriteByte (IntPtr ptr, int ofs, byte val);
1052 public static void WriteByte ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, byte val)
1054 throw new NotImplementedException ();
1057 public static void WriteInt16 (IntPtr ptr, short val)
1059 WriteInt16 (ptr, 0, val);
1062 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1063 public extern static void WriteInt16 (IntPtr ptr, int ofs, short val);
1066 public static void WriteInt16 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, short val)
1068 throw new NotImplementedException ();
1071 public static void WriteInt16 (IntPtr ptr, char val)
1073 WriteInt16 (ptr, 0, val);
1077 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1078 public extern static void WriteInt16 (IntPtr ptr, int ofs, char val);
1081 public static void WriteInt16([In, Out] object ptr, int ofs, char val)
1083 throw new NotImplementedException ();
1086 public static void WriteInt32 (IntPtr ptr, int val)
1088 WriteInt32 (ptr, 0, val);
1091 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1092 public extern static void WriteInt32 (IntPtr ptr, int ofs, int val);
1095 public static void WriteInt32([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, int val)
1097 throw new NotImplementedException ();
1100 public static void WriteInt64 (IntPtr ptr, long val)
1102 WriteInt64 (ptr, 0, val);
1105 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1106 public extern static void WriteInt64 (IntPtr ptr, int ofs, long val);
1109 public static void WriteInt64 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, long val)
1111 throw new NotImplementedException ();
1114 public static void WriteIntPtr (IntPtr ptr, IntPtr val)
1116 WriteIntPtr (ptr, 0, val);
1119 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1120 public extern static void WriteIntPtr (IntPtr ptr, int ofs, IntPtr val);
1123 public static void WriteIntPtr([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val)
1125 throw new NotImplementedException ();
1133 static Exception GetExceptionForHR (int errorCode) {
1134 return GetExceptionForHR (errorCode, IntPtr.Zero);
1142 static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo) {
1144 const int E_OUTOFMEMORY = unchecked ((int)0x8007000EL);
1145 const int E_INVALIDARG = unchecked ((int)0X80070057);
1150 return new OutOfMemoryException ();
1152 return new ArgumentException ();
1155 return new COMException ("", errorCode);
1159 #if NET_2_0 && !NET_2_1
1160 public static int FinalReleaseComObject (object o)
1162 while (ReleaseComObject (o) != 0);
1167 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1168 private static extern Delegate GetDelegateForFunctionPointerInternal (IntPtr ptr, Type t);
1170 public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t)
1173 throw new ArgumentNullException ("t");
1174 if (!t.IsSubclassOf (typeof (MulticastDelegate)) || (t == typeof (MulticastDelegate)))
1175 throw new ArgumentException ("Type is not a delegate", "t");
1176 if (ptr == IntPtr.Zero)
1177 throw new ArgumentNullException ("ptr");
1179 return GetDelegateForFunctionPointerInternal (ptr, t);
1182 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1183 private static extern IntPtr GetFunctionPointerForDelegateInternal (Delegate d);
1185 public static IntPtr GetFunctionPointerForDelegate (Delegate d)
1188 throw new ArgumentNullException ("d");
1190 return GetFunctionPointerForDelegateInternal (d);