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.Collections.Generic;
35 using System.Runtime.CompilerServices;
37 using System.Security;
38 using System.Reflection;
39 using System.Threading;
41 using System.Runtime.ConstrainedExecution;
43 using System.Runtime.InteropServices.ComTypes;
50 namespace System.Runtime.InteropServices
52 public static class Marshal
55 public static readonly int SystemMaxDBCSCharSize = 2; // don't know what this is
56 public static readonly int SystemDefaultCharSize = Environment.OSVersion.Platform == PlatformID.Win32NT ? 2 : 1;
58 [MethodImplAttribute (MethodImplOptions.InternalCall)]
59 private extern static int AddRefInternal (IntPtr pUnk);
61 public static int AddRef (IntPtr pUnk)
63 if (pUnk == IntPtr.Zero)
64 throw new ArgumentException ("Value cannot be null.", "pUnk");
65 return AddRefInternal (pUnk);
68 [MethodImplAttribute(MethodImplOptions.InternalCall)]
69 public extern static IntPtr AllocCoTaskMem (int cb);
71 [MethodImplAttribute(MethodImplOptions.InternalCall)]
72 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
73 public extern static IntPtr AllocHGlobal (IntPtr cb);
75 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
76 public static IntPtr AllocHGlobal (int cb)
78 return AllocHGlobal ((IntPtr)cb);
82 public static object BindToMoniker (string monikerName)
84 throw new NotImplementedException ();
88 public static void ChangeWrapperHandleStrength (object otp, bool fIsWeak)
90 throw new NotImplementedException ();
93 [MethodImplAttribute(MethodImplOptions.InternalCall)]
94 internal extern static void copy_to_unmanaged (Array source, int startIndex,
95 IntPtr destination, int length);
97 [MethodImplAttribute(MethodImplOptions.InternalCall)]
98 internal extern static void copy_from_unmanaged (IntPtr source, int startIndex,
99 Array destination, int length);
101 public static void Copy (byte[] source, int startIndex, IntPtr destination, int length)
103 copy_to_unmanaged (source, startIndex, destination, length);
106 public static void Copy (char[] source, int startIndex, IntPtr destination, int length)
108 copy_to_unmanaged (source, startIndex, destination, length);
111 public static void Copy (short[] source, int startIndex, IntPtr destination, int length)
113 copy_to_unmanaged (source, startIndex, destination, length);
116 public static void Copy (int[] source, int startIndex, IntPtr destination, int length)
118 copy_to_unmanaged (source, startIndex, destination, length);
121 public static void Copy (long[] source, int startIndex, IntPtr destination, int length)
123 copy_to_unmanaged (source, startIndex, destination, length);
126 public static void Copy (float[] source, int startIndex, IntPtr destination, int length)
128 copy_to_unmanaged (source, startIndex, destination, length);
131 public static void Copy (double[] source, int startIndex, IntPtr destination, int length)
133 copy_to_unmanaged (source, startIndex, destination, length);
136 public static void Copy (IntPtr[] source, int startIndex, IntPtr destination, int length)
138 copy_to_unmanaged (source, startIndex, destination, length);
141 public static void Copy (IntPtr source, byte[] destination, int startIndex, int length)
143 copy_from_unmanaged (source, startIndex, destination, length);
146 public static void Copy (IntPtr source, char[] destination, int startIndex, int length)
148 copy_from_unmanaged (source, startIndex, destination, length);
151 public static void Copy (IntPtr source, short[] destination, int startIndex, int length)
153 copy_from_unmanaged (source, startIndex, destination, length);
156 public static void Copy (IntPtr source, int[] destination, int startIndex, int length)
158 copy_from_unmanaged (source, startIndex, destination, length);
161 public static void Copy (IntPtr source, long[] destination, int startIndex, int length)
163 copy_from_unmanaged (source, startIndex, destination, length);
166 public static void Copy (IntPtr source, float[] destination, int startIndex, int length)
168 copy_from_unmanaged (source, startIndex, destination, length);
171 public static void Copy (IntPtr source, double[] destination, int startIndex, int length)
173 copy_from_unmanaged (source, startIndex, destination, length);
176 public static void Copy (IntPtr source, IntPtr[] destination, int startIndex, int length)
178 copy_from_unmanaged (source, startIndex, destination, length);
181 public static IntPtr CreateAggregatedObject (IntPtr pOuter,
184 throw new NotImplementedException ();
188 public static object CreateWrapperOfType (object o, Type t)
190 __ComObject co = o as __ComObject;
192 throw new ArgumentException ("o must derive from __ComObject", "o");
194 throw new ArgumentNullException ("t");
196 Type[] itfs = o.GetType ().GetInterfaces ();
197 foreach (Type itf in itfs) {
198 if (itf.IsImport && co.GetInterface (itf) == IntPtr.Zero)
199 throw new InvalidCastException ();
202 return ComInteropProxy.GetProxy (co.IUnknown, t).GetTransparentProxy ();
206 [MethodImplAttribute(MethodImplOptions.InternalCall)]
208 public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
210 [MethodImplAttribute(MethodImplOptions.InternalCall)]
211 public extern static void FreeBSTR (IntPtr ptr);
213 [MethodImplAttribute(MethodImplOptions.InternalCall)]
214 public extern static void FreeCoTaskMem (IntPtr ptr);
216 [MethodImplAttribute(MethodImplOptions.InternalCall)]
217 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
218 public extern static void FreeHGlobal (IntPtr hglobal);
220 static void ClearBSTR (IntPtr ptr)
222 int len = ReadInt32 (ptr, -4);
224 for (int i = 0; i < len; i++)
225 WriteByte (ptr, i, 0);
228 public static void ZeroFreeBSTR (IntPtr s)
234 static void ClearAnsi (IntPtr ptr)
236 for (int i = 0; ReadByte (ptr, i) != 0; i++)
237 WriteByte (ptr, i, 0);
240 static void ClearUnicode (IntPtr ptr)
242 for (int i = 0; ReadInt16 (ptr, i) != 0; i += 2)
243 WriteInt16 (ptr, i, 0);
246 public static void ZeroFreeCoTaskMemAnsi (IntPtr s)
252 public static void ZeroFreeCoTaskMemUnicode (IntPtr s)
258 public static void ZeroFreeGlobalAllocAnsi (IntPtr s)
264 public static void ZeroFreeGlobalAllocUnicode (IntPtr s)
271 public static Guid GenerateGuidForType (Type type)
276 public static string GenerateProgIdForType (Type type)
278 IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes (type);
280 foreach (var a in attrs)
282 var dt = a.Constructor.DeclaringType;
283 string name = dt.Name;
284 if (name == "ProgIdAttribute")
286 var args = a.ConstructorArguments;
287 string text = a.ConstructorArguments[0].Value as string;
296 return type.FullName;
300 public static object GetActiveObject (string progID)
302 throw new NotImplementedException ();
305 [MethodImplAttribute (MethodImplOptions.InternalCall)]
306 private extern static IntPtr GetCCW (object o, Type T);
308 private static IntPtr GetComInterfaceForObjectInternal (object o, Type T)
311 return ((__ComObject)o).GetInterface (T);
313 return GetCCW (o, T);
316 public static IntPtr GetComInterfaceForObject (object o, Type T)
318 IntPtr pItf = GetComInterfaceForObjectInternal (o, T);
324 public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
326 throw new NotImplementedException ();
329 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
330 public static object GetComObjectData (object obj, object key)
332 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
335 [MethodImplAttribute(MethodImplOptions.InternalCall)]
336 private extern static int GetComSlotForMethodInfoInternal (MemberInfo m);
338 public static int GetComSlotForMethodInfo (MemberInfo m)
341 throw new ArgumentNullException ("m");
342 if (!(m is MethodInfo))
343 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
344 if (!m.DeclaringType.IsInterface)
345 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
346 return GetComSlotForMethodInfoInternal (m);
350 public static int GetEndComSlot (Type t)
352 throw new NotImplementedException ();
356 public static int GetExceptionCode()
358 throw new NotImplementedException ();
363 public static IntPtr GetExceptionPointers()
365 throw new NotImplementedException ();
368 public static IntPtr GetHINSTANCE (Module m)
371 throw new ArgumentNullException ("m");
373 return m.GetHINSTANCE ();
377 [MonoTODO ("SetErrorInfo")]
378 public static int GetHRForException (Exception e)
384 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
385 public static int GetHRForLastWin32Error()
387 throw new NotImplementedException ();
390 [MethodImplAttribute (MethodImplOptions.InternalCall)]
391 private extern static IntPtr GetIDispatchForObjectInternal (object o);
393 public static IntPtr GetIDispatchForObject (object o)
395 IntPtr pUnk = GetIDispatchForObjectInternal (o);
396 // Internal method does not AddRef
402 public static IntPtr GetIDispatchForObjectInContext (object o)
404 throw new NotImplementedException ();
408 public static IntPtr GetITypeInfoForType (Type t)
410 throw new NotImplementedException ();
413 [MethodImplAttribute (MethodImplOptions.InternalCall)]
414 private extern static IntPtr GetIUnknownForObjectInternal (object o);
416 public static IntPtr GetIUnknownForObject (object o)
418 IntPtr pUnk = GetIUnknownForObjectInternal (o);
419 // Internal method does not AddRef
425 public static IntPtr GetIUnknownForObjectInContext (object o)
427 throw new NotImplementedException ();
431 [Obsolete ("This method has been deprecated")]
432 public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
434 throw new NotImplementedException ();
438 public static MemberInfo GetMethodInfoForComSlot (Type t, int slot, ref ComMemberType memberType)
440 throw new NotImplementedException ();
443 public static void GetNativeVariantForObject (object obj, IntPtr pDstNativeVariant)
445 Variant vt = new Variant();
447 Marshal.StructureToPtr(vt, pDstNativeVariant, false);
450 [MethodImplAttribute (MethodImplOptions.InternalCall)]
451 private static extern object GetObjectForCCW (IntPtr pUnk);
453 public static object GetObjectForIUnknown (IntPtr pUnk)
455 object obj = GetObjectForCCW (pUnk);
458 ComInteropProxy proxy = ComInteropProxy.GetProxy (pUnk, typeof (__ComObject));
459 obj = proxy.GetTransparentProxy ();
464 public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant)
466 Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
467 return vt.GetValue();
470 public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars)
473 throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
474 object[] objects = new object[cVars];
475 for (int i = 0; i < cVars; i++)
476 objects[i] = GetObjectForNativeVariant ((IntPtr)(aSrcNativeVariant.ToInt64 () +
477 i * SizeOf (typeof(Variant))));
482 public static int GetStartComSlot (Type t)
484 throw new NotImplementedException ();
488 [Obsolete ("This method has been deprecated")]
489 public static Thread GetThreadFromFiberCookie (int cookie)
491 throw new NotImplementedException ();
494 public static object GetTypedObjectForIUnknown (IntPtr pUnk, Type t)
496 ComInteropProxy proxy = new ComInteropProxy (pUnk, t);
497 __ComObject co = (__ComObject)proxy.GetTransparentProxy ();
498 foreach (Type itf in t.GetInterfaces ()) {
499 if ((itf.Attributes & TypeAttributes.Import) == TypeAttributes.Import) {
500 if (co.GetInterface (itf) == IntPtr.Zero)
508 public static Type GetTypeForITypeInfo (IntPtr piTypeInfo)
510 throw new NotImplementedException ();
515 public static string GetTypeInfoName (UCOMITypeInfo pTI)
517 throw new NotImplementedException ();
520 public static string GetTypeInfoName (ITypeInfo typeInfo)
522 throw new NotImplementedException ();
527 public static Guid GetTypeLibGuid (UCOMITypeLib pTLB)
529 throw new NotImplementedException ();
533 public static Guid GetTypeLibGuid (ITypeLib typelib)
535 throw new NotImplementedException ();
539 public static Guid GetTypeLibGuidForAssembly (Assembly asm)
541 throw new NotImplementedException ();
546 public static int GetTypeLibLcid (UCOMITypeLib pTLB)
548 throw new NotImplementedException ();
552 public static int GetTypeLibLcid (ITypeLib typelib)
554 throw new NotImplementedException ();
559 public static string GetTypeLibName (UCOMITypeLib pTLB)
561 throw new NotImplementedException ();
565 public static string GetTypeLibName (ITypeLib typelib)
567 throw new NotImplementedException ();
571 public static void GetTypeLibVersionForAssembly (Assembly inputAssembly, out int majorVersion, out int minorVersion)
573 throw new NotImplementedException ();
576 public static object GetUniqueObjectForIUnknown (IntPtr unknown)
578 throw new NotImplementedException ();
582 [Obsolete ("This method has been deprecated")]
583 public static IntPtr GetUnmanagedThunkForManagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
585 throw new NotImplementedException ();
588 [MethodImplAttribute (MethodImplOptions.InternalCall)]
589 public extern static bool IsComObject (object o);
592 public static bool IsTypeVisibleFromCom (Type t)
594 throw new NotImplementedException ();
598 public static int NumParamBytes (MethodInfo m)
600 throw new NotImplementedException ();
604 [MethodImplAttribute(MethodImplOptions.InternalCall)]
605 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
606 public static extern int GetLastWin32Error();
608 [MethodImplAttribute(MethodImplOptions.InternalCall)]
609 public extern static IntPtr OffsetOf (Type t, string fieldName);
611 [MethodImplAttribute(MethodImplOptions.InternalCall)]
612 public extern static void Prelink (MethodInfo m);
614 [MethodImplAttribute(MethodImplOptions.InternalCall)]
615 public extern static void PrelinkAll (Type c);
617 [MethodImplAttribute(MethodImplOptions.InternalCall)]
618 public extern static string PtrToStringAnsi (IntPtr ptr);
620 [MethodImplAttribute(MethodImplOptions.InternalCall)]
621 public extern static string PtrToStringAnsi (IntPtr ptr, int len);
623 public static string PtrToStringAuto (IntPtr ptr)
625 return SystemDefaultCharSize == 2
626 ? PtrToStringUni (ptr) : PtrToStringAnsi (ptr);
629 public static string PtrToStringAuto (IntPtr ptr, int len)
631 return SystemDefaultCharSize == 2
632 ? PtrToStringUni (ptr, len) : PtrToStringAnsi (ptr, len);
635 [MethodImplAttribute(MethodImplOptions.InternalCall)]
636 public extern static string PtrToStringUni (IntPtr ptr);
638 [MethodImplAttribute(MethodImplOptions.InternalCall)]
639 public extern static string PtrToStringUni (IntPtr ptr, int len);
641 [MethodImplAttribute(MethodImplOptions.InternalCall)]
642 public extern static string PtrToStringBSTR (IntPtr ptr);
644 [MethodImplAttribute(MethodImplOptions.InternalCall)]
646 public extern static void PtrToStructure (IntPtr ptr, object structure);
648 [MethodImplAttribute(MethodImplOptions.InternalCall)]
650 public extern static object PtrToStructure (IntPtr ptr, Type structureType);
652 [MethodImplAttribute (MethodImplOptions.InternalCall)]
653 private extern static int QueryInterfaceInternal (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
655 public static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv)
657 if (pUnk == IntPtr.Zero)
658 throw new ArgumentException ("Value cannot be null.", "pUnk");
659 return QueryInterfaceInternal (pUnk, ref iid, out ppv);
662 public static byte ReadByte (IntPtr ptr)
664 return ReadByte (ptr, 0);
667 [MethodImplAttribute(MethodImplOptions.InternalCall)]
668 public extern static byte ReadByte (IntPtr ptr, int ofs);
671 [SuppressUnmanagedCodeSecurity]
672 public static byte ReadByte ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
674 throw new NotImplementedException ();
677 public static short ReadInt16 (IntPtr ptr)
679 return ReadInt16 (ptr, 0);
682 [MethodImplAttribute(MethodImplOptions.InternalCall)]
683 public extern static short ReadInt16 (IntPtr ptr, int ofs);
686 [SuppressUnmanagedCodeSecurity]
687 public static short ReadInt16 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
689 throw new NotImplementedException ();
692 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
693 public static int ReadInt32 (IntPtr ptr)
695 return ReadInt32 (ptr, 0);
698 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
699 [MethodImplAttribute(MethodImplOptions.InternalCall)]
700 public extern static int ReadInt32 (IntPtr ptr, int ofs);
702 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
704 [SuppressUnmanagedCodeSecurity]
705 public static int ReadInt32 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
707 throw new NotImplementedException ();
710 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
711 public static long ReadInt64 (IntPtr ptr)
713 return ReadInt64 (ptr, 0);
716 [MethodImplAttribute(MethodImplOptions.InternalCall)]
717 public extern static long ReadInt64 (IntPtr ptr, int ofs);
719 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
721 [SuppressUnmanagedCodeSecurity]
722 public static long ReadInt64 ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
724 throw new NotImplementedException ();
727 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
728 public static IntPtr ReadIntPtr (IntPtr ptr)
730 return ReadIntPtr (ptr, 0);
733 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
734 [MethodImplAttribute(MethodImplOptions.InternalCall)]
735 public extern static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
737 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
739 public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
741 throw new NotImplementedException ();
744 [MethodImplAttribute (MethodImplOptions.InternalCall)]
745 public extern static IntPtr ReAllocCoTaskMem (IntPtr pv, int cb);
747 [MethodImplAttribute(MethodImplOptions.InternalCall)]
748 public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
750 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
751 [MethodImplAttribute (MethodImplOptions.InternalCall)]
752 private extern static int ReleaseInternal (IntPtr pUnk);
754 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
755 public static int Release (IntPtr pUnk)
757 if (pUnk == IntPtr.Zero)
758 throw new ArgumentException ("Value cannot be null.", "pUnk");
759 return ReleaseInternal (pUnk);
763 [MethodImplAttribute (MethodImplOptions.InternalCall)]
764 private extern static int ReleaseComObjectInternal (object co);
766 public static int ReleaseComObject (object o)
769 throw new ArgumentException ("Value cannot be null.", "o");
770 if (!IsComObject (o))
771 throw new ArgumentException ("Value must be a Com object.", "o");
772 return ReleaseComObjectInternal (o);
777 public static void ReleaseThreadCache()
779 throw new NotImplementedException ();
782 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
783 public static bool SetComObjectData (object obj, object key, object data)
785 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
790 public static int SizeOf (object structure)
792 return SizeOf (structure.GetType ());
795 [MethodImplAttribute(MethodImplOptions.InternalCall)]
796 public extern static int SizeOf (Type t);
798 [MethodImplAttribute(MethodImplOptions.InternalCall)]
799 public extern static IntPtr StringToBSTR (string s);
802 // I believe this is wrong, because in Mono and in P/Invoke
803 // we treat "Ansi" conversions as UTF-8 conversions, while
804 // this one does not do this
806 public static IntPtr StringToCoTaskMemAnsi (string s)
808 int length = s.Length + 1;
809 IntPtr ctm = AllocCoTaskMem (length);
811 byte[] asBytes = new byte[length];
812 for (int i = 0; i < s.Length; i++)
813 asBytes[i] = (byte)s[i];
814 asBytes[s.Length] = 0;
816 copy_to_unmanaged (asBytes, 0, ctm, length);
820 public static IntPtr StringToCoTaskMemAuto (string s)
822 return SystemDefaultCharSize == 2
823 ? StringToCoTaskMemUni (s) : StringToCoTaskMemAnsi (s);
826 public static IntPtr StringToCoTaskMemUni (string s)
828 int length = s.Length + 1;
829 IntPtr ctm = AllocCoTaskMem (length * 2);
831 char[] asChars = new char[length];
832 s.CopyTo (0, asChars, 0, s.Length);
833 asChars[s.Length] = '\0';
835 copy_to_unmanaged (asChars, 0, ctm, length);
839 [MethodImplAttribute(MethodImplOptions.InternalCall)]
840 public extern static IntPtr StringToHGlobalAnsi (string s);
842 public static IntPtr StringToHGlobalAuto (string s)
844 return SystemDefaultCharSize == 2
845 ? StringToHGlobalUni (s) : StringToHGlobalAnsi (s);
848 [MethodImplAttribute(MethodImplOptions.InternalCall)]
849 public extern static IntPtr StringToHGlobalUni (string s);
852 public static IntPtr SecureStringToBSTR (SecureString s)
855 throw new ArgumentNullException ("s");
857 IntPtr ctm = AllocCoTaskMem ((len+1) * 2 + 4);
858 byte [] buffer = null;
859 WriteInt32 (ctm, 0, len*2);
861 buffer = s.GetBuffer ();
863 for (int i = 0; i < len; i++)
864 WriteInt16 (ctm, 4 + (i * 2), (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
865 WriteInt16 (ctm, 4 + buffer.Length, 0);
868 for (int i = buffer.Length; i > 0; ){
873 return (IntPtr) ((long)ctm + 4);
876 public static IntPtr SecureStringToCoTaskMemAnsi (SecureString s)
879 throw new ArgumentNullException ("s");
881 IntPtr ctm = AllocCoTaskMem (len + 1);
882 byte [] copy = new byte [len+1];
885 byte [] buffer = s.GetBuffer ();
887 for (; i < len; i++, j += 2){
888 copy [i] = buffer [j+1];
893 copy_to_unmanaged (copy, 0, ctm, len+1);
895 // Ensure that we clear the buffer.
896 for (int i = len; i > 0; ){
904 public static IntPtr SecureStringToCoTaskMemUnicode (SecureString s)
907 throw new ArgumentNullException ("s");
909 IntPtr ctm = AllocCoTaskMem (len * 2 + 2);
910 byte [] buffer = null;
912 buffer = s.GetBuffer ();
913 for (int i = 0; i < len; i++)
914 WriteInt16 (ctm, i * 2, (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
915 WriteInt16 (ctm, buffer.Length, 0);
918 for (int i = buffer.Length; i > 0; ){
926 public static IntPtr SecureStringToGlobalAllocAnsi (SecureString s)
929 throw new ArgumentNullException ("s");
930 return SecureStringToCoTaskMemAnsi (s);
933 public static IntPtr SecureStringToGlobalAllocUnicode (SecureString s)
936 throw new ArgumentNullException ("s");
937 return SecureStringToCoTaskMemUnicode (s);
941 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
943 [MethodImplAttribute(MethodImplOptions.InternalCall)]
944 public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
946 public static void ThrowExceptionForHR (int errorCode) {
947 Exception ex = GetExceptionForHR (errorCode);
952 public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo) {
953 Exception ex = GetExceptionForHR (errorCode, errorInfo);
958 [MethodImplAttribute(MethodImplOptions.InternalCall)]
959 public extern static IntPtr UnsafeAddrOfPinnedArrayElement (Array arr, int index);
961 public static void WriteByte (IntPtr ptr, byte val)
963 WriteByte (ptr, 0, val);
966 [MethodImplAttribute(MethodImplOptions.InternalCall)]
967 public extern static void WriteByte (IntPtr ptr, int ofs, byte val);
970 [SuppressUnmanagedCodeSecurity]
971 public static void WriteByte ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, byte val)
973 throw new NotImplementedException ();
976 public static void WriteInt16 (IntPtr ptr, short val)
978 WriteInt16 (ptr, 0, val);
981 [MethodImplAttribute(MethodImplOptions.InternalCall)]
982 public extern static void WriteInt16 (IntPtr ptr, int ofs, short val);
985 [SuppressUnmanagedCodeSecurity]
986 public static void WriteInt16 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, short val)
988 throw new NotImplementedException ();
991 public static void WriteInt16 (IntPtr ptr, char val)
993 WriteInt16 (ptr, 0, val);
997 [MethodImplAttribute(MethodImplOptions.InternalCall)]
998 public extern static void WriteInt16 (IntPtr ptr, int ofs, char val);
1001 public static void WriteInt16([In, Out] object ptr, int ofs, char val)
1003 throw new NotImplementedException ();
1006 public static void WriteInt32 (IntPtr ptr, int val)
1008 WriteInt32 (ptr, 0, val);
1011 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1012 public extern static void WriteInt32 (IntPtr ptr, int ofs, int val);
1015 [SuppressUnmanagedCodeSecurity]
1016 public static void WriteInt32([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, int val)
1018 throw new NotImplementedException ();
1021 public static void WriteInt64 (IntPtr ptr, long val)
1023 WriteInt64 (ptr, 0, val);
1026 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1027 public extern static void WriteInt64 (IntPtr ptr, int ofs, long val);
1030 [SuppressUnmanagedCodeSecurity]
1031 public static void WriteInt64 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, long val)
1033 throw new NotImplementedException ();
1036 public static void WriteIntPtr (IntPtr ptr, IntPtr val)
1038 WriteIntPtr (ptr, 0, val);
1041 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1042 public extern static void WriteIntPtr (IntPtr ptr, int ofs, IntPtr val);
1045 public static void WriteIntPtr([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val)
1047 throw new NotImplementedException ();
1050 public static Exception GetExceptionForHR (int errorCode) {
1051 return GetExceptionForHR (errorCode, IntPtr.Zero);
1054 public static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo) {
1056 const int E_OUTOFMEMORY = unchecked ((int)0x8007000EL);
1057 const int E_INVALIDARG = unchecked ((int)0X80070057);
1062 return new OutOfMemoryException ();
1064 return new ArgumentException ();
1067 return new COMException ("", errorCode);
1072 public static int FinalReleaseComObject (object o)
1074 while (ReleaseComObject (o) != 0);
1079 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1080 private static extern Delegate GetDelegateForFunctionPointerInternal (IntPtr ptr, Type t);
1082 public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t)
1085 throw new ArgumentNullException ("t");
1086 if (!t.IsSubclassOf (typeof (MulticastDelegate)) || (t == typeof (MulticastDelegate)))
1087 throw new ArgumentException ("Type is not a delegate", "t");
1088 if (t.IsGenericType)
1089 throw new ArgumentException ("The specified Type must not be a generic type definition.");
1090 if (ptr == IntPtr.Zero)
1091 throw new ArgumentNullException ("ptr");
1093 return GetDelegateForFunctionPointerInternal (ptr, t);
1096 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1097 private static extern IntPtr GetFunctionPointerForDelegateInternal (Delegate d);
1099 public static IntPtr GetFunctionPointerForDelegate (Delegate d)
1102 throw new ArgumentNullException ("d");
1104 return GetFunctionPointerForDelegateInternal (d);