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