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
);
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
);
188 public static void Copy (IntPtr source
, IntPtr
[] destination
, int startIndex
, int length
)
190 copy_from_unmanaged (source
, startIndex
, destination
, length
);
194 public static object CreateWrapperOfType (object o
, Type t
)
196 __ComObject co
= o
as __ComObject
;
198 throw new ArgumentException ("o must derive from __ComObject", "o");
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
)]
222 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
224 public extern static void FreeHGlobal (IntPtr hglobal
);
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
)
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
)
260 public static void ZeroFreeCoTaskMemUnicode (IntPtr ptr
)
266 public static void ZeroFreeGlobalAllocAnsi (IntPtr hglobal
)
269 FreeHGlobal (hglobal
);
272 public static void ZeroFreeGlobalAllocUnicode (IntPtr hglobal
)
274 ClearUnicode (hglobal
);
275 FreeHGlobal (hglobal
);
280 public static Guid
GenerateGuidForType (Type type
)
282 throw new NotImplementedException ();
286 public static string GenerateProgIdForType (Type type
)
288 throw new NotImplementedException ();
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
)
303 return ((__ComObject
)o
).GetInterface (T
);
305 return GetCCW (o
, T
);
308 public static IntPtr
GetComInterfaceForObject (object o
, Type T
)
310 IntPtr pItf
= GetComInterfaceForObjectInternal (o
, T
);
317 public static IntPtr
GetComInterfaceForObjectInContext (object o
, Type t
)
319 throw new NotImplementedException ();
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
)
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
);
344 public static int GetEndComSlot (Type t
)
346 throw new NotImplementedException ();
350 public static int GetExceptionCode()
352 throw new NotImplementedException ();
356 public static IntPtr
GetExceptionPointers()
358 throw new NotImplementedException ();
362 public static IntPtr
GetHINSTANCE (Module m
)
364 throw new NotImplementedException ();
367 [MonoTODO ("SetErrorInfo")]
368 public static int GetHRForException (Exception e
)
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
392 public static IntPtr
GetIDispatchForObjectInContext (object o
)
394 throw new NotImplementedException ();
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
417 public static IntPtr
GetIUnknownForObjectInContext (object o
)
419 throw new NotImplementedException ();
423 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
425 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
427 public static extern int GetLastWin32Error();
430 public static IntPtr
GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap
, IntPtr pbSignature
, int cbSignature
)
432 throw new NotImplementedException ();
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();
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
);
456 ComInteropProxy proxy
= ComInteropProxy
.GetProxy (pUnk
, typeof (__ComObject
));
457 obj
= proxy
.GetTransparentProxy ();
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
)
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
))));
480 public static int GetStartComSlot (Type t
)
482 throw new NotImplementedException ();
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
)
505 public static Type
GetTypeForITypeInfo (IntPtr piTypeInfo
)
507 throw new NotImplementedException ();
514 public static string GetTypeInfoName (UCOMITypeInfo pTI
)
516 throw new NotImplementedException ();
523 public static Guid
GetTypeLibGuid (UCOMITypeLib pTLB
)
525 throw new NotImplementedException ();
530 public static Guid
GetTypeLibGuid (ITypeLib typelib
)
532 throw new NotImplementedException ();
537 public static Guid
GetTypeLibGuidForAssembly (Assembly asm
)
539 throw new NotImplementedException ();
546 public static int GetTypeLibLcid (UCOMITypeLib pTLB
)
548 throw new NotImplementedException ();
553 public static int GetTypeLibLcid (ITypeLib typelib
)
555 throw new NotImplementedException ();
563 public static string GetTypeLibName (UCOMITypeLib pTLB
)
565 throw new NotImplementedException ();
570 public static string GetTypeLibName (ITypeLib typelib
)
572 throw new NotImplementedException ();
576 public static void GetTypeLibVersionForAssembly (Assembly inputAssembly
, out int majorVersion
, out int minorVersion
)
578 throw new NotImplementedException ();
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 ();
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
);
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
);
678 public static short ReadInt16 ([In
, MarshalAs(UnmanagedType
.AsAny
)] object ptr
, int ofs
)
680 throw new NotImplementedException ();
684 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
686 public static int ReadInt32 (IntPtr ptr
)
688 return ReadInt32 (ptr
, 0);
692 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
694 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
695 public extern static int ReadInt32 (IntPtr ptr
, int ofs
);
698 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
701 public static int ReadInt32 ([In
, MarshalAs(UnmanagedType
.AsAny
)] object ptr
, int ofs
)
703 throw new NotImplementedException ();
707 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
709 public static long ReadInt64 (IntPtr ptr
)
711 return ReadInt64 (ptr
, 0);
715 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
717 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
718 public extern static long ReadInt64 (IntPtr ptr
, int ofs
);
721 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
724 public static long ReadInt64 ([In
, MarshalAs (UnmanagedType
.AsAny
)] object ptr
, int ofs
)
726 throw new NotImplementedException ();
730 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
732 public static IntPtr
ReadIntPtr (IntPtr ptr
)
734 return ReadIntPtr (ptr
, 0);
738 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
740 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
741 public extern static IntPtr
ReadIntPtr (IntPtr ptr
, int ofs
);
744 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
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
);
759 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
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
)
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
);
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
);
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
);
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
);
860 public static IntPtr
SecureStringToBSTR (SecureString s
)
863 throw new ArgumentNullException ("s");
865 IntPtr ctm
= AllocCoTaskMem ((len
+1) * 2 + 4);
866 byte [] buffer
= null;
867 WriteInt32 (ctm
, 0, len
);
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);
876 for (int i
= buffer
.Length
; i
> 0; ){
881 return (IntPtr
) ((long)ctm
+ 4);
884 public static IntPtr
SecureStringToCoTaskMemAnsi (SecureString s
)
887 throw new ArgumentNullException ("s");
889 IntPtr ctm
= AllocCoTaskMem (len
+ 1);
890 byte [] copy
= new byte [len
+1];
893 byte [] buffer
= s
.GetBuffer ();
895 for (; i
< len
; i
++, j
+= 2){
896 copy
[i
] = buffer
[j
+1];
901 copy_to_unmanaged (copy
, 0, ctm
, len
+1);
903 // Ensure that we clear the buffer.
904 for (int i
= len
; i
> 0; ){
912 public static IntPtr
SecureStringToCoTaskMemUnicode (SecureString s
)
915 throw new ArgumentNullException ("s");
917 IntPtr ctm
= AllocCoTaskMem (len
* 2 + 2);
918 byte [] buffer
= null;
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);
926 for (int i
= buffer
.Length
; i
> 0; ){
934 public static IntPtr
SecureStringToGlobalAllocAnsi (SecureString s
)
937 throw new ArgumentNullException ("s");
938 return SecureStringToCoTaskMemAnsi (s
);
941 public static IntPtr
SecureStringToGlobalAllocUnicode (SecureString s
)
944 throw new ArgumentNullException ("s");
945 return SecureStringToCoTaskMemUnicode (s
);
950 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.MayFail
)]
952 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
953 public extern static void StructureToPtr (object structure
, IntPtr ptr
, bool fDeleteOld
);
955 public static void ThrowExceptionForHR (int errorCode
)
958 throw new COMException ("", errorCode
);
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
);
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
);
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
);
1004 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
1005 public extern static void WriteInt16 (IntPtr ptr
, int ofs
, char val
);
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
);
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
);
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
);
1050 public static void WriteIntPtr([In
, Out
, MarshalAs(UnmanagedType
.AsAny
)] object ptr
, int ofs
, IntPtr val
)
1052 throw new NotImplementedException ();
1056 public static int FinalReleaseComObject (object o
)
1058 while (ReleaseComObject (o
) != 0);
1063 public static Exception
GetExceptionForHR (int errorCode
)
1065 throw new NotImplementedException ();
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
)
1093 throw new ArgumentNullException ("d");
1095 return GetFunctionPointerForDelegateInternal (d
);