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.
34 using System.Collections;
35 using System.Runtime.CompilerServices;
37 using System.Security;
38 using System.Reflection;
39 using System.Threading;
42 using System.Runtime.ConstrainedExecution;
43 using System.Runtime.InteropServices.ComTypes;
46 namespace System.Runtime.InteropServices
48 [SuppressUnmanagedCodeSecurity ()]
58 public static readonly int SystemMaxDBCSCharSize = 2; // don't know what this is
59 public static readonly int SystemDefaultCharSize;
63 SystemDefaultCharSize = Environment.OSVersion.Platform == PlatformID.Win32NT ? 2 : 1;
70 [MethodImplAttribute (MethodImplOptions.InternalCall)]
71 private extern static int AddRefInternal (IntPtr pUnk);
73 public static int AddRef (IntPtr pUnk)
75 if (pUnk == IntPtr.Zero)
76 throw new ArgumentException ("Value cannot be null.", "pUnk");
77 return AddRefInternal (pUnk);
80 [MethodImplAttribute(MethodImplOptions.InternalCall)]
81 public extern static IntPtr AllocCoTaskMem (int cb);
83 [MethodImplAttribute(MethodImplOptions.InternalCall)]
85 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
87 public extern static IntPtr AllocHGlobal (IntPtr cb);
90 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
92 public static IntPtr AllocHGlobal (int cb)
94 return AllocHGlobal ((IntPtr)cb);
98 public static object BindToMoniker (string monikerName)
100 throw new NotImplementedException ();
104 public static void ChangeWrapperHandleStrength (object otp, bool fIsWeak)
106 throw new NotImplementedException ();
109 [MethodImplAttribute(MethodImplOptions.InternalCall)]
110 extern static void copy_to_unmanaged (Array source, int startIndex,
111 IntPtr destination, int length);
113 [MethodImplAttribute(MethodImplOptions.InternalCall)]
114 extern static void copy_from_unmanaged (IntPtr source, int startIndex,
115 Array destination, int length);
117 public static void Copy (byte[] source, int startIndex, IntPtr destination, int length)
119 copy_to_unmanaged (source, startIndex, destination, length);
122 public static void Copy (char[] source, int startIndex, IntPtr destination, int length)
124 copy_to_unmanaged (source, startIndex, destination, length);
127 public static void Copy (short[] source, int startIndex, IntPtr destination, int length)
129 copy_to_unmanaged (source, startIndex, destination, length);
132 public static void Copy (int[] source, int startIndex, IntPtr destination, int length)
134 copy_to_unmanaged (source, startIndex, destination, length);
137 public static void Copy (long[] source, int startIndex, IntPtr destination, int length)
139 copy_to_unmanaged (source, startIndex, destination, length);
142 public static void Copy (float[] source, int startIndex, IntPtr destination, int length)
144 copy_to_unmanaged (source, startIndex, destination, length);
147 public static void Copy (double[] source, int startIndex, IntPtr destination, int length)
149 copy_to_unmanaged (source, startIndex, destination, length);
153 public static void Copy (IntPtr[] source, int startIndex, IntPtr destination, int length)
155 copy_to_unmanaged (source, startIndex, destination, length);
159 public static void Copy (IntPtr source, byte[] destination, int startIndex, int length)
161 copy_from_unmanaged (source, startIndex, destination, length);
164 public static void Copy (IntPtr source, char[] destination, int startIndex, int length)
166 copy_from_unmanaged (source, startIndex, destination, length);
169 public static void Copy (IntPtr source, short[] destination, int startIndex, int length)
171 copy_from_unmanaged (source, startIndex, destination, length);
174 public static void Copy (IntPtr source, int[] destination, int startIndex, int length)
176 copy_from_unmanaged (source, startIndex, destination, length);
179 public static void Copy (IntPtr source, long[] destination, int startIndex, int length)
181 copy_from_unmanaged (source, startIndex, destination, length);
184 public static void Copy (IntPtr source, float[] destination, int startIndex, int length)
186 copy_from_unmanaged (source, startIndex, destination, length);
189 public static void Copy (IntPtr source, double[] destination, int startIndex, int length)
191 copy_from_unmanaged (source, startIndex, destination, length);
195 public static void Copy (IntPtr source, IntPtr[] destination, int startIndex, int length)
197 copy_from_unmanaged (source, startIndex, destination, length);
200 public static IntPtr CreateAggregatedObject (IntPtr pOuter,
203 throw new NotImplementedException ();
207 public static object CreateWrapperOfType (object o, Type t)
209 __ComObject co = o as __ComObject;
211 throw new ArgumentException ("o must derive from __ComObject", "o");
213 throw new ArgumentNullException ("t");
215 Type[] itfs = o.GetType ().GetInterfaces ();
216 foreach (Type itf in itfs) {
217 if (itf.IsImport && co.GetInterface (itf) == IntPtr.Zero)
218 throw new InvalidCastException ();
221 return ComInteropProxy.GetProxy (co.IUnknown, t).GetTransparentProxy ();
224 [MethodImplAttribute(MethodImplOptions.InternalCall)]
228 public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
230 [MethodImplAttribute(MethodImplOptions.InternalCall)]
231 public extern static void FreeBSTR (IntPtr ptr);
233 [MethodImplAttribute(MethodImplOptions.InternalCall)]
234 public extern static void FreeCoTaskMem (IntPtr ptr);
236 [MethodImplAttribute(MethodImplOptions.InternalCall)]
238 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
240 public extern static void FreeHGlobal (IntPtr hglobal);
244 static void ClearBSTR (IntPtr ptr)
246 int len = ReadInt32 (ptr, -4);
248 for (int i = 0; i < len; i++)
249 WriteByte (ptr, i, 0);
252 public static void ZeroFreeBSTR (IntPtr s)
258 static void ClearAnsi (IntPtr ptr)
260 for (int i = 0; ReadByte (ptr, i) != 0; i++)
261 WriteByte (ptr, i, 0);
264 static void ClearUnicode (IntPtr ptr)
266 for (int i = 0; ReadInt16 (ptr, i) != 0; i += 2)
267 WriteInt16 (ptr, i, 0);
270 public static void ZeroFreeCoTaskMemAnsi (IntPtr s)
276 public static void ZeroFreeCoTaskMemUnicode (IntPtr s)
282 public static void ZeroFreeGlobalAllocAnsi (IntPtr s)
288 public static void ZeroFreeGlobalAllocUnicode (IntPtr s)
296 public static Guid GenerateGuidForType (Type type)
298 throw new NotImplementedException ();
302 public static string GenerateProgIdForType (Type type)
304 throw new NotImplementedException ();
308 public static object GetActiveObject (string progID)
310 throw new NotImplementedException ();
313 [MethodImplAttribute (MethodImplOptions.InternalCall)]
314 private extern static IntPtr GetCCW (object o, Type T);
316 private static IntPtr GetComInterfaceForObjectInternal (object o, Type T)
319 return ((__ComObject)o).GetInterface (T);
321 return GetCCW (o, T);
324 public static IntPtr GetComInterfaceForObject (object o, Type T)
326 IntPtr pItf = GetComInterfaceForObjectInternal (o, T);
333 public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
335 throw new NotImplementedException ();
339 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
340 public static object GetComObjectData (object obj, object key)
342 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
345 [MethodImplAttribute(MethodImplOptions.InternalCall)]
346 private extern static int GetComSlotForMethodInfoInternal (MemberInfo m);
348 public static int GetComSlotForMethodInfo (MemberInfo m)
352 throw new ArgumentNullException ("m");
354 throw new ArgumentNullException (null, "Value cannot be null.");
356 if (!(m is MethodInfo))
357 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
358 if (!m.DeclaringType.IsInterface)
359 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
360 return GetComSlotForMethodInfoInternal (m);
364 public static int GetEndComSlot (Type t)
366 throw new NotImplementedException ();
370 public static int GetExceptionCode()
372 throw new NotImplementedException ();
379 public static IntPtr GetExceptionPointers()
381 throw new NotImplementedException ();
384 public static IntPtr GetHINSTANCE (Module m)
387 throw new ArgumentNullException ("m");
389 return m.GetHINSTANCE ();
392 [MonoTODO ("SetErrorInfo")]
393 public static int GetHRForException (Exception e)
400 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
402 public static int GetHRForLastWin32Error()
404 throw new NotImplementedException ();
407 [MethodImplAttribute (MethodImplOptions.InternalCall)]
408 private extern static IntPtr GetIDispatchForObjectInternal (object o);
410 public static IntPtr GetIDispatchForObject (object o)
412 IntPtr pUnk = GetIDispatchForObjectInternal (o);
413 // Internal method does not AddRef
420 public static IntPtr GetIDispatchForObjectInContext (object o)
422 throw new NotImplementedException ();
427 public static IntPtr GetITypeInfoForType (Type t)
429 throw new NotImplementedException ();
432 [MethodImplAttribute (MethodImplOptions.InternalCall)]
433 private extern static IntPtr GetIUnknownForObjectInternal (object o);
435 public static IntPtr GetIUnknownForObject (object o)
437 IntPtr pUnk = GetIUnknownForObjectInternal (o);
438 // Internal method does not AddRef
445 public static IntPtr GetIUnknownForObjectInContext (object o)
447 throw new NotImplementedException ();
451 [MethodImplAttribute(MethodImplOptions.InternalCall)]
453 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
455 public static extern int GetLastWin32Error();
459 [Obsolete ("This method has been deprecated")]
461 public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
463 throw new NotImplementedException ();
467 public static MemberInfo GetMethodInfoForComSlot (Type t, int slot, ref ComMemberType memberType)
469 throw new NotImplementedException ();
472 public static void GetNativeVariantForObject (object obj, IntPtr pDstNativeVariant)
474 Variant vt = new Variant();
476 Marshal.StructureToPtr(vt, pDstNativeVariant, false);
479 [MethodImplAttribute (MethodImplOptions.InternalCall)]
480 private static extern object GetObjectForCCW (IntPtr pUnk);
482 public static object GetObjectForIUnknown (IntPtr pUnk)
484 object obj = GetObjectForCCW (pUnk);
487 ComInteropProxy proxy = ComInteropProxy.GetProxy (pUnk, typeof (__ComObject));
488 obj = proxy.GetTransparentProxy ();
493 public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant)
495 Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
496 return vt.GetValue();
499 public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars)
502 throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
503 object[] objects = new object[cVars];
504 for (int i = 0; i < cVars; i++)
505 objects[i] = GetObjectForNativeVariant ((IntPtr)(aSrcNativeVariant.ToInt64 () +
506 i * SizeOf (typeof(Variant))));
511 public static int GetStartComSlot (Type t)
513 throw new NotImplementedException ();
518 [Obsolete ("This method has been deprecated")]
520 public static Thread GetThreadFromFiberCookie (int cookie)
522 throw new NotImplementedException ();
525 public static object GetTypedObjectForIUnknown (IntPtr pUnk, Type t)
527 ComInteropProxy proxy = new ComInteropProxy (pUnk, t);
528 __ComObject co = (__ComObject)proxy.GetTransparentProxy ();
529 foreach (Type itf in t.GetInterfaces ()) {
530 if ((itf.Attributes & TypeAttributes.Import) == TypeAttributes.Import) {
531 if (co.GetInterface (itf) == IntPtr.Zero)
539 public static Type GetTypeForITypeInfo (IntPtr piTypeInfo)
541 throw new NotImplementedException ();
548 public static string GetTypeInfoName (UCOMITypeInfo pTI)
550 throw new NotImplementedException ();
554 public static string GetTypeInfoName (ITypeInfo typeInfo)
556 throw new NotImplementedException ();
564 public static Guid GetTypeLibGuid (UCOMITypeLib pTLB)
566 throw new NotImplementedException ();
571 public static Guid GetTypeLibGuid (ITypeLib typelib)
573 throw new NotImplementedException ();
578 public static Guid GetTypeLibGuidForAssembly (Assembly asm)
580 throw new NotImplementedException ();
587 public static int GetTypeLibLcid (UCOMITypeLib pTLB)
589 throw new NotImplementedException ();
594 public static int GetTypeLibLcid (ITypeLib typelib)
596 throw new NotImplementedException ();
604 public static string GetTypeLibName (UCOMITypeLib pTLB)
606 throw new NotImplementedException ();
611 public static string GetTypeLibName (ITypeLib typelib)
613 throw new NotImplementedException ();
617 public static void GetTypeLibVersionForAssembly (Assembly inputAssembly, out int majorVersion, out int minorVersion)
619 throw new NotImplementedException ();
622 public static object GetUniqueObjectForIUnknown (IntPtr unknown)
624 throw new NotImplementedException ();
630 [Obsolete ("This method has been deprecated")]
632 public static IntPtr GetUnmanagedThunkForManagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
634 throw new NotImplementedException ();
637 [MethodImplAttribute (MethodImplOptions.InternalCall)]
638 public extern static bool IsComObject (object o);
641 public static bool IsTypeVisibleFromCom (Type t)
643 throw new NotImplementedException ();
647 public static int NumParamBytes (MethodInfo m)
649 throw new NotImplementedException ();
652 [MethodImplAttribute(MethodImplOptions.InternalCall)]
653 public extern static IntPtr OffsetOf (Type t, string fieldName);
655 [MethodImplAttribute(MethodImplOptions.InternalCall)]
656 public extern static void Prelink (MethodInfo m);
658 [MethodImplAttribute(MethodImplOptions.InternalCall)]
659 public extern static void PrelinkAll (Type c);
661 [MethodImplAttribute(MethodImplOptions.InternalCall)]
662 public extern static string PtrToStringAnsi (IntPtr ptr);
664 [MethodImplAttribute(MethodImplOptions.InternalCall)]
665 public extern static string PtrToStringAnsi (IntPtr ptr, int len);
667 public static string PtrToStringAuto (IntPtr ptr)
669 return SystemDefaultCharSize == 2
670 ? PtrToStringUni (ptr) : PtrToStringAnsi (ptr);
673 public static string PtrToStringAuto (IntPtr ptr, int len)
675 return SystemDefaultCharSize == 2
676 ? PtrToStringUni (ptr, len) : PtrToStringAnsi (ptr, len);
679 [MethodImplAttribute(MethodImplOptions.InternalCall)]
680 public extern static string PtrToStringUni (IntPtr ptr);
682 [MethodImplAttribute(MethodImplOptions.InternalCall)]
683 public extern static string PtrToStringUni (IntPtr ptr, int len);
685 [MethodImplAttribute(MethodImplOptions.InternalCall)]
686 public extern static string PtrToStringBSTR (IntPtr ptr);
688 [MethodImplAttribute(MethodImplOptions.InternalCall)]
692 public extern static void PtrToStructure (IntPtr ptr, object structure);
694 [MethodImplAttribute(MethodImplOptions.InternalCall)]
698 public extern static object PtrToStructure (IntPtr ptr, Type structureType);
700 [MethodImplAttribute (MethodImplOptions.InternalCall)]
701 private extern static int QueryInterfaceInternal (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
703 public static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv)
705 if (pUnk == IntPtr.Zero)
706 throw new ArgumentException ("Value cannot be null.", "pUnk");
707 return QueryInterfaceInternal (pUnk, ref iid, out ppv);
710 public static byte ReadByte (IntPtr ptr)
712 return ReadByte (ptr, 0);
715 [MethodImplAttribute(MethodImplOptions.InternalCall)]
716 public extern static byte ReadByte (IntPtr ptr, int ofs);
719 public static byte ReadByte ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
721 throw new NotImplementedException ();
724 public static short ReadInt16 (IntPtr ptr)
726 return ReadInt16 (ptr, 0);
729 [MethodImplAttribute(MethodImplOptions.InternalCall)]
730 public extern static short ReadInt16 (IntPtr ptr, int ofs);
733 public static short ReadInt16 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
735 throw new NotImplementedException ();
739 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
741 public static int ReadInt32 (IntPtr ptr)
743 return ReadInt32 (ptr, 0);
747 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
749 [MethodImplAttribute(MethodImplOptions.InternalCall)]
750 public extern static int ReadInt32 (IntPtr ptr, int ofs);
753 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
756 public static int ReadInt32 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
758 throw new NotImplementedException ();
762 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
764 public static long ReadInt64 (IntPtr ptr)
766 return ReadInt64 (ptr, 0);
770 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
772 [MethodImplAttribute(MethodImplOptions.InternalCall)]
773 public extern static long ReadInt64 (IntPtr ptr, int ofs);
776 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
779 public static long ReadInt64 ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
781 throw new NotImplementedException ();
785 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
787 public static IntPtr ReadIntPtr (IntPtr ptr)
789 return ReadIntPtr (ptr, 0);
793 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
795 [MethodImplAttribute(MethodImplOptions.InternalCall)]
796 public extern static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
799 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
802 public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
804 throw new NotImplementedException ();
807 [MethodImplAttribute (MethodImplOptions.InternalCall)]
808 public extern static IntPtr ReAllocCoTaskMem (IntPtr pv, int cb);
810 [MethodImplAttribute(MethodImplOptions.InternalCall)]
811 public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
814 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
816 [MethodImplAttribute (MethodImplOptions.InternalCall)]
817 private extern static int ReleaseInternal (IntPtr pUnk);
820 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
822 public static int Release (IntPtr pUnk)
824 if (pUnk == IntPtr.Zero)
825 throw new ArgumentException ("Value cannot be null.", "pUnk");
826 return ReleaseInternal (pUnk);
829 [MethodImplAttribute (MethodImplOptions.InternalCall)]
830 private extern static int ReleaseComObjectInternal (object co);
832 public static int ReleaseComObject (object o)
835 throw new ArgumentException ("Value cannot be null.", "o");
836 if (!IsComObject (o))
837 throw new ArgumentException ("Value must be a Com object.", "o");
838 return ReleaseComObjectInternal (o);
845 public static void ReleaseThreadCache()
847 throw new NotImplementedException ();
850 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
851 public static bool SetComObjectData (object obj, object key, object data)
853 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
859 public static int SizeOf (object structure)
861 return SizeOf (structure.GetType ());
864 [MethodImplAttribute(MethodImplOptions.InternalCall)]
865 public extern static int SizeOf (Type t);
867 [MethodImplAttribute(MethodImplOptions.InternalCall)]
868 public extern static IntPtr StringToBSTR (string s);
871 // I believe this is wrong, because in Mono and in P/Invoke
872 // we treat "Ansi" conversions as UTF-8 conversions, while
873 // this one does not do this
875 public static IntPtr StringToCoTaskMemAnsi (string s)
877 int length = s.Length + 1;
878 IntPtr ctm = AllocCoTaskMem (length);
880 byte[] asBytes = new byte[length];
881 for (int i = 0; i < s.Length; i++)
882 asBytes[i] = (byte)s[i];
883 asBytes[s.Length] = 0;
885 copy_to_unmanaged (asBytes, 0, ctm, length);
889 public static IntPtr StringToCoTaskMemAuto (string s)
891 return SystemDefaultCharSize == 2
892 ? StringToCoTaskMemUni (s) : StringToCoTaskMemAnsi (s);
895 public static IntPtr StringToCoTaskMemUni (string s)
897 int length = s.Length + 1;
898 IntPtr ctm = AllocCoTaskMem (length * 2);
900 char[] asChars = new char[length];
901 s.CopyTo (0, asChars, 0, s.Length);
902 asChars[s.Length] = '\0';
904 copy_to_unmanaged (asChars, 0, ctm, length);
908 [MethodImplAttribute(MethodImplOptions.InternalCall)]
909 public extern static IntPtr StringToHGlobalAnsi (string s);
911 public static IntPtr StringToHGlobalAuto (string s)
913 return SystemDefaultCharSize == 2
914 ? StringToHGlobalUni (s) : StringToHGlobalAnsi (s);
917 [MethodImplAttribute(MethodImplOptions.InternalCall)]
918 public extern static IntPtr StringToHGlobalUni (string s);
921 public static IntPtr SecureStringToBSTR (SecureString s)
924 throw new ArgumentNullException ("s");
926 IntPtr ctm = AllocCoTaskMem ((len+1) * 2 + 4);
927 byte [] buffer = null;
928 WriteInt32 (ctm, 0, len*2);
930 buffer = s.GetBuffer ();
932 for (int i = 0; i < len; i++)
933 WriteInt16 (ctm, 4 + (i * 2), (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
934 WriteInt16 (ctm, 4 + buffer.Length, 0);
937 for (int i = buffer.Length; i > 0; ){
942 return (IntPtr) ((long)ctm + 4);
945 public static IntPtr SecureStringToCoTaskMemAnsi (SecureString s)
948 throw new ArgumentNullException ("s");
950 IntPtr ctm = AllocCoTaskMem (len + 1);
951 byte [] copy = new byte [len+1];
954 byte [] buffer = s.GetBuffer ();
956 for (; i < len; i++, j += 2){
957 copy [i] = buffer [j+1];
962 copy_to_unmanaged (copy, 0, ctm, len+1);
964 // Ensure that we clear the buffer.
965 for (int i = len; i > 0; ){
973 public static IntPtr SecureStringToCoTaskMemUnicode (SecureString s)
976 throw new ArgumentNullException ("s");
978 IntPtr ctm = AllocCoTaskMem (len * 2 + 2);
979 byte [] buffer = null;
981 buffer = s.GetBuffer ();
982 for (int i = 0; i < len; i++)
983 WriteInt16 (ctm, i * 2, (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
984 WriteInt16 (ctm, buffer.Length, 0);
987 for (int i = buffer.Length; i > 0; ){
995 public static IntPtr SecureStringToGlobalAllocAnsi (SecureString s)
998 throw new ArgumentNullException ("s");
999 return SecureStringToCoTaskMemAnsi (s);
1002 public static IntPtr SecureStringToGlobalAllocUnicode (SecureString s)
1005 throw new ArgumentNullException ("s");
1006 return SecureStringToCoTaskMemUnicode (s);
1011 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1014 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1015 public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
1017 public static void ThrowExceptionForHR (int errorCode) {
1018 Exception ex = GetExceptionForHR (errorCode);
1023 public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo) {
1024 Exception ex = GetExceptionForHR (errorCode, errorInfo);
1029 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1030 public extern static IntPtr UnsafeAddrOfPinnedArrayElement (Array arr, int index);
1032 public static void WriteByte (IntPtr ptr, byte val)
1034 WriteByte (ptr, 0, val);
1037 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1038 public extern static void WriteByte (IntPtr ptr, int ofs, byte val);
1041 public static void WriteByte ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, byte val)
1043 throw new NotImplementedException ();
1046 public static void WriteInt16 (IntPtr ptr, short val)
1048 WriteInt16 (ptr, 0, val);
1051 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1052 public extern static void WriteInt16 (IntPtr ptr, int ofs, short val);
1055 public static void WriteInt16 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, short val)
1057 throw new NotImplementedException ();
1060 public static void WriteInt16 (IntPtr ptr, char val)
1062 WriteInt16 (ptr, 0, val);
1066 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1067 public extern static void WriteInt16 (IntPtr ptr, int ofs, char val);
1070 public static void WriteInt16([In, Out] object ptr, int ofs, char val)
1072 throw new NotImplementedException ();
1075 public static void WriteInt32 (IntPtr ptr, int val)
1077 WriteInt32 (ptr, 0, val);
1080 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1081 public extern static void WriteInt32 (IntPtr ptr, int ofs, int val);
1084 public static void WriteInt32([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, int val)
1086 throw new NotImplementedException ();
1089 public static void WriteInt64 (IntPtr ptr, long val)
1091 WriteInt64 (ptr, 0, val);
1094 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1095 public extern static void WriteInt64 (IntPtr ptr, int ofs, long val);
1098 public static void WriteInt64 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, long val)
1100 throw new NotImplementedException ();
1103 public static void WriteIntPtr (IntPtr ptr, IntPtr val)
1105 WriteIntPtr (ptr, 0, val);
1108 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1109 public extern static void WriteIntPtr (IntPtr ptr, int ofs, IntPtr val);
1112 public static void WriteIntPtr([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val)
1114 throw new NotImplementedException ();
1122 static Exception GetExceptionForHR (int errorCode) {
1123 return GetExceptionForHR (errorCode, IntPtr.Zero);
1131 static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo) {
1133 return new COMException ("", errorCode);
1138 public static int FinalReleaseComObject (object o)
1140 while (ReleaseComObject (o) != 0);
1144 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1145 private static extern Delegate GetDelegateForFunctionPointerInternal (IntPtr ptr, Type t);
1147 public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t)
1150 throw new ArgumentNullException ("t");
1151 if (!t.IsSubclassOf (typeof (MulticastDelegate)) || (t == typeof (MulticastDelegate)))
1152 throw new ArgumentException ("Type is not a delegate", "t");
1153 if (ptr == IntPtr.Zero)
1154 throw new ArgumentNullException ("ptr");
1156 return GetDelegateForFunctionPointerInternal (ptr, t);
1159 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1160 private static extern IntPtr GetFunctionPointerForDelegateInternal (Delegate d);
1162 public static IntPtr GetFunctionPointerForDelegate (Delegate d)
1165 throw new ArgumentNullException ("d");
1167 return GetFunctionPointerForDelegateInternal (d);