7a3c51b043e64aac96184f9234c69a609948630c
[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 //
7 // (C) 2001-2002 Ximian, Inc.
8
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Runtime.CompilerServices;
33 using System;
34 using System.Security;
35 using System.Reflection;
36 using System.Threading;
37
38 namespace System.Runtime.InteropServices
39 {
40         [SuppressUnmanagedCodeSecurity ()]
41         public sealed class Marshal
42         {
43                 /* fields */
44                 public static readonly int SystemMaxDBCSCharSize = 2; // don't know what this is
45                 public static readonly int SystemDefaultCharSize = 2;
46                 
47                 private Marshal () {}
48
49                 [MonoTODO]
50                 public static int AddRef (IntPtr pUnk) {
51                         throw new NotImplementedException ();
52                 }
53
54                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
55                 public extern static IntPtr AllocCoTaskMem (int cb);
56
57                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
58                 public extern static IntPtr AllocHGlobal (IntPtr cb);
59
60                 public static IntPtr AllocHGlobal (int cb) {
61                         return AllocHGlobal ((IntPtr)cb);
62                 }
63
64                 [MonoTODO]
65                 public static object BindToMoniker (string monikerName) {
66                         throw new NotImplementedException ();
67                 }
68
69                 [MonoTODO]
70                 public static void ChangeWrapperHandleStrength (object otp, bool fIsWeak) {
71                         throw new NotImplementedException ();
72                 }
73
74                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
75                 extern static void copy_to_unmanaged (Array source, int startIndex,
76                                                       IntPtr destination, int length);
77
78                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
79                 extern static void copy_from_unmanaged (IntPtr source, int startIndex,
80                                                         Array destination, int length);
81
82                 public static void Copy (byte[] source, int startIndex, IntPtr destination, int length) {
83                         copy_to_unmanaged (source, startIndex, destination, length);
84                 }
85
86                 public static void Copy (char[] source, int startIndex, IntPtr destination, int length) {
87                         copy_to_unmanaged (source, startIndex, destination, length);
88                 }
89
90                 public static void Copy (short[] source, int startIndex, IntPtr destination, int length) {
91                         copy_to_unmanaged (source, startIndex, destination, length);
92                 }
93
94                 public static void Copy (int[] source, int startIndex, IntPtr destination, int length) {
95                         copy_to_unmanaged (source, startIndex, destination, length);
96                 }
97
98                 public static void Copy (long[] source, int startIndex, IntPtr destination, int length) {
99                         copy_to_unmanaged (source, startIndex, destination, length);
100                 }
101
102                 public static void Copy (float[] source, int startIndex, IntPtr destination, int length) {
103                         copy_to_unmanaged (source, startIndex, destination, length);
104                 }
105
106                 public static void Copy (double[] source, int startIndex, IntPtr destination, int length) {
107                         copy_to_unmanaged (source, startIndex, destination, length);
108                 }
109
110                 public static void Copy (IntPtr source, byte[] destination, int startIndex, int length) {
111                         copy_from_unmanaged (source, startIndex, destination, length);
112                 }
113
114                 public static void Copy (IntPtr source, char[] destination, int startIndex, int length) {
115                         copy_from_unmanaged (source, startIndex, destination, length);
116                 }
117
118                 public static void Copy (IntPtr source, short[] destination, int startIndex, int length) {
119                         copy_from_unmanaged (source, startIndex, destination, length);
120                 }
121
122                 public static void Copy (IntPtr source, int[] destination, int startIndex, int length) {
123                         copy_from_unmanaged (source, startIndex, destination, length);
124                 }
125
126                 public static void Copy (IntPtr source, long[] destination, int startIndex, int length) {
127                         copy_from_unmanaged (source, startIndex, destination, length);
128                 }
129
130                 public static void Copy (IntPtr source, float[] destination, int startIndex, int length) {
131                         copy_from_unmanaged (source, startIndex, destination, length);
132                 }
133
134                 public static void Copy (IntPtr source, double[] destination, int startIndex, int length) {
135                         copy_from_unmanaged (source, startIndex, destination, length);
136                 }
137
138                 [MonoTODO]
139                 public static object CreateWrapperOfType (object o, Type t) {
140                         throw new NotImplementedException ();
141                 }
142
143                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
144                 public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
145
146                 [MonoTODO]
147                 public static void FreeBSTR (IntPtr ptr) {
148                         throw new NotImplementedException ();
149                 }
150
151                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
152                 public extern static void FreeCoTaskMem (IntPtr ptr);
153
154                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
155                 public extern static void FreeHGlobal (IntPtr hglobal);
156
157                 [MonoTODO]
158                 public static Guid GenerateGuidForType (Type type) {
159                         throw new NotImplementedException ();
160                 }
161
162                 [MonoTODO]
163                 public static string GenerateProgIdForType (Type type) {
164                         throw new NotImplementedException ();
165                 }
166
167                 [MonoTODO]
168                 public static object GetActiveObject (string progID) {
169                         throw new NotImplementedException ();
170                 }
171
172                 [MonoTODO]
173                 public static IntPtr GetComInterfaceForObject (object o, Type T) {
174                         throw new NotImplementedException ();
175                 }
176
177                 [MonoTODO]
178                 public static object GetComObjectData (object obj, object key) {
179                         throw new NotImplementedException ();
180                 }
181
182                 [MonoTODO]
183                 public static int GetComSlotForMethodInfo (MemberInfo m) {
184                         throw new NotImplementedException ();
185                 }
186
187                 [MonoTODO]
188                 public static int GetEndComSlot (Type t) {
189                         throw new NotImplementedException ();
190                 }
191
192                 [MonoTODO]
193                 public static int GetExceptionCode() {
194                         throw new NotImplementedException ();
195                 }
196
197                 [MonoTODO]
198                 public static IntPtr GetExceptionPointers() {
199                         throw new NotImplementedException ();
200                 }
201
202                 [MonoTODO]
203                 public static IntPtr GetHINSTANCE (Module m) {
204                         throw new NotImplementedException ();
205                 }
206
207                 [MonoTODO]
208                 public static int GetHRForException (Exception e) {
209                         throw new NotImplementedException ();
210                 }
211
212                 [MonoTODO]
213                 public static int GetHRForLastWin32Error() {
214                         throw new NotImplementedException ();
215                 }
216
217                 [MonoTODO]
218                 public static IntPtr GetIDispatchForObject (object o) {
219                         throw new NotImplementedException ();
220                 }
221
222                 [MonoTODO]
223                 public static IntPtr GetITypeInfoForType (Type t) {
224                         throw new NotImplementedException ();
225                 }
226
227                 [MonoTODO]
228                 public static IntPtr GetIUnknownForObject (object o) {
229                         throw new NotImplementedException ();
230                 }
231
232                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
233                 public static extern int GetLastWin32Error();
234
235                 [MonoTODO]
236                 public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature) {
237                         throw new NotImplementedException ();
238                 }
239
240                 [MonoTODO]
241                 public static MemberInfo GetMethodInfoForComSlot (Type t, int slot, ref ComMemberType memberType) {
242                         throw new NotImplementedException ();
243                 }
244
245                 [MonoTODO]
246                 public static void GetNativeVariantForObject (object obj, IntPtr pDstNativeVariant) {
247                         throw new NotImplementedException ();
248                 }
249
250                 [MonoTODO]
251                 public static object GetObjectForIUnknown (IntPtr pUnk) {
252                         throw new NotImplementedException ();
253                 }
254
255                 [MonoTODO]
256                 public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant) {
257                         throw new NotImplementedException ();
258                 }
259
260                 [MonoTODO]
261                 public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars) {
262                         throw new NotImplementedException ();
263                 }
264
265                 [MonoTODO]
266                 public static int GetStartComSlot (Type t) {
267                         throw new NotImplementedException ();
268                 }
269
270                 [MonoTODO]
271                 public static Thread GetThreadFromFiberCookie (int cookie) {
272                         throw new NotImplementedException ();
273                 }
274
275                 [MonoTODO]
276                 public static object GetTypedObjectForIUnknown (IntPtr pUnk, Type t) {
277                         throw new NotImplementedException ();
278                 }
279
280                 [MonoTODO]
281                 public static Type GetTypeForITypeInfo (IntPtr piTypeInfo) {
282                         throw new NotImplementedException ();
283                 }
284
285                 [MonoTODO]
286                 public static string GetTypeInfoName (UCOMITypeInfo pTI) {
287                         throw new NotImplementedException ();
288                 }
289
290                 [MonoTODO]
291                 public static Guid GetTypeLibGuid (UCOMITypeLib pTLB) {
292                         throw new NotImplementedException ();
293                 }
294
295                 [MonoTODO]
296                 public static Guid GetTypeLibGuidForAssembly (Assembly asm) {
297                         throw new NotImplementedException ();
298                 }
299
300                 [MonoTODO]
301                 public static int GetTypeLibLcid (UCOMITypeLib pTLB) {
302                         throw new NotImplementedException ();
303                 }
304
305                 [MonoTODO]
306                 public static string GetTypeLibName (UCOMITypeLib pTLB) {
307                         throw new NotImplementedException ();
308                 }
309
310                 [MonoTODO]
311                 public static IntPtr GetUnmanagedThunkForManagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature) {
312                         throw new NotImplementedException ();
313                 }
314
315                 [MonoTODO]
316                 public static bool IsComObject (object o) {
317                         throw new NotImplementedException ();
318                 }
319
320                 [MonoTODO]
321                 public static bool IsTypeVisibleFromCom (Type t) {
322                         throw new NotImplementedException ();
323                 }
324
325                 [MonoTODO]
326                 public static int NumParamBytes (MethodInfo m) {
327                         throw new NotImplementedException ();
328                 }
329
330                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
331                 public extern static IntPtr OffsetOf (Type t, string fieldName);
332
333                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
334                 public extern static void Prelink (MethodInfo m);
335
336                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
337                 public extern static void PrelinkAll (Type c);
338
339                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
340                 public extern static string PtrToStringAnsi (IntPtr ptr);
341                 
342                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
343                 public extern static string PtrToStringAnsi (IntPtr ptr, int len);
344
345                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
346                 public extern static string PtrToStringAuto (IntPtr ptr);
347                 
348                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
349                 public extern static string PtrToStringAuto (IntPtr ptr, int len);
350
351                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
352                 public extern static string PtrToStringUni (IntPtr ptr);
353
354                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
355                 public extern static string PtrToStringUni (IntPtr ptr, int len);
356
357                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
358                 public extern static string PtrToStringBSTR (IntPtr ptr);
359                 
360                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
361                 public extern static void PtrToStructure (IntPtr ptr, object structure);
362
363                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
364                 public extern static object PtrToStructure (IntPtr ptr, Type structureType);
365
366                 [MonoTODO]
367                 public static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv) {
368                         throw new NotImplementedException ();
369                 }
370
371                 public static byte ReadByte (IntPtr ptr) {
372                         return ReadByte (ptr, 0);
373                 }
374
375                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
376                 public extern static byte ReadByte (IntPtr ptr, int ofs);
377
378                 [MonoTODO]
379                 public static byte ReadByte ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs) {
380                         throw new NotImplementedException ();
381                 }
382
383                 public static short ReadInt16 (IntPtr ptr) {
384                         return ReadInt16 (ptr, 0);
385                 }
386
387                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
388                 public extern static short ReadInt16 (IntPtr ptr, int ofs);
389
390                 [MonoTODO]
391                 public static short ReadInt16 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs) {
392                         throw new NotImplementedException ();
393                 }
394
395                 public static int ReadInt32 (IntPtr ptr) {
396                         return ReadInt32 (ptr, 0);
397                 }
398
399                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
400                 public extern static int ReadInt32 (IntPtr ptr, int ofs);
401
402                 [MonoTODO]
403                 public static int ReadInt32 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs) {
404                         throw new NotImplementedException ();
405                 }
406
407                 public static long ReadInt64 (IntPtr ptr) {
408                         return ReadInt64 (ptr, 0);
409                 }
410
411                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
412                 public extern static long ReadInt64 (IntPtr ptr, int ofs);
413
414                 [MonoTODO]
415                 public static long ReadInt64 ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs) {
416                         throw new NotImplementedException ();
417                 }
418
419                 public static IntPtr ReadIntPtr (IntPtr ptr) {
420                         return ReadIntPtr (ptr, 0);
421                 }
422                 
423                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
424                 public extern static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
425
426                 [MonoTODO]
427                 public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs) {
428                         throw new NotImplementedException ();
429                 }
430
431                 [MonoTODO]
432                 public static IntPtr ReAllocCoTaskMem (IntPtr pv, int cb) {
433                         throw new NotImplementedException ();
434                 }
435
436                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
437                 public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
438
439                 [MonoTODO]
440                 public static int Release (IntPtr pUnk) {
441                         throw new NotImplementedException ();
442                 }
443
444                 [MonoTODO]
445                 public static int ReleaseComObject (object o) {
446                         throw new NotImplementedException ();
447                 }
448
449                 [MonoTODO]
450                 public static void ReleaseThreadCache() {
451                         throw new NotImplementedException ();
452                 }
453
454                 [MonoTODO]
455                 public static bool SetComObjectData (object obj, object key, object data) {
456                         throw new NotImplementedException ();
457                 }
458
459                 public static int SizeOf (object structure) {
460                         return SizeOf (structure.GetType ());
461                 }
462
463                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
464                 public extern static int SizeOf (Type t);
465
466                 [MonoTODO]
467                 public static IntPtr StringToBSTR (string s) {
468                         throw new NotImplementedException ();
469                 }
470
471                 public static IntPtr StringToCoTaskMemAnsi (string s) {
472                         int length = s.Length + 1;
473                         IntPtr ctm = AllocCoTaskMem (length);
474
475                         byte[] asBytes = new byte[length];
476                         for (int i = 0; i < s.Length; i++)
477                                 asBytes[i] = (byte)s[i];
478                         asBytes[s.Length] = 0;
479
480                         copy_to_unmanaged (asBytes, 0, ctm, length);
481                         return ctm;
482                 }
483
484                 [MonoTODO]
485                 public static IntPtr StringToCoTaskMemAuto (string s) {
486                         throw new NotImplementedException ();
487                 }
488
489                 public static IntPtr StringToCoTaskMemUni (string s) {
490                         int length = s.Length + 1;
491                         IntPtr ctm = AllocCoTaskMem (length * 2);
492
493                         char[] asChars = new char[length];
494                         s.CopyTo (0, asChars, 0, s.Length);
495                         asChars[s.Length] = '\0';
496
497                         copy_to_unmanaged (asChars, 0, ctm, length);
498                         return ctm;
499                 }
500
501                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
502                 public extern static IntPtr StringToHGlobalAnsi (string s);
503
504                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
505                 public extern static IntPtr StringToHGlobalAuto (string s);
506
507                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
508                 public extern static IntPtr StringToHGlobalUni (string s);
509
510                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
511                 public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
512
513                 [MonoTODO]
514                 public static void ThrowExceptionForHR (int errorCode) {
515                         throw new NotImplementedException ();
516                 }
517
518                 [MonoTODO]
519                 public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo) {
520                         throw new NotImplementedException ();
521                 }
522
523                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
524                 public extern static IntPtr UnsafeAddrOfPinnedArrayElement (Array arr, int index);
525
526                 public static void WriteByte (IntPtr ptr, byte val) {
527                         WriteByte (ptr, 0, val);
528                 }
529
530                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
531                 public extern static void WriteByte (IntPtr ptr, int ofs, byte val);
532
533                 [MonoTODO]
534                 public static void WriteByte ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, byte val) {
535                         throw new NotImplementedException ();
536                 }
537
538                 public static void WriteInt16 (IntPtr ptr, short val) {
539                         WriteInt16 (ptr, 0, val);
540                 }
541
542                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
543                 public extern static void WriteInt16 (IntPtr ptr, int ofs, short val);
544
545                 [MonoTODO]
546                 public static void WriteInt16 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, short val) {
547                         throw new NotImplementedException ();
548                 }
549
550                 public static void WriteInt16 (IntPtr ptr, char val) {
551                         WriteInt16 (ptr, 0, val);
552                 }
553
554                 [MonoTODO]
555                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
556                 public extern static void WriteInt16 (IntPtr ptr, int ofs, char val);
557
558                 [MonoTODO]
559                 public static void WriteInt16([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, char val) {
560                         throw new NotImplementedException ();
561                 }
562
563                 public static void WriteInt32 (IntPtr ptr, int val) {
564                         WriteInt32 (ptr, 0, val);
565                 }
566
567                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
568                 public extern static void WriteInt32 (IntPtr ptr, int ofs, int val);
569
570                 [MonoTODO]
571                 public static void WriteInt32([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, int val) {
572                         throw new NotImplementedException ();
573                 }
574
575                 public static void WriteInt64 (IntPtr ptr, long val) {
576                         WriteInt64 (ptr, 0, val);
577                 }
578
579                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
580                 public extern static void WriteInt64 (IntPtr ptr, int ofs, long val);
581
582                 [MonoTODO]
583                 public static void WriteInt64 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, long val) {
584                         throw new NotImplementedException ();
585                 }
586
587                 public static void WriteIntPtr (IntPtr ptr, IntPtr val) {
588                         WriteIntPtr (ptr, 0, val);
589                 }
590
591                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
592                 public extern static void WriteIntPtr (IntPtr ptr, int ofs, IntPtr val);
593
594                 [MonoTODO]
595                 public static void WriteIntPtr([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val) {
596                         throw new NotImplementedException ();
597                 }
598
599         }
600 }