New test.
[mono.git] / mcs / class / corlib / System.Runtime.InteropServices / Marshal.cs
1 // System.Runtime.InteropServices.Marshal.cs
2 //
3 // Sean MacIsaac (macisaac@ximian.com)
4 // Paolo Molaro (lupus@ximian.com)
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Jonathan Chambers (joncham@gmail.com)
7 //
8 // (C) 2001-2002 Ximian, Inc.
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using Mono.Interop;
34 using System.Runtime.CompilerServices;
35 using System;
36 using System.Security;
37 using System.Reflection;
38 using System.Threading;
39
40 #if NET_2_0
41 using System.Runtime.ConstrainedExecution;
42 using System.Runtime.InteropServices.ComTypes;
43 #endif
44
45 namespace System.Runtime.InteropServices
46 {
47         [SuppressUnmanagedCodeSecurity ()]
48         public
49 #if NET_2_0
50         static
51 #else
52         sealed
53 #endif
54         class Marshal
55         {
56                 /* fields */
57                 public static readonly int SystemMaxDBCSCharSize = 2; // don't know what this is
58                 public static readonly int SystemDefaultCharSize;
59
60                 static Marshal ()
61                 {
62                         SystemDefaultCharSize = Environment.OSVersion.Platform == PlatformID.Win32NT ? 2 : 1;
63                 }
64
65 #if !NET_2_0
66                 private Marshal () {}
67 #endif
68
69         [MethodImplAttribute (MethodImplOptions.InternalCall)]
70         public extern static int AddRef (IntPtr pUnk);
71
72                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
73                 public extern static IntPtr AllocCoTaskMem (int cb);
74
75                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
76 #if NET_2_0
77                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
78 #endif
79                 public extern static IntPtr AllocHGlobal (IntPtr cb);
80
81 #if NET_2_0
82                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
83 #endif
84                 public static IntPtr AllocHGlobal (int cb) {
85                         return AllocHGlobal ((IntPtr)cb);
86                 }
87
88                 [MonoTODO]
89                 public static object BindToMoniker (string monikerName) {
90                         throw new NotImplementedException ();
91                 }
92
93                 [MonoTODO]
94                 public static void ChangeWrapperHandleStrength (object otp, bool fIsWeak) {
95                         throw new NotImplementedException ();
96                 }
97
98                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
99                 extern static void copy_to_unmanaged (Array source, int startIndex,
100                                                       IntPtr destination, int length);
101
102                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
103                 extern static void copy_from_unmanaged (IntPtr source, int startIndex,
104                                                         Array destination, int length);
105
106                 public static void Copy (byte[] source, int startIndex, IntPtr destination, int length) {
107                         copy_to_unmanaged (source, startIndex, destination, length);
108                 }
109
110                 public static void Copy (char[] source, int startIndex, IntPtr destination, int length) {
111                         copy_to_unmanaged (source, startIndex, destination, length);
112                 }
113
114                 public static void Copy (short[] source, int startIndex, IntPtr destination, int length) {
115                         copy_to_unmanaged (source, startIndex, destination, length);
116                 }
117
118                 public static void Copy (int[] source, int startIndex, IntPtr destination, int length) {
119                         copy_to_unmanaged (source, startIndex, destination, length);
120                 }
121
122                 public static void Copy (long[] source, int startIndex, IntPtr destination, int length) {
123                         copy_to_unmanaged (source, startIndex, destination, length);
124                 }
125
126                 public static void Copy (float[] source, int startIndex, IntPtr destination, int length) {
127                         copy_to_unmanaged (source, startIndex, destination, length);
128                 }
129
130                 public static void Copy (double[] source, int startIndex, IntPtr destination, int length) {
131                         copy_to_unmanaged (source, startIndex, destination, length);
132                 }
133
134                 public static void Copy (IntPtr source, byte[] destination, int startIndex, int length) {
135                         copy_from_unmanaged (source, startIndex, destination, length);
136                 }
137
138                 public static void Copy (IntPtr source, char[] destination, int startIndex, int length) {
139                         copy_from_unmanaged (source, startIndex, destination, length);
140                 }
141
142                 public static void Copy (IntPtr source, short[] destination, int startIndex, int length) {
143                         copy_from_unmanaged (source, startIndex, destination, length);
144                 }
145
146                 public static void Copy (IntPtr source, int[] destination, int startIndex, int length) {
147                         copy_from_unmanaged (source, startIndex, destination, length);
148                 }
149
150                 public static void Copy (IntPtr source, long[] destination, int startIndex, int length) {
151                         copy_from_unmanaged (source, startIndex, destination, length);
152                 }
153
154                 public static void Copy (IntPtr source, float[] destination, int startIndex, int length) {
155                         copy_from_unmanaged (source, startIndex, destination, length);
156                 }
157
158                 public static void Copy (IntPtr source, double[] destination, int startIndex, int length) {
159                         copy_from_unmanaged (source, startIndex, destination, length);
160                 }
161
162                 public static object CreateWrapperOfType (object o, Type t) {
163                         __ComObject co = o as __ComObject;
164                         if (co == null)
165                                 throw new ArgumentException ("o must derive from __ComObject", "o");
166                         if (t == null)
167                                 throw new ArgumentNullException ("t");
168
169                         Type[] itfs = o.GetType ().GetInterfaces ();
170                         foreach (Type itf in itfs) {
171                                 if (itf.IsImport && co.GetInterface (itf) == IntPtr.Zero)
172                                         throw new InvalidCastException ();
173                         }
174
175                         return ComInteropProxy.GetProxy (co.IUnknown, t).GetTransparentProxy ();
176                 }
177
178                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
179                 public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
180
181         [MethodImplAttribute(MethodImplOptions.InternalCall)]
182         public extern static void FreeBSTR (IntPtr ptr);
183
184                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
185                 public extern static void FreeCoTaskMem (IntPtr ptr);
186
187                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
188 #if NET_2_0
189                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
190 #endif
191                 public extern static void FreeHGlobal (IntPtr hglobal);
192
193 #if NET_2_0
194                 [MonoTODO]
195                 public static void ZeroFreeBSTR (IntPtr ptr)
196                 {
197                         throw new NotImplementedException ();
198                 }
199
200                 [MonoTODO]
201                 public static void ZeroFreeCoTaskMemAnsi (IntPtr ptr)
202                 {
203                         throw new NotImplementedException ();
204                 }
205
206                 [MonoTODO]
207                 public static void ZeroFreeCoTaskMemUnicode (IntPtr ptr)
208                 {
209                         throw new NotImplementedException ();
210                 }
211
212                 [MonoTODO]
213                 public static void ZeroFreeGlobalAllocAnsi (IntPtr hglobal)
214                 {
215                         throw new NotImplementedException ();
216                 }
217
218                 [MonoTODO]
219                 public static void ZeroFreeGlobalAllocUnicode (IntPtr hglobal)
220                 {
221                         throw new NotImplementedException ();
222                 }
223 #endif
224
225                 [MonoTODO]
226                 public static Guid GenerateGuidForType (Type type) {
227                         throw new NotImplementedException ();
228                 }
229
230                 [MonoTODO]
231                 public static string GenerateProgIdForType (Type type) {
232                         throw new NotImplementedException ();
233                 }
234
235                 [MonoTODO]
236                 public static object GetActiveObject (string progID) {
237                         throw new NotImplementedException ();
238                 }
239
240                 [MonoTODO]
241                 public static IntPtr GetComInterfaceForObject (object o, Type T) {
242             __ComObject co = o as __ComObject;
243             if (co == null)
244                             throw new NotSupportedException ("Only RCWs are currently supported");
245
246                         IntPtr pUnk = co.GetInterface (T);
247                         AddRef (pUnk);
248                         return pUnk;
249                 }
250
251 #if NET_2_0
252                 [MonoTODO]
253                 public static IntPtr GetComInterfaceForObjectInContext (object o, Type t) {
254                         throw new NotImplementedException ();
255                 }
256 #endif
257
258                 public static object GetComObjectData (object obj, object key) {
259                         if (obj == null)
260                                 throw new ArgumentNullException ("obj");
261                         if (key == null)
262                                 throw new ArgumentNullException ("key");
263
264                         __ComObject com_object = obj as __ComObject;
265                         if (com_object == null)
266                                 throw new ArgumentException ("obj is not a COM object", "obj");
267
268                         return com_object.Hashtable[key];
269                 }
270
271                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
272                 private extern static int GetComSlotForMethodInfoInternal (MemberInfo m);
273
274                 public static int GetComSlotForMethodInfo (MemberInfo m)
275                 {
276                         if (m == null)
277                                 throw new ArgumentNullException("m", "Value cannot be null.");
278                         if (!(m is MethodInfo))
279                                 throw new ArgumentException("m", "The MemberInfo must be an interface method.");
280                         if (!m.DeclaringType.IsInterface)
281                                 throw new ArgumentException("m", "The MemberInfo must be an interface method.");
282                         return GetComSlotForMethodInfoInternal (m);
283                 }
284
285                 [MonoTODO]
286                 public static int GetEndComSlot (Type t) {
287                         throw new NotImplementedException ();
288                 }
289
290                 [MonoTODO]
291                 public static int GetExceptionCode() {
292                         throw new NotImplementedException ();
293                 }
294
295                 [MonoTODO]
296                 public static IntPtr GetExceptionPointers() {
297                         throw new NotImplementedException ();
298                 }
299
300                 [MonoTODO]
301                 public static IntPtr GetHINSTANCE (Module m) {
302                         throw new NotImplementedException ();
303                 }
304
305                 [MonoTODO ("SetErrorInfo")]
306                 public static int GetHRForException (Exception e) {
307                         return e.hresult;
308                 }
309
310                 [MonoTODO]
311                 public static int GetHRForLastWin32Error() {
312                         throw new NotImplementedException ();
313                 }
314
315                 [MonoTODO]
316                 public static IntPtr GetIDispatchForObject (object o)
317                 {                       // only handle case of RCW objects for now
318                         __ComObject co = o as __ComObject;
319                         if (co != null) {
320                                 IntPtr pUnk = co.IDispatch;
321                                 AddRef (pUnk);
322                                 return pUnk;
323                         }
324                         throw new NotImplementedException ();
325                 }
326
327 #if NET_2_0
328                 [MonoTODO]
329                 public static IntPtr GetIDispatchForObjectInContext (object o) {
330                         throw new NotImplementedException ();
331                 }
332 #endif
333
334                 [MonoTODO]
335                 public static IntPtr GetITypeInfoForType (Type t) {
336                         throw new NotImplementedException ();
337                 }
338
339                 [MonoTODO]
340                 public static IntPtr GetIUnknownForObject (object o) {
341                         // only handle case of RCW objects for now
342                         __ComObject co = o as __ComObject;
343                         if (co != null) {
344                                 IntPtr pUnk = co.IUnknown;
345                                 AddRef (pUnk);
346                                 return pUnk;
347                         }
348                         throw new NotImplementedException ();
349                 }
350
351 #if NET_2_0
352                 [MonoTODO]
353                 public static IntPtr GetIUnknownForObjectInContext (object o) {
354                         throw new NotImplementedException ();
355                 }
356 #endif
357
358                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
359 #if NET_2_0
360                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
361 #endif
362                 public static extern int GetLastWin32Error();
363
364                 [MonoTODO]
365                 public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature) {
366                         throw new NotImplementedException ();
367                 }
368
369                 [MonoTODO]
370                 public static MemberInfo GetMethodInfoForComSlot (Type t, int slot, ref ComMemberType memberType) {
371                         throw new NotImplementedException ();
372                 }
373
374                 public static void GetNativeVariantForObject (object obj, IntPtr pDstNativeVariant) {
375                         Variant vt = new Variant();
376                         vt.SetValue(obj);
377                         Marshal.StructureToPtr(vt, pDstNativeVariant, false);
378                 }
379
380                 public static object GetObjectForIUnknown (IntPtr pUnk) {
381                         ComInteropProxy proxy = ComInteropProxy.GetProxy (pUnk, typeof(__ComObject));
382             return proxy.GetTransparentProxy ();
383                 }
384
385                 public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant) {
386                         Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
387                         return vt.GetValue();
388                 }
389
390                 [MonoTODO]
391                 public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars) {
392                         throw new NotImplementedException ();
393                 }
394
395                 [MonoTODO]
396                 public static int GetStartComSlot (Type t) {
397                         throw new NotImplementedException ();
398                 }
399
400                 [MonoTODO]
401                 public static Thread GetThreadFromFiberCookie (int cookie) {
402                         throw new NotImplementedException ();
403                 }
404
405         public static object GetTypedObjectForIUnknown (IntPtr pUnk, Type t)
406         {
407                         ComInteropProxy proxy = new ComInteropProxy (pUnk, t);
408             __ComObject co = (__ComObject)proxy.GetTransparentProxy ();
409             foreach (Type itf in t.GetInterfaces ()) {
410                 if ((itf.Attributes & TypeAttributes.Import) == TypeAttributes.Import) {
411                     if (co.GetInterface (itf) == IntPtr.Zero)
412                         return null;
413                 }
414             }
415             return co;
416         }
417
418                 [MonoTODO]
419                 public static Type GetTypeForITypeInfo (IntPtr piTypeInfo) {
420                         throw new NotImplementedException ();
421                 }
422
423 #if NET_2_0
424                 [Obsolete]
425 #endif
426                 [MonoTODO]
427                 public static string GetTypeInfoName (UCOMITypeInfo pTI) {
428                         throw new NotImplementedException ();
429                 }
430
431 #if NET_2_0
432                 [Obsolete]
433 #endif
434                 [MonoTODO]
435                 public static Guid GetTypeLibGuid (UCOMITypeLib pTLB) {
436                         throw new NotImplementedException ();
437                 }
438
439 #if NET_2_0
440                 [MonoTODO]
441                 public static Guid GetTypeLibGuid (ITypeLib typelib) {
442                         throw new NotImplementedException ();
443                 }
444 #endif
445
446                 [MonoTODO]
447                 public static Guid GetTypeLibGuidForAssembly (Assembly asm) {
448                         throw new NotImplementedException ();
449                 }
450
451 #if NET_2_0
452                 [Obsolete]
453 #endif
454                 [MonoTODO]
455                 public static int GetTypeLibLcid (UCOMITypeLib pTLB) {
456                         throw new NotImplementedException ();
457                 }
458
459 #if NET_2_0
460                 [MonoTODO]
461                 public static int GetTypeLibLcid (ITypeLib typelib) {
462                         throw new NotImplementedException ();
463                 }
464 #endif
465
466 #if NET_2_0
467                 [Obsolete]
468 #endif
469                 [MonoTODO]
470                 public static string GetTypeLibName (UCOMITypeLib pTLB) {
471                         throw new NotImplementedException ();
472                 }
473
474 #if NET_2_0
475                 [MonoTODO]
476                 public static string GetTypeLibName (ITypeLib typelib)
477                 {
478                         throw new NotImplementedException ();
479                 }
480
481                 [MonoTODO]
482                 public static void GetTypeLibVersionForAssembly (Assembly inputAssembly, out int majorVersion, out int minorVersion) {
483                         throw new NotImplementedException ();
484                 }
485 #endif
486
487                 [MonoTODO]
488                 public static IntPtr GetUnmanagedThunkForManagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature) {
489                         throw new NotImplementedException ();
490                 }
491
492                 public static bool IsComObject (object o) {
493                         Type t = o.GetType ();
494                         object[] attrs = t.GetCustomAttributes (typeof (ComImportAttribute), true);
495                         return (attrs != null && attrs.Length > 0);
496                 }
497
498                 [MonoTODO]
499                 public static bool IsTypeVisibleFromCom (Type t) {
500                         throw new NotImplementedException ();
501                 }
502
503                 [MonoTODO]
504                 public static int NumParamBytes (MethodInfo m) {
505                         throw new NotImplementedException ();
506                 }
507
508                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
509                 public extern static IntPtr OffsetOf (Type t, string fieldName);
510
511                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
512                 public extern static void Prelink (MethodInfo m);
513
514                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
515                 public extern static void PrelinkAll (Type c);
516
517                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
518                 public extern static string PtrToStringAnsi (IntPtr ptr);
519                 
520                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
521                 public extern static string PtrToStringAnsi (IntPtr ptr, int len);
522
523                 public static string PtrToStringAuto (IntPtr ptr)
524                 {
525                         return SystemDefaultCharSize == 2
526                                 ? PtrToStringUni (ptr) : PtrToStringAnsi (ptr);
527                 }
528                 
529                 public static string PtrToStringAuto (IntPtr ptr, int len)
530                 {
531                         return SystemDefaultCharSize == 2
532                                 ? PtrToStringUni (ptr, len) : PtrToStringAnsi (ptr, len);
533                 }
534
535                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
536                 public extern static string PtrToStringUni (IntPtr ptr);
537
538                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
539                 public extern static string PtrToStringUni (IntPtr ptr, int len);
540
541                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
542                 public extern static string PtrToStringBSTR (IntPtr ptr);
543                 
544                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
545                 public extern static void PtrToStructure (IntPtr ptr, object structure);
546
547                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
548                 public extern static object PtrToStructure (IntPtr ptr, Type structureType);
549
550         [MethodImplAttribute (MethodImplOptions.InternalCall)]
551         public extern static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
552
553                 public static byte ReadByte (IntPtr ptr) {
554                         return ReadByte (ptr, 0);
555                 }
556
557                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
558                 public extern static byte ReadByte (IntPtr ptr, int ofs);
559
560                 [MonoTODO]
561                 public static byte ReadByte ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs) {
562                         throw new NotImplementedException ();
563                 }
564
565                 public static short ReadInt16 (IntPtr ptr) {
566                         return ReadInt16 (ptr, 0);
567                 }
568
569                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
570                 public extern static short ReadInt16 (IntPtr ptr, int ofs);
571
572                 [MonoTODO]
573                 public static short ReadInt16 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs) {
574                         throw new NotImplementedException ();
575                 }
576
577 #if NET_2_0
578                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
579 #endif
580                 public static int ReadInt32 (IntPtr ptr) {
581                         return ReadInt32 (ptr, 0);
582                 }
583
584 #if NET_2_0
585                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
586 #endif
587                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
588                 public extern static int ReadInt32 (IntPtr ptr, int ofs);
589
590 #if NET_2_0
591                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
592 #endif
593                 [MonoTODO]
594                 public static int ReadInt32 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs) {
595                         throw new NotImplementedException ();
596                 }
597
598 #if NET_2_0
599                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
600 #endif
601                 public static long ReadInt64 (IntPtr ptr) {
602                         return ReadInt64 (ptr, 0);
603                 }
604
605 #if NET_2_0
606                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
607 #endif
608                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
609                 public extern static long ReadInt64 (IntPtr ptr, int ofs);
610
611 #if NET_2_0
612                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
613 #endif
614                 [MonoTODO]
615                 public static long ReadInt64 ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs) {
616                         throw new NotImplementedException ();
617                 }
618
619 #if NET_2_0
620                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
621 #endif
622                 public static IntPtr ReadIntPtr (IntPtr ptr) {
623                         return ReadIntPtr (ptr, 0);
624                 }
625                 
626 #if NET_2_0
627                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
628 #endif
629                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
630                 public extern static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
631
632 #if NET_2_0
633                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
634 #endif
635                 [MonoTODO]
636                 public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs) {
637                         throw new NotImplementedException ();
638                 }
639
640                 [MonoTODO]
641                 public static IntPtr ReAllocCoTaskMem (IntPtr pv, int cb) {
642                         throw new NotImplementedException ();
643                 }
644
645                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
646                 public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
647
648 #if NET_2_0
649                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
650 #endif
651         [MethodImplAttribute (MethodImplOptions.InternalCall)]
652         public extern static int Release (IntPtr pUnk);
653
654                 public static int ReleaseComObject (object o) {
655                         if (o == null)
656                                 throw new ArgumentException ("o");
657                         __ComObject co = o as __ComObject;
658                         if (co == null)
659                                 throw new ArgumentException ("o");
660                         return ComInteropProxy.ReleaseComObject (co);
661                 }
662
663 #if NET_2_0
664                 [Obsolete]
665 #endif
666                 [MonoTODO]
667                 public static void ReleaseThreadCache() {
668                         throw new NotImplementedException ();
669                 }
670
671                 public static bool SetComObjectData (object obj, object key, object data) {
672                         if (obj == null)
673                                 throw new ArgumentNullException ("obj");
674                         if (key == null)
675                                 throw new ArgumentNullException ("key");
676
677                         __ComObject com_object = obj as __ComObject;
678                         if (com_object == null)
679                                 throw new ArgumentException ("obj is not a COM object", "obj");
680
681                         com_object.Hashtable[key] = data;
682                         return true;
683                 }
684
685                 public static int SizeOf (object structure) {
686                         return SizeOf (structure.GetType ());
687                 }
688
689                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
690                 public extern static int SizeOf (Type t);
691
692                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
693                 public extern static IntPtr StringToBSTR (string s);
694
695                 public static IntPtr StringToCoTaskMemAnsi (string s) {
696                         int length = s.Length + 1;
697                         IntPtr ctm = AllocCoTaskMem (length);
698
699                         byte[] asBytes = new byte[length];
700                         for (int i = 0; i < s.Length; i++)
701                                 asBytes[i] = (byte)s[i];
702                         asBytes[s.Length] = 0;
703
704                         copy_to_unmanaged (asBytes, 0, ctm, length);
705                         return ctm;
706                 }
707
708                 [MonoTODO]
709                 public static IntPtr StringToCoTaskMemAuto (string s) {
710                         throw new NotImplementedException ();
711                 }
712
713                 public static IntPtr StringToCoTaskMemUni (string s) {
714                         int length = s.Length + 1;
715                         IntPtr ctm = AllocCoTaskMem (length * 2);
716
717                         char[] asChars = new char[length];
718                         s.CopyTo (0, asChars, 0, s.Length);
719                         asChars[s.Length] = '\0';
720
721                         copy_to_unmanaged (asChars, 0, ctm, length);
722                         return ctm;
723                 }
724
725                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
726                 public extern static IntPtr StringToHGlobalAnsi (string s);
727
728                 public static IntPtr StringToHGlobalAuto (string s)
729                 {
730                         return SystemDefaultCharSize == 2
731                                 ? StringToHGlobalUni (s) : StringToHGlobalAnsi (s);
732                 }
733
734                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
735                 public extern static IntPtr StringToHGlobalUni (string s);
736
737 #if NET_2_0
738                 [MonoTODO]
739                 public static IntPtr SecureStringToBSTR (SecureString s)
740                 {
741                         if (s == null)
742                                 throw new ArgumentNullException ("s");
743                         throw new NotSupportedException ();
744                 }
745
746                 [MonoTODO]
747                 public static IntPtr SecureStringToCoTaskMemAnsi (SecureString s)
748                 {
749                         if (s == null)
750                                 throw new ArgumentNullException ("s");
751                         throw new NotSupportedException ();
752                 }
753
754                 [MonoTODO]
755                 public static IntPtr SecureStringToCoTaskMemUnicode (SecureString s)
756                 {
757                         if (s == null)
758                                 throw new ArgumentNullException ("s");
759                         throw new NotSupportedException ();
760                 }
761
762                 [MonoTODO]
763                 public static IntPtr SecureStringToGlobalAllocAnsi (SecureString s)
764                 {
765                         if (s == null)
766                                 throw new ArgumentNullException ("s");
767                         throw new NotSupportedException ();
768                 }
769
770                 [MonoTODO]
771                 public static IntPtr SecureStringToGlobalAllocUnicode (SecureString s)
772                 {
773                         if (s == null)
774                                 throw new ArgumentNullException ("s");
775                         throw new NotSupportedException ();
776                 }
777 #endif
778
779 #if NET_2_0
780                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
781 #endif
782                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
783                 public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
784
785                 public static void ThrowExceptionForHR (int errorCode) {
786             if (errorCode < 0)
787                             throw new COMException ("", errorCode);
788                 }
789
790                 [MonoTODO]
791                 public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo) {
792                         throw new NotImplementedException ();
793                 }
794
795                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
796                 public extern static IntPtr UnsafeAddrOfPinnedArrayElement (Array arr, int index);
797
798                 public static void WriteByte (IntPtr ptr, byte val) {
799                         WriteByte (ptr, 0, val);
800                 }
801
802                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
803                 public extern static void WriteByte (IntPtr ptr, int ofs, byte val);
804
805                 [MonoTODO]
806                 public static void WriteByte ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, byte val) {
807                         throw new NotImplementedException ();
808                 }
809
810                 public static void WriteInt16 (IntPtr ptr, short val) {
811                         WriteInt16 (ptr, 0, val);
812                 }
813
814                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
815                 public extern static void WriteInt16 (IntPtr ptr, int ofs, short val);
816
817                 [MonoTODO]
818                 public static void WriteInt16 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, short val) {
819                         throw new NotImplementedException ();
820                 }
821
822                 public static void WriteInt16 (IntPtr ptr, char val) {
823                         WriteInt16 (ptr, 0, val);
824                 }
825
826                 [MonoTODO]
827                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
828                 public extern static void WriteInt16 (IntPtr ptr, int ofs, char val);
829
830                 [MonoTODO]
831                 public static void WriteInt16([In, Out] object ptr, int ofs, char val) {
832                         throw new NotImplementedException ();
833                 }
834
835                 public static void WriteInt32 (IntPtr ptr, int val) {
836                         WriteInt32 (ptr, 0, val);
837                 }
838
839                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
840                 public extern static void WriteInt32 (IntPtr ptr, int ofs, int val);
841
842                 [MonoTODO]
843                 public static void WriteInt32([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, int val) {
844                         throw new NotImplementedException ();
845                 }
846
847                 public static void WriteInt64 (IntPtr ptr, long val) {
848                         WriteInt64 (ptr, 0, val);
849                 }
850
851                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
852                 public extern static void WriteInt64 (IntPtr ptr, int ofs, long val);
853
854                 [MonoTODO]
855                 public static void WriteInt64 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, long val) {
856                         throw new NotImplementedException ();
857                 }
858
859                 public static void WriteIntPtr (IntPtr ptr, IntPtr val) {
860                         WriteIntPtr (ptr, 0, val);
861                 }
862
863                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
864                 public extern static void WriteIntPtr (IntPtr ptr, int ofs, IntPtr val);
865
866                 [MonoTODO]
867                 public static void WriteIntPtr([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val) {
868                         throw new NotImplementedException ();
869                 }
870
871 #if NET_2_0
872                 [MonoTODO]
873                 public static int FinalReleaseComObject (object o) {
874                         throw new NotImplementedException ();
875                 }
876
877                 [MonoTODO]
878                 public static Exception GetExceptionForHR (int errorCode) {
879                         throw new NotImplementedException ();
880                 }
881
882                 [MonoTODO]
883                 public static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo) {
884                         throw new NotImplementedException ();
885                 }
886
887                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
888                 private static extern Delegate GetDelegateForFunctionPointerInternal (IntPtr ptr, Type t);
889
890                 public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t) {
891                         if (!t.IsSubclassOf (typeof (MulticastDelegate)) || (t == typeof (MulticastDelegate)))
892                                 throw new ArgumentException ("Type is not a delegate", "t");
893                         if (ptr == IntPtr.Zero)
894                                 throw new ArgumentNullException ("ptr");
895
896                         return GetDelegateForFunctionPointerInternal (ptr, t);
897                 }
898
899                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
900                 private static extern IntPtr GetFunctionPointerForDelegateInternal (Delegate d);
901                 
902                 public static IntPtr GetFunctionPointerForDelegate (Delegate d) {
903                         if (d == null)
904                                 throw new ArgumentNullException ("d");
905                         
906                         return GetFunctionPointerForDelegateInternal (d);
907                 }
908 #endif
909         }
910 }