1 // System.Runtime.InteropServices.Marshal.cs
3 // Sean MacIsaac (macisaac@ximian.com)
4 // Paolo Molaro (lupus@ximian.com)
5 // Dietmar Maurer (dietmar@ximian.com)
7 // (C) 2001-2002 Ximian, Inc.
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.CompilerServices;
34 using System.Security;
35 using System.Reflection;
36 using System.Threading;
39 using System.Runtime.ConstrainedExecution;
42 namespace System.Runtime.InteropServices
44 [SuppressUnmanagedCodeSecurity ()]
54 public static readonly int SystemMaxDBCSCharSize = 2; // don't know what this is
55 public static readonly int SystemDefaultCharSize = 2;
62 public static int AddRef (IntPtr pUnk) {
63 throw new NotImplementedException ();
66 [MethodImplAttribute(MethodImplOptions.InternalCall)]
67 public extern static IntPtr AllocCoTaskMem (int cb);
69 [MethodImplAttribute(MethodImplOptions.InternalCall)]
71 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
73 public extern static IntPtr AllocHGlobal (IntPtr cb);
76 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
78 public static IntPtr AllocHGlobal (int cb) {
79 return AllocHGlobal ((IntPtr)cb);
83 public static object BindToMoniker (string monikerName) {
84 throw new NotImplementedException ();
88 public static void ChangeWrapperHandleStrength (object otp, bool fIsWeak) {
89 throw new NotImplementedException ();
92 [MethodImplAttribute(MethodImplOptions.InternalCall)]
93 extern static void copy_to_unmanaged (Array source, int startIndex,
94 IntPtr destination, int length);
96 [MethodImplAttribute(MethodImplOptions.InternalCall)]
97 extern static void copy_from_unmanaged (IntPtr source, int startIndex,
98 Array destination, int length);
100 public static void Copy (byte[] source, int startIndex, IntPtr destination, int length) {
101 copy_to_unmanaged (source, startIndex, destination, length);
104 public static void Copy (char[] source, int startIndex, IntPtr destination, int length) {
105 copy_to_unmanaged (source, startIndex, destination, length);
108 public static void Copy (short[] source, int startIndex, IntPtr destination, int length) {
109 copy_to_unmanaged (source, startIndex, destination, length);
112 public static void Copy (int[] source, int startIndex, IntPtr destination, int length) {
113 copy_to_unmanaged (source, startIndex, destination, length);
116 public static void Copy (long[] source, int startIndex, IntPtr destination, int length) {
117 copy_to_unmanaged (source, startIndex, destination, length);
120 public static void Copy (float[] source, int startIndex, IntPtr destination, int length) {
121 copy_to_unmanaged (source, startIndex, destination, length);
124 public static void Copy (double[] source, int startIndex, IntPtr destination, int length) {
125 copy_to_unmanaged (source, startIndex, destination, length);
128 public static void Copy (IntPtr source, byte[] destination, int startIndex, int length) {
129 copy_from_unmanaged (source, startIndex, destination, length);
132 public static void Copy (IntPtr source, char[] destination, int startIndex, int length) {
133 copy_from_unmanaged (source, startIndex, destination, length);
136 public static void Copy (IntPtr source, short[] destination, int startIndex, int length) {
137 copy_from_unmanaged (source, startIndex, destination, length);
140 public static void Copy (IntPtr source, int[] destination, int startIndex, int length) {
141 copy_from_unmanaged (source, startIndex, destination, length);
144 public static void Copy (IntPtr source, long[] destination, int startIndex, int length) {
145 copy_from_unmanaged (source, startIndex, destination, length);
148 public static void Copy (IntPtr source, float[] destination, int startIndex, int length) {
149 copy_from_unmanaged (source, startIndex, destination, length);
152 public static void Copy (IntPtr source, double[] destination, int startIndex, int length) {
153 copy_from_unmanaged (source, startIndex, destination, length);
157 public static object CreateWrapperOfType (object o, Type t) {
158 throw new NotImplementedException ();
161 [MethodImplAttribute(MethodImplOptions.InternalCall)]
162 public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
165 public static void FreeBSTR (IntPtr ptr) {
166 throw new NotImplementedException ();
169 [MethodImplAttribute(MethodImplOptions.InternalCall)]
170 public extern static void FreeCoTaskMem (IntPtr ptr);
172 [MethodImplAttribute(MethodImplOptions.InternalCall)]
174 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
176 public extern static void FreeHGlobal (IntPtr hglobal);
180 public static void ZeroFreeBSTR (IntPtr ptr) {
181 throw new NotImplementedException ();
185 public static void ZeroFreeCoTaskMemAnsi (IntPtr ptr) {
186 throw new NotImplementedException ();
190 public static void ZeroFreeCoTaskMemUni (IntPtr ptr) {
191 throw new NotImplementedException ();
195 public static void ZeroFreeGlobalAllocAnsi (IntPtr hglobal) {
196 throw new NotImplementedException ();
200 public static void ZeroFreeGlobalAllocUni (IntPtr hglobal) {
201 throw new NotImplementedException ();
206 public static Guid GenerateGuidForType (Type type) {
207 throw new NotImplementedException ();
211 public static string GenerateProgIdForType (Type type) {
212 throw new NotImplementedException ();
216 public static object GetActiveObject (string progID) {
217 throw new NotImplementedException ();
221 public static IntPtr GetComInterfaceForObject (object o, Type T) {
222 throw new NotImplementedException ();
227 public static IntPtr GetComInterfaceForObjectInContext (object o, Type t) {
228 throw new NotImplementedException ();
233 public static object GetComObjectData (object obj, object key) {
234 throw new NotImplementedException ();
238 public static int GetComSlotForMethodInfo (MemberInfo m) {
239 throw new NotImplementedException ();
243 public static int GetEndComSlot (Type t) {
244 throw new NotImplementedException ();
248 public static int GetExceptionCode() {
249 throw new NotImplementedException ();
253 public static IntPtr GetExceptionPointers() {
254 throw new NotImplementedException ();
258 public static IntPtr GetHINSTANCE (Module m) {
259 throw new NotImplementedException ();
263 public static int GetHRForException (Exception e) {
264 throw new NotImplementedException ();
268 public static int GetHRForLastWin32Error() {
269 throw new NotImplementedException ();
273 public static IntPtr GetIDispatchForObject (object o) {
274 throw new NotImplementedException ();
279 public static IntPtr GetIDispatchForObjectInContext (object o) {
280 throw new NotImplementedException ();
285 public static IntPtr GetITypeInfoForType (Type t) {
286 throw new NotImplementedException ();
290 public static IntPtr GetIUnknownForObject (object o) {
291 throw new NotImplementedException ();
296 public static IntPtr GetIUnknownForObjectInContext (object o) {
297 throw new NotImplementedException ();
301 [MethodImplAttribute(MethodImplOptions.InternalCall)]
303 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
305 public static extern int GetLastWin32Error();
308 public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature) {
309 throw new NotImplementedException ();
313 public static MemberInfo GetMethodInfoForComSlot (Type t, int slot, ref ComMemberType memberType) {
314 throw new NotImplementedException ();
318 public static void GetNativeVariantForObject (object obj, IntPtr pDstNativeVariant) {
319 throw new NotImplementedException ();
323 public static object GetObjectForIUnknown (IntPtr pUnk) {
324 throw new NotImplementedException ();
328 public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant) {
329 throw new NotImplementedException ();
333 public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars) {
334 throw new NotImplementedException ();
338 public static int GetStartComSlot (Type t) {
339 throw new NotImplementedException ();
343 public static Thread GetThreadFromFiberCookie (int cookie) {
344 throw new NotImplementedException ();
348 public static object GetTypedObjectForIUnknown (IntPtr pUnk, Type t) {
349 throw new NotImplementedException ();
353 public static Type GetTypeForITypeInfo (IntPtr piTypeInfo) {
354 throw new NotImplementedException ();
361 public static string GetTypeInfoName (UCOMITypeInfo pTI) {
362 throw new NotImplementedException ();
369 public static Guid GetTypeLibGuid (UCOMITypeLib pTLB) {
370 throw new NotImplementedException ();
374 public static Guid GetTypeLibGuidForAssembly (Assembly asm) {
375 throw new NotImplementedException ();
382 public static int GetTypeLibLcid (UCOMITypeLib pTLB) {
383 throw new NotImplementedException ();
390 public static string GetTypeLibName (UCOMITypeLib pTLB) {
391 throw new NotImplementedException ();
396 public static void GetTypeLibVersionForAssembly (Assembly inputAssembly, out int majorVersion, out int minorVersion) {
397 throw new NotImplementedException ();
402 public static IntPtr GetUnmanagedThunkForManagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature) {
403 throw new NotImplementedException ();
407 public static bool IsComObject (object o) {
408 throw new NotImplementedException ();
412 public static bool IsTypeVisibleFromCom (Type t) {
413 throw new NotImplementedException ();
417 public static int NumParamBytes (MethodInfo m) {
418 throw new NotImplementedException ();
421 [MethodImplAttribute(MethodImplOptions.InternalCall)]
422 public extern static IntPtr OffsetOf (Type t, string fieldName);
424 [MethodImplAttribute(MethodImplOptions.InternalCall)]
425 public extern static void Prelink (MethodInfo m);
427 [MethodImplAttribute(MethodImplOptions.InternalCall)]
428 public extern static void PrelinkAll (Type c);
430 [MethodImplAttribute(MethodImplOptions.InternalCall)]
431 public extern static string PtrToStringAnsi (IntPtr ptr);
433 [MethodImplAttribute(MethodImplOptions.InternalCall)]
434 public extern static string PtrToStringAnsi (IntPtr ptr, int len);
436 [MethodImplAttribute(MethodImplOptions.InternalCall)]
437 public extern static string PtrToStringAuto (IntPtr ptr);
439 [MethodImplAttribute(MethodImplOptions.InternalCall)]
440 public extern static string PtrToStringAuto (IntPtr ptr, int len);
442 [MethodImplAttribute(MethodImplOptions.InternalCall)]
443 public extern static string PtrToStringUni (IntPtr ptr);
445 [MethodImplAttribute(MethodImplOptions.InternalCall)]
446 public extern static string PtrToStringUni (IntPtr ptr, int len);
448 [MethodImplAttribute(MethodImplOptions.InternalCall)]
449 public extern static string PtrToStringBSTR (IntPtr ptr);
451 [MethodImplAttribute(MethodImplOptions.InternalCall)]
452 public extern static void PtrToStructure (IntPtr ptr, object structure);
454 [MethodImplAttribute(MethodImplOptions.InternalCall)]
455 public extern static object PtrToStructure (IntPtr ptr, Type structureType);
458 public static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv) {
459 throw new NotImplementedException ();
462 public static byte ReadByte (IntPtr ptr) {
463 return ReadByte (ptr, 0);
466 [MethodImplAttribute(MethodImplOptions.InternalCall)]
467 public extern static byte ReadByte (IntPtr ptr, int ofs);
470 public static byte ReadByte ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs) {
471 throw new NotImplementedException ();
474 public static short ReadInt16 (IntPtr ptr) {
475 return ReadInt16 (ptr, 0);
478 [MethodImplAttribute(MethodImplOptions.InternalCall)]
479 public extern static short ReadInt16 (IntPtr ptr, int ofs);
482 public static short ReadInt16 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs) {
483 throw new NotImplementedException ();
487 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
489 public static int ReadInt32 (IntPtr ptr) {
490 return ReadInt32 (ptr, 0);
494 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
496 [MethodImplAttribute(MethodImplOptions.InternalCall)]
497 public extern static int ReadInt32 (IntPtr ptr, int ofs);
500 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
503 public static int ReadInt32 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs) {
504 throw new NotImplementedException ();
508 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
510 public static long ReadInt64 (IntPtr ptr) {
511 return ReadInt64 (ptr, 0);
515 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
517 [MethodImplAttribute(MethodImplOptions.InternalCall)]
518 public extern static long ReadInt64 (IntPtr ptr, int ofs);
521 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
524 public static long ReadInt64 ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs) {
525 throw new NotImplementedException ();
529 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
531 public static IntPtr ReadIntPtr (IntPtr ptr) {
532 return ReadIntPtr (ptr, 0);
536 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
538 [MethodImplAttribute(MethodImplOptions.InternalCall)]
539 public extern static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
542 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
545 public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs) {
546 throw new NotImplementedException ();
550 public static IntPtr ReAllocCoTaskMem (IntPtr pv, int cb) {
551 throw new NotImplementedException ();
554 [MethodImplAttribute(MethodImplOptions.InternalCall)]
555 public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
558 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
561 public static int Release (IntPtr pUnk) {
562 throw new NotImplementedException ();
566 public static int ReleaseComObject (object o) {
567 throw new NotImplementedException ();
574 public static void ReleaseThreadCache() {
575 throw new NotImplementedException ();
579 public static bool SetComObjectData (object obj, object key, object data) {
580 throw new NotImplementedException ();
583 public static int SizeOf (object structure) {
584 return SizeOf (structure.GetType ());
587 [MethodImplAttribute(MethodImplOptions.InternalCall)]
588 public extern static int SizeOf (Type t);
591 public static IntPtr StringToBSTR (string s) {
592 throw new NotImplementedException ();
595 public static IntPtr StringToCoTaskMemAnsi (string s) {
596 int length = s.Length + 1;
597 IntPtr ctm = AllocCoTaskMem (length);
599 byte[] asBytes = new byte[length];
600 for (int i = 0; i < s.Length; i++)
601 asBytes[i] = (byte)s[i];
602 asBytes[s.Length] = 0;
604 copy_to_unmanaged (asBytes, 0, ctm, length);
609 public static IntPtr StringToCoTaskMemAuto (string s) {
610 throw new NotImplementedException ();
613 public static IntPtr StringToCoTaskMemUni (string s) {
614 int length = s.Length + 1;
615 IntPtr ctm = AllocCoTaskMem (length * 2);
617 char[] asChars = new char[length];
618 s.CopyTo (0, asChars, 0, s.Length);
619 asChars[s.Length] = '\0';
621 copy_to_unmanaged (asChars, 0, ctm, length);
625 [MethodImplAttribute(MethodImplOptions.InternalCall)]
626 public extern static IntPtr StringToHGlobalAnsi (string s);
628 [MethodImplAttribute(MethodImplOptions.InternalCall)]
629 public extern static IntPtr StringToHGlobalAuto (string s);
631 [MethodImplAttribute(MethodImplOptions.InternalCall)]
632 public extern static IntPtr StringToHGlobalUni (string s);
636 public static IntPtr SecureStringToBSTR (SecureString s) {
637 throw new NotImplementedException ();
641 public static IntPtr SecureStringToCoTaskMemAnsi (SecureString s) {
642 throw new NotImplementedException ();
646 public static IntPtr SecureStringToCoTaskMemUni (SecureString s) {
647 throw new NotImplementedException ();
651 public static IntPtr SecureStringToGlobalAllocAnsi (SecureString s) {
652 throw new NotImplementedException ();
656 public static IntPtr SecureStringToGlobalAllocUni (SecureString s) {
657 throw new NotImplementedException ();
662 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
664 [MethodImplAttribute(MethodImplOptions.InternalCall)]
665 public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
668 public static void ThrowExceptionForHR (int errorCode) {
669 throw new NotImplementedException ();
673 public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo) {
674 throw new NotImplementedException ();
677 [MethodImplAttribute(MethodImplOptions.InternalCall)]
678 public extern static IntPtr UnsafeAddrOfPinnedArrayElement (Array arr, int index);
680 public static void WriteByte (IntPtr ptr, byte val) {
681 WriteByte (ptr, 0, val);
684 [MethodImplAttribute(MethodImplOptions.InternalCall)]
685 public extern static void WriteByte (IntPtr ptr, int ofs, byte val);
688 public static void WriteByte ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, byte val) {
689 throw new NotImplementedException ();
692 public static void WriteInt16 (IntPtr ptr, short val) {
693 WriteInt16 (ptr, 0, val);
696 [MethodImplAttribute(MethodImplOptions.InternalCall)]
697 public extern static void WriteInt16 (IntPtr ptr, int ofs, short val);
700 public static void WriteInt16 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, short val) {
701 throw new NotImplementedException ();
704 public static void WriteInt16 (IntPtr ptr, char val) {
705 WriteInt16 (ptr, 0, val);
709 [MethodImplAttribute(MethodImplOptions.InternalCall)]
710 public extern static void WriteInt16 (IntPtr ptr, int ofs, char val);
713 public static void WriteInt16([In, Out] object ptr, int ofs, char val) {
714 throw new NotImplementedException ();
717 public static void WriteInt32 (IntPtr ptr, int val) {
718 WriteInt32 (ptr, 0, val);
721 [MethodImplAttribute(MethodImplOptions.InternalCall)]
722 public extern static void WriteInt32 (IntPtr ptr, int ofs, int val);
725 public static void WriteInt32([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, int val) {
726 throw new NotImplementedException ();
729 public static void WriteInt64 (IntPtr ptr, long val) {
730 WriteInt64 (ptr, 0, val);
733 [MethodImplAttribute(MethodImplOptions.InternalCall)]
734 public extern static void WriteInt64 (IntPtr ptr, int ofs, long val);
737 public static void WriteInt64 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, long val) {
738 throw new NotImplementedException ();
741 public static void WriteIntPtr (IntPtr ptr, IntPtr val) {
742 WriteIntPtr (ptr, 0, val);
745 [MethodImplAttribute(MethodImplOptions.InternalCall)]
746 public extern static void WriteIntPtr (IntPtr ptr, int ofs, IntPtr val);
749 public static void WriteIntPtr([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val) {
750 throw new NotImplementedException ();
755 public static int FinalReleaseComObject (object o) {
756 throw new NotImplementedException ();
760 public static Exception GetExceptionForHR (int errorCode) {
761 throw new NotImplementedException ();
765 public static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo) {
766 throw new NotImplementedException ();
769 [MethodImplAttribute(MethodImplOptions.InternalCall)]
770 private static extern Delegate GetDelegateForFunctionPointerInternal (IntPtr ptr, Type t);
772 public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t) {
773 if (!t.IsSubclassOf (typeof (MulticastDelegate)) || (t == typeof (MulticastDelegate)))
774 throw new ArgumentException ("Type is not a delegate", "t");
775 if (ptr == IntPtr.Zero)
776 throw new ArgumentNullException ("ptr");
778 return GetDelegateForFunctionPointerInternal (ptr, t);
781 [MethodImplAttribute(MethodImplOptions.InternalCall)]
782 private static extern IntPtr GetFunctionPointerForDelegateInternal (Delegate d);
784 public static IntPtr GetFunctionPointerForDelegate (Delegate d) {
786 throw new ArgumentNullException ("d");
788 return GetFunctionPointerForDelegateInternal (d);