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