2008-12-08 Ivan N. Zlatev <contact@i-nz.net>
[mono.git] / mcs / class / corlib / System.Runtime.InteropServices / Marshal.cs
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.
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
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.
31 //
32
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;
40
41 #if NET_2_0
42 using System.Runtime.ConstrainedExecution;
43 using System.Runtime.InteropServices.ComTypes;
44 #endif
45
46 namespace System.Runtime.InteropServices
47 {
48         [SuppressUnmanagedCodeSecurity ()]
49         public
50 #if NET_2_0
51         static
52 #else
53         sealed
54 #endif
55         class Marshal
56         {
57                 /* fields */
58                 public static readonly int SystemMaxDBCSCharSize = 2; // don't know what this is
59                 public static readonly int SystemDefaultCharSize;
60
61                 static Marshal ()
62                 {
63                         SystemDefaultCharSize = Environment.OSVersion.Platform == PlatformID.Win32NT ? 2 : 1;
64                 }
65
66 #if !NET_2_0
67                 private Marshal () {}
68 #endif
69
70                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
71                 private extern static int AddRefInternal (IntPtr pUnk);
72
73                 public static int AddRef (IntPtr pUnk)
74                 {
75                         if (pUnk == IntPtr.Zero)
76                                 throw new ArgumentException ("Value cannot be null.", "pUnk");
77                         return AddRefInternal (pUnk);
78                 }
79
80                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
81                 public extern static IntPtr AllocCoTaskMem (int cb);
82
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);
88
89 #if NET_2_0
90                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
91 #endif
92                 public static IntPtr AllocHGlobal (int cb)
93                 {
94                         return AllocHGlobal ((IntPtr)cb);
95                 }
96
97                 [MonoTODO]
98                 public static object BindToMoniker (string monikerName)
99                 {
100                         throw new NotImplementedException ();
101                 }
102
103                 [MonoTODO]
104                 public static void ChangeWrapperHandleStrength (object otp, bool fIsWeak)
105                 {
106                         throw new NotImplementedException ();
107                 }
108
109                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
110                 extern static void copy_to_unmanaged (Array source, int startIndex,
111                                                       IntPtr destination, int length);
112
113                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
114                 extern static void copy_from_unmanaged (IntPtr source, int startIndex,
115                                                         Array destination, int length);
116
117                 public static void Copy (byte[] source, int startIndex, IntPtr destination, int length)
118                 {
119                         copy_to_unmanaged (source, startIndex, destination, length);
120                 }
121
122                 public static void Copy (char[] source, int startIndex, IntPtr destination, int length)
123                 {
124                         copy_to_unmanaged (source, startIndex, destination, length);
125                 }
126
127                 public static void Copy (short[] source, int startIndex, IntPtr destination, int length)
128                 {
129                         copy_to_unmanaged (source, startIndex, destination, length);
130                 }
131
132                 public static void Copy (int[] source, int startIndex, IntPtr destination, int length)
133                 {
134                         copy_to_unmanaged (source, startIndex, destination, length);
135                 }
136
137                 public static void Copy (long[] source, int startIndex, IntPtr destination, int length)
138                 {
139                         copy_to_unmanaged (source, startIndex, destination, length);
140                 }
141
142                 public static void Copy (float[] source, int startIndex, IntPtr destination, int length)
143                 {
144                         copy_to_unmanaged (source, startIndex, destination, length);
145                 }
146
147                 public static void Copy (double[] source, int startIndex, IntPtr destination, int length)
148                 {
149                         copy_to_unmanaged (source, startIndex, destination, length);
150                 }
151
152 #if NET_2_0
153                 public static void Copy (IntPtr[] source, int startIndex, IntPtr destination, int length)
154                 {
155                         copy_to_unmanaged (source, startIndex, destination, length);
156                 }
157 #endif
158
159                 public static void Copy (IntPtr source, byte[] destination, int startIndex, int length)
160                 {
161                         copy_from_unmanaged (source, startIndex, destination, length);
162                 }
163
164                 public static void Copy (IntPtr source, char[] destination, int startIndex, int length)
165                 {
166                         copy_from_unmanaged (source, startIndex, destination, length);
167                 }
168
169                 public static void Copy (IntPtr source, short[] destination, int startIndex, int length)
170                 {
171                         copy_from_unmanaged (source, startIndex, destination, length);
172                 }
173
174                 public static void Copy (IntPtr source, int[] destination, int startIndex, int length)
175                 {
176                         copy_from_unmanaged (source, startIndex, destination, length);
177                 }
178
179                 public static void Copy (IntPtr source, long[] destination, int startIndex, int length)
180                 {
181                         copy_from_unmanaged (source, startIndex, destination, length);
182                 }
183
184                 public static void Copy (IntPtr source, float[] destination, int startIndex, int length)
185                 {
186                         copy_from_unmanaged (source, startIndex, destination, length);
187                 }
188
189                 public static void Copy (IntPtr source, double[] destination, int startIndex, int length)
190                 {
191                         copy_from_unmanaged (source, startIndex, destination, length);
192                 }
193
194 #if NET_2_0
195                 public static void Copy (IntPtr source, IntPtr[] destination, int startIndex, int length)
196                 {
197                         copy_from_unmanaged (source, startIndex, destination, length);
198                 }
199
200                 public static IntPtr CreateAggregatedObject (IntPtr pOuter,
201                                                              object o)
202                 {
203                         throw new NotImplementedException ();
204                 }
205 #endif
206                 
207                 public static object CreateWrapperOfType (object o, Type t)
208                 {
209                         __ComObject co = o as __ComObject;
210                         if (co == null)
211                                 throw new ArgumentException ("o must derive from __ComObject", "o");
212                         if (t == null)
213                                 throw new ArgumentNullException ("t");
214
215                         Type[] itfs = o.GetType ().GetInterfaces ();
216                         foreach (Type itf in itfs) {
217                                 if (itf.IsImport && co.GetInterface (itf) == IntPtr.Zero)
218                                         throw new InvalidCastException ();
219                         }
220
221                         return ComInteropProxy.GetProxy (co.IUnknown, t).GetTransparentProxy ();
222                 }
223
224                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
225 #if NET_2_0
226                 [ComVisible (true)]
227 #endif
228                 public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
229
230                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
231                 public extern static void FreeBSTR (IntPtr ptr);
232
233                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
234                 public extern static void FreeCoTaskMem (IntPtr ptr);
235
236                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
237 #if NET_2_0
238                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
239 #endif
240                 public extern static void FreeHGlobal (IntPtr hglobal);
241
242 #if NET_2_0
243
244                 static void ClearBSTR (IntPtr ptr)
245                 {
246                         int len = ReadInt32 (ptr, -4);
247
248                         for (int i = 0; i < len; i++)
249                                 WriteByte (ptr, i, 0);
250                 }
251                 
252                 public static void ZeroFreeBSTR (IntPtr s)
253                 {
254                         ClearBSTR (s);
255                         FreeBSTR (s);
256                 }
257
258                 static void ClearAnsi (IntPtr ptr)
259                 {
260                         for (int i = 0; ReadByte (ptr, i) != 0; i++)
261                                 WriteByte (ptr, i, 0);
262                 }
263
264                 static void ClearUnicode (IntPtr ptr)
265                 {
266                         for (int i = 0; ReadInt16 (ptr, i) != 0; i += 2)
267                                 WriteInt16 (ptr, i, 0);
268                 }
269                 
270                 public static void ZeroFreeCoTaskMemAnsi (IntPtr s)
271                 {
272                         ClearAnsi (s);
273                         FreeCoTaskMem (s);
274                 }
275
276                 public static void ZeroFreeCoTaskMemUnicode (IntPtr s)
277                 {
278                         ClearUnicode (s);
279                         FreeCoTaskMem (s);
280                 }
281
282                 public static void ZeroFreeGlobalAllocAnsi (IntPtr s)
283                 {
284                         ClearAnsi (s);
285                         FreeHGlobal (s);
286                 }
287
288                 public static void ZeroFreeGlobalAllocUnicode (IntPtr s)
289                 {
290                         ClearUnicode (s);
291                         FreeHGlobal (s);
292                 }
293 #endif
294
295                 [MonoTODO]
296                 public static Guid GenerateGuidForType (Type type)
297                 {
298                         throw new NotImplementedException ();
299                 }
300
301                 [MonoTODO]
302                 public static string GenerateProgIdForType (Type type)
303                 {
304                         throw new NotImplementedException ();
305                 }
306
307                 [MonoTODO]
308                 public static object GetActiveObject (string progID)
309                 {
310                         throw new NotImplementedException ();
311                 }
312
313                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
314                 private extern static IntPtr GetCCW (object o, Type T);
315
316                 private static IntPtr GetComInterfaceForObjectInternal (object o, Type T)
317                 {
318                         if (IsComObject (o))
319                                 return ((__ComObject)o).GetInterface (T);
320                         else
321                                 return GetCCW (o, T);
322                 }
323
324                 public static IntPtr GetComInterfaceForObject (object o, Type T)
325                 {
326                         IntPtr pItf = GetComInterfaceForObjectInternal (o, T);
327                         AddRef (pItf);
328                         return pItf;
329                 }
330
331 #if NET_2_0
332                 [MonoTODO]
333                 public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
334                 {
335                         throw new NotImplementedException ();
336                 }
337 #endif
338
339                 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
340                 public static object GetComObjectData (object obj, object key)
341                 {
342                         throw new NotSupportedException ("MSDN states user code should never need to call this method.");
343                 }
344
345                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
346                 private extern static int GetComSlotForMethodInfoInternal (MemberInfo m);
347
348                 public static int GetComSlotForMethodInfo (MemberInfo m)
349                 {
350                         if (m == null)
351 #if NET_2_0
352                                 throw new ArgumentNullException ("m");
353 #else
354                                 throw new ArgumentNullException (null, "Value cannot be null.");
355 #endif
356                         if (!(m is MethodInfo))
357                                 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
358                         if (!m.DeclaringType.IsInterface)
359                                 throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
360                         return GetComSlotForMethodInfoInternal (m);
361                 }
362
363                 [MonoTODO]
364                 public static int GetEndComSlot (Type t)
365                 {
366                         throw new NotImplementedException ();
367                 }
368
369                 [MonoTODO]
370                 public static int GetExceptionCode()
371                 {
372                         throw new NotImplementedException ();
373                 }
374
375                 [MonoTODO]
376 #if NET_2_0
377                 [ComVisible (true)]
378 #endif
379                 public static IntPtr GetExceptionPointers()
380                 {
381                         throw new NotImplementedException ();
382                 }
383
384                 public static IntPtr GetHINSTANCE (Module m)
385                 {
386                         if (m == null)
387                                 throw new ArgumentNullException ("m");
388
389                         return m.GetHINSTANCE ();
390                 }
391
392                 [MonoTODO ("SetErrorInfo")]
393                 public static int GetHRForException (Exception e)
394                 {
395                         return e.hresult;
396                 }
397
398                 [MonoTODO]
399 #if NET_2_0
400                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
401 #endif
402                 public static int GetHRForLastWin32Error()
403                 {
404                         throw new NotImplementedException ();
405                 }
406
407                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
408                 private extern static IntPtr GetIDispatchForObjectInternal (object o);
409
410                 public static IntPtr GetIDispatchForObject (object o)
411                 {
412                         IntPtr pUnk = GetIDispatchForObjectInternal (o);
413                         // Internal method does not AddRef
414                         AddRef (pUnk);
415                         return pUnk;
416                 }
417
418 #if NET_2_0
419                 [MonoTODO]
420                 public static IntPtr GetIDispatchForObjectInContext (object o)
421                 {
422                         throw new NotImplementedException ();
423                 }
424 #endif
425
426                 [MonoTODO]
427                 public static IntPtr GetITypeInfoForType (Type t)
428                 {
429                         throw new NotImplementedException ();
430                 }
431
432                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
433                 private extern static IntPtr GetIUnknownForObjectInternal (object o);
434
435                 public static IntPtr GetIUnknownForObject (object o)
436                 {
437                         IntPtr pUnk = GetIUnknownForObjectInternal (o);
438                         // Internal method does not AddRef
439                         AddRef (pUnk);
440                         return pUnk;
441                 }
442
443 #if NET_2_0
444                 [MonoTODO]
445                 public static IntPtr GetIUnknownForObjectInContext (object o)
446                 {
447                         throw new NotImplementedException ();
448                 }
449 #endif
450
451                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
452 #if NET_2_0
453                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
454 #endif
455                 public static extern int GetLastWin32Error();
456
457                 [MonoTODO]
458 #if NET_2_0
459                 [Obsolete ("This method has been deprecated")]
460 #endif
461                 public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
462                 {
463                         throw new NotImplementedException ();
464                 }
465
466                 [MonoTODO]
467                 public static MemberInfo GetMethodInfoForComSlot (Type t, int slot, ref ComMemberType memberType)
468                 {
469                         throw new NotImplementedException ();
470                 }
471
472                 public static void GetNativeVariantForObject (object obj, IntPtr pDstNativeVariant)
473                 {
474                         Variant vt = new Variant();
475                         vt.SetValue(obj);
476                         Marshal.StructureToPtr(vt, pDstNativeVariant, false);
477                 }
478
479                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
480                 private static extern object GetObjectForCCW (IntPtr pUnk);
481
482                 public static object GetObjectForIUnknown (IntPtr pUnk)
483                 {
484                         object obj = GetObjectForCCW (pUnk);
485                         // was not a CCW
486                         if (obj == null) {
487                                 ComInteropProxy proxy = ComInteropProxy.GetProxy (pUnk, typeof (__ComObject));
488                                 obj = proxy.GetTransparentProxy ();
489                         }
490                         return obj;
491                 }
492
493                 public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant)
494                 {
495                         Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
496                         return vt.GetValue();
497                 }
498
499                 public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars)
500                 {
501                         if (cVars < 0)
502                                 throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
503                         object[] objects = new object[cVars];
504                         for (int i = 0; i < cVars; i++)
505                                 objects[i] = GetObjectForNativeVariant ((IntPtr)(aSrcNativeVariant.ToInt64 () +
506                                         i * SizeOf (typeof(Variant))));
507                         return objects;
508                 }
509
510                 [MonoTODO]
511                 public static int GetStartComSlot (Type t)
512                 {
513                         throw new NotImplementedException ();
514                 }
515
516                 [MonoTODO]
517 #if NET_2_0
518                 [Obsolete ("This method has been deprecated")]
519 #endif
520                 public static Thread GetThreadFromFiberCookie (int cookie)
521                 {
522                         throw new NotImplementedException ();
523                 }
524
525                 public static object GetTypedObjectForIUnknown (IntPtr pUnk, Type t)
526                 {
527                         ComInteropProxy proxy = new ComInteropProxy (pUnk, t);
528                         __ComObject co = (__ComObject)proxy.GetTransparentProxy ();
529                         foreach (Type itf in t.GetInterfaces ()) {
530                                 if ((itf.Attributes & TypeAttributes.Import) == TypeAttributes.Import) {
531                                         if (co.GetInterface (itf) == IntPtr.Zero)
532                                                 return null;
533                                 }
534                         }
535                         return co;
536                 }
537
538                 [MonoTODO]
539                 public static Type GetTypeForITypeInfo (IntPtr piTypeInfo)
540                 {
541                         throw new NotImplementedException ();
542                 }
543
544 #if NET_2_0
545                 [Obsolete]
546 #endif
547                 [MonoTODO]
548                 public static string GetTypeInfoName (UCOMITypeInfo pTI)
549                 {
550                         throw new NotImplementedException ();
551                 }
552
553 #if NET_2_0
554                 public static string GetTypeInfoName (ITypeInfo typeInfo)
555                 {
556                         throw new NotImplementedException ();
557                 }
558 #endif
559
560 #if NET_2_0
561                 [Obsolete]
562 #endif
563                 [MonoTODO]
564                 public static Guid GetTypeLibGuid (UCOMITypeLib pTLB)
565                 {
566                         throw new NotImplementedException ();
567                 }
568
569 #if NET_2_0
570                 [MonoTODO]
571                 public static Guid GetTypeLibGuid (ITypeLib typelib)
572                 {
573                         throw new NotImplementedException ();
574                 }
575 #endif
576
577                 [MonoTODO]
578                 public static Guid GetTypeLibGuidForAssembly (Assembly asm)
579                 {
580                         throw new NotImplementedException ();
581                 }
582
583 #if NET_2_0
584                 [Obsolete]
585 #endif
586                 [MonoTODO]
587                 public static int GetTypeLibLcid (UCOMITypeLib pTLB)
588                 {
589                         throw new NotImplementedException ();
590                 }
591
592 #if NET_2_0
593                 [MonoTODO]
594                 public static int GetTypeLibLcid (ITypeLib typelib)
595                 {
596                         throw new NotImplementedException ();
597                 }
598 #endif
599
600 #if NET_2_0
601                 [Obsolete]
602 #endif
603                 [MonoTODO]
604                 public static string GetTypeLibName (UCOMITypeLib pTLB)
605                 {
606                         throw new NotImplementedException ();
607                 }
608
609 #if NET_2_0
610                 [MonoTODO]
611                 public static string GetTypeLibName (ITypeLib typelib)
612                 {
613                         throw new NotImplementedException ();
614                 }
615
616                 [MonoTODO]
617                 public static void GetTypeLibVersionForAssembly (Assembly inputAssembly, out int majorVersion, out int minorVersion)
618                 {
619                         throw new NotImplementedException ();
620                 }
621
622                 public static object GetUniqueObjectForIUnknown (IntPtr unknown)
623                 {
624                         throw new NotImplementedException ();
625                 }
626 #endif
627
628                 [MonoTODO]
629 #if NET_2_0
630                 [Obsolete ("This method has been deprecated")]
631 #endif
632                 public static IntPtr GetUnmanagedThunkForManagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
633                 {
634                         throw new NotImplementedException ();
635                 }
636
637                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
638                 public extern static bool IsComObject (object o);
639
640                 [MonoTODO]
641                 public static bool IsTypeVisibleFromCom (Type t)
642                 {
643                         throw new NotImplementedException ();
644                 }
645
646                 [MonoTODO]
647                 public static int NumParamBytes (MethodInfo m)
648                 {
649                         throw new NotImplementedException ();
650                 }
651
652                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
653                 public extern static IntPtr OffsetOf (Type t, string fieldName);
654
655                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
656                 public extern static void Prelink (MethodInfo m);
657
658                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
659                 public extern static void PrelinkAll (Type c);
660
661                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
662                 public extern static string PtrToStringAnsi (IntPtr ptr);
663                 
664                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
665                 public extern static string PtrToStringAnsi (IntPtr ptr, int len);
666
667                 public static string PtrToStringAuto (IntPtr ptr)
668                 {
669                         return SystemDefaultCharSize == 2
670                                 ? PtrToStringUni (ptr) : PtrToStringAnsi (ptr);
671                 }
672                 
673                 public static string PtrToStringAuto (IntPtr ptr, int len)
674                 {
675                         return SystemDefaultCharSize == 2
676                                 ? PtrToStringUni (ptr, len) : PtrToStringAnsi (ptr, len);
677                 }
678
679                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
680                 public extern static string PtrToStringUni (IntPtr ptr);
681
682                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
683                 public extern static string PtrToStringUni (IntPtr ptr, int len);
684
685                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
686                 public extern static string PtrToStringBSTR (IntPtr ptr);
687                 
688                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
689 #if NET_2_0
690                 [ComVisible (true)]
691 #endif
692                 public extern static void PtrToStructure (IntPtr ptr, object structure);
693
694                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
695 #if NET_2_0
696                 [ComVisible (true)]
697 #endif
698                 public extern static object PtrToStructure (IntPtr ptr, Type structureType);
699
700                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
701                 private extern static int QueryInterfaceInternal (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
702
703                 public static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv)
704                 {
705                         if (pUnk == IntPtr.Zero)
706                                 throw new ArgumentException ("Value cannot be null.", "pUnk");
707                         return QueryInterfaceInternal (pUnk, ref iid, out ppv);
708                 }
709
710                 public static byte ReadByte (IntPtr ptr)
711                 {
712                         return ReadByte (ptr, 0);
713                 }
714
715                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
716                 public extern static byte ReadByte (IntPtr ptr, int ofs);
717
718                 [MonoTODO]
719                 public static byte ReadByte ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
720                 {
721                         throw new NotImplementedException ();
722                 }
723
724                 public static short ReadInt16 (IntPtr ptr)
725                 {
726                         return ReadInt16 (ptr, 0);
727                 }
728
729                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
730                 public extern static short ReadInt16 (IntPtr ptr, int ofs);
731
732                 [MonoTODO]
733                 public static short ReadInt16 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
734                 {
735                         throw new NotImplementedException ();
736                 }
737
738 #if NET_2_0
739                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
740 #endif
741                 public static int ReadInt32 (IntPtr ptr)
742                 {
743                         return ReadInt32 (ptr, 0);
744                 }
745
746 #if NET_2_0
747                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
748 #endif
749                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
750                 public extern static int ReadInt32 (IntPtr ptr, int ofs);
751
752 #if NET_2_0
753                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
754 #endif
755                 [MonoTODO]
756                 public static int ReadInt32 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
757                 {
758                         throw new NotImplementedException ();
759                 }
760
761 #if NET_2_0
762                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
763 #endif
764                 public static long ReadInt64 (IntPtr ptr)
765                 {
766                         return ReadInt64 (ptr, 0);
767                 }
768
769 #if NET_2_0
770                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
771 #endif
772                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
773                 public extern static long ReadInt64 (IntPtr ptr, int ofs);
774
775 #if NET_2_0
776                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
777 #endif
778                 [MonoTODO]
779                 public static long ReadInt64 ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
780                 {
781                         throw new NotImplementedException ();
782                 }
783
784 #if NET_2_0
785                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
786 #endif
787                 public static IntPtr ReadIntPtr (IntPtr ptr)
788                 {
789                         return ReadIntPtr (ptr, 0);
790                 }
791                 
792 #if NET_2_0
793                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
794 #endif
795                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
796                 public extern static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
797
798 #if NET_2_0
799                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
800 #endif
801                 [MonoTODO]
802                 public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
803                 {
804                         throw new NotImplementedException ();
805                 }
806
807                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
808                 public extern static IntPtr ReAllocCoTaskMem (IntPtr pv, int cb);
809
810                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
811                 public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
812
813 #if NET_2_0
814                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
815 #endif
816                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
817                 private extern static int ReleaseInternal (IntPtr pUnk);
818
819 #if NET_2_0
820                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
821 #endif
822                 public static int Release (IntPtr pUnk)
823                 {
824                         if (pUnk == IntPtr.Zero)
825                                 throw new ArgumentException ("Value cannot be null.", "pUnk");
826                         return ReleaseInternal (pUnk);
827                 }
828
829                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
830                 private extern static int ReleaseComObjectInternal (object co);
831
832                 public static int ReleaseComObject (object o)
833                 {
834                         if (o == null)
835                                 throw new ArgumentException ("Value cannot be null.", "o");
836                         if (!IsComObject (o))
837                                 throw new ArgumentException ("Value must be a Com object.", "o");
838                         return ReleaseComObjectInternal (o);
839                 }
840
841 #if NET_2_0
842                 [Obsolete]
843 #endif
844                 [MonoTODO]
845                 public static void ReleaseThreadCache()
846                 {
847                         throw new NotImplementedException ();
848                 }
849
850                 [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
851                 public static bool SetComObjectData (object obj, object key, object data)
852                 {
853                         throw new NotSupportedException ("MSDN states user code should never need to call this method.");
854                 }
855
856 #if NET_2_0
857                 [ComVisible (true)]
858 #endif
859                 public static int SizeOf (object structure)
860                 {
861                         return SizeOf (structure.GetType ());
862                 }
863
864                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
865                 public extern static int SizeOf (Type t);
866
867                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
868                 public extern static IntPtr StringToBSTR (string s);
869
870                 //
871                 // I believe this is wrong, because in Mono and in P/Invoke
872                 // we treat "Ansi" conversions as UTF-8 conversions, while
873                 // this one does not do this
874                 //
875                 public static IntPtr StringToCoTaskMemAnsi (string s)
876                 {
877                         int length = s.Length + 1;
878                         IntPtr ctm = AllocCoTaskMem (length);
879
880                         byte[] asBytes = new byte[length];
881                         for (int i = 0; i < s.Length; i++)
882                                 asBytes[i] = (byte)s[i];
883                         asBytes[s.Length] = 0;
884
885                         copy_to_unmanaged (asBytes, 0, ctm, length);
886                         return ctm;
887                 }
888
889                 public static IntPtr StringToCoTaskMemAuto (string s)
890                 {
891                         return SystemDefaultCharSize == 2
892                                 ? StringToCoTaskMemUni (s) : StringToCoTaskMemAnsi (s);
893                 }
894
895                 public static IntPtr StringToCoTaskMemUni (string s)
896                 {
897                         int length = s.Length + 1;
898                         IntPtr ctm = AllocCoTaskMem (length * 2);
899                         
900                         char[] asChars = new char[length];
901                         s.CopyTo (0, asChars, 0, s.Length);
902                         asChars[s.Length] = '\0';
903
904                         copy_to_unmanaged (asChars, 0, ctm, length);
905                         return ctm;
906                 }
907
908                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
909                 public extern static IntPtr StringToHGlobalAnsi (string s);
910
911                 public static IntPtr StringToHGlobalAuto (string s)
912                 {
913                         return SystemDefaultCharSize == 2
914                                 ? StringToHGlobalUni (s) : StringToHGlobalAnsi (s);
915                 }
916
917                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
918                 public extern static IntPtr StringToHGlobalUni (string s);
919
920 #if NET_2_0
921                 public static IntPtr SecureStringToBSTR (SecureString s)
922                 {
923                         if (s == null)
924                                 throw new ArgumentNullException ("s");
925                         int len = s.Length;
926                         IntPtr ctm = AllocCoTaskMem ((len+1) * 2 + 4);
927                         byte [] buffer = null;
928                         WriteInt32 (ctm, 0, len*2);
929                         try {
930                                 buffer = s.GetBuffer ();
931
932                                 for (int i = 0; i < len; i++)
933                                         WriteInt16 (ctm, 4 + (i * 2), (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
934                                 WriteInt16 (ctm, 4 + buffer.Length, 0);
935                         } finally {
936                                 if (buffer != null)
937                                         for (int i = buffer.Length; i > 0; ){
938                                                 i--;
939                                                 buffer [i] = 0;
940                                         }
941                         }
942                         return (IntPtr) ((long)ctm + 4);
943                 }
944
945                 public static IntPtr SecureStringToCoTaskMemAnsi (SecureString s)
946                 {
947                         if (s == null)
948                                 throw new ArgumentNullException ("s");
949                         int len = s.Length;
950                         IntPtr ctm = AllocCoTaskMem (len + 1);
951                         byte [] copy = new byte [len+1];
952
953                         try {
954                                 byte [] buffer = s.GetBuffer ();
955                                 int i = 0, j = 0;
956                                 for (; i < len; i++, j += 2){
957                                         copy [i] = buffer [j+1];
958                                         buffer [j] = 0;
959                                         buffer [j+1] = 0;
960                                 }
961                                 copy [i] = 0;
962                                 copy_to_unmanaged (copy, 0, ctm, len+1);
963                         } finally {
964                                 // Ensure that we clear the buffer.
965                                 for (int i = len; i > 0; ){
966                                         i--;
967                                         copy [i] = 0;
968                                 }
969                         }
970                         return ctm;
971                 }
972
973                 public static IntPtr SecureStringToCoTaskMemUnicode (SecureString s)
974                 {
975                         if (s == null)
976                                 throw new ArgumentNullException ("s");
977                         int len = s.Length;
978                         IntPtr ctm = AllocCoTaskMem (len * 2 + 2);
979                         byte [] buffer = null;
980                         try {
981                                 buffer = s.GetBuffer ();
982                                 for (int i = 0; i < len; i++)
983                                         WriteInt16 (ctm, i * 2, (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
984                                 WriteInt16 (ctm, buffer.Length, 0);
985                         } finally {
986                                 if (buffer != null)
987                                         for (int i = buffer.Length; i > 0; ){
988                                                 i--;
989                                                 buffer [i] = 0;
990                                         }
991                         }
992                         return ctm;
993                 }
994
995                 public static IntPtr SecureStringToGlobalAllocAnsi (SecureString s)
996                 {
997                         if (s == null)
998                                 throw new ArgumentNullException ("s");
999                         return SecureStringToCoTaskMemAnsi (s);
1000                 }
1001
1002                 public static IntPtr SecureStringToGlobalAllocUnicode (SecureString s)
1003                 {
1004                         if (s == null)
1005                                 throw new ArgumentNullException ("s");
1006                         return SecureStringToCoTaskMemUnicode (s);
1007                 }
1008 #endif
1009
1010 #if NET_2_0
1011                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
1012                 [ComVisible (true)]
1013 #endif
1014                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1015                 public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
1016
1017                 public static void ThrowExceptionForHR (int errorCode) {
1018                         Exception ex = GetExceptionForHR (errorCode);
1019                         if (ex != null)
1020                                 throw ex;
1021                 }
1022
1023                 public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo) {
1024                         Exception ex = GetExceptionForHR (errorCode, errorInfo);
1025                         if (ex != null)
1026                                 throw ex;
1027                 }
1028
1029                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1030                 public extern static IntPtr UnsafeAddrOfPinnedArrayElement (Array arr, int index);
1031
1032                 public static void WriteByte (IntPtr ptr, byte val)
1033                 {
1034                         WriteByte (ptr, 0, val);
1035                 }
1036
1037                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1038                 public extern static void WriteByte (IntPtr ptr, int ofs, byte val);
1039
1040                 [MonoTODO]
1041                 public static void WriteByte ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, byte val)
1042                 {
1043                         throw new NotImplementedException ();
1044                 }
1045
1046                 public static void WriteInt16 (IntPtr ptr, short val)
1047                 {
1048                         WriteInt16 (ptr, 0, val);
1049                 }
1050
1051                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1052                 public extern static void WriteInt16 (IntPtr ptr, int ofs, short val);
1053
1054                 [MonoTODO]
1055                 public static void WriteInt16 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, short val)
1056                 {
1057                         throw new NotImplementedException ();
1058                 }
1059
1060                 public static void WriteInt16 (IntPtr ptr, char val)
1061                 {
1062                         WriteInt16 (ptr, 0, val);
1063                 }
1064
1065                 [MonoTODO]
1066                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1067                 public extern static void WriteInt16 (IntPtr ptr, int ofs, char val);
1068
1069                 [MonoTODO]
1070                 public static void WriteInt16([In, Out] object ptr, int ofs, char val)
1071                 {
1072                         throw new NotImplementedException ();
1073                 }
1074
1075                 public static void WriteInt32 (IntPtr ptr, int val)
1076                 {
1077                         WriteInt32 (ptr, 0, val);
1078                 }
1079
1080                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1081                 public extern static void WriteInt32 (IntPtr ptr, int ofs, int val);
1082
1083                 [MonoTODO]
1084                 public static void WriteInt32([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, int val)
1085                 {
1086                         throw new NotImplementedException ();
1087                 }
1088
1089                 public static void WriteInt64 (IntPtr ptr, long val)
1090                 {
1091                         WriteInt64 (ptr, 0, val);
1092                 }
1093
1094                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1095                 public extern static void WriteInt64 (IntPtr ptr, int ofs, long val);
1096
1097                 [MonoTODO]
1098                 public static void WriteInt64 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, long val)
1099                 {
1100                         throw new NotImplementedException ();
1101                 }
1102
1103                 public static void WriteIntPtr (IntPtr ptr, IntPtr val)
1104                 {
1105                         WriteIntPtr (ptr, 0, val);
1106                 }
1107
1108                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1109                 public extern static void WriteIntPtr (IntPtr ptr, int ofs, IntPtr val);
1110
1111                 [MonoTODO]
1112                 public static void WriteIntPtr([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val)
1113                 {
1114                         throw new NotImplementedException ();
1115                 }
1116
1117 #if NET_2_0
1118                 public
1119 #else
1120                 internal
1121 #endif
1122                 static Exception GetExceptionForHR (int errorCode) {
1123                         return GetExceptionForHR (errorCode, IntPtr.Zero);
1124                 }
1125
1126 #if NET_2_0
1127                 public
1128 #else
1129                 internal
1130 #endif
1131                 static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo) {
1132                         if (errorCode < 0)
1133                                 return new COMException ("", errorCode);
1134                         return null;
1135                 }
1136
1137 #if NET_2_0
1138                 public static int FinalReleaseComObject (object o)
1139                 {
1140                         while (ReleaseComObject (o) != 0);
1141                         return 0;
1142                 }
1143
1144                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1145                 private static extern Delegate GetDelegateForFunctionPointerInternal (IntPtr ptr, Type t);
1146
1147                 public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t)
1148                 {
1149                         if (t == null)
1150                                 throw new ArgumentNullException ("t");
1151                         if (!t.IsSubclassOf (typeof (MulticastDelegate)) || (t == typeof (MulticastDelegate)))
1152                                 throw new ArgumentException ("Type is not a delegate", "t");
1153                         if (ptr == IntPtr.Zero)
1154                                 throw new ArgumentNullException ("ptr");
1155
1156                         return GetDelegateForFunctionPointerInternal (ptr, t);
1157                 }
1158
1159                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1160                 private static extern IntPtr GetFunctionPointerForDelegateInternal (Delegate d);
1161                 
1162                 public static IntPtr GetFunctionPointerForDelegate (Delegate d)
1163                 {
1164                         if (d == null)
1165                                 throw new ArgumentNullException ("d");
1166                         
1167                         return GetFunctionPointerForDelegateInternal (d);
1168                 }
1169 #endif
1170         }
1171 }