mscorlib(4.0.0.0) API with additions
Marshal.cs
1 using Microsoft.Win32;
3 using System.Reflection;
8 using System.Security;
9 using System.Threading;
10 
12 {
14  [__DynamicallyInvokable]
15  public static class Marshal
16  {
17  private const int LMEM_FIXED = 0;
18 
19  private const int LMEM_MOVEABLE = 2;
20 
21  private const long HIWORDMASK = -65536L;
22 
23  private static Guid IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");
24 
26  public static readonly int SystemDefaultCharSize = 2;
27 
29  public static readonly int SystemMaxDBCSCharSize = GetSystemMaxDBCSCharSize();
30 
31  private const string s_strConvertedTypeInfoAssemblyName = "InteropDynamicTypes";
32 
33  private const string s_strConvertedTypeInfoAssemblyTitle = "Interop Dynamic Types";
34 
35  private const string s_strConvertedTypeInfoAssemblyDesc = "Type dynamically generated from ITypeInfo's";
36 
37  private const string s_strConvertedTypeInfoNameSpace = "InteropDynamicTypes";
38 
39  internal static readonly Guid ManagedNameGuid = new Guid("{0F21F359-AB84-41E8-9A78-36D110E6D2F9}");
40 
41  private static bool IsWin32Atom(IntPtr ptr)
42  {
43  long num = (long)ptr;
44  return (num & -65536) == 0;
45  }
46 
47  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
48  private static bool IsNotWin32Atom(IntPtr ptr)
49  {
50  long num = (long)ptr;
51  return (num & -65536) != 0;
52  }
53 
54  [MethodImpl(MethodImplOptions.InternalCall)]
55  private static extern int GetSystemMaxDBCSCharSize();
56 
60  [SecurityCritical]
61  public unsafe static string PtrToStringAnsi(IntPtr ptr)
62  {
63  if (IntPtr.Zero == ptr)
64  {
65  return null;
66  }
67  if (IsWin32Atom(ptr))
68  {
69  return null;
70  }
71  if (Win32Native.lstrlenA(ptr) == 0)
72  {
73  return string.Empty;
74  }
75  return new string((sbyte*)(void*)ptr);
76  }
77 
84  [SecurityCritical]
85  public unsafe static string PtrToStringAnsi(IntPtr ptr, int len)
86  {
87  if (ptr == IntPtr.Zero)
88  {
89  throw new ArgumentNullException("ptr");
90  }
91  if (len < 0)
92  {
93  throw new ArgumentException("len");
94  }
95  return new string((sbyte*)(void*)ptr, 0, len);
96  }
97 
102  [SecurityCritical]
103  public unsafe static string PtrToStringUni(IntPtr ptr, int len)
104  {
105  if (ptr == IntPtr.Zero)
106  {
107  throw new ArgumentNullException("ptr");
108  }
109  if (len < 0)
110  {
111  throw new ArgumentException("len");
112  }
113  return new string((char*)(void*)ptr, 0, len);
114  }
115 
122  [SecurityCritical]
123  public static string PtrToStringAuto(IntPtr ptr, int len)
124  {
125  return PtrToStringUni(ptr, len);
126  }
127 
131  [SecurityCritical]
132  public unsafe static string PtrToStringUni(IntPtr ptr)
133  {
134  if (IntPtr.Zero == ptr)
135  {
136  return null;
137  }
138  if (IsWin32Atom(ptr))
139  {
140  return null;
141  }
142  return new string((char*)(void*)ptr);
143  }
144 
148  [SecurityCritical]
149  public static string PtrToStringAuto(IntPtr ptr)
150  {
151  return PtrToStringUni(ptr);
152  }
153 
158  [ComVisible(true)]
159  public static int SizeOf(object structure)
160  {
161  if (structure == null)
162  {
163  throw new ArgumentNullException("structure");
164  }
165  return SizeOfHelper(structure.GetType(), throwIfNotMarshalable: true);
166  }
167 
173  public static int SizeOf<T>(T structure)
174  {
175  return SizeOf((object)structure);
176  }
177 
183  public static int SizeOf(Type t)
184  {
185  if (t == null)
186  {
187  throw new ArgumentNullException("t");
188  }
189  if (!(t is RuntimeType))
190  {
191  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "t");
192  }
193  if (t.IsGenericType)
194  {
195  throw new ArgumentException(Environment.GetResourceString("Argument_NeedNonGenericType"), "t");
196  }
197  return SizeOfHelper(t, throwIfNotMarshalable: true);
198  }
199 
203  public static int SizeOf<T>()
204  {
205  return SizeOf(typeof(T));
206  }
207 
208  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
209  internal static uint AlignedSizeOf<T>() where T : struct
210  {
211  uint num = SizeOfType(typeof(T));
212  if (num == 1 || num == 2)
213  {
214  return num;
215  }
216  if (IntPtr.Size == 8 && num == 4)
217  {
218  return num;
219  }
220  return AlignedSizeOfType(typeof(T));
221  }
222 
223  [MethodImpl(MethodImplOptions.InternalCall)]
224  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
225  internal static extern uint SizeOfType(Type type);
226 
227  [MethodImpl(MethodImplOptions.InternalCall)]
228  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
229  private static extern uint AlignedSizeOfType(Type type);
230 
231  [MethodImpl(MethodImplOptions.InternalCall)]
232  [SecuritySafeCritical]
233  internal static extern int SizeOfHelper(Type t, bool throwIfNotMarshalable);
234 
241  public static IntPtr OffsetOf(Type t, string fieldName)
242  {
243  if (t == null)
244  {
245  throw new ArgumentNullException("t");
246  }
247  FieldInfo field = t.GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
248  if (field == null)
249  {
250  throw new ArgumentException(Environment.GetResourceString("Argument_OffsetOfFieldNotFound", t.FullName), "fieldName");
251  }
252  RtFieldInfo rtFieldInfo = field as RtFieldInfo;
253  if (rtFieldInfo == null)
254  {
255  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeFieldInfo"), "fieldName");
256  }
257  return OffsetOfHelper(rtFieldInfo);
258  }
259 
264  public static IntPtr OffsetOf<T>(string fieldName)
265  {
266  return OffsetOf(typeof(T), fieldName);
267  }
268 
269  [MethodImpl(MethodImplOptions.InternalCall)]
270  private static extern IntPtr OffsetOfHelper(IRuntimeFieldInfo f);
271 
276  [MethodImpl(MethodImplOptions.InternalCall)]
277  [SecurityCritical]
278  public static extern IntPtr UnsafeAddrOfPinnedArrayElement(Array arr, int index);
279 
285  [SecurityCritical]
286  public static IntPtr UnsafeAddrOfPinnedArrayElement<T>(T[] arr, int index)
287  {
288  return UnsafeAddrOfPinnedArrayElement((Array)arr, index);
289  }
290 
300  [SecurityCritical]
301  public static void Copy(int[] source, int startIndex, IntPtr destination, int length)
302  {
303  CopyToNative(source, startIndex, destination, length);
304  }
305 
315  [SecurityCritical]
316  public static void Copy(char[] source, int startIndex, IntPtr destination, int length)
317  {
318  CopyToNative(source, startIndex, destination, length);
319  }
320 
330  [SecurityCritical]
331  public static void Copy(short[] source, int startIndex, IntPtr destination, int length)
332  {
333  CopyToNative(source, startIndex, destination, length);
334  }
335 
345  [SecurityCritical]
346  public static void Copy(long[] source, int startIndex, IntPtr destination, int length)
347  {
348  CopyToNative(source, startIndex, destination, length);
349  }
350 
360  [SecurityCritical]
361  public static void Copy(float[] source, int startIndex, IntPtr destination, int length)
362  {
363  CopyToNative(source, startIndex, destination, length);
364  }
365 
375  [SecurityCritical]
376  public static void Copy(double[] source, int startIndex, IntPtr destination, int length)
377  {
378  CopyToNative(source, startIndex, destination, length);
379  }
380 
390  [SecurityCritical]
391  public static void Copy(byte[] source, int startIndex, IntPtr destination, int length)
392  {
393  CopyToNative(source, startIndex, destination, length);
394  }
395 
403  [SecurityCritical]
404  public static void Copy(IntPtr[] source, int startIndex, IntPtr destination, int length)
405  {
406  CopyToNative(source, startIndex, destination, length);
407  }
408 
409  [MethodImpl(MethodImplOptions.InternalCall)]
410  private static extern void CopyToNative(object source, int startIndex, IntPtr destination, int length);
411 
419  [SecurityCritical]
420  public static void Copy(IntPtr source, int[] destination, int startIndex, int length)
421  {
422  CopyToManaged(source, destination, startIndex, length);
423  }
424 
432  [SecurityCritical]
433  public static void Copy(IntPtr source, char[] destination, int startIndex, int length)
434  {
435  CopyToManaged(source, destination, startIndex, length);
436  }
437 
445  [SecurityCritical]
446  public static void Copy(IntPtr source, short[] destination, int startIndex, int length)
447  {
448  CopyToManaged(source, destination, startIndex, length);
449  }
450 
458  [SecurityCritical]
459  public static void Copy(IntPtr source, long[] destination, int startIndex, int length)
460  {
461  CopyToManaged(source, destination, startIndex, length);
462  }
463 
471  [SecurityCritical]
472  public static void Copy(IntPtr source, float[] destination, int startIndex, int length)
473  {
474  CopyToManaged(source, destination, startIndex, length);
475  }
476 
484  [SecurityCritical]
485  public static void Copy(IntPtr source, double[] destination, int startIndex, int length)
486  {
487  CopyToManaged(source, destination, startIndex, length);
488  }
489 
497  [SecurityCritical]
498  public static void Copy(IntPtr source, byte[] destination, int startIndex, int length)
499  {
500  CopyToManaged(source, destination, startIndex, length);
501  }
502 
510  [SecurityCritical]
511  public static void Copy(IntPtr source, IntPtr[] destination, int startIndex, int length)
512  {
513  CopyToManaged(source, destination, startIndex, length);
514  }
515 
516  [MethodImpl(MethodImplOptions.InternalCall)]
517  private static extern void CopyToManaged(IntPtr source, object destination, int startIndex, int length);
518 
526  [DllImport("mscoree.dll", EntryPoint = "ND_RU1")]
527  [SuppressUnmanagedCodeSecurity]
528  [SecurityCritical]
529  public static extern byte ReadByte([In] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs);
530 
536  [SecurityCritical]
537  public unsafe static byte ReadByte(IntPtr ptr, int ofs)
538  {
539  try
540  {
541  byte* ptr2 = (byte*)(void*)ptr + ofs;
542  return *ptr2;
543  }
544  catch (NullReferenceException)
545  {
546  throw new AccessViolationException();
547  }
548  }
549 
557  [SecurityCritical]
558  public static byte ReadByte(IntPtr ptr)
559  {
560  return ReadByte(ptr, 0);
561  }
562 
570  [DllImport("mscoree.dll", EntryPoint = "ND_RI2")]
571  [SuppressUnmanagedCodeSecurity]
572  [SecurityCritical]
573  public static extern short ReadInt16([In] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs);
574 
580  [SecurityCritical]
581  public unsafe static short ReadInt16(IntPtr ptr, int ofs)
582  {
583  try
584  {
585  byte* ptr2 = (byte*)(void*)ptr + ofs;
586  if (((int)ptr2 & 1) == 0)
587  {
588  return *(short*)ptr2;
589  }
590  short result = default(short);
591  byte* ptr3 = (byte*)(&result);
592  *ptr3 = *ptr2;
593  ptr3[1] = ptr2[1];
594  return result;
595  }
596  catch (NullReferenceException)
597  {
598  throw new AccessViolationException();
599  }
600  }
601 
609  [SecurityCritical]
610  public static short ReadInt16(IntPtr ptr)
611  {
612  return ReadInt16(ptr, 0);
613  }
614 
622  [DllImport("mscoree.dll", EntryPoint = "ND_RI4")]
623  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
624  [SuppressUnmanagedCodeSecurity]
625  [SecurityCritical]
626  public static extern int ReadInt32([In] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs);
627 
633  [SecurityCritical]
634  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
635  public unsafe static int ReadInt32(IntPtr ptr, int ofs)
636  {
637  try
638  {
639  byte* ptr2 = (byte*)(void*)ptr + ofs;
640  if (((int)ptr2 & 3) == 0)
641  {
642  return *(int*)ptr2;
643  }
644  int result = default(int);
645  byte* ptr3 = (byte*)(&result);
646  *ptr3 = *ptr2;
647  ptr3[1] = ptr2[1];
648  ptr3[2] = ptr2[2];
649  ptr3[3] = ptr2[3];
650  return result;
651  }
652  catch (NullReferenceException)
653  {
654  throw new AccessViolationException();
655  }
656  }
657 
665  [SecurityCritical]
666  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
667  public static int ReadInt32(IntPtr ptr)
668  {
669  return ReadInt32(ptr, 0);
670  }
671 
679  [SecurityCritical]
680  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
681  public static IntPtr ReadIntPtr([In] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
682  {
683  return (IntPtr)ReadInt64(ptr, ofs);
684  }
685 
691  [SecurityCritical]
692  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
693  public static IntPtr ReadIntPtr(IntPtr ptr, int ofs)
694  {
695  return (IntPtr)ReadInt64(ptr, ofs);
696  }
697 
705  [SecurityCritical]
706  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
707  public static IntPtr ReadIntPtr(IntPtr ptr)
708  {
709  return (IntPtr)ReadInt64(ptr, 0);
710  }
711 
719  [DllImport("mscoree.dll", EntryPoint = "ND_RI8")]
720  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
721  [SuppressUnmanagedCodeSecurity]
722  [SecurityCritical]
723  public static extern long ReadInt64([In] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs);
724 
730  [SecurityCritical]
731  public unsafe static long ReadInt64(IntPtr ptr, int ofs)
732  {
733  try
734  {
735  byte* ptr2 = (byte*)(void*)ptr + ofs;
736  if (((int)ptr2 & 7) == 0)
737  {
738  return *(long*)ptr2;
739  }
740  long result = default(long);
741  byte* ptr3 = (byte*)(&result);
742  *ptr3 = *ptr2;
743  ptr3[1] = ptr2[1];
744  ptr3[2] = ptr2[2];
745  ptr3[3] = ptr2[3];
746  ptr3[4] = ptr2[4];
747  ptr3[5] = ptr2[5];
748  ptr3[6] = ptr2[6];
749  ptr3[7] = ptr2[7];
750  return result;
751  }
752  catch (NullReferenceException)
753  {
754  throw new AccessViolationException();
755  }
756  }
757 
765  [SecurityCritical]
766  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
767  public static long ReadInt64(IntPtr ptr)
768  {
769  return ReadInt64(ptr, 0);
770  }
771 
777  [SecurityCritical]
778  public unsafe static void WriteByte(IntPtr ptr, int ofs, byte val)
779  {
780  try
781  {
782  byte* ptr2 = (byte*)(void*)ptr + ofs;
783  *ptr2 = val;
784  }
785  catch (NullReferenceException)
786  {
787  throw new AccessViolationException();
788  }
789  }
790 
798  [DllImport("mscoree.dll", EntryPoint = "ND_WU1")]
799  [SuppressUnmanagedCodeSecurity]
800  [SecurityCritical]
801  public static extern void WriteByte([In] [Out] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, byte val);
802 
810  [SecurityCritical]
811  public static void WriteByte(IntPtr ptr, byte val)
812  {
813  WriteByte(ptr, 0, val);
814  }
815 
821  [SecurityCritical]
822  public unsafe static void WriteInt16(IntPtr ptr, int ofs, short val)
823  {
824  try
825  {
826  byte* ptr2 = (byte*)(void*)ptr + ofs;
827  if (((int)ptr2 & 1) == 0)
828  {
829  *(short*)ptr2 = val;
830  }
831  else
832  {
833  byte* ptr3 = (byte*)(&val);
834  *ptr2 = *ptr3;
835  ptr2[1] = ptr3[1];
836  }
837  }
838  catch (NullReferenceException)
839  {
840  throw new AccessViolationException();
841  }
842  }
843 
851  [DllImport("mscoree.dll", EntryPoint = "ND_WI2")]
852  [SuppressUnmanagedCodeSecurity]
853  [SecurityCritical]
854  public static extern void WriteInt16([In] [Out] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, short val);
855 
863  [SecurityCritical]
864  public static void WriteInt16(IntPtr ptr, short val)
865  {
866  WriteInt16(ptr, 0, val);
867  }
868 
874  [SecurityCritical]
875  public static void WriteInt16(IntPtr ptr, int ofs, char val)
876  {
877  WriteInt16(ptr, ofs, (short)val);
878  }
879 
887  [SecurityCritical]
888  public static void WriteInt16([In] [Out] object ptr, int ofs, char val)
889  {
890  WriteInt16(ptr, ofs, (short)val);
891  }
892 
900  [SecurityCritical]
901  public static void WriteInt16(IntPtr ptr, char val)
902  {
903  WriteInt16(ptr, 0, (short)val);
904  }
905 
911  [SecurityCritical]
912  public unsafe static void WriteInt32(IntPtr ptr, int ofs, int val)
913  {
914  try
915  {
916  byte* ptr2 = (byte*)(void*)ptr + ofs;
917  if (((int)ptr2 & 3) == 0)
918  {
919  *(int*)ptr2 = val;
920  }
921  else
922  {
923  byte* ptr3 = (byte*)(&val);
924  *ptr2 = *ptr3;
925  ptr2[1] = ptr3[1];
926  ptr2[2] = ptr3[2];
927  ptr2[3] = ptr3[3];
928  }
929  }
930  catch (NullReferenceException)
931  {
932  throw new AccessViolationException();
933  }
934  }
935 
943  [DllImport("mscoree.dll", EntryPoint = "ND_WI4")]
944  [SuppressUnmanagedCodeSecurity]
945  [SecurityCritical]
946  public static extern void WriteInt32([In] [Out] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, int val);
947 
955  [SecurityCritical]
956  public static void WriteInt32(IntPtr ptr, int val)
957  {
958  WriteInt32(ptr, 0, val);
959  }
960 
966  [SecurityCritical]
967  public static void WriteIntPtr(IntPtr ptr, int ofs, IntPtr val)
968  {
969  WriteInt64(ptr, ofs, (long)val);
970  }
971 
979  [SecurityCritical]
980  public static void WriteIntPtr([In] [Out] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val)
981  {
982  WriteInt64(ptr, ofs, (long)val);
983  }
984 
992  [SecurityCritical]
993  public static void WriteIntPtr(IntPtr ptr, IntPtr val)
994  {
995  WriteInt64(ptr, 0, (long)val);
996  }
997 
1003  [SecurityCritical]
1004  public unsafe static void WriteInt64(IntPtr ptr, int ofs, long val)
1005  {
1006  try
1007  {
1008  byte* ptr2 = (byte*)(void*)ptr + ofs;
1009  if (((int)ptr2 & 7) == 0)
1010  {
1011  *(long*)ptr2 = val;
1012  }
1013  else
1014  {
1015  byte* ptr3 = (byte*)(&val);
1016  *ptr2 = *ptr3;
1017  ptr2[1] = ptr3[1];
1018  ptr2[2] = ptr3[2];
1019  ptr2[3] = ptr3[3];
1020  ptr2[4] = ptr3[4];
1021  ptr2[5] = ptr3[5];
1022  ptr2[6] = ptr3[6];
1023  ptr2[7] = ptr3[7];
1024  }
1025  }
1026  catch (NullReferenceException)
1027  {
1028  throw new AccessViolationException();
1029  }
1030  }
1031 
1039  [DllImport("mscoree.dll", EntryPoint = "ND_WI8")]
1040  [SuppressUnmanagedCodeSecurity]
1041  [SecurityCritical]
1042  public static extern void WriteInt64([In] [Out] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, long val);
1043 
1051  [SecurityCritical]
1052  public static void WriteInt64(IntPtr ptr, long val)
1053  {
1054  WriteInt64(ptr, 0, val);
1055  }
1056 
1059  [MethodImpl(MethodImplOptions.InternalCall)]
1060  [SecurityCritical]
1061  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
1062  public static extern int GetLastWin32Error();
1063 
1064  [MethodImpl(MethodImplOptions.InternalCall)]
1065  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
1066  internal static extern void SetLastWin32Error(int error);
1067 
1070  [SecurityCritical]
1071  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
1072  public static int GetHRForLastWin32Error()
1073  {
1074  int lastWin32Error = GetLastWin32Error();
1075  if ((lastWin32Error & 2147483648u) == 2147483648u)
1076  {
1077  return lastWin32Error;
1078  }
1079  return (lastWin32Error & 0xFFFF) | -2147024896;
1080  }
1081 
1086  [SecurityCritical]
1087  public static void Prelink(MethodInfo m)
1088  {
1089  if (m == null)
1090  {
1091  throw new ArgumentNullException("m");
1092  }
1093  RuntimeMethodInfo runtimeMethodInfo = m as RuntimeMethodInfo;
1094  if (runtimeMethodInfo == null)
1095  {
1096  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"));
1097  }
1098  InternalPrelink(runtimeMethodInfo);
1099  }
1100 
1101  [DllImport("QCall", CharSet = CharSet.Unicode)]
1102  [SuppressUnmanagedCodeSecurity]
1103  [SecurityCritical]
1104  private static extern void InternalPrelink(IRuntimeMethodInfo m);
1105 
1109  [SecurityCritical]
1110  public static void PrelinkAll(Type c)
1111  {
1112  if (c == null)
1113  {
1114  throw new ArgumentNullException("c");
1115  }
1116  MethodInfo[] methods = c.GetMethods();
1117  if (methods != null)
1118  {
1119  for (int i = 0; i < methods.Length; i++)
1120  {
1121  Prelink(methods[i]);
1122  }
1123  }
1124  }
1125 
1131  [SecurityCritical]
1132  public static int NumParamBytes(MethodInfo m)
1133  {
1134  if (m == null)
1135  {
1136  throw new ArgumentNullException("m");
1137  }
1138  RuntimeMethodInfo runtimeMethodInfo = m as RuntimeMethodInfo;
1139  if (runtimeMethodInfo == null)
1140  {
1141  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"));
1142  }
1143  return InternalNumParamBytes(runtimeMethodInfo);
1144  }
1145 
1146  [DllImport("QCall", CharSet = CharSet.Unicode)]
1147  [SuppressUnmanagedCodeSecurity]
1148  [SecurityCritical]
1149  private static extern int InternalNumParamBytes(IRuntimeMethodInfo m);
1150 
1153  [MethodImpl(MethodImplOptions.InternalCall)]
1154  [SecurityCritical]
1155  [ComVisible(true)]
1156  public static extern IntPtr GetExceptionPointers();
1157 
1160  [MethodImpl(MethodImplOptions.InternalCall)]
1161  [SecurityCritical]
1162  public static extern int GetExceptionCode();
1163 
1172  [MethodImpl(MethodImplOptions.InternalCall)]
1173  [SecurityCritical]
1174  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
1175  [ComVisible(true)]
1176  public static extern void StructureToPtr(object structure, IntPtr ptr, bool fDeleteOld);
1177 
1186  [SecurityCritical]
1187  public static void StructureToPtr<T>(T structure, IntPtr ptr, bool fDeleteOld)
1188  {
1189  StructureToPtr((object)structure, ptr, fDeleteOld);
1190  }
1191 
1196  [SecurityCritical]
1197  [ComVisible(true)]
1198  public static void PtrToStructure(IntPtr ptr, object structure)
1199  {
1200  PtrToStructureHelper(ptr, structure, allowValueClasses: false);
1201  }
1202 
1208  [SecurityCritical]
1209  public static void PtrToStructure<T>(IntPtr ptr, T structure)
1210  {
1211  PtrToStructure(ptr, (object)structure);
1212  }
1213 
1223  [MethodImpl(MethodImplOptions.NoInlining)]
1224  [SecurityCritical]
1225  [ComVisible(true)]
1226  public static object PtrToStructure(IntPtr ptr, Type structureType)
1227  {
1228  if (ptr == IntPtr.Zero)
1229  {
1230  return null;
1231  }
1232  if (structureType == null)
1233  {
1234  throw new ArgumentNullException("structureType");
1235  }
1236  if (structureType.IsGenericType)
1237  {
1238  throw new ArgumentException(Environment.GetResourceString("Argument_NeedNonGenericType"), "structureType");
1239  }
1240  RuntimeType runtimeType = structureType.UnderlyingSystemType as RuntimeType;
1241  if (runtimeType == null)
1242  {
1243  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type");
1244  }
1245  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
1246  object obj = runtimeType.CreateInstanceDefaultCtor(publicOnly: false, skipCheckThis: false, fillCache: false, ref stackMark);
1247  PtrToStructureHelper(ptr, obj, allowValueClasses: true);
1248  return obj;
1249  }
1250 
1257  [SecurityCritical]
1258  public static T PtrToStructure<T>(IntPtr ptr)
1259  {
1260  return (T)PtrToStructure(ptr, typeof(T));
1261  }
1262 
1263  [MethodImpl(MethodImplOptions.InternalCall)]
1264  private static extern void PtrToStructureHelper(IntPtr ptr, object structure, bool allowValueClasses);
1265 
1271  [MethodImpl(MethodImplOptions.InternalCall)]
1272  [SecurityCritical]
1273  [ComVisible(true)]
1274  public static extern void DestroyStructure(IntPtr ptr, Type structuretype);
1275 
1281  [SecurityCritical]
1282  public static void DestroyStructure<T>(IntPtr ptr)
1283  {
1284  DestroyStructure(ptr, typeof(T));
1285  }
1286 
1291  [SecurityCritical]
1292  public static IntPtr GetHINSTANCE(Module m)
1293  {
1294  if (m == null)
1295  {
1296  throw new ArgumentNullException("m");
1297  }
1298  RuntimeModule runtimeModule = m as RuntimeModule;
1299  if (runtimeModule == null)
1300  {
1301  ModuleBuilder moduleBuilder = m as ModuleBuilder;
1302  if (moduleBuilder != null)
1303  {
1304  runtimeModule = moduleBuilder.InternalModule;
1305  }
1306  }
1307  if (runtimeModule == null)
1308  {
1309  throw new ArgumentNullException(Environment.GetResourceString("Argument_MustBeRuntimeModule"));
1310  }
1311  return GetHINSTANCE(runtimeModule.GetNativeHandle());
1312  }
1313 
1314  [DllImport("QCall", CharSet = CharSet.Unicode)]
1315  [SecurityCritical]
1316  [SuppressUnmanagedCodeSecurity]
1317  [SuppressUnmanagedCodeSecurity]
1318  private static extern IntPtr GetHINSTANCE(RuntimeModule m);
1319 
1322  [SecurityCritical]
1323  public static void ThrowExceptionForHR(int errorCode)
1324  {
1325  if (errorCode < 0)
1326  {
1327  ThrowExceptionForHRInternal(errorCode, IntPtr.Zero);
1328  }
1329  }
1330 
1335  [SecurityCritical]
1336  public static void ThrowExceptionForHR(int errorCode, IntPtr errorInfo)
1337  {
1338  if (errorCode < 0)
1339  {
1340  ThrowExceptionForHRInternal(errorCode, errorInfo);
1341  }
1342  }
1343 
1344  [MethodImpl(MethodImplOptions.InternalCall)]
1345  internal static extern void ThrowExceptionForHRInternal(int errorCode, IntPtr errorInfo);
1346 
1350  [SecurityCritical]
1351  public static Exception GetExceptionForHR(int errorCode)
1352  {
1353  if (errorCode < 0)
1354  {
1355  return GetExceptionForHRInternal(errorCode, IntPtr.Zero);
1356  }
1357  return null;
1358  }
1359 
1364  [SecurityCritical]
1365  public static Exception GetExceptionForHR(int errorCode, IntPtr errorInfo)
1366  {
1367  if (errorCode < 0)
1368  {
1369  return GetExceptionForHRInternal(errorCode, errorInfo);
1370  }
1371  return null;
1372  }
1373 
1374  [MethodImpl(MethodImplOptions.InternalCall)]
1375  internal static extern Exception GetExceptionForHRInternal(int errorCode, IntPtr errorInfo);
1376 
1380  [MethodImpl(MethodImplOptions.InternalCall)]
1381  [SecurityCritical]
1382  public static extern int GetHRForException(Exception e);
1383 
1384  [MethodImpl(MethodImplOptions.InternalCall)]
1385  [SecurityCritical]
1386  internal static extern int GetHRForException_WinRT(Exception e);
1387 
1393  [MethodImpl(MethodImplOptions.InternalCall)]
1394  [SecurityCritical]
1395  [Obsolete("The GetUnmanagedThunkForManagedMethodPtr method has been deprecated and will be removed in a future release.", false)]
1396  public static extern IntPtr GetUnmanagedThunkForManagedMethodPtr(IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature);
1397 
1403  [MethodImpl(MethodImplOptions.InternalCall)]
1404  [SecurityCritical]
1405  [Obsolete("The GetManagedThunkForUnmanagedMethodPtr method has been deprecated and will be removed in a future release.", false)]
1406  public static extern IntPtr GetManagedThunkForUnmanagedMethodPtr(IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature);
1407 
1412  [SecurityCritical]
1413  [Obsolete("The GetThreadFromFiberCookie method has been deprecated. Use the hosting API to perform this operation.", false)]
1414  public static Thread GetThreadFromFiberCookie(int cookie)
1415  {
1416  if (cookie == 0)
1417  {
1418  throw new ArgumentException(Environment.GetResourceString("Argument_ArgumentZero"), "cookie");
1419  }
1420  return InternalGetThreadFromFiberCookie(cookie);
1421  }
1422 
1423  [MethodImpl(MethodImplOptions.InternalCall)]
1424  private static extern Thread InternalGetThreadFromFiberCookie(int cookie);
1425 
1430  [SecurityCritical]
1431  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
1432  public static IntPtr AllocHGlobal(IntPtr cb)
1433  {
1434  UIntPtr sizetdwBytes = new UIntPtr((ulong)cb.ToInt64());
1435  IntPtr intPtr = Win32Native.LocalAlloc_NoSafeHandle(0, sizetdwBytes);
1436  if (intPtr == IntPtr.Zero)
1437  {
1438  throw new OutOfMemoryException();
1439  }
1440  return intPtr;
1441  }
1442 
1447  [SecurityCritical]
1448  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
1449  public static IntPtr AllocHGlobal(int cb)
1450  {
1451  return AllocHGlobal((IntPtr)cb);
1452  }
1453 
1456  [SecurityCritical]
1457  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
1458  public static void FreeHGlobal(IntPtr hglobal)
1459  {
1460  if (IsNotWin32Atom(hglobal) && IntPtr.Zero != Win32Native.LocalFree(hglobal))
1461  {
1463  }
1464  }
1465 
1471  [SecurityCritical]
1472  public static IntPtr ReAllocHGlobal(IntPtr pv, IntPtr cb)
1473  {
1474  IntPtr intPtr = Win32Native.LocalReAlloc(pv, cb, 2);
1475  if (intPtr == IntPtr.Zero)
1476  {
1477  throw new OutOfMemoryException();
1478  }
1479  return intPtr;
1480  }
1481 
1487  [SecurityCritical]
1488  public unsafe static IntPtr StringToHGlobalAnsi(string s)
1489  {
1490  if (s == null)
1491  {
1492  return IntPtr.Zero;
1493  }
1494  int num = (s.Length + 1) * SystemMaxDBCSCharSize;
1495  if (num < s.Length)
1496  {
1497  throw new ArgumentOutOfRangeException("s");
1498  }
1499  UIntPtr sizetdwBytes = new UIntPtr((uint)num);
1500  IntPtr intPtr = Win32Native.LocalAlloc_NoSafeHandle(0, sizetdwBytes);
1501  if (intPtr == IntPtr.Zero)
1502  {
1503  throw new OutOfMemoryException();
1504  }
1505  s.ConvertToAnsi((byte*)(void*)intPtr, num, fBestFit: false, fThrowOnUnmappableChar: false);
1506  return intPtr;
1507  }
1508 
1514  [SecurityCritical]
1515  public unsafe static IntPtr StringToHGlobalUni(string s)
1516  {
1517  if (s == null)
1518  {
1519  return IntPtr.Zero;
1520  }
1521  int num = (s.Length + 1) * 2;
1522  if (num < s.Length)
1523  {
1524  throw new ArgumentOutOfRangeException("s");
1525  }
1526  UIntPtr sizetdwBytes = new UIntPtr((uint)num);
1527  IntPtr intPtr = Win32Native.LocalAlloc_NoSafeHandle(0, sizetdwBytes);
1528  if (intPtr == IntPtr.Zero)
1529  {
1530  throw new OutOfMemoryException();
1531  }
1532  fixed (char* smem = s)
1533  {
1534  string.wstrcpy((char*)(void*)intPtr, smem, s.Length + 1);
1535  }
1536  return intPtr;
1537  }
1538 
1543  [SecurityCritical]
1544  public static IntPtr StringToHGlobalAuto(string s)
1545  {
1546  return StringToHGlobalUni(s);
1547  }
1548 
1552  [SecurityCritical]
1553  [Obsolete("Use System.Runtime.InteropServices.Marshal.GetTypeLibName(ITypeLib pTLB) instead. http://go.microsoft.com/fwlink/?linkid=14202&ID=0000011.", false)]
1554  public static string GetTypeLibName(UCOMITypeLib pTLB)
1555  {
1556  return GetTypeLibName((ITypeLib)pTLB);
1557  }
1558 
1563  [SecurityCritical]
1564  public static string GetTypeLibName(ITypeLib typelib)
1565  {
1566  if (typelib == null)
1567  {
1568  throw new ArgumentNullException("typelib");
1569  }
1570  string strName = null;
1571  string strDocString = null;
1572  int dwHelpContext = 0;
1573  string strHelpFile = null;
1574  typelib.GetDocumentation(-1, out strName, out strDocString, out dwHelpContext, out strHelpFile);
1575  return strName;
1576  }
1577 
1578  [SecurityCritical]
1579  internal static string GetTypeLibNameInternal(ITypeLib typelib)
1580  {
1581  if (typelib == null)
1582  {
1583  throw new ArgumentNullException("typelib");
1584  }
1585  ITypeLib2 typeLib = typelib as ITypeLib2;
1586  if (typeLib != null)
1587  {
1588  Guid guid = ManagedNameGuid;
1589  object pVarVal;
1590  try
1591  {
1592  typeLib.GetCustData(ref guid, out pVarVal);
1593  }
1594  catch (Exception)
1595  {
1596  pVarVal = null;
1597  }
1598  if (pVarVal != null && pVarVal.GetType() == typeof(string))
1599  {
1600  string text = (string)pVarVal;
1601  text = text.Trim();
1602  if (text.EndsWith(".DLL", StringComparison.OrdinalIgnoreCase))
1603  {
1604  text = text.Substring(0, text.Length - 4);
1605  }
1606  else if (text.EndsWith(".EXE", StringComparison.OrdinalIgnoreCase))
1607  {
1608  text = text.Substring(0, text.Length - 4);
1609  }
1610  return text;
1611  }
1612  }
1613  return GetTypeLibName(typelib);
1614  }
1615 
1619  [SecurityCritical]
1620  [Obsolete("Use System.Runtime.InteropServices.Marshal.GetTypeLibGuid(ITypeLib pTLB) instead. http://go.microsoft.com/fwlink/?linkid=14202&ID=0000011.", false)]
1621  public static Guid GetTypeLibGuid(UCOMITypeLib pTLB)
1622  {
1623  return GetTypeLibGuid((ITypeLib)pTLB);
1624  }
1625 
1629  [SecurityCritical]
1630  public static Guid GetTypeLibGuid(ITypeLib typelib)
1631  {
1632  Guid result = default(Guid);
1633  FCallGetTypeLibGuid(ref result, typelib);
1634  return result;
1635  }
1636 
1637  [MethodImpl(MethodImplOptions.InternalCall)]
1638  private static extern void FCallGetTypeLibGuid(ref Guid result, ITypeLib pTLB);
1639 
1643  [SecurityCritical]
1644  [Obsolete("Use System.Runtime.InteropServices.Marshal.GetTypeLibLcid(ITypeLib pTLB) instead. http://go.microsoft.com/fwlink/?linkid=14202&ID=0000011.", false)]
1645  public static int GetTypeLibLcid(UCOMITypeLib pTLB)
1646  {
1647  return GetTypeLibLcid((ITypeLib)pTLB);
1648  }
1649 
1653  [MethodImpl(MethodImplOptions.InternalCall)]
1654  [SecurityCritical]
1655  public static extern int GetTypeLibLcid(ITypeLib typelib);
1656 
1657  [MethodImpl(MethodImplOptions.InternalCall)]
1658  internal static extern void GetTypeLibVersion(ITypeLib typeLibrary, out int major, out int minor);
1659 
1660  [SecurityCritical]
1661  internal static Guid GetTypeInfoGuid(ITypeInfo typeInfo)
1662  {
1663  Guid result = default(Guid);
1664  FCallGetTypeInfoGuid(ref result, typeInfo);
1665  return result;
1666  }
1667 
1668  [MethodImpl(MethodImplOptions.InternalCall)]
1669  private static extern void FCallGetTypeInfoGuid(ref Guid result, ITypeInfo typeInfo);
1670 
1676  [SecurityCritical]
1678  {
1679  if (asm == null)
1680  {
1681  throw new ArgumentNullException("asm");
1682  }
1683  RuntimeAssembly runtimeAssembly = asm as RuntimeAssembly;
1684  if (runtimeAssembly == null)
1685  {
1686  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeAssembly"), "asm");
1687  }
1688  Guid result = default(Guid);
1689  FCallGetTypeLibGuidForAssembly(ref result, runtimeAssembly);
1690  return result;
1691  }
1692 
1693  [MethodImpl(MethodImplOptions.InternalCall)]
1694  private static extern void FCallGetTypeLibGuidForAssembly(ref Guid result, RuntimeAssembly asm);
1695 
1696  [MethodImpl(MethodImplOptions.InternalCall)]
1697  private static extern void _GetTypeLibVersionForAssembly(RuntimeAssembly inputAssembly, out int majorVersion, out int minorVersion);
1698 
1705  [SecurityCritical]
1706  public static void GetTypeLibVersionForAssembly(Assembly inputAssembly, out int majorVersion, out int minorVersion)
1707  {
1708  if (inputAssembly == null)
1709  {
1710  throw new ArgumentNullException("inputAssembly");
1711  }
1712  RuntimeAssembly runtimeAssembly = inputAssembly as RuntimeAssembly;
1713  if (runtimeAssembly == null)
1714  {
1715  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeAssembly"), "inputAssembly");
1716  }
1717  _GetTypeLibVersionForAssembly(runtimeAssembly, out majorVersion, out minorVersion);
1718  }
1719 
1723  [SecurityCritical]
1724  [Obsolete("Use System.Runtime.InteropServices.Marshal.GetTypeInfoName(ITypeInfo pTLB) instead. http://go.microsoft.com/fwlink/?linkid=14202&ID=0000011.", false)]
1725  public static string GetTypeInfoName(UCOMITypeInfo pTI)
1726  {
1727  return GetTypeInfoName((ITypeInfo)pTI);
1728  }
1729 
1734  [SecurityCritical]
1735  public static string GetTypeInfoName(ITypeInfo typeInfo)
1736  {
1737  if (typeInfo == null)
1738  {
1739  throw new ArgumentNullException("typeInfo");
1740  }
1741  string strName = null;
1742  string strDocString = null;
1743  int dwHelpContext = 0;
1744  string strHelpFile = null;
1745  typeInfo.GetDocumentation(-1, out strName, out strDocString, out dwHelpContext, out strHelpFile);
1746  return strName;
1747  }
1748 
1749  [SecurityCritical]
1750  internal static string GetTypeInfoNameInternal(ITypeInfo typeInfo, out bool hasManagedName)
1751  {
1752  if (typeInfo == null)
1753  {
1754  throw new ArgumentNullException("typeInfo");
1755  }
1756  ITypeInfo2 typeInfo2 = typeInfo as ITypeInfo2;
1757  if (typeInfo2 != null)
1758  {
1759  Guid guid = ManagedNameGuid;
1760  object pVarVal;
1761  try
1762  {
1763  typeInfo2.GetCustData(ref guid, out pVarVal);
1764  }
1765  catch (Exception)
1766  {
1767  pVarVal = null;
1768  }
1769  if (pVarVal != null && pVarVal.GetType() == typeof(string))
1770  {
1771  hasManagedName = true;
1772  return (string)pVarVal;
1773  }
1774  }
1775  hasManagedName = false;
1776  return GetTypeInfoName(typeInfo);
1777  }
1778 
1779  [SecurityCritical]
1780  internal static string GetManagedTypeInfoNameInternal(ITypeLib typeLib, ITypeInfo typeInfo)
1781  {
1782  bool hasManagedName;
1783  string typeInfoNameInternal = GetTypeInfoNameInternal(typeInfo, out hasManagedName);
1784  if (hasManagedName)
1785  {
1786  return typeInfoNameInternal;
1787  }
1788  return GetTypeLibNameInternal(typeLib) + "." + typeInfoNameInternal;
1789  }
1790 
1791  [MethodImpl(MethodImplOptions.InternalCall)]
1792  private static extern Type GetLoadedTypeForGUID(ref Guid guid);
1793 
1797  [SecurityCritical]
1798  public static Type GetTypeForITypeInfo(IntPtr piTypeInfo)
1799  {
1800  ITypeInfo typeInfo = null;
1801  ITypeLib ppTLB = null;
1802  Type type = null;
1803  Assembly assembly = null;
1804  TypeLibConverter typeLibConverter = null;
1805  int pIndex = 0;
1806  if (piTypeInfo == IntPtr.Zero)
1807  {
1808  return null;
1809  }
1810  typeInfo = (ITypeInfo)GetObjectForIUnknown(piTypeInfo);
1811  Guid guid = GetTypeInfoGuid(typeInfo);
1812  type = GetLoadedTypeForGUID(ref guid);
1813  if (type != null)
1814  {
1815  return type;
1816  }
1817  try
1818  {
1819  typeInfo.GetContainingTypeLib(out ppTLB, out pIndex);
1820  }
1821  catch (COMException)
1822  {
1823  ppTLB = null;
1824  }
1825  if (ppTLB != null)
1826  {
1827  AssemblyName assemblyNameFromTypelib = TypeLibConverter.GetAssemblyNameFromTypelib(ppTLB, null, null, null, null, AssemblyNameFlags.None);
1828  string fullName = assemblyNameFromTypelib.FullName;
1829  Assembly[] assemblies = Thread.GetDomain().GetAssemblies();
1830  int num = assemblies.Length;
1831  for (int i = 0; i < num; i++)
1832  {
1833  if (string.Compare(assemblies[i].FullName, fullName, StringComparison.Ordinal) == 0)
1834  {
1835  assembly = assemblies[i];
1836  }
1837  }
1838  if (assembly == null)
1839  {
1840  typeLibConverter = new TypeLibConverter();
1841  assembly = typeLibConverter.ConvertTypeLibToAssembly(ppTLB, GetTypeLibName(ppTLB) + ".dll", TypeLibImporterFlags.None, new ImporterCallback(), null, null, null, null);
1842  }
1843  type = assembly.GetType(GetManagedTypeInfoNameInternal(ppTLB, typeInfo), throwOnError: true, ignoreCase: false);
1844  if (type != null && !type.IsVisible)
1845  {
1846  type = null;
1847  }
1848  }
1849  else
1850  {
1851  type = typeof(object);
1852  }
1853  return type;
1854  }
1855 
1860  [SecuritySafeCritical]
1861  public static Type GetTypeFromCLSID(Guid clsid)
1862  {
1863  return RuntimeType.GetTypeFromCLSIDImpl(clsid, null, throwOnError: false);
1864  }
1865 
1873  [MethodImpl(MethodImplOptions.InternalCall)]
1874  [SecurityCritical]
1875  public static extern IntPtr GetITypeInfoForType(Type t);
1876 
1880  [SecurityCritical]
1881  [__DynamicallyInvokable]
1882  public static IntPtr GetIUnknownForObject(object o)
1883  {
1884  return GetIUnknownForObjectNative(o, onlyInContext: false);
1885  }
1886 
1890  [SecurityCritical]
1891  public static IntPtr GetIUnknownForObjectInContext(object o)
1892  {
1893  return GetIUnknownForObjectNative(o, onlyInContext: true);
1894  }
1895 
1896  [MethodImpl(MethodImplOptions.InternalCall)]
1897  private static extern IntPtr GetIUnknownForObjectNative(object o, bool onlyInContext);
1898 
1899  [MethodImpl(MethodImplOptions.InternalCall)]
1900  internal static extern IntPtr GetRawIUnknownForComObjectNoAddRef(object o);
1901 
1907  [SecurityCritical]
1908  [__DynamicallyInvokable]
1909  public static IntPtr GetIDispatchForObject(object o)
1910  {
1911  return GetIDispatchForObjectNative(o, onlyInContext: false);
1912  }
1913 
1921  [SecurityCritical]
1922  public static IntPtr GetIDispatchForObjectInContext(object o)
1923  {
1924  return GetIDispatchForObjectNative(o, onlyInContext: true);
1925  }
1926 
1927  [MethodImpl(MethodImplOptions.InternalCall)]
1928  private static extern IntPtr GetIDispatchForObjectNative(object o, bool onlyInContext);
1929 
1937  [SecurityCritical]
1938  public static IntPtr GetComInterfaceForObject(object o, Type T)
1939  {
1940  return GetComInterfaceForObjectNative(o, T, onlyInContext: false, fEnalbeCustomizedQueryInterface: true);
1941  }
1942 
1951  [SecurityCritical]
1953  {
1954  return GetComInterfaceForObject(o, typeof(TInterface));
1955  }
1956 
1965  [SecurityCritical]
1967  {
1968  bool fEnalbeCustomizedQueryInterface = (mode == CustomQueryInterfaceMode.Allow) ? true : false;
1969  return GetComInterfaceForObjectNative(o, T, onlyInContext: false, fEnalbeCustomizedQueryInterface);
1970  }
1971 
1983  [SecurityCritical]
1984  public static IntPtr GetComInterfaceForObjectInContext(object o, Type t)
1985  {
1986  return GetComInterfaceForObjectNative(o, t, onlyInContext: true, fEnalbeCustomizedQueryInterface: true);
1987  }
1988 
1989  [MethodImpl(MethodImplOptions.InternalCall)]
1990  private static extern IntPtr GetComInterfaceForObjectNative(object o, Type t, bool onlyInContext, bool fEnalbeCustomizedQueryInterface);
1991 
1995  [MethodImpl(MethodImplOptions.InternalCall)]
1996  [SecurityCritical]
1997  [__DynamicallyInvokable]
1998  public static extern object GetObjectForIUnknown(IntPtr pUnk);
1999 
2003  [MethodImpl(MethodImplOptions.InternalCall)]
2004  [SecurityCritical]
2005  public static extern object GetUniqueObjectForIUnknown(IntPtr unknown);
2006 
2014  [MethodImpl(MethodImplOptions.InternalCall)]
2015  [SecurityCritical]
2016  public static extern object GetTypedObjectForIUnknown(IntPtr pUnk, Type t);
2017 
2024  [MethodImpl(MethodImplOptions.InternalCall)]
2025  [SecurityCritical]
2026  public static extern IntPtr CreateAggregatedObject(IntPtr pOuter, object o);
2027 
2035  [SecurityCritical]
2036  public static IntPtr CreateAggregatedObject<T>(IntPtr pOuter, T o)
2037  {
2038  return CreateAggregatedObject(pOuter, (object)o);
2039  }
2040 
2042  [MethodImpl(MethodImplOptions.InternalCall)]
2043  [SecurityCritical]
2044  public static extern void CleanupUnusedObjectsInCurrentContext();
2045 
2049  [MethodImpl(MethodImplOptions.InternalCall)]
2050  [SecurityCritical]
2051  public static extern bool AreComObjectsAvailableForCleanup();
2052 
2059  [MethodImpl(MethodImplOptions.InternalCall)]
2060  [SecuritySafeCritical]
2061  public static extern bool IsComObject(object o);
2062 
2067  [SecurityCritical]
2068  public static IntPtr AllocCoTaskMem(int cb)
2069  {
2070  IntPtr intPtr = Win32Native.CoTaskMemAlloc(new UIntPtr((uint)cb));
2071  if (intPtr == IntPtr.Zero)
2072  {
2073  throw new OutOfMemoryException();
2074  }
2075  return intPtr;
2076  }
2077 
2083  [SecurityCritical]
2084  public unsafe static IntPtr StringToCoTaskMemUni(string s)
2085  {
2086  if (s == null)
2087  {
2088  return IntPtr.Zero;
2089  }
2090  int num = (s.Length + 1) * 2;
2091  if (num < s.Length)
2092  {
2093  throw new ArgumentOutOfRangeException("s");
2094  }
2095  IntPtr intPtr = Win32Native.CoTaskMemAlloc(new UIntPtr((uint)num));
2096  if (intPtr == IntPtr.Zero)
2097  {
2098  throw new OutOfMemoryException();
2099  }
2100  fixed (char* smem = s)
2101  {
2102  string.wstrcpy((char*)(void*)intPtr, smem, s.Length + 1);
2103  }
2104  return intPtr;
2105  }
2106 
2112  [SecurityCritical]
2113  public static IntPtr StringToCoTaskMemAuto(string s)
2114  {
2115  return StringToCoTaskMemUni(s);
2116  }
2117 
2123  [SecurityCritical]
2124  public unsafe static IntPtr StringToCoTaskMemAnsi(string s)
2125  {
2126  if (s == null)
2127  {
2128  return IntPtr.Zero;
2129  }
2130  int num = (s.Length + 1) * SystemMaxDBCSCharSize;
2131  if (num < s.Length)
2132  {
2133  throw new ArgumentOutOfRangeException("s");
2134  }
2135  IntPtr intPtr = Win32Native.CoTaskMemAlloc(new UIntPtr((uint)num));
2136  if (intPtr == IntPtr.Zero)
2137  {
2138  throw new OutOfMemoryException();
2139  }
2140  s.ConvertToAnsi((byte*)(void*)intPtr, num, fBestFit: false, fThrowOnUnmappableChar: false);
2141  return intPtr;
2142  }
2143 
2146  [SecurityCritical]
2147  public static void FreeCoTaskMem(IntPtr ptr)
2148  {
2149  if (IsNotWin32Atom(ptr))
2150  {
2151  Win32Native.CoTaskMemFree(ptr);
2152  }
2153  }
2154 
2160  [SecurityCritical]
2161  public static IntPtr ReAllocCoTaskMem(IntPtr pv, int cb)
2162  {
2163  IntPtr intPtr = Win32Native.CoTaskMemRealloc(pv, new UIntPtr((uint)cb));
2164  if (intPtr == IntPtr.Zero && cb != 0)
2165  {
2166  throw new OutOfMemoryException();
2167  }
2168  return intPtr;
2169  }
2170 
2178  [SecurityCritical]
2179  public static int ReleaseComObject(object o)
2180  {
2181  __ComObject _ComObject = null;
2182  try
2183  {
2184  _ComObject = (__ComObject)o;
2185  }
2186  catch (InvalidCastException)
2187  {
2188  throw new ArgumentException(Environment.GetResourceString("Argument_ObjNotComObject"), "o");
2189  }
2190  return _ComObject.ReleaseSelf();
2191  }
2192 
2193  [MethodImpl(MethodImplOptions.InternalCall)]
2194  internal static extern int InternalReleaseComObject(object o);
2195 
2203  [SecurityCritical]
2204  public static int FinalReleaseComObject(object o)
2205  {
2206  if (o == null)
2207  {
2208  throw new ArgumentNullException("o");
2209  }
2210  __ComObject _ComObject = null;
2211  try
2212  {
2213  _ComObject = (__ComObject)o;
2214  }
2215  catch (InvalidCastException)
2216  {
2217  throw new ArgumentException(Environment.GetResourceString("Argument_ObjNotComObject"), "o");
2218  }
2219  _ComObject.FinalReleaseSelf();
2220  return 0;
2221  }
2222 
2223  [MethodImpl(MethodImplOptions.InternalCall)]
2224  internal static extern void InternalFinalReleaseComObject(object o);
2225 
2236  [SecurityCritical]
2237  public static object GetComObjectData(object obj, object key)
2238  {
2239  if (obj == null)
2240  {
2241  throw new ArgumentNullException("obj");
2242  }
2243  if (key == null)
2244  {
2245  throw new ArgumentNullException("key");
2246  }
2247  __ComObject _ComObject = null;
2248  try
2249  {
2250  _ComObject = (__ComObject)obj;
2251  }
2252  catch (InvalidCastException)
2253  {
2254  throw new ArgumentException(Environment.GetResourceString("Argument_ObjNotComObject"), "obj");
2255  }
2256  if (obj.GetType().IsWindowsRuntimeObject)
2257  {
2258  throw new ArgumentException(Environment.GetResourceString("Argument_ObjIsWinRTObject"), "obj");
2259  }
2260  return _ComObject.GetData(key);
2261  }
2262 
2275  [SecurityCritical]
2276  public static bool SetComObjectData(object obj, object key, object data)
2277  {
2278  if (obj == null)
2279  {
2280  throw new ArgumentNullException("obj");
2281  }
2282  if (key == null)
2283  {
2284  throw new ArgumentNullException("key");
2285  }
2286  __ComObject _ComObject = null;
2287  try
2288  {
2289  _ComObject = (__ComObject)obj;
2290  }
2291  catch (InvalidCastException)
2292  {
2293  throw new ArgumentException(Environment.GetResourceString("Argument_ObjNotComObject"), "obj");
2294  }
2295  if (obj.GetType().IsWindowsRuntimeObject)
2296  {
2297  throw new ArgumentException(Environment.GetResourceString("Argument_ObjIsWinRTObject"), "obj");
2298  }
2299  return _ComObject.SetData(key, data);
2300  }
2301 
2312  [SecurityCritical]
2313  public static object CreateWrapperOfType(object o, Type t)
2314  {
2315  if (t == null)
2316  {
2317  throw new ArgumentNullException("t");
2318  }
2319  if (!t.IsCOMObject)
2320  {
2321  throw new ArgumentException(Environment.GetResourceString("Argument_TypeNotComObject"), "t");
2322  }
2323  if (t.IsGenericType)
2324  {
2325  throw new ArgumentException(Environment.GetResourceString("Argument_NeedNonGenericType"), "t");
2326  }
2327  if (t.IsWindowsRuntimeObject)
2328  {
2329  throw new ArgumentException(Environment.GetResourceString("Argument_TypeIsWinRTType"), "t");
2330  }
2331  if (o == null)
2332  {
2333  return null;
2334  }
2335  if (!o.GetType().IsCOMObject)
2336  {
2337  throw new ArgumentException(Environment.GetResourceString("Argument_ObjNotComObject"), "o");
2338  }
2339  if (o.GetType().IsWindowsRuntimeObject)
2340  {
2341  throw new ArgumentException(Environment.GetResourceString("Argument_ObjIsWinRTObject"), "o");
2342  }
2343  if (o.GetType() == t)
2344  {
2345  return o;
2346  }
2347  object obj = GetComObjectData(o, t);
2348  if (obj == null)
2349  {
2350  obj = InternalCreateWrapperOfType(o, t);
2351  if (!SetComObjectData(o, t, obj))
2352  {
2353  obj = GetComObjectData(o, t);
2354  }
2355  }
2356  return obj;
2357  }
2358 
2369  [SecurityCritical]
2370  public static TWrapper CreateWrapperOfType<T, TWrapper>(T o)
2371  {
2372  return (TWrapper)CreateWrapperOfType(o, typeof(TWrapper));
2373  }
2374 
2375  [MethodImpl(MethodImplOptions.InternalCall)]
2376  [SecurityCritical]
2377  private static extern object InternalCreateWrapperOfType(object o, Type t);
2378 
2380  [SecurityCritical]
2381  [Obsolete("This API did not perform any operation and will be removed in future versions of the CLR.", false)]
2382  public static void ReleaseThreadCache()
2383  {
2384  }
2385 
2390  [MethodImpl(MethodImplOptions.InternalCall)]
2391  [SecuritySafeCritical]
2392  public static extern bool IsTypeVisibleFromCom(Type t);
2393 
2399  [MethodImpl(MethodImplOptions.InternalCall)]
2400  [SecurityCritical]
2401  public static extern int QueryInterface(IntPtr pUnk, ref Guid iid, out IntPtr ppv);
2402 
2406  [MethodImpl(MethodImplOptions.InternalCall)]
2407  [SecurityCritical]
2408  public static extern int AddRef(IntPtr pUnk);
2409 
2413  [MethodImpl(MethodImplOptions.InternalCall)]
2414  [SecurityCritical]
2415  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
2416  public static extern int Release(IntPtr pUnk);
2417 
2420  [SecurityCritical]
2421  [__DynamicallyInvokable]
2422  public static void FreeBSTR(IntPtr ptr)
2423  {
2424  if (IsNotWin32Atom(ptr))
2425  {
2426  Win32Native.SysFreeString(ptr);
2427  }
2428  }
2429 
2435  [SecurityCritical]
2436  [__DynamicallyInvokable]
2437  public static IntPtr StringToBSTR(string s)
2438  {
2439  if (s == null)
2440  {
2441  return IntPtr.Zero;
2442  }
2443  if (s.Length + 1 < s.Length)
2444  {
2445  throw new ArgumentOutOfRangeException("s");
2446  }
2447  IntPtr intPtr = Win32Native.SysAllocStringLen(s, s.Length);
2448  if (intPtr == IntPtr.Zero)
2449  {
2450  throw new OutOfMemoryException();
2451  }
2452  return intPtr;
2453  }
2454 
2460  [SecurityCritical]
2461  [__DynamicallyInvokable]
2462  public static string PtrToStringBSTR(IntPtr ptr)
2463  {
2464  return PtrToStringUni(ptr, (int)Win32Native.SysStringLen(ptr));
2465  }
2466 
2471  [MethodImpl(MethodImplOptions.InternalCall)]
2472  [SecurityCritical]
2473  public static extern void GetNativeVariantForObject(object obj, IntPtr pDstNativeVariant);
2474 
2479  [SecurityCritical]
2480  public static void GetNativeVariantForObject<T>(T obj, IntPtr pDstNativeVariant)
2481  {
2482  GetNativeVariantForObject((object)obj, pDstNativeVariant);
2483  }
2484 
2492  [MethodImpl(MethodImplOptions.InternalCall)]
2493  [SecurityCritical]
2494  public static extern object GetObjectForNativeVariant(IntPtr pSrcNativeVariant);
2495 
2504  [SecurityCritical]
2505  public static T GetObjectForNativeVariant<T>(IntPtr pSrcNativeVariant)
2506  {
2507  return (T)GetObjectForNativeVariant(pSrcNativeVariant);
2508  }
2509 
2516  [MethodImpl(MethodImplOptions.InternalCall)]
2517  [SecurityCritical]
2518  public static extern object[] GetObjectsForNativeVariants(IntPtr aSrcNativeVariant, int cVars);
2519 
2527  [SecurityCritical]
2528  public static T[] GetObjectsForNativeVariants<T>(IntPtr aSrcNativeVariant, int cVars)
2529  {
2530  object[] objectsForNativeVariants = GetObjectsForNativeVariants(aSrcNativeVariant, cVars);
2531  T[] array = null;
2532  if (objectsForNativeVariants != null)
2533  {
2534  array = new T[objectsForNativeVariants.Length];
2535  Array.Copy(objectsForNativeVariants, array, objectsForNativeVariants.Length);
2536  }
2537  return array;
2538  }
2539 
2545  [MethodImpl(MethodImplOptions.InternalCall)]
2546  [SecurityCritical]
2547  public static extern int GetStartComSlot(Type t);
2548 
2552  [MethodImpl(MethodImplOptions.InternalCall)]
2553  [SecurityCritical]
2554  public static extern int GetEndComSlot(Type t);
2555 
2563  [MethodImpl(MethodImplOptions.InternalCall)]
2564  [SecurityCritical]
2565  public static extern MemberInfo GetMethodInfoForComSlot(Type t, int slot, ref ComMemberType memberType);
2566 
2572  [SecurityCritical]
2574  {
2575  if (m == null)
2576  {
2577  throw new ArgumentNullException("m");
2578  }
2579  if (!(m is RuntimeMethodInfo))
2580  {
2581  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), "m");
2582  }
2583  if (!m.DeclaringType.IsInterface)
2584  {
2585  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeInterfaceMethod"), "m");
2586  }
2588  {
2589  throw new ArgumentException(Environment.GetResourceString("Argument_NeedNonGenericType"), "m");
2590  }
2591  return InternalGetComSlotForMethodInfo((IRuntimeMethodInfo)m);
2592  }
2593 
2594  [MethodImpl(MethodImplOptions.InternalCall)]
2595  private static extern int InternalGetComSlotForMethodInfo(IRuntimeMethodInfo m);
2596 
2600  [SecurityCritical]
2601  public static Guid GenerateGuidForType(Type type)
2602  {
2603  Guid result = default(Guid);
2604  FCallGenerateGuidForType(ref result, type);
2605  return result;
2606  }
2607 
2608  [MethodImpl(MethodImplOptions.InternalCall)]
2609  private static extern void FCallGenerateGuidForType(ref Guid result, Type type);
2610 
2616  [SecurityCritical]
2617  public static string GenerateProgIdForType(Type type)
2618  {
2619  if (type == null)
2620  {
2621  throw new ArgumentNullException("type");
2622  }
2623  if (type.IsImport)
2624  {
2625  throw new ArgumentException(Environment.GetResourceString("Argument_TypeMustNotBeComImport"), "type");
2626  }
2627  if (type.IsGenericType)
2628  {
2629  throw new ArgumentException(Environment.GetResourceString("Argument_NeedNonGenericType"), "type");
2630  }
2631  if (!RegistrationServices.TypeRequiresRegistrationHelper(type))
2632  {
2633  throw new ArgumentException(Environment.GetResourceString("Argument_TypeMustBeComCreatable"), "type");
2634  }
2636  for (int i = 0; i < customAttributes.Count; i++)
2637  {
2638  if (customAttributes[i].Constructor.DeclaringType == typeof(ProgIdAttribute))
2639  {
2640  IList<CustomAttributeTypedArgument> constructorArguments = customAttributes[i].ConstructorArguments;
2641  string text = (string)constructorArguments[0].Value;
2642  if (text == null)
2643  {
2644  text = string.Empty;
2645  }
2646  return text;
2647  }
2648  }
2649  return type.FullName;
2650  }
2651 
2656  [SecurityCritical]
2657  public static object BindToMoniker(string monikerName)
2658  {
2659  object ppvResult = null;
2660  IBindCtx ppbc = null;
2661  CreateBindCtx(0u, out ppbc);
2662  IMoniker ppmk = null;
2663  MkParseDisplayName(ppbc, monikerName, out uint _, out ppmk);
2664  BindMoniker(ppmk, 0u, ref IID_IUnknown, out ppvResult);
2665  return ppvResult;
2666  }
2667 
2672  [SecurityCritical]
2673  public static object GetActiveObject(string progID)
2674  {
2675  object ppunk = null;
2676  Guid clsid;
2677  try
2678  {
2679  CLSIDFromProgIDEx(progID, out clsid);
2680  }
2681  catch (Exception)
2682  {
2683  CLSIDFromProgID(progID, out clsid);
2684  }
2685  GetActiveObject(ref clsid, IntPtr.Zero, out ppunk);
2686  return ppunk;
2687  }
2688 
2689  [DllImport("ole32.dll", PreserveSig = false)]
2690  [SuppressUnmanagedCodeSecurity]
2691  [SecurityCritical]
2692  private static extern void CLSIDFromProgIDEx([MarshalAs(UnmanagedType.LPWStr)] string progId, out Guid clsid);
2693 
2694  [DllImport("ole32.dll", PreserveSig = false)]
2695  [SuppressUnmanagedCodeSecurity]
2696  [SecurityCritical]
2697  private static extern void CLSIDFromProgID([MarshalAs(UnmanagedType.LPWStr)] string progId, out Guid clsid);
2698 
2699  [DllImport("ole32.dll", PreserveSig = false)]
2700  [SuppressUnmanagedCodeSecurity]
2701  [SecurityCritical]
2702  private static extern void CreateBindCtx(uint reserved, out IBindCtx ppbc);
2703 
2704  [DllImport("ole32.dll", PreserveSig = false)]
2705  [SuppressUnmanagedCodeSecurity]
2706  [SecurityCritical]
2707  private static extern void MkParseDisplayName(IBindCtx pbc, [MarshalAs(UnmanagedType.LPWStr)] string szUserName, out uint pchEaten, out IMoniker ppmk);
2708 
2709  [DllImport("ole32.dll", PreserveSig = false)]
2710  [SuppressUnmanagedCodeSecurity]
2711  [SecurityCritical]
2712  private static extern void BindMoniker(IMoniker pmk, uint grfOpt, ref Guid iidResult, [MarshalAs(UnmanagedType.Interface)] out object ppvResult);
2713 
2714  [DllImport("oleaut32.dll", PreserveSig = false)]
2715  [SuppressUnmanagedCodeSecurity]
2716  [SecurityCritical]
2717  private static extern void GetActiveObject(ref Guid rclsid, IntPtr reserved, [MarshalAs(UnmanagedType.Interface)] out object ppunk);
2718 
2719  [MethodImpl(MethodImplOptions.InternalCall)]
2720  internal static extern bool InternalSwitchCCW(object oldtp, object newtp);
2721 
2722  [MethodImpl(MethodImplOptions.InternalCall)]
2723  internal static extern object InternalWrapIUnknownWithComObject(IntPtr i);
2724 
2725  [SecurityCritical]
2726  private static IntPtr LoadLicenseManager()
2727  {
2728  Assembly assembly = Assembly.Load("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
2729  Type type = assembly.GetType("System.ComponentModel.LicenseManager");
2730  if (type == null || !type.IsVisible)
2731  {
2732  return IntPtr.Zero;
2733  }
2734  return type.TypeHandle.Value;
2735  }
2736 
2741  [MethodImpl(MethodImplOptions.InternalCall)]
2742  [SecurityCritical]
2743  public static extern void ChangeWrapperHandleStrength(object otp, bool fIsWeak);
2744 
2745  [MethodImpl(MethodImplOptions.InternalCall)]
2746  [SecurityCritical]
2747  internal static extern void InitializeWrapperForWinRT(object o, ref IntPtr pUnk);
2748 
2749  [MethodImpl(MethodImplOptions.InternalCall)]
2750  [SecurityCritical]
2751  internal static extern void InitializeManagedWinRTFactoryObject(object o, RuntimeType runtimeClassType);
2752 
2753  [MethodImpl(MethodImplOptions.InternalCall)]
2754  [SecurityCritical]
2755  internal static extern object GetNativeActivationFactory(Type type);
2756 
2757  [DllImport("QCall", CharSet = CharSet.Unicode)]
2758  [SecurityCritical]
2759  [SuppressUnmanagedCodeSecurity]
2760  private static extern void _GetInspectableIids(ObjectHandleOnStack obj, ObjectHandleOnStack guids);
2761 
2762  [SecurityCritical]
2763  internal static Guid[] GetInspectableIids(object obj)
2764  {
2765  Guid[] o = null;
2766  __ComObject o2 = obj as __ComObject;
2767  if (o2 != null)
2768  {
2769  _GetInspectableIids(JitHelpers.GetObjectHandleOnStack(ref o2), JitHelpers.GetObjectHandleOnStack(ref o));
2770  }
2771  return o;
2772  }
2773 
2774  [DllImport("QCall", CharSet = CharSet.Unicode)]
2775  [SecurityCritical]
2776  [SuppressUnmanagedCodeSecurity]
2777  private static extern void _GetCachedWinRTTypeByIid(ObjectHandleOnStack appDomainObj, Guid iid, out IntPtr rthHandle);
2778 
2779  [SecurityCritical]
2780  internal static Type GetCachedWinRTTypeByIid(AppDomain ad, Guid iid)
2781  {
2782  _GetCachedWinRTTypeByIid(JitHelpers.GetObjectHandleOnStack(ref ad), iid, out IntPtr rthHandle);
2783  return Type.GetTypeFromHandleUnsafe(rthHandle);
2784  }
2785 
2786  [DllImport("QCall", CharSet = CharSet.Unicode)]
2787  [SecurityCritical]
2788  [SuppressUnmanagedCodeSecurity]
2789  private static extern void _GetCachedWinRTTypes(ObjectHandleOnStack appDomainObj, ref int epoch, ObjectHandleOnStack winrtTypes);
2790 
2791  [SecurityCritical]
2792  internal static Type[] GetCachedWinRTTypes(AppDomain ad, ref int epoch)
2793  {
2794  IntPtr[] o = null;
2795  _GetCachedWinRTTypes(JitHelpers.GetObjectHandleOnStack(ref ad), ref epoch, JitHelpers.GetObjectHandleOnStack(ref o));
2796  Type[] array = new Type[o.Length];
2797  for (int i = 0; i < o.Length; i++)
2798  {
2799  array[i] = Type.GetTypeFromHandleUnsafe(o[i]);
2800  }
2801  return array;
2802  }
2803 
2804  [SecurityCritical]
2805  internal static Type[] GetCachedWinRTTypes(AppDomain ad)
2806  {
2807  int epoch = 0;
2808  return GetCachedWinRTTypes(ad, ref epoch);
2809  }
2810 
2817  [SecurityCritical]
2819  {
2820  if (ptr == IntPtr.Zero)
2821  {
2822  throw new ArgumentNullException("ptr");
2823  }
2824  if (t == null)
2825  {
2826  throw new ArgumentNullException("t");
2827  }
2828  if (t as RuntimeType == null)
2829  {
2830  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "t");
2831  }
2832  if (t.IsGenericType)
2833  {
2834  throw new ArgumentException(Environment.GetResourceString("Argument_NeedNonGenericType"), "t");
2835  }
2836  Type baseType = t.BaseType;
2837  if (baseType == null || (baseType != typeof(Delegate) && baseType != typeof(MulticastDelegate)))
2838  {
2839  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), "t");
2840  }
2841  return GetDelegateForFunctionPointerInternal(ptr, t);
2842  }
2843 
2850  [SecurityCritical]
2852  {
2853  return (TDelegate)(object)GetDelegateForFunctionPointer(ptr, typeof(TDelegate));
2854  }
2855 
2856  [MethodImpl(MethodImplOptions.InternalCall)]
2857  internal static extern Delegate GetDelegateForFunctionPointerInternal(IntPtr ptr, Type t);
2858 
2864  [SecurityCritical]
2866  {
2867  if ((object)d == null)
2868  {
2869  throw new ArgumentNullException("d");
2870  }
2871  return GetFunctionPointerForDelegateInternal(d);
2872  }
2873 
2879  [SecurityCritical]
2881  {
2882  return GetFunctionPointerForDelegate((Delegate)(object)d);
2883  }
2884 
2885  [MethodImpl(MethodImplOptions.InternalCall)]
2886  internal static extern IntPtr GetFunctionPointerForDelegateInternal(Delegate d);
2887 
2894  [SecurityCritical]
2896  {
2897  if (s == null)
2898  {
2899  throw new ArgumentNullException("s");
2900  }
2901  return s.ToBSTR();
2902  }
2903 
2910  [SecurityCritical]
2912  {
2913  if (s == null)
2914  {
2915  throw new ArgumentNullException("s");
2916  }
2917  return s.ToAnsiStr(allocateFromHeap: false);
2918  }
2919 
2926  [SecurityCritical]
2928  {
2929  if (s == null)
2930  {
2931  throw new ArgumentNullException("s");
2932  }
2933  return s.ToUniStr(allocateFromHeap: false);
2934  }
2935 
2938  [SecurityCritical]
2939  public static void ZeroFreeBSTR(IntPtr s)
2940  {
2941  Win32Native.ZeroMemory(s, (UIntPtr)(Win32Native.SysStringLen(s) * 2));
2942  FreeBSTR(s);
2943  }
2944 
2947  [SecurityCritical]
2948  public static void ZeroFreeCoTaskMemAnsi(IntPtr s)
2949  {
2950  Win32Native.ZeroMemory(s, (UIntPtr)(ulong)Win32Native.lstrlenA(s));
2951  FreeCoTaskMem(s);
2952  }
2953 
2956  [SecurityCritical]
2957  public static void ZeroFreeCoTaskMemUnicode(IntPtr s)
2958  {
2959  Win32Native.ZeroMemory(s, (UIntPtr)(ulong)(Win32Native.lstrlenW(s) * 2));
2960  FreeCoTaskMem(s);
2961  }
2962 
2969  [SecurityCritical]
2971  {
2972  if (s == null)
2973  {
2974  throw new ArgumentNullException("s");
2975  }
2976  return s.ToAnsiStr(allocateFromHeap: true);
2977  }
2978 
2985  [SecurityCritical]
2987  {
2988  if (s == null)
2989  {
2990  throw new ArgumentNullException("s");
2991  }
2992  return s.ToUniStr(allocateFromHeap: true);
2993  }
2994 
2997  [SecurityCritical]
2998  public static void ZeroFreeGlobalAllocAnsi(IntPtr s)
2999  {
3000  Win32Native.ZeroMemory(s, (UIntPtr)(ulong)Win32Native.lstrlenA(s));
3001  FreeHGlobal(s);
3002  }
3003 
3006  [SecurityCritical]
3007  public static void ZeroFreeGlobalAllocUnicode(IntPtr s)
3008  {
3009  Win32Native.ZeroMemory(s, (UIntPtr)(ulong)(Win32Native.lstrlenW(s) * 2));
3010  FreeHGlobal(s);
3011  }
3012  }
3013 }
static readonly int SystemMaxDBCSCharSize
Represents the maximum size of a double byte character set (DBCS) size, in bytes, for the current ope...
Definition: Marshal.cs:29
Obtains information about the attributes of a member and provides access to member metadata.
Definition: MemberInfo.cs:14
abstract Type BaseType
Gets the type from which the current T:System.Type directly inherits.
Definition: Type.cs:180
A platform-specific type that is used to represent a pointer or a handle.
Definition: UIntPtr.cs:14
Performs reflection on a module.
Definition: Module.cs:17
Provides the managed definition of the Component Automation ITypeInfo interface.
Definition: ITypeInfo.cs:8
static void PrelinkAll(Type c)
Performs a pre-link check for all methods on a class.
Definition: Marshal.cs:1110
abstract FieldInfo GetField(string name, BindingFlags bindingAttr)
Searches for the specified field, using the specified binding constraints.
static unsafe void WriteInt64(IntPtr ptr, int ofs, long val)
Writes a 64-bit signed integer value to unmanaged memory at a specified offset.
Definition: Marshal.cs:1004
static unsafe void WriteByte(IntPtr ptr, int ofs, byte val)
Writes a single byte value to unmanaged memory at a specified offset.
Definition: Marshal.cs:778
static long ReadInt64(IntPtr ptr)
Reads a 64-bit signed integer from unmanaged memory.
Definition: Marshal.cs:767
bool IsVisible
Gets a value indicating whether the T:System.Type can be accessed by code outside the assembly.
Definition: Type.cs:240
static int ReadInt32([In] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
Reads a 32-bit signed integer at a given offset from unmanaged memory.
static bool IsComObject(object o)
Indicates whether a specified object represents a COM object.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static void ReleaseThreadCache()
Releases the thread cache.
Definition: Marshal.cs:2382
The exception that is thrown when there is an attempt to dereference a null object reference.
static IntPtr GetHINSTANCE(Module m)
Returns the instance handle (HINSTANCE) for the specified module.
Definition: Marshal.cs:1292
static IntPtr ReAllocHGlobal(IntPtr pv, IntPtr cb)
Resizes a block of memory previously allocated with M:System.Runtime.InteropServices....
Definition: Marshal.cs:1472
static T [] GetObjectsForNativeVariants< T >(IntPtr aSrcNativeVariant, int cVars)
[Supported in the .NET Framework 4.5.1 and later versions] Converts an array of COM VARIANTs to an ar...
Definition: Marshal.cs:2528
static string GetTypeInfoName(UCOMITypeInfo pTI)
Retrieves the name of the type represented by an ITypeInfo object.
Definition: Marshal.cs:1725
static void WriteIntPtr([In] [Out] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, IntPtr val)
Writes a processor native sized integer value to unmanaged memory.
Definition: Marshal.cs:980
virtual RuntimeTypeHandle TypeHandle
Gets the handle for the current T:System.Type.
Definition: Type.cs:141
static unsafe IntPtr StringToHGlobalUni(string s)
Copies the contents of a managed T:System.String into unmanaged memory.
Definition: Marshal.cs:1515
static int ReleaseComObject(object o)
Decrements the reference count of the Runtime Callable Wrapper (RCW) associated with the specified CO...
Definition: Marshal.cs:2179
static bool AreComObjectsAvailableForCleanup()
Indicates whether runtime callable wrappers (RCWs) from any context are available for cleanup.
abstract Type DeclaringType
Gets the class that declares this member.
Definition: MemberInfo.cs:36
static void FreeHGlobal(IntPtr hglobal)
Frees memory previously allocated from the unmanaged memory of the process.
Definition: Marshal.cs:1458
static void FreeCoTaskMem(IntPtr ptr)
Frees a block of memory allocated by the unmanaged COM task memory allocator.
Definition: Marshal.cs:2147
void GetCustData(ref Guid guid, out object pVarVal)
Gets the custom data.
static int GetHRForException(Exception e)
Converts the specified exception to an HRESULT.
static int SizeOf< T >()
[Supported in the .NET Framework 4.5.1 and later versions] Returns the size of an unmanaged type in b...
Definition: Marshal.cs:203
static Assembly Load(string assemblyString)
Loads an assembly given the long form of its name.
Definition: Assembly.cs:507
Discovers the attributes of a method and provides access to method metadata.
Definition: MethodInfo.cs:13
The exception that is thrown when there is an attempt to read or write protected memory.
static IntPtr OffsetOf(Type t, string fieldName)
Returns the field offset of the unmanaged form of the managed class.
Definition: Marshal.cs:241
static string GetTypeLibName(UCOMITypeLib pTLB)
Retrieves the name of a type library.
Definition: Marshal.cs:1554
abstract string FullName
Gets the fully qualified name of the type, including its namespace but not its assembly.
Definition: Type.cs:153
CustomQueryInterfaceMode
Indicates whether the M:System.Runtime.InteropServices.Marshal.GetComInterfaceForObject(System....
static Exception GetExceptionForHR(int errorCode, IntPtr errorInfo)
Converts the specified HRESULT error code to a corresponding T:System.Exception object,...
Definition: Marshal.cs:1365
static IntPtr SecureStringToCoTaskMemUnicode(SecureString s)
Copies the contents of a managed T:System.Security.SecureString object to a block of memory allocated...
Definition: Marshal.cs:2927
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
static void ChangeWrapperHandleStrength(object otp, bool fIsWeak)
Changes the strength of an object's COM Callable Wrapper (CCW) handle.
static Guid GetTypeLibGuid(UCOMITypeLib pTLB)
Retrieves the library identifier (LIBID) of a type library.
Definition: Marshal.cs:1621
static void GetNativeVariantForObject< T >(T obj, IntPtr pDstNativeVariant)
[Supported in the .NET Framework 4.5.1 and later versions] Converts an object of a specified type to ...
Definition: Marshal.cs:2480
static void WriteByte(IntPtr ptr, byte val)
Writes a single byte value to unmanaged memory.
Definition: Marshal.cs:811
static IntPtr SecureStringToGlobalAllocUnicode(SecureString s)
Copies the contents of a managed T:System.Security.SecureString object into unmanaged memory.
Definition: Marshal.cs:2986
static void Copy(IntPtr source, long[] destination, int startIndex, int length)
Copies data from an unmanaged memory pointer to a managed 64-bit signed integer array.
Definition: Marshal.cs:459
bool IsImport
Gets a value indicating whether the T:System.Type has a T:System.Runtime.InteropServices....
Definition: Type.cs:508
static IntPtr GetComInterfaceForObject(object o, Type T, CustomQueryInterfaceMode mode)
Returns a pointer to an IUnknown interface that represents the specified interface on the specified o...
Definition: Marshal.cs:1966
static int ReadInt32(IntPtr ptr)
Reads a 32-bit signed integer from unmanaged memory.
Definition: Marshal.cs:667
static IntPtr GetFunctionPointerForDelegate< TDelegate >(TDelegate d)
[Supported in the .NET Framework 4.5.1 and later versions] Converts a delegate of a specified type to...
Definition: Marshal.cs:2880
bool IsInterface
Gets a value indicating whether the T:System.Type is an interface; that is, not a class or a value ty...
Definition: Type.cs:426
static void WriteInt16(IntPtr ptr, char val)
Writes a character as a 16-bit integer value to unmanaged memory.
Definition: Marshal.cs:901
static object GetActiveObject(string progID)
Obtains a running instance of the specified object from the running object table (ROT).
Definition: Marshal.cs:2673
static int QueryInterface(IntPtr pUnk, ref Guid iid, out IntPtr ppv)
Requests a pointer to a specified interface from a COM object.
Represents a multicast delegate; that is, a delegate that can have more than one element in its invoc...
Discovers the attributes of a field and provides access to field metadata.
Definition: FieldInfo.cs:15
static unsafe string PtrToStringUni(IntPtr ptr)
Allocates a managed T:System.String and copies all characters up to the first null character from an ...
Definition: Marshal.cs:132
static int NumParamBytes(MethodInfo m)
Calculates the number of bytes in unmanaged memory that are required to hold the parameters for the s...
Definition: Marshal.cs:1132
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
Definition: BindingFlags.cs:10
Definition: __Canon.cs:3
static Type GetTypeFromCLSID(Guid clsid)
Returns the type associated with the specified class identifier (CLSID).
Definition: Marshal.cs:1861
static void Copy(IntPtr[] source, int startIndex, IntPtr destination, int length)
Copies data from a one-dimensional, managed T:System.IntPtr array to an unmanaged memory pointer.
Definition: Marshal.cs:404
static int Release(IntPtr pUnk)
Decrements the reference count on the specified interface.
The exception that is thrown when the value of an argument is outside the allowable range of values a...
Provides access to custom attribute data for assemblies, modules, types, members and parameters that ...
The exception that is thrown for invalid casting or explicit conversion.
unsafe long ToInt64()
Converts the value of this instance to a 64-bit signed integer.
Definition: IntPtr.cs:147
static int SizeOf(object structure)
Returns the unmanaged size of an object in bytes.
Definition: Marshal.cs:159
static IntPtr CreateAggregatedObject(IntPtr pOuter, object o)
Aggregates a managed object with the specified COM object.
static string GetTypeLibName(ITypeLib typelib)
Retrieves the name of a type library.
Definition: Marshal.cs:1564
static IntPtr GetFunctionPointerForDelegate(Delegate d)
Converts a delegate into a function pointer that is callable from unmanaged code.
Definition: Marshal.cs:2865
static IntPtr ReadIntPtr(IntPtr ptr)
Reads a processor native-sized integer from unmanaged memory.
Definition: Marshal.cs:707
abstract Type UnderlyingSystemType
Indicates the type provided by the common language runtime that represents this type.
Definition: Type.cs:816
static string GenerateProgIdForType(Type type)
Returns a programmatic identifier (ProgID) for the specified type.
Definition: Marshal.cs:2617
AssemblyNameFlags
Provides information about an T:System.Reflection.Assembly reference.
static void CleanupUnusedObjectsInCurrentContext()
Notifies the runtime to clean up all Runtime Callable Wrappers (RCWs) allocated in the current contex...
static int GetHRForLastWin32Error()
Returns the HRESULT corresponding to the last error incurred by Win32 code executed using T:System....
Definition: Marshal.cs:1072
virtual bool IsGenericType
Gets a value indicating whether the current type is a generic type.
Definition: Type.cs:566
static IList< CustomAttributeData > GetCustomAttributes(MemberInfo target)
Returns a list of T:System.Reflection.CustomAttributeData objects representing data about the attribu...
static void ZeroFreeCoTaskMemAnsi(IntPtr s)
Frees an unmanaged string pointer that was allocated using the M:System.Runtime.InteropServices....
Definition: Marshal.cs:2948
AssemblyBuilder ConvertTypeLibToAssembly([MarshalAs(UnmanagedType.Interface)] object typeLib, string asmFileName, int flags, ITypeLibImporterNotifySink notifySink, byte[] publicKey, StrongNameKeyPair keyPair, bool unsafeInterfaces)
Converts a COM type library to an assembly.
static IntPtr GetExceptionPointers()
Retrieves a computer-independent description of an exception, and information about the state that ex...
static void WriteInt16(IntPtr ptr, short val)
Writes a 16-bit integer value to unmanaged memory.
Definition: Marshal.cs:864
static void Copy(IntPtr source, char[] destination, int startIndex, int length)
Copies data from an unmanaged memory pointer to a managed character array.
Definition: Marshal.cs:433
static void ZeroFreeGlobalAllocAnsi(IntPtr s)
Frees an unmanaged string pointer that was allocated using the M:System.Runtime.InteropServices....
Definition: Marshal.cs:2998
The exception that is thrown when an unrecognized HRESULT is returned from a COM method call.
Definition: COMException.cs:12
static Type GetTypeForITypeInfo(IntPtr piTypeInfo)
Converts an unmanaged ITypeInfo object into a managed T:System.Type object.
Definition: Marshal.cs:1798
static int AddRef(IntPtr pUnk)
Increments the reference count on the specified interface.
static byte ReadByte([In] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
Reads a single byte at a given offset (or index) from unmanaged memory.
static object PtrToStructure(IntPtr ptr, Type structureType)
Marshals data from an unmanaged block of memory to a newly allocated managed object of the specified ...
Definition: Marshal.cs:1226
static void ZeroFreeBSTR(IntPtr s)
Frees a BSTR pointer that was allocated using the M:System.Runtime.InteropServices....
Definition: Marshal.cs:2939
static IntPtr GetComInterfaceForObjectInContext(object o, Type t)
Returns an interface pointer that represents the specified interface for an object,...
Definition: Marshal.cs:1984
IntPtr Value
Gets a handle to the type represented by this instance.
Use T:System.Runtime.InteropServices.ComTypes.ITypeInfo instead.
Definition: UCOMITypeInfo.cs:8
static IntPtr ReadIntPtr(IntPtr ptr, int ofs)
Reads a processor native sized integer at a given offset from unmanaged memory.
Definition: Marshal.cs:693
static unsafe int ReadInt32(IntPtr ptr, int ofs)
Reads a 32-bit signed integer at a given offset from unmanaged memory.
Definition: Marshal.cs:635
static IntPtr GetITypeInfoForType(Type t)
Returns a T:System.Runtime.InteropServices.ComTypes.ITypeInfo interface from a managed type.
static void Copy(IntPtr source, byte[] destination, int startIndex, int length)
Copies data from an unmanaged memory pointer to a managed 8-bit unsigned integer array.
Definition: Marshal.cs:498
static long ReadInt64([In] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
Reads a 64-bit signed integer at a given offset from unmanaged memory.
static string GetTypeInfoName(ITypeInfo typeInfo)
Retrieves the name of the type represented by an ITypeInfo object.
Definition: Marshal.cs:1735
static unsafe void WriteInt16(IntPtr ptr, int ofs, short val)
Writes a 16-bit signed integer value into unmanaged memory at a specified offset.
Definition: Marshal.cs:822
static bool SetComObjectData(object obj, object key, object data)
Sets data referenced by the specified key in the specified COM object.
Definition: Marshal.cs:2276
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
static IntPtr StringToBSTR(string s)
Allocates a BSTR and copies the contents of a managed T:System.String into it.
Definition: Marshal.cs:2437
static void WriteInt64(IntPtr ptr, long val)
Writes a 64-bit signed integer value to unmanaged memory.
Definition: Marshal.cs:1052
static IntPtr SecureStringToCoTaskMemAnsi(SecureString s)
Copies the contents of a managed T:System.Security.SecureString object to a block of memory allocated...
Definition: Marshal.cs:2911
static object GetObjectForNativeVariant(IntPtr pSrcNativeVariant)
Converts a COM VARIANT to an object.
static MemberInfo GetMethodInfoForComSlot(Type t, int slot, ref ComMemberType memberType)
Retrieves a T:System.Reflection.MemberInfo object for the specified virtual function table (v-table o...
static void Copy(IntPtr source, double[] destination, int startIndex, int length)
Copies data from an unmanaged memory pointer to a managed double-precision floating-point number arra...
Definition: Marshal.cs:485
static void ThrowExceptionForHR(int errorCode, IntPtr errorInfo)
Throws an exception with a specific failure HRESULT, based on the specified IErrorInfo interface.
Definition: Marshal.cs:1336
Attribute can be applied to a constructor.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static IntPtr SecureStringToGlobalAllocAnsi(SecureString s)
Copies the contents of a managed T:System.Security.SecureString into unmanaged memory,...
Definition: Marshal.cs:2970
Represents a globally unique identifier (GUID).To browse the .NET Framework source code for this type...
Definition: Guid.cs:14
void GetDocumentation(int index, out string strName, out string strDocString, out int dwHelpContext, out string strHelpFile)
Retrieves the documentation string, the complete Help file name and path, and the context ID for the ...
static Guid GetTypeLibGuidForAssembly(Assembly asm)
Retrieves the library identifier (LIBID) that is assigned to a type library when it was exported from...
Definition: Marshal.cs:1677
static IntPtr AllocCoTaskMem(int cb)
Allocates a block of memory of specified size from the COM task memory allocator.
Definition: Marshal.cs:2068
MethodInfo [] GetMethods()
Returns all the public methods of the current T:System.Type.
Definition: Type.cs:1660
static unsafe void WriteInt32(IntPtr ptr, int ofs, int val)
Writes a 32-bit signed integer value into unmanaged memory at a specified offset.
Definition: Marshal.cs:912
Provides a set of services for registering and unregistering managed assemblies for use from COM.
static void Copy(int[] source, int startIndex, IntPtr destination, int length)
Copies data from a one-dimensional, managed 32-bit signed integer array to an unmanaged memory pointe...
Definition: Marshal.cs:301
static object CreateWrapperOfType(object o, Type t)
Wraps the specified COM object in an object of the specified type.
Definition: Marshal.cs:2313
static bool IsTypeVisibleFromCom(Type t)
Indicates whether a type is visible to COM clients.
static int SizeOf(Type t)
Returns the size of an unmanaged type in bytes.
Definition: Marshal.cs:183
static T GetObjectForNativeVariant< T >(IntPtr pSrcNativeVariant)
[Supported in the .NET Framework 4.5.1 and later versions] Converts a COM VARIANT to an object of a s...
Definition: Marshal.cs:2505
static object GetComObjectData(object obj, object key)
Retrieves data that is referenced by the specified key from the specified COM object.
Definition: Marshal.cs:2237
static int GetEndComSlot(Type t)
Retrieves the last slot in the virtual function table (v-table or VTBL) of a type when exposed to COM...
static int GetExceptionCode()
Retrieves a code that identifies the type of the exception that occurred.
static IntPtr StringToCoTaskMemAuto(string s)
Copies the contents of a managed T:System.String to a block of memory allocated from the unmanaged CO...
Definition: Marshal.cs:2113
UnmanagedType
Identifies how to marshal parameters or fields to unmanaged code.
Definition: UnmanagedType.cs:7
static object [] GetObjectsForNativeVariants(IntPtr aSrcNativeVariant, int cVars)
Converts an array of COM VARIANTs to an array of objects.
static void ZeroFreeGlobalAllocUnicode(IntPtr s)
Frees an unmanaged string pointer that was allocated using the M:System.Runtime.InteropServices....
Definition: Marshal.cs:3007
static unsafe IntPtr StringToCoTaskMemAnsi(string s)
Copies the contents of a managed T:System.String to a block of memory allocated from the unmanaged CO...
Definition: Marshal.cs:2124
static IntPtr AllocHGlobal(int cb)
Allocates memory from the unmanaged memory of the process by using the specified number of bytes.
Definition: Marshal.cs:1449
static int GetComSlotForMethodInfo(MemberInfo m)
Retrieves the virtual function table (v-table or VTBL) slot for a specified T:System....
Definition: Marshal.cs:2573
static void WriteInt32(IntPtr ptr, int val)
Writes a 32-bit signed integer value to unmanaged memory.
Definition: Marshal.cs:956
Defines and represents a module in a dynamic assembly.
static void StructureToPtr(object structure, IntPtr ptr, bool fDeleteOld)
Marshals data from a managed object to an unmanaged block of memory.
static Delegate GetDelegateForFunctionPointer(IntPtr ptr, Type t)
Converts an unmanaged function pointer to a delegate.
Definition: Marshal.cs:2818
static void ThrowExceptionForHR(int errorCode)
Throws an exception with a specific failure HRESULT value.
Definition: Marshal.cs:1323
static void Copy(IntPtr source, IntPtr[] destination, int startIndex, int length)
Copies data from an unmanaged memory pointer to a managed T:System.IntPtr array.
Definition: Marshal.cs:511
static Guid GenerateGuidForType(Type type)
Returns the globally unique identifier (GUID) for the specified type, or generates a GUID using the a...
Definition: Marshal.cs:2601
static IntPtr GetManagedThunkForUnmanagedMethodPtr(IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
Gets a pointer to a runtime-generated function that marshals a call from managed to unmanaged code.
static IntPtr GetIDispatchForObjectInContext(object o)
Returns an IDispatch interface pointer from a managed object, if the caller is in the same context as...
Definition: Marshal.cs:1922
static IntPtr ReAllocCoTaskMem(IntPtr pv, int cb)
Resizes a block of memory previously allocated with M:System.Runtime.InteropServices....
Definition: Marshal.cs:2161
Represents text that should be kept confidential, such as by deleting it from computer memory when no...
Definition: SecureString.cs:11
static TDelegate GetDelegateForFunctionPointer< TDelegate >(IntPtr ptr)
[Supported in the .NET Framework 4.5.1 and later versions] Converts an unmanaged function pointer to ...
Definition: Marshal.cs:2851
static object GetTypedObjectForIUnknown(IntPtr pUnk, Type t)
Returns a managed object of a specified type that represents a COM object.
Represents an assembly, which is a reusable, versionable, and self-describing building block of a com...
Definition: Assembly.cs:22
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
static string PtrToStringAuto(IntPtr ptr, int len)
Allocates a managed T:System.String and copies the specified number of characters from a string store...
Definition: Marshal.cs:123
static void Copy(float[] source, int startIndex, IntPtr destination, int length)
Copies data from a one-dimensional, managed single-precision floating-point number array to an unmana...
Definition: Marshal.cs:361
void GetCustData(ref Guid guid, out object pVarVal)
Gets the custom data.
static void DestroyStructure(IntPtr ptr, Type structuretype)
Frees all substructures that the specified unmanaged memory block points to.
static Guid GetTypeLibGuid(ITypeLib typelib)
Retrieves the library identifier (LIBID) of a type library.
Definition: Marshal.cs:1630
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Definition: Delegate.cs:15
The exception that is thrown when there is not enough memory to continue the execution of a program.
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
static IntPtr GetComInterfaceForObject< T, TInterface >(T o)
[Supported in the .NET Framework 4.5.1 and later versions] Returns a pointer to an IUnknown interface...
Definition: Marshal.cs:1952
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
static int FinalReleaseComObject(object o)
Releases all references to a Runtime Callable Wrapper (RCW) by setting its reference count to 0.
Definition: Marshal.cs:2204
Assembly [] GetAssemblies()
Gets the assemblies that have been loaded into the execution context of this application domain.
Definition: AppDomain.cs:2325
static unsafe long ReadInt64(IntPtr ptr, int ofs)
Reads a 64-bit signed integer at a given offset from unmanaged memory.
Definition: Marshal.cs:731
static void Copy(IntPtr source, float[] destination, int startIndex, int length)
Copies data from an unmanaged memory pointer to a managed single-precision floating-point number arra...
Definition: Marshal.cs:472
bool IsCOMObject
Gets a value indicating whether the T:System.Type is a COM object.
Definition: Type.cs:701
Provides the managed definition of the IBindCtx interface.
Definition: IBindCtx.cs:8
MethodImplOptions
Defines the details of how a method is implemented.
static IntPtr SecureStringToBSTR(SecureString s)
Allocates an unmanaged binary string (BSTR) and copies the contents of a managed T:System....
Definition: Marshal.cs:2895
static AppDomain GetDomain()
Returns the current domain in which the current thread is running.
Definition: Thread.cs:1096
static byte ReadByte(IntPtr ptr)
Reads a single byte from unmanaged memory.
Definition: Marshal.cs:558
static void WriteInt16([In] [Out] object ptr, int ofs, char val)
Writes a 16-bit signed integer value to unmanaged memory at a specified offset.
Definition: Marshal.cs:888
Provides the managed definition of the IMoniker interface, with COM functionality from IPersist and I...
Definition: IMoniker.cs:8
CharSet
Dictates which character set marshaled strings should use.
Definition: CharSet.cs:7
static void FreeBSTR(IntPtr ptr)
Frees a BSTR using the COM SysFreeString function.
Definition: Marshal.cs:2422
static void Prelink(MethodInfo m)
Executes one-time method setup tasks without calling the method.
Definition: Marshal.cs:1087
static unsafe IntPtr StringToCoTaskMemUni(string s)
Copies the contents of a managed T:System.String to a block of memory allocated from the unmanaged CO...
Definition: Marshal.cs:2084
static void Copy(IntPtr source, int[] destination, int startIndex, int length)
Copies data from an unmanaged memory pointer to a managed 32-bit signed integer array.
Definition: Marshal.cs:420
Describes an assembly's unique identity in full.
Definition: AssemblyName.cs:19
static void Copy(long[] source, int startIndex, IntPtr destination, int length)
Copies data from a one-dimensional, managed 64-bit signed integer array to an unmanaged memory pointe...
Definition: Marshal.cs:346
static int GetTypeLibLcid(UCOMITypeLib pTLB)
Retrieves the LCID of a type library.
Definition: Marshal.cs:1645
Represents a collection of objects that can be individually accessed by index.
Definition: IList.cs:9
static string PtrToStringAuto(IntPtr ptr)
Allocates a managed T:System.String and copies all characters up to the first null character from a s...
Definition: Marshal.cs:149
The exception that is thrown when one of the arguments provided to a method is not valid.
static void Copy(short[] source, int startIndex, IntPtr destination, int length)
Copies data from a one-dimensional, managed 16-bit signed integer array to an unmanaged memory pointe...
Definition: Marshal.cs:331
static readonly int SystemDefaultCharSize
Represents the default character size on the system; the default is 2 for Unicode systems and 1 for A...
Definition: Marshal.cs:26
static int Size
Gets the size of this instance.
Definition: IntPtr.cs:27
static void StructureToPtr< T >(T structure, IntPtr ptr, bool fDeleteOld)
[Supported in the .NET Framework 4.5.1 and later versions] Marshals data from a managed object of a s...
Definition: Marshal.cs:1187
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
int Count
Gets the number of elements contained in the T:System.Collections.Generic.ICollection`1.
Definition: ICollection.cs:15
void GetDocumentation(int index, out string strName, out string strDocString, out int dwHelpContext, out string strHelpFile)
Retrieves the library's documentation string, the complete Help file name and path,...
static unsafe string PtrToStringUni(IntPtr ptr, int len)
Allocates a managed T:System.String and copies a specified number of characters from an unmanaged Uni...
Definition: Marshal.cs:103
static Exception GetExceptionForHR(int errorCode)
Converts the specified HRESULT error code to a corresponding T:System.Exception object.
Definition: Marshal.cs:1351
static unsafe IntPtr StringToHGlobalAnsi(string s)
Copies the contents of a managed T:System.String into unmanaged memory, converting into ANSI format a...
Definition: Marshal.cs:1488
static void ZeroFreeCoTaskMemUnicode(IntPtr s)
Frees an unmanaged string pointer that was allocated using the M:System.Runtime.InteropServices....
Definition: Marshal.cs:2957
static void PtrToStructure(IntPtr ptr, object structure)
Marshals data from an unmanaged block of memory to a managed object.
Definition: Marshal.cs:1198
static TWrapper CreateWrapperOfType< T, TWrapper >(T o)
[Supported in the .NET Framework 4.5.1 and later versions] Wraps the specified COM object in an objec...
Definition: Marshal.cs:2370
static IntPtr CreateAggregatedObject< T >(IntPtr pOuter, T o)
[Supported in the .NET Framework 4.5.1 and later versions] Aggregates a managed object of the specifi...
Definition: Marshal.cs:2036
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
void GetContainingTypeLib(out ITypeLib ppTLB, out int pIndex)
Retrieves the type library that contains this type description and its index within that type library...
TypeLibImporterFlags
Indicates how an assembly should be produced.
ComMemberType
Describes the type of a COM member.
Definition: ComMemberType.cs:7
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
Definition: IntPtr.cs:20
static unsafe string PtrToStringAnsi(IntPtr ptr, int len)
Allocates a managed T:System.String, copies a specified number of characters from an unmanaged ANSI s...
Definition: Marshal.cs:85
static short ReadInt16([In] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
Reads a 16-bit signed integer at a given offset from unmanaged memory.
static IntPtr OffsetOf< T >(string fieldName)
[Supported in the .NET Framework 4.5.1 and later versions] Returns the field offset of the unmanaged ...
Definition: Marshal.cs:264
static void Copy(IntPtr source, short[] destination, int startIndex, int length)
Copies data from an unmanaged memory pointer to a managed 16-bit signed integer array.
Definition: Marshal.cs:446
Use T:System.Runtime.InteropServices.ComTypes.ITypeLib instead.
Definition: UCOMITypeLib.cs:8
static string PtrToStringBSTR(IntPtr ptr)
Allocates a managed T:System.String and copies a binary string (BSTR) stored in unmanaged memory into...
Definition: Marshal.cs:2462
Provides the managed definition of the ITypeInfo2 interface.
Definition: ITypeInfo2.cs:8
static void Copy(char[] source, int startIndex, IntPtr destination, int length)
Copies data from a one-dimensional, managed character array to an unmanaged memory pointer.
Definition: Marshal.cs:316
static IntPtr StringToHGlobalAuto(string s)
Copies the contents of a managed T:System.String into unmanaged memory, converting into ANSI format i...
Definition: Marshal.cs:1544
static IntPtr AllocHGlobal(IntPtr cb)
Allocates memory from the unmanaged memory of the process by using the pointer to the specified numbe...
Definition: Marshal.cs:1432
static void GetNativeVariantForObject(object obj, IntPtr pDstNativeVariant)
Converts an object to a COM VARIANT.
static IntPtr GetIDispatchForObject(object o)
Returns an IDispatch interface from a managed object.
Definition: Marshal.cs:1909
static void WriteInt16(IntPtr ptr, int ofs, char val)
Writes a 16-bit signed integer value to unmanaged memory at a specified offset.
Definition: Marshal.cs:875
Consistency
Specifies a reliability contract.
Definition: Consistency.cs:5
static IntPtr GetComInterfaceForObject(object o, Type T)
Returns a pointer to an IUnknown interface that represents the specified interface on the specified o...
Definition: Marshal.cs:1938
static IntPtr GetIUnknownForObject(object o)
Returns an IUnknown interface from a managed object.
Definition: Marshal.cs:1882
Provides a set of services that convert a managed assembly to a COM type library and vice versa.
static void Copy(byte[] source, int startIndex, IntPtr destination, int length)
Copies data from a one-dimensional, managed 8-bit unsigned integer array to an unmanaged memory point...
Definition: Marshal.cs:391
static Thread GetThreadFromFiberCookie(int cookie)
Converts a fiber cookie into the corresponding T:System.Threading.Thread instance.
Definition: Marshal.cs:1414
static void PtrToStructure< T >(IntPtr ptr, T structure)
[Supported in the .NET Framework 4.5.1 and later versions] Marshals data from an unmanaged block of m...
Definition: Marshal.cs:1209
Provides a managed definition of the ITypeLib2 interface.
Definition: ITypeLib2.cs:8
Allows the user to specify the ProgID of a class.
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
static unsafe string PtrToStringAnsi(IntPtr ptr)
Copies all characters up to the first null character from an unmanaged ANSI string to a managed T:Sys...
Definition: Marshal.cs:61
static void WriteIntPtr(IntPtr ptr, int ofs, IntPtr val)
Writes a processor native-sized integer value to unmanaged memory at a specified offset.
Definition: Marshal.cs:967
static int GetStartComSlot(Type t)
Gets the first slot in the virtual function table (v-table or VTBL) that contains user-defined method...
static IntPtr UnsafeAddrOfPinnedArrayElement< T >(T[] arr, int index)
[Supported in the .NET Framework 4.5.1 and later versions] Gets the address of the element at the spe...
Definition: Marshal.cs:286
static void DestroyStructure< T >(IntPtr ptr)
[Supported in the .NET Framework 4.5.1 and later versions] Frees all substructures of a specified typ...
Definition: Marshal.cs:1282
static void WriteIntPtr(IntPtr ptr, IntPtr val)
Writes a processor native sized integer value into unmanaged memory.
Definition: Marshal.cs:993
static void GetTypeLibVersionForAssembly(Assembly inputAssembly, out int majorVersion, out int minorVersion)
Retrieves the version number of a type library that will be exported from the specified assembly.
Definition: Marshal.cs:1706
static IntPtr UnsafeAddrOfPinnedArrayElement(Array arr, int index)
Gets the address of the element at the specified index inside the specified array.
Provides the managed definition of the ITypeLib interface.
Definition: ITypeLib.cs:8
static IntPtr GetUnmanagedThunkForManagedMethodPtr(IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
Gets a pointer to a runtime-generated function that marshals a call from unmanaged to managed code.
static unsafe short ReadInt16(IntPtr ptr, int ofs)
Reads a 16-bit signed integer at a given offset from unmanaged memory.
Definition: Marshal.cs:581
static object GetObjectForIUnknown(IntPtr pUnk)
Returns an instance of a type that represents a COM object by a pointer to its IUnknown interface.
static object BindToMoniker(string monikerName)
Gets an interface pointer identified by the specified moniker.
Definition: Marshal.cs:2657
static IntPtr GetIUnknownForObjectInContext(object o)
Returns an IUnknown interface from a managed object, if the caller is in the same context as that obj...
Definition: Marshal.cs:1891
string FullName
Gets the full name of the assembly, also known as the display name.
static short ReadInt16(IntPtr ptr)
Reads a 16-bit signed integer from unmanaged memory.
Definition: Marshal.cs:610
static object GetUniqueObjectForIUnknown(IntPtr unknown)
Creates a unique Runtime Callable Wrapper (RCW) object for a given IUnknown interface.
static void Copy(double[] source, int startIndex, IntPtr destination, int length)
Copies data from a one-dimensional, managed double-precision floating-point number array to an unmana...
Definition: Marshal.cs:376
static IntPtr ReadIntPtr([In] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
Reads a processor native sized integer from unmanaged memory.
Definition: Marshal.cs:681
static unsafe byte ReadByte(IntPtr ptr, int ofs)
Reads a single byte at a given offset (or index) from unmanaged memory.
Definition: Marshal.cs:537
Creates and controls a thread, sets its priority, and gets its status.
Definition: Thread.cs:18