In Test/System.Windows.Forms:
[mono-project.git] / mcs / class / corlib / System.Runtime.InteropServices / Marshal.cs
blob947081265a4a9de591b53bd8d468b43092ea9054
1 // System.Runtime.InteropServices.Marshal.cs
2 //
3 // Sean MacIsaac (macisaac@ximian.com)
4 // Paolo Molaro (lupus@ximian.com)
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Jonathan Chambers (joncham@gmail.com)
7 //
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:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
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 Mono.Interop;
34 using System.Collections;
35 using System.Runtime.CompilerServices;
36 using System;
37 using System.Security;
38 using System.Reflection;
39 using System.Threading;
41 #if NET_2_0
42 using System.Runtime.ConstrainedExecution;
43 using System.Runtime.InteropServices.ComTypes;
44 #endif
46 namespace System.Runtime.InteropServices
48 [SuppressUnmanagedCodeSecurity ()]
49 public
50 #if NET_2_0
51 static
52 #else
53 sealed
54 #endif
55 class Marshal
57 /* fields */
58 public static readonly int SystemMaxDBCSCharSize = 2; // don't know what this is
59 public static readonly int SystemDefaultCharSize;
61 static Marshal ()
63 SystemDefaultCharSize = Environment.OSVersion.Platform == PlatformID.Win32NT ? 2 : 1;
66 #if !NET_2_0
67 private Marshal () {}
68 #endif
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)]
84 #if NET_2_0
85 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
86 #endif
87 public extern static IntPtr AllocHGlobal (IntPtr cb);
89 #if NET_2_0
90 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
91 #endif
92 public static IntPtr AllocHGlobal (int cb)
94 return AllocHGlobal ((IntPtr)cb);
97 [MonoTODO]
98 public static object BindToMoniker (string monikerName)
100 throw new NotImplementedException ();
103 [MonoTODO]
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);
152 public static void Copy (IntPtr source, byte[] destination, int startIndex, int length)
154 copy_from_unmanaged (source, startIndex, destination, length);
157 public static void Copy (IntPtr source, char[] destination, int startIndex, int length)
159 copy_from_unmanaged (source, startIndex, destination, length);
162 public static void Copy (IntPtr source, short[] destination, int startIndex, int length)
164 copy_from_unmanaged (source, startIndex, destination, length);
167 public static void Copy (IntPtr source, int[] destination, int startIndex, int length)
169 copy_from_unmanaged (source, startIndex, destination, length);
172 public static void Copy (IntPtr source, long[] destination, int startIndex, int length)
174 copy_from_unmanaged (source, startIndex, destination, length);
177 public static void Copy (IntPtr source, float[] destination, int startIndex, int length)
179 copy_from_unmanaged (source, startIndex, destination, length);
182 public static void Copy (IntPtr source, double[] destination, int startIndex, int length)
184 copy_from_unmanaged (source, startIndex, destination, length);
187 #if NET_2_0
188 public static void Copy (IntPtr source, IntPtr[] destination, int startIndex, int length)
190 copy_from_unmanaged (source, startIndex, destination, length);
192 #endif
194 public static object CreateWrapperOfType (object o, Type t)
196 __ComObject co = o as __ComObject;
197 if (co == null)
198 throw new ArgumentException ("o must derive from __ComObject", "o");
199 if (t == null)
200 throw new ArgumentNullException ("t");
202 Type[] itfs = o.GetType ().GetInterfaces ();
203 foreach (Type itf in itfs) {
204 if (itf.IsImport && co.GetInterface (itf) == IntPtr.Zero)
205 throw new InvalidCastException ();
208 return ComInteropProxy.GetProxy (co.IUnknown, t).GetTransparentProxy ();
211 [MethodImplAttribute(MethodImplOptions.InternalCall)]
212 public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
214 [MethodImplAttribute(MethodImplOptions.InternalCall)]
215 public extern static void FreeBSTR (IntPtr ptr);
217 [MethodImplAttribute(MethodImplOptions.InternalCall)]
218 public extern static void FreeCoTaskMem (IntPtr ptr);
220 [MethodImplAttribute(MethodImplOptions.InternalCall)]
221 #if NET_2_0
222 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
223 #endif
224 public extern static void FreeHGlobal (IntPtr hglobal);
226 #if NET_2_0
228 static void ClearBSTR (IntPtr ptr)
230 int len = ReadInt32 (ptr, -4);
232 for (int i = 0; i < len; i++)
233 WriteByte (ptr, i, 0);
236 public static void ZeroFreeBSTR (IntPtr ptr)
238 ClearBSTR (ptr);
239 FreeBSTR (ptr);
242 static void ClearAnsi (IntPtr ptr)
244 for (int i = 0; ReadByte (ptr, i) != 0; i++)
245 WriteByte (ptr, i, 0);
248 static void ClearUnicode (IntPtr ptr)
250 for (int i = 0; ReadInt16 (ptr, i) != 0; i += 2)
251 WriteInt16 (ptr, i, 0);
254 public static void ZeroFreeCoTaskMemAnsi (IntPtr ptr)
256 ClearAnsi (ptr);
257 FreeCoTaskMem (ptr);
260 public static void ZeroFreeCoTaskMemUnicode (IntPtr ptr)
262 ClearUnicode (ptr);
263 FreeCoTaskMem (ptr);
266 public static void ZeroFreeGlobalAllocAnsi (IntPtr hglobal)
268 ClearAnsi (hglobal);
269 FreeHGlobal (hglobal);
272 public static void ZeroFreeGlobalAllocUnicode (IntPtr hglobal)
274 ClearUnicode (hglobal);
275 FreeHGlobal (hglobal);
277 #endif
279 [MonoTODO]
280 public static Guid GenerateGuidForType (Type type)
282 throw new NotImplementedException ();
285 [MonoTODO]
286 public static string GenerateProgIdForType (Type type)
288 throw new NotImplementedException ();
291 [MonoTODO]
292 public static object GetActiveObject (string progID)
294 throw new NotImplementedException ();
297 [MethodImplAttribute (MethodImplOptions.InternalCall)]
298 private extern static IntPtr GetCCW (object o, Type T);
300 private static IntPtr GetComInterfaceForObjectInternal (object o, Type T)
302 if (IsComObject (o))
303 return ((__ComObject)o).GetInterface (T);
304 else
305 return GetCCW (o, T);
308 public static IntPtr GetComInterfaceForObject (object o, Type T)
310 IntPtr pItf = GetComInterfaceForObjectInternal (o, T);
311 AddRef (pItf);
312 return pItf;
315 #if NET_2_0
316 [MonoTODO]
317 public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
319 throw new NotImplementedException ();
321 #endif
323 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
324 public static object GetComObjectData (object obj, object key)
326 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
329 [MethodImplAttribute(MethodImplOptions.InternalCall)]
330 private extern static int GetComSlotForMethodInfoInternal (MemberInfo m);
332 public static int GetComSlotForMethodInfo (MemberInfo m)
334 if (m == null)
335 throw new ArgumentNullException("m", "Value cannot be null.");
336 if (!(m is MethodInfo))
337 throw new ArgumentException("m", "The MemberInfo must be an interface method.");
338 if (!m.DeclaringType.IsInterface)
339 throw new ArgumentException("m", "The MemberInfo must be an interface method.");
340 return GetComSlotForMethodInfoInternal (m);
343 [MonoTODO]
344 public static int GetEndComSlot (Type t)
346 throw new NotImplementedException ();
349 [MonoTODO]
350 public static int GetExceptionCode()
352 throw new NotImplementedException ();
355 [MonoTODO]
356 public static IntPtr GetExceptionPointers()
358 throw new NotImplementedException ();
361 [MonoTODO]
362 public static IntPtr GetHINSTANCE (Module m)
364 throw new NotImplementedException ();
367 [MonoTODO ("SetErrorInfo")]
368 public static int GetHRForException (Exception e)
370 return e.hresult;
373 [MonoTODO]
374 public static int GetHRForLastWin32Error()
376 throw new NotImplementedException ();
379 [MethodImplAttribute (MethodImplOptions.InternalCall)]
380 private extern static IntPtr GetIDispatchForObjectInternal (object o);
382 public static IntPtr GetIDispatchForObject (object o)
384 IntPtr pUnk = GetIDispatchForObjectInternal (o);
385 // Internal method does not AddRef
386 AddRef (pUnk);
387 return pUnk;
390 #if NET_2_0
391 [MonoTODO]
392 public static IntPtr GetIDispatchForObjectInContext (object o)
394 throw new NotImplementedException ();
396 #endif
398 [MonoTODO]
399 public static IntPtr GetITypeInfoForType (Type t)
401 throw new NotImplementedException ();
404 [MethodImplAttribute (MethodImplOptions.InternalCall)]
405 private extern static IntPtr GetIUnknownForObjectInternal (object o);
407 public static IntPtr GetIUnknownForObject (object o)
409 IntPtr pUnk = GetIUnknownForObjectInternal (o);
410 // Internal method does not AddRef
411 AddRef (pUnk);
412 return pUnk;
415 #if NET_2_0
416 [MonoTODO]
417 public static IntPtr GetIUnknownForObjectInContext (object o)
419 throw new NotImplementedException ();
421 #endif
423 [MethodImplAttribute(MethodImplOptions.InternalCall)]
424 #if NET_2_0
425 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
426 #endif
427 public static extern int GetLastWin32Error();
429 [MonoTODO]
430 public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
432 throw new NotImplementedException ();
435 [MonoTODO]
436 public static MemberInfo GetMethodInfoForComSlot (Type t, int slot, ref ComMemberType memberType)
438 throw new NotImplementedException ();
441 public static void GetNativeVariantForObject (object obj, IntPtr pDstNativeVariant)
443 Variant vt = new Variant();
444 vt.SetValue(obj);
445 Marshal.StructureToPtr(vt, pDstNativeVariant, false);
448 [MethodImplAttribute (MethodImplOptions.InternalCall)]
449 private static extern object GetObjectForCCW (IntPtr pUnk);
451 public static object GetObjectForIUnknown (IntPtr pUnk)
453 object obj = GetObjectForCCW (pUnk);
454 // was not a CCW
455 if (obj == null) {
456 ComInteropProxy proxy = ComInteropProxy.GetProxy (pUnk, typeof (__ComObject));
457 obj = proxy.GetTransparentProxy ();
459 return obj;
462 public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant)
464 Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
465 return vt.GetValue();
468 public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars)
470 if (cVars < 0)
471 throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
472 object[] objects = new object[cVars];
473 for (int i = 0; i < cVars; i++)
474 objects[i] = GetObjectForNativeVariant ((IntPtr)(aSrcNativeVariant.ToInt64 () +
475 i * SizeOf (typeof(Variant))));
476 return objects;
479 [MonoTODO]
480 public static int GetStartComSlot (Type t)
482 throw new NotImplementedException ();
485 [MonoTODO]
486 public static Thread GetThreadFromFiberCookie (int cookie)
488 throw new NotImplementedException ();
491 public static object GetTypedObjectForIUnknown (IntPtr pUnk, Type t)
493 ComInteropProxy proxy = new ComInteropProxy (pUnk, t);
494 __ComObject co = (__ComObject)proxy.GetTransparentProxy ();
495 foreach (Type itf in t.GetInterfaces ()) {
496 if ((itf.Attributes & TypeAttributes.Import) == TypeAttributes.Import) {
497 if (co.GetInterface (itf) == IntPtr.Zero)
498 return null;
501 return co;
504 [MonoTODO]
505 public static Type GetTypeForITypeInfo (IntPtr piTypeInfo)
507 throw new NotImplementedException ();
510 #if NET_2_0
511 [Obsolete]
512 #endif
513 [MonoTODO]
514 public static string GetTypeInfoName (UCOMITypeInfo pTI)
516 throw new NotImplementedException ();
519 #if NET_2_0
520 [Obsolete]
521 #endif
522 [MonoTODO]
523 public static Guid GetTypeLibGuid (UCOMITypeLib pTLB)
525 throw new NotImplementedException ();
528 #if NET_2_0
529 [MonoTODO]
530 public static Guid GetTypeLibGuid (ITypeLib typelib)
532 throw new NotImplementedException ();
534 #endif
536 [MonoTODO]
537 public static Guid GetTypeLibGuidForAssembly (Assembly asm)
539 throw new NotImplementedException ();
542 #if NET_2_0
543 [Obsolete]
544 #endif
545 [MonoTODO]
546 public static int GetTypeLibLcid (UCOMITypeLib pTLB)
548 throw new NotImplementedException ();
551 #if NET_2_0
552 [MonoTODO]
553 public static int GetTypeLibLcid (ITypeLib typelib)
555 throw new NotImplementedException ();
557 #endif
559 #if NET_2_0
560 [Obsolete]
561 #endif
562 [MonoTODO]
563 public static string GetTypeLibName (UCOMITypeLib pTLB)
565 throw new NotImplementedException ();
568 #if NET_2_0
569 [MonoTODO]
570 public static string GetTypeLibName (ITypeLib typelib)
572 throw new NotImplementedException ();
575 [MonoTODO]
576 public static void GetTypeLibVersionForAssembly (Assembly inputAssembly, out int majorVersion, out int minorVersion)
578 throw new NotImplementedException ();
580 #endif
582 [MonoTODO]
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);
591 [MonoTODO]
592 public static bool IsTypeVisibleFromCom (Type t)
594 throw new NotImplementedException ();
597 [MonoTODO]
598 public static int NumParamBytes (MethodInfo m)
600 throw new NotImplementedException ();
603 [MethodImplAttribute(MethodImplOptions.InternalCall)]
604 public extern static IntPtr OffsetOf (Type t, string fieldName);
606 [MethodImplAttribute(MethodImplOptions.InternalCall)]
607 public extern static void Prelink (MethodInfo m);
609 [MethodImplAttribute(MethodImplOptions.InternalCall)]
610 public extern static void PrelinkAll (Type c);
612 [MethodImplAttribute(MethodImplOptions.InternalCall)]
613 public extern static string PtrToStringAnsi (IntPtr ptr);
615 [MethodImplAttribute(MethodImplOptions.InternalCall)]
616 public extern static string PtrToStringAnsi (IntPtr ptr, int len);
618 public static string PtrToStringAuto (IntPtr ptr)
620 return SystemDefaultCharSize == 2
621 ? PtrToStringUni (ptr) : PtrToStringAnsi (ptr);
624 public static string PtrToStringAuto (IntPtr ptr, int len)
626 return SystemDefaultCharSize == 2
627 ? PtrToStringUni (ptr, len) : PtrToStringAnsi (ptr, len);
630 [MethodImplAttribute(MethodImplOptions.InternalCall)]
631 public extern static string PtrToStringUni (IntPtr ptr);
633 [MethodImplAttribute(MethodImplOptions.InternalCall)]
634 public extern static string PtrToStringUni (IntPtr ptr, int len);
636 [MethodImplAttribute(MethodImplOptions.InternalCall)]
637 public extern static string PtrToStringBSTR (IntPtr ptr);
639 [MethodImplAttribute(MethodImplOptions.InternalCall)]
640 public extern static void PtrToStructure (IntPtr ptr, object structure);
642 [MethodImplAttribute(MethodImplOptions.InternalCall)]
643 public extern static object PtrToStructure (IntPtr ptr, Type structureType);
645 [MethodImplAttribute (MethodImplOptions.InternalCall)]
646 private extern static int QueryInterfaceInternal (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
648 public static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv)
650 if (pUnk == IntPtr.Zero)
651 throw new ArgumentException ("Value cannot be null.", "pUnk");
652 return QueryInterfaceInternal (pUnk, ref iid, out ppv);
655 public static byte ReadByte (IntPtr ptr)
657 return ReadByte (ptr, 0);
660 [MethodImplAttribute(MethodImplOptions.InternalCall)]
661 public extern static byte ReadByte (IntPtr ptr, int ofs);
663 [MonoTODO]
664 public static byte ReadByte ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
666 throw new NotImplementedException ();
669 public static short ReadInt16 (IntPtr ptr)
671 return ReadInt16 (ptr, 0);
674 [MethodImplAttribute(MethodImplOptions.InternalCall)]
675 public extern static short ReadInt16 (IntPtr ptr, int ofs);
677 [MonoTODO]
678 public static short ReadInt16 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
680 throw new NotImplementedException ();
683 #if NET_2_0
684 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
685 #endif
686 public static int ReadInt32 (IntPtr ptr)
688 return ReadInt32 (ptr, 0);
691 #if NET_2_0
692 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
693 #endif
694 [MethodImplAttribute(MethodImplOptions.InternalCall)]
695 public extern static int ReadInt32 (IntPtr ptr, int ofs);
697 #if NET_2_0
698 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
699 #endif
700 [MonoTODO]
701 public static int ReadInt32 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
703 throw new NotImplementedException ();
706 #if NET_2_0
707 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
708 #endif
709 public static long ReadInt64 (IntPtr ptr)
711 return ReadInt64 (ptr, 0);
714 #if NET_2_0
715 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
716 #endif
717 [MethodImplAttribute(MethodImplOptions.InternalCall)]
718 public extern static long ReadInt64 (IntPtr ptr, int ofs);
720 #if NET_2_0
721 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
722 #endif
723 [MonoTODO]
724 public static long ReadInt64 ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
726 throw new NotImplementedException ();
729 #if NET_2_0
730 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
731 #endif
732 public static IntPtr ReadIntPtr (IntPtr ptr)
734 return ReadIntPtr (ptr, 0);
737 #if NET_2_0
738 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
739 #endif
740 [MethodImplAttribute(MethodImplOptions.InternalCall)]
741 public extern static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
743 #if NET_2_0
744 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
745 #endif
746 [MonoTODO]
747 public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
749 throw new NotImplementedException ();
752 [MethodImplAttribute (MethodImplOptions.InternalCall)]
753 public extern static IntPtr ReAllocCoTaskMem (IntPtr pv, int cb);
755 [MethodImplAttribute(MethodImplOptions.InternalCall)]
756 public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
758 #if NET_2_0
759 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
760 #endif
761 [MethodImplAttribute (MethodImplOptions.InternalCall)]
762 private extern static int ReleaseInternal (IntPtr pUnk);
764 public static int Release (IntPtr pUnk)
766 if (pUnk == IntPtr.Zero)
767 throw new ArgumentException ("Value cannot be null.", "pUnk");
768 return ReleaseInternal (pUnk);
771 [MethodImplAttribute (MethodImplOptions.InternalCall)]
772 private extern static int ReleaseComObjectInternal (object co);
774 public static int ReleaseComObject (object o)
776 if (o == null)
777 throw new ArgumentException ("Value cannot be null.", "o");
778 if (!IsComObject (o))
779 throw new ArgumentException ("Value must be a Com object.", "o");
780 return ReleaseComObjectInternal (o);
783 #if NET_2_0
784 [Obsolete]
785 #endif
786 [MonoTODO]
787 public static void ReleaseThreadCache()
789 throw new NotImplementedException ();
792 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
793 public static bool SetComObjectData (object obj, object key, object data)
795 throw new NotSupportedException ("MSDN states user code should never need to call this method.");
798 public static int SizeOf (object structure)
800 return SizeOf (structure.GetType ());
803 [MethodImplAttribute(MethodImplOptions.InternalCall)]
804 public extern static int SizeOf (Type t);
806 [MethodImplAttribute(MethodImplOptions.InternalCall)]
807 public extern static IntPtr StringToBSTR (string s);
810 // I believe this is wrong, because in Mono and in P/Invoke
811 // we treat "Ansi" conversions as UTF-8 conversions, while
812 // this one does not do this
814 public static IntPtr StringToCoTaskMemAnsi (string s)
816 int length = s.Length + 1;
817 IntPtr ctm = AllocCoTaskMem (length);
819 byte[] asBytes = new byte[length];
820 for (int i = 0; i < s.Length; i++)
821 asBytes[i] = (byte)s[i];
822 asBytes[s.Length] = 0;
824 copy_to_unmanaged (asBytes, 0, ctm, length);
825 return ctm;
828 public static IntPtr StringToCoTaskMemAuto (string s)
830 return SystemDefaultCharSize == 2
831 ? StringToCoTaskMemUni (s) : StringToCoTaskMemAnsi (s);
834 public static IntPtr StringToCoTaskMemUni (string s)
836 int length = s.Length + 1;
837 IntPtr ctm = AllocCoTaskMem (length * 2);
839 char[] asChars = new char[length];
840 s.CopyTo (0, asChars, 0, s.Length);
841 asChars[s.Length] = '\0';
843 copy_to_unmanaged (asChars, 0, ctm, length);
844 return ctm;
847 [MethodImplAttribute(MethodImplOptions.InternalCall)]
848 public extern static IntPtr StringToHGlobalAnsi (string s);
850 public static IntPtr StringToHGlobalAuto (string s)
852 return SystemDefaultCharSize == 2
853 ? StringToHGlobalUni (s) : StringToHGlobalAnsi (s);
856 [MethodImplAttribute(MethodImplOptions.InternalCall)]
857 public extern static IntPtr StringToHGlobalUni (string s);
859 #if NET_2_0
860 public static IntPtr SecureStringToBSTR (SecureString s)
862 if (s == null)
863 throw new ArgumentNullException ("s");
864 int len = s.Length;
865 IntPtr ctm = AllocCoTaskMem ((len+1) * 2 + 4);
866 byte [] buffer = null;
867 WriteInt32 (ctm, 0, len);
868 try {
869 buffer = s.GetBuffer ();
871 for (int i = 0; i < len; i++)
872 WriteInt16 (ctm, 4 + (i * 2), (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
873 WriteInt16 (ctm, 4 + buffer.Length, 0);
874 } finally {
875 if (buffer != null)
876 for (int i = buffer.Length; i > 0; ){
877 i--;
878 buffer [i] = 0;
881 return (IntPtr) ((long)ctm + 4);
884 public static IntPtr SecureStringToCoTaskMemAnsi (SecureString s)
886 if (s == null)
887 throw new ArgumentNullException ("s");
888 int len = s.Length;
889 IntPtr ctm = AllocCoTaskMem (len + 1);
890 byte [] copy = new byte [len+1];
892 try {
893 byte [] buffer = s.GetBuffer ();
894 int i = 0, j = 0;
895 for (; i < len; i++, j += 2){
896 copy [i] = buffer [j+1];
897 buffer [j] = 0;
898 buffer [j+1] = 0;
900 copy [i] = 0;
901 copy_to_unmanaged (copy, 0, ctm, len+1);
902 } finally {
903 // Ensure that we clear the buffer.
904 for (int i = len; i > 0; ){
905 i--;
906 copy [i] = 0;
909 return ctm;
912 public static IntPtr SecureStringToCoTaskMemUnicode (SecureString s)
914 if (s == null)
915 throw new ArgumentNullException ("s");
916 int len = s.Length;
917 IntPtr ctm = AllocCoTaskMem (len * 2 + 2);
918 byte [] buffer = null;
919 try {
920 buffer = s.GetBuffer ();
921 for (int i = 0; i < len; i++)
922 WriteInt16 (ctm, i * 2, (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
923 WriteInt16 (ctm, buffer.Length, 0);
924 } finally {
925 if (buffer != null)
926 for (int i = buffer.Length; i > 0; ){
927 i--;
928 buffer [i] = 0;
931 return ctm;
934 public static IntPtr SecureStringToGlobalAllocAnsi (SecureString s)
936 if (s == null)
937 throw new ArgumentNullException ("s");
938 return SecureStringToCoTaskMemAnsi (s);
941 public static IntPtr SecureStringToGlobalAllocUnicode (SecureString s)
943 if (s == null)
944 throw new ArgumentNullException ("s");
945 return SecureStringToCoTaskMemUnicode (s);
947 #endif
949 #if NET_2_0
950 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
951 #endif
952 [MethodImplAttribute(MethodImplOptions.InternalCall)]
953 public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
955 public static void ThrowExceptionForHR (int errorCode)
957 if (errorCode < 0)
958 throw new COMException ("", errorCode);
961 [MonoTODO]
962 public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo)
964 throw new NotImplementedException ();
967 [MethodImplAttribute(MethodImplOptions.InternalCall)]
968 public extern static IntPtr UnsafeAddrOfPinnedArrayElement (Array arr, int index);
970 public static void WriteByte (IntPtr ptr, byte val)
972 WriteByte (ptr, 0, val);
975 [MethodImplAttribute(MethodImplOptions.InternalCall)]
976 public extern static void WriteByte (IntPtr ptr, int ofs, byte val);
978 [MonoTODO]
979 public static void WriteByte ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, byte val)
981 throw new NotImplementedException ();
984 public static void WriteInt16 (IntPtr ptr, short val)
986 WriteInt16 (ptr, 0, val);
989 [MethodImplAttribute(MethodImplOptions.InternalCall)]
990 public extern static void WriteInt16 (IntPtr ptr, int ofs, short val);
992 [MonoTODO]
993 public static void WriteInt16 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, short val)
995 throw new NotImplementedException ();
998 public static void WriteInt16 (IntPtr ptr, char val)
1000 WriteInt16 (ptr, 0, val);
1003 [MonoTODO]
1004 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1005 public extern static void WriteInt16 (IntPtr ptr, int ofs, char val);
1007 [MonoTODO]
1008 public static void WriteInt16([In, Out] object ptr, int ofs, char val)
1010 throw new NotImplementedException ();
1013 public static void WriteInt32 (IntPtr ptr, int val)
1015 WriteInt32 (ptr, 0, val);
1018 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1019 public extern static void WriteInt32 (IntPtr ptr, int ofs, int val);
1021 [MonoTODO]
1022 public static void WriteInt32([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, int val)
1024 throw new NotImplementedException ();
1027 public static void WriteInt64 (IntPtr ptr, long val)
1029 WriteInt64 (ptr, 0, val);
1032 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1033 public extern static void WriteInt64 (IntPtr ptr, int ofs, long val);
1035 [MonoTODO]
1036 public static void WriteInt64 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, long val)
1038 throw new NotImplementedException ();
1041 public static void WriteIntPtr (IntPtr ptr, IntPtr val)
1043 WriteIntPtr (ptr, 0, val);
1046 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1047 public extern static void WriteIntPtr (IntPtr ptr, int ofs, IntPtr val);
1049 [MonoTODO]
1050 public static void WriteIntPtr([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val)
1052 throw new NotImplementedException ();
1055 #if NET_2_0
1056 public static int FinalReleaseComObject (object o)
1058 while (ReleaseComObject (o) != 0);
1059 return 0;
1062 [MonoTODO]
1063 public static Exception GetExceptionForHR (int errorCode)
1065 throw new NotImplementedException ();
1068 [MonoTODO]
1069 public static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo)
1071 throw new NotImplementedException ();
1074 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1075 private static extern Delegate GetDelegateForFunctionPointerInternal (IntPtr ptr, Type t);
1077 public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t)
1079 if (!t.IsSubclassOf (typeof (MulticastDelegate)) || (t == typeof (MulticastDelegate)))
1080 throw new ArgumentException ("Type is not a delegate", "t");
1081 if (ptr == IntPtr.Zero)
1082 throw new ArgumentNullException ("ptr");
1084 return GetDelegateForFunctionPointerInternal (ptr, t);
1087 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1088 private static extern IntPtr GetFunctionPointerForDelegateInternal (Delegate d);
1090 public static IntPtr GetFunctionPointerForDelegate (Delegate d)
1092 if (d == null)
1093 throw new ArgumentNullException ("d");
1095 return GetFunctionPointerForDelegateInternal (d);
1097 #endif