mscorlib(4.0.0.0) API with additions
RegistrationServices.cs
1 using Microsoft.Win32;
2 using System.Collections;
4 using System.Reflection;
6 using System.Security;
7 
9 {
11  [Guid("475E398F-8AFA-43a7-A3BE-F4EF8D6787C9")]
12  [ClassInterface(ClassInterfaceType.None)]
13  [ComVisible(true)]
15  {
16  private const string strManagedCategoryGuid = "{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}";
17 
18  private const string strDocStringPrefix = "";
19 
20  private const string strManagedTypeThreadingModel = "Both";
21 
22  private const string strComponentCategorySubKey = "Component Categories";
23 
24  private const string strManagedCategoryDescription = ".NET Category";
25 
26  private const string strImplementedCategoriesSubKey = "Implemented Categories";
27 
28  private const string strMsCorEEFileName = "mscoree.dll";
29 
30  private const string strRecordRootName = "Record";
31 
32  private const string strClsIdRootName = "CLSID";
33 
34  private const string strTlbRootName = "TypeLib";
35 
36  private static Guid s_ManagedCategoryGuid = new Guid("{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}");
37 
47  [SecurityCritical]
48  public virtual bool RegisterAssembly(Assembly assembly, AssemblyRegistrationFlags flags)
49  {
50  if (assembly == null)
51  {
52  throw new ArgumentNullException("assembly");
53  }
54  if (assembly.ReflectionOnly)
55  {
56  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_AsmLoadedForReflectionOnly"));
57  }
58  RuntimeAssembly runtimeAssembly = assembly as RuntimeAssembly;
59  if (runtimeAssembly == null)
60  {
61  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeAssembly"));
62  }
63  string fullName = assembly.FullName;
64  if (fullName == null)
65  {
66  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NoAsmName"));
67  }
68  string text = null;
69  if ((flags & AssemblyRegistrationFlags.SetCodeBase) != 0)
70  {
71  text = runtimeAssembly.GetCodeBase(copiedName: false);
72  if (text == null)
73  {
74  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NoAsmCodeBase"));
75  }
76  }
77  Type[] registrableTypesInAssembly = GetRegistrableTypesInAssembly(assembly);
78  int num = registrableTypesInAssembly.Length;
79  string strAsmVersion = runtimeAssembly.GetVersion().ToString();
80  string imageRuntimeVersion = assembly.ImageRuntimeVersion;
81  for (int i = 0; i < num; i++)
82  {
83  if (IsRegisteredAsValueType(registrableTypesInAssembly[i]))
84  {
85  RegisterValueType(registrableTypesInAssembly[i], fullName, strAsmVersion, text, imageRuntimeVersion);
86  }
87  else if (TypeRepresentsComType(registrableTypesInAssembly[i]))
88  {
89  RegisterComImportedType(registrableTypesInAssembly[i], fullName, strAsmVersion, text, imageRuntimeVersion);
90  }
91  else
92  {
93  RegisterManagedType(registrableTypesInAssembly[i], fullName, strAsmVersion, text, imageRuntimeVersion);
94  }
95  CallUserDefinedRegistrationMethod(registrableTypesInAssembly[i], bRegister: true);
96  }
97  object[] customAttributes = assembly.GetCustomAttributes(typeof(PrimaryInteropAssemblyAttribute), inherit: false);
98  int num2 = customAttributes.Length;
99  for (int j = 0; j < num2; j++)
100  {
101  RegisterPrimaryInteropAssembly(runtimeAssembly, text, (PrimaryInteropAssemblyAttribute)customAttributes[j]);
102  }
103  if (registrableTypesInAssembly.Length != 0 || num2 > 0)
104  {
105  return true;
106  }
107  return false;
108  }
109 
118  [SecurityCritical]
119  public virtual bool UnregisterAssembly(Assembly assembly)
120  {
121  if (assembly == null)
122  {
123  throw new ArgumentNullException("assembly");
124  }
125  if (assembly.ReflectionOnly)
126  {
127  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_AsmLoadedForReflectionOnly"));
128  }
129  RuntimeAssembly runtimeAssembly = assembly as RuntimeAssembly;
130  if (runtimeAssembly == null)
131  {
132  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeAssembly"));
133  }
134  bool flag = true;
135  Type[] registrableTypesInAssembly = GetRegistrableTypesInAssembly(assembly);
136  int num = registrableTypesInAssembly.Length;
137  string strAsmVersion = runtimeAssembly.GetVersion().ToString();
138  for (int i = 0; i < num; i++)
139  {
140  CallUserDefinedRegistrationMethod(registrableTypesInAssembly[i], bRegister: false);
141  if (IsRegisteredAsValueType(registrableTypesInAssembly[i]))
142  {
143  if (!UnregisterValueType(registrableTypesInAssembly[i], strAsmVersion))
144  {
145  flag = false;
146  }
147  }
148  else if (TypeRepresentsComType(registrableTypesInAssembly[i]))
149  {
150  if (!UnregisterComImportedType(registrableTypesInAssembly[i], strAsmVersion))
151  {
152  flag = false;
153  }
154  }
155  else if (!UnregisterManagedType(registrableTypesInAssembly[i], strAsmVersion))
156  {
157  flag = false;
158  }
159  }
160  object[] customAttributes = assembly.GetCustomAttributes(typeof(PrimaryInteropAssemblyAttribute), inherit: false);
161  int num2 = customAttributes.Length;
162  if (flag)
163  {
164  for (int j = 0; j < num2; j++)
165  {
166  UnregisterPrimaryInteropAssembly(assembly, (PrimaryInteropAssemblyAttribute)customAttributes[j]);
167  }
168  }
169  if (registrableTypesInAssembly.Length != 0 || num2 > 0)
170  {
171  return true;
172  }
173  return false;
174  }
175 
180  [SecurityCritical]
181  public virtual Type[] GetRegistrableTypesInAssembly(Assembly assembly)
182  {
183  if (assembly == null)
184  {
185  throw new ArgumentNullException("assembly");
186  }
187  if (!(assembly is RuntimeAssembly))
188  {
189  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeAssembly"), "assembly");
190  }
191  Type[] exportedTypes = assembly.GetExportedTypes();
192  int num = exportedTypes.Length;
193  ArrayList arrayList = new ArrayList();
194  for (int i = 0; i < num; i++)
195  {
196  Type type = exportedTypes[i];
197  if (TypeRequiresRegistration(type))
198  {
199  arrayList.Add(type);
200  }
201  }
202  Type[] array = new Type[arrayList.Count];
203  arrayList.CopyTo(array);
204  return array;
205  }
206 
210  [SecurityCritical]
211  public virtual string GetProgIdForType(Type type)
212  {
213  return Marshal.GenerateProgIdForType(type);
214  }
215 
221  [SecurityCritical]
222  public virtual void RegisterTypeForComClients(Type type, ref Guid g)
223  {
224  if (type == null)
225  {
226  throw new ArgumentNullException("type");
227  }
228  if (type as RuntimeType == null)
229  {
230  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type");
231  }
232  if (!TypeRequiresRegistration(type))
233  {
234  throw new ArgumentException(Environment.GetResourceString("Argument_TypeMustBeComCreatable"), "type");
235  }
236  RegisterTypeForComClientsNative(type, ref g);
237  }
238 
241  public virtual Guid GetManagedCategoryGuid()
242  {
243  return s_ManagedCategoryGuid;
244  }
245 
250  [SecurityCritical]
251  public virtual bool TypeRequiresRegistration(Type type)
252  {
253  return TypeRequiresRegistrationHelper(type);
254  }
255 
260  [SecuritySafeCritical]
261  public virtual bool TypeRepresentsComType(Type type)
262  {
263  if (!type.IsCOMObject)
264  {
265  return false;
266  }
267  if (type.IsImport)
268  {
269  return true;
270  }
271  Type baseComImportType = GetBaseComImportType(type);
272  if (Marshal.GenerateGuidForType(type) == Marshal.GenerateGuidForType(baseComImportType))
273  {
274  return true;
275  }
276  return false;
277  }
278 
286  [SecurityCritical]
287  [ComVisible(false)]
289  {
290  if (type == null)
291  {
292  throw new ArgumentNullException("type");
293  }
294  if (type as RuntimeType == null)
295  {
296  throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type");
297  }
298  if (!TypeRequiresRegistration(type))
299  {
300  throw new ArgumentException(Environment.GetResourceString("Argument_TypeMustBeComCreatable"), "type");
301  }
302  return RegisterTypeForComClientsExNative(type, classContext, flags);
303  }
304 
307  [SecurityCritical]
308  [ComVisible(false)]
309  public virtual void UnregisterTypeForComClients(int cookie)
310  {
311  CoRevokeClassObject(cookie);
312  }
313 
314  [SecurityCritical]
315  internal static bool TypeRequiresRegistrationHelper(Type type)
316  {
317  if (!type.IsClass && !type.IsValueType)
318  {
319  return false;
320  }
321  if (type.IsAbstract)
322  {
323  return false;
324  }
325  if (!type.IsValueType && type.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, new Type[0], null) == null)
326  {
327  return false;
328  }
329  return Marshal.IsTypeVisibleFromCom(type);
330  }
331 
332  [SecurityCritical]
333  private void RegisterValueType(Type type, string strAsmName, string strAsmVersion, string strAsmCodeBase, string strRuntimeVersion)
334  {
335  string subkey = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
336  using (RegistryKey registryKey = Registry.ClassesRoot.CreateSubKey("Record"))
337  {
338  using (RegistryKey registryKey2 = registryKey.CreateSubKey(subkey))
339  {
340  using (RegistryKey registryKey3 = registryKey2.CreateSubKey(strAsmVersion))
341  {
342  registryKey3.SetValue("Class", type.FullName);
343  registryKey3.SetValue("Assembly", strAsmName);
344  registryKey3.SetValue("RuntimeVersion", strRuntimeVersion);
345  if (strAsmCodeBase != null)
346  {
347  registryKey3.SetValue("CodeBase", strAsmCodeBase);
348  }
349  }
350  }
351  }
352  }
353 
354  [SecurityCritical]
355  private void RegisterManagedType(Type type, string strAsmName, string strAsmVersion, string strAsmCodeBase, string strRuntimeVersion)
356  {
357  string value = type.FullName ?? "";
358  string text = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
359  string progIdForType = GetProgIdForType(type);
360  if (progIdForType != string.Empty)
361  {
362  using (RegistryKey registryKey = Registry.ClassesRoot.CreateSubKey(progIdForType))
363  {
364  registryKey.SetValue("", value);
365  using (RegistryKey registryKey2 = registryKey.CreateSubKey("CLSID"))
366  {
367  registryKey2.SetValue("", text);
368  }
369  }
370  }
371  using (RegistryKey registryKey3 = Registry.ClassesRoot.CreateSubKey("CLSID"))
372  {
373  using (RegistryKey registryKey4 = registryKey3.CreateSubKey(text))
374  {
375  registryKey4.SetValue("", value);
376  using (RegistryKey registryKey5 = registryKey4.CreateSubKey("InprocServer32"))
377  {
378  registryKey5.SetValue("", "mscoree.dll");
379  registryKey5.SetValue("ThreadingModel", "Both");
380  registryKey5.SetValue("Class", type.FullName);
381  registryKey5.SetValue("Assembly", strAsmName);
382  registryKey5.SetValue("RuntimeVersion", strRuntimeVersion);
383  if (strAsmCodeBase != null)
384  {
385  registryKey5.SetValue("CodeBase", strAsmCodeBase);
386  }
387  using (RegistryKey registryKey6 = registryKey5.CreateSubKey(strAsmVersion))
388  {
389  registryKey6.SetValue("Class", type.FullName);
390  registryKey6.SetValue("Assembly", strAsmName);
391  registryKey6.SetValue("RuntimeVersion", strRuntimeVersion);
392  if (strAsmCodeBase != null)
393  {
394  registryKey6.SetValue("CodeBase", strAsmCodeBase);
395  }
396  }
397  if (progIdForType != string.Empty)
398  {
399  using (RegistryKey registryKey7 = registryKey4.CreateSubKey("ProgId"))
400  {
401  registryKey7.SetValue("", progIdForType);
402  }
403  }
404  }
405  using (RegistryKey registryKey8 = registryKey4.CreateSubKey("Implemented Categories"))
406  {
407  using (registryKey8.CreateSubKey("{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}"))
408  {
409  }
410  }
411  }
412  }
413  EnsureManagedCategoryExists();
414  }
415 
416  [SecurityCritical]
417  private void RegisterComImportedType(Type type, string strAsmName, string strAsmVersion, string strAsmCodeBase, string strRuntimeVersion)
418  {
419  string subkey = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
420  using (RegistryKey registryKey = Registry.ClassesRoot.CreateSubKey("CLSID"))
421  {
422  using (RegistryKey registryKey2 = registryKey.CreateSubKey(subkey))
423  {
424  using (RegistryKey registryKey3 = registryKey2.CreateSubKey("InprocServer32"))
425  {
426  registryKey3.SetValue("Class", type.FullName);
427  registryKey3.SetValue("Assembly", strAsmName);
428  registryKey3.SetValue("RuntimeVersion", strRuntimeVersion);
429  if (strAsmCodeBase != null)
430  {
431  registryKey3.SetValue("CodeBase", strAsmCodeBase);
432  }
433  using (RegistryKey registryKey4 = registryKey3.CreateSubKey(strAsmVersion))
434  {
435  registryKey4.SetValue("Class", type.FullName);
436  registryKey4.SetValue("Assembly", strAsmName);
437  registryKey4.SetValue("RuntimeVersion", strRuntimeVersion);
438  if (strAsmCodeBase != null)
439  {
440  registryKey4.SetValue("CodeBase", strAsmCodeBase);
441  }
442  }
443  }
444  }
445  }
446  }
447 
448  [SecurityCritical]
449  private bool UnregisterValueType(Type type, string strAsmVersion)
450  {
451  bool result = true;
452  string text = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
453  using (RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey("Record", writable: true))
454  {
455  if (registryKey == null)
456  {
457  return result;
458  }
459  using (RegistryKey registryKey2 = registryKey.OpenSubKey(text, writable: true))
460  {
461  if (registryKey2 != null)
462  {
463  using (RegistryKey registryKey3 = registryKey2.OpenSubKey(strAsmVersion, writable: true))
464  {
465  if (registryKey3 != null)
466  {
467  registryKey3.DeleteValue("Assembly", throwOnMissingValue: false);
468  registryKey3.DeleteValue("Class", throwOnMissingValue: false);
469  registryKey3.DeleteValue("CodeBase", throwOnMissingValue: false);
470  registryKey3.DeleteValue("RuntimeVersion", throwOnMissingValue: false);
471  if (registryKey3.SubKeyCount == 0 && registryKey3.ValueCount == 0)
472  {
473  registryKey2.DeleteSubKey(strAsmVersion);
474  }
475  }
476  }
477  if (registryKey2.SubKeyCount != 0)
478  {
479  result = false;
480  }
481  if (registryKey2.SubKeyCount == 0 && registryKey2.ValueCount == 0)
482  {
483  registryKey.DeleteSubKey(text);
484  }
485  }
486  }
487  if (registryKey.SubKeyCount != 0)
488  {
489  return result;
490  }
491  if (registryKey.ValueCount != 0)
492  {
493  return result;
494  }
495  Registry.ClassesRoot.DeleteSubKey("Record");
496  return result;
497  }
498  }
499 
500  [SecurityCritical]
501  private bool UnregisterManagedType(Type type, string strAsmVersion)
502  {
503  bool flag = true;
504  string text = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
505  string progIdForType = GetProgIdForType(type);
506  using (RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey("CLSID", writable: true))
507  {
508  if (registryKey != null)
509  {
510  using (RegistryKey registryKey2 = registryKey.OpenSubKey(text, writable: true))
511  {
512  if (registryKey2 != null)
513  {
514  using (RegistryKey registryKey3 = registryKey2.OpenSubKey("InprocServer32", writable: true))
515  {
516  if (registryKey3 != null)
517  {
518  using (RegistryKey registryKey4 = registryKey3.OpenSubKey(strAsmVersion, writable: true))
519  {
520  if (registryKey4 != null)
521  {
522  registryKey4.DeleteValue("Assembly", throwOnMissingValue: false);
523  registryKey4.DeleteValue("Class", throwOnMissingValue: false);
524  registryKey4.DeleteValue("RuntimeVersion", throwOnMissingValue: false);
525  registryKey4.DeleteValue("CodeBase", throwOnMissingValue: false);
526  if (registryKey4.SubKeyCount == 0 && registryKey4.ValueCount == 0)
527  {
528  registryKey3.DeleteSubKey(strAsmVersion);
529  }
530  }
531  }
532  if (registryKey3.SubKeyCount != 0)
533  {
534  flag = false;
535  }
536  if (flag)
537  {
538  registryKey3.DeleteValue("", throwOnMissingValue: false);
539  registryKey3.DeleteValue("ThreadingModel", throwOnMissingValue: false);
540  }
541  registryKey3.DeleteValue("Assembly", throwOnMissingValue: false);
542  registryKey3.DeleteValue("Class", throwOnMissingValue: false);
543  registryKey3.DeleteValue("RuntimeVersion", throwOnMissingValue: false);
544  registryKey3.DeleteValue("CodeBase", throwOnMissingValue: false);
545  if (registryKey3.SubKeyCount == 0 && registryKey3.ValueCount == 0)
546  {
547  registryKey2.DeleteSubKey("InprocServer32");
548  }
549  }
550  }
551  if (flag)
552  {
553  registryKey2.DeleteValue("", throwOnMissingValue: false);
554  if (progIdForType != string.Empty)
555  {
556  using (RegistryKey registryKey5 = registryKey2.OpenSubKey("ProgId", writable: true))
557  {
558  if (registryKey5 != null)
559  {
560  registryKey5.DeleteValue("", throwOnMissingValue: false);
561  if (registryKey5.SubKeyCount == 0 && registryKey5.ValueCount == 0)
562  {
563  registryKey2.DeleteSubKey("ProgId");
564  }
565  }
566  }
567  }
568  using (RegistryKey registryKey6 = registryKey2.OpenSubKey("Implemented Categories", writable: true))
569  {
570  if (registryKey6 != null)
571  {
572  using (RegistryKey registryKey7 = registryKey6.OpenSubKey("{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}", writable: true))
573  {
574  if (registryKey7 != null && registryKey7.SubKeyCount == 0 && registryKey7.ValueCount == 0)
575  {
576  registryKey6.DeleteSubKey("{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}");
577  }
578  }
579  if (registryKey6.SubKeyCount == 0 && registryKey6.ValueCount == 0)
580  {
581  registryKey2.DeleteSubKey("Implemented Categories");
582  }
583  }
584  }
585  }
586  if (registryKey2.SubKeyCount == 0 && registryKey2.ValueCount == 0)
587  {
588  registryKey.DeleteSubKey(text);
589  }
590  }
591  }
592  if (registryKey.SubKeyCount == 0 && registryKey.ValueCount == 0)
593  {
594  Registry.ClassesRoot.DeleteSubKey("CLSID");
595  }
596  }
597  if (!flag)
598  {
599  return flag;
600  }
601  if (progIdForType != string.Empty)
602  {
603  using (RegistryKey registryKey8 = Registry.ClassesRoot.OpenSubKey(progIdForType, writable: true))
604  {
605  if (registryKey8 == null)
606  {
607  return flag;
608  }
609  registryKey8.DeleteValue("", throwOnMissingValue: false);
610  using (RegistryKey registryKey9 = registryKey8.OpenSubKey("CLSID", writable: true))
611  {
612  if (registryKey9 != null)
613  {
614  registryKey9.DeleteValue("", throwOnMissingValue: false);
615  if (registryKey9.SubKeyCount == 0 && registryKey9.ValueCount == 0)
616  {
617  registryKey8.DeleteSubKey("CLSID");
618  }
619  }
620  }
621  if (registryKey8.SubKeyCount != 0)
622  {
623  return flag;
624  }
625  if (registryKey8.ValueCount != 0)
626  {
627  return flag;
628  }
629  Registry.ClassesRoot.DeleteSubKey(progIdForType);
630  return flag;
631  }
632  }
633  return flag;
634  }
635  }
636 
637  [SecurityCritical]
638  private bool UnregisterComImportedType(Type type, string strAsmVersion)
639  {
640  bool result = true;
641  string text = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
642  using (RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey("CLSID", writable: true))
643  {
644  if (registryKey == null)
645  {
646  return result;
647  }
648  using (RegistryKey registryKey2 = registryKey.OpenSubKey(text, writable: true))
649  {
650  if (registryKey2 != null)
651  {
652  using (RegistryKey registryKey3 = registryKey2.OpenSubKey("InprocServer32", writable: true))
653  {
654  if (registryKey3 != null)
655  {
656  registryKey3.DeleteValue("Assembly", throwOnMissingValue: false);
657  registryKey3.DeleteValue("Class", throwOnMissingValue: false);
658  registryKey3.DeleteValue("RuntimeVersion", throwOnMissingValue: false);
659  registryKey3.DeleteValue("CodeBase", throwOnMissingValue: false);
660  using (RegistryKey registryKey4 = registryKey3.OpenSubKey(strAsmVersion, writable: true))
661  {
662  if (registryKey4 != null)
663  {
664  registryKey4.DeleteValue("Assembly", throwOnMissingValue: false);
665  registryKey4.DeleteValue("Class", throwOnMissingValue: false);
666  registryKey4.DeleteValue("RuntimeVersion", throwOnMissingValue: false);
667  registryKey4.DeleteValue("CodeBase", throwOnMissingValue: false);
668  if (registryKey4.SubKeyCount == 0 && registryKey4.ValueCount == 0)
669  {
670  registryKey3.DeleteSubKey(strAsmVersion);
671  }
672  }
673  }
674  if (registryKey3.SubKeyCount != 0)
675  {
676  result = false;
677  }
678  if (registryKey3.SubKeyCount == 0 && registryKey3.ValueCount == 0)
679  {
680  registryKey2.DeleteSubKey("InprocServer32");
681  }
682  }
683  }
684  if (registryKey2.SubKeyCount == 0 && registryKey2.ValueCount == 0)
685  {
686  registryKey.DeleteSubKey(text);
687  }
688  }
689  }
690  if (registryKey.SubKeyCount != 0)
691  {
692  return result;
693  }
694  if (registryKey.ValueCount != 0)
695  {
696  return result;
697  }
698  Registry.ClassesRoot.DeleteSubKey("CLSID");
699  return result;
700  }
701  }
702 
703  [SecurityCritical]
704  private void RegisterPrimaryInteropAssembly(RuntimeAssembly assembly, string strAsmCodeBase, PrimaryInteropAssemblyAttribute attr)
705  {
706  if (assembly.GetPublicKey().Length == 0)
707  {
708  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_PIAMustBeStrongNamed"));
709  }
710  string subkey = "{" + Marshal.GetTypeLibGuidForAssembly(assembly).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
711  string subkey2 = attr.MajorVersion.ToString("x", CultureInfo.InvariantCulture) + "." + attr.MinorVersion.ToString("x", CultureInfo.InvariantCulture);
712  using (RegistryKey registryKey = Registry.ClassesRoot.CreateSubKey("TypeLib"))
713  {
714  using (RegistryKey registryKey2 = registryKey.CreateSubKey(subkey))
715  {
716  using (RegistryKey registryKey3 = registryKey2.CreateSubKey(subkey2))
717  {
718  registryKey3.SetValue("PrimaryInteropAssemblyName", assembly.FullName);
719  if (strAsmCodeBase != null)
720  {
721  registryKey3.SetValue("PrimaryInteropAssemblyCodeBase", strAsmCodeBase);
722  }
723  }
724  }
725  }
726  }
727 
728  [SecurityCritical]
729  private void UnregisterPrimaryInteropAssembly(Assembly assembly, PrimaryInteropAssemblyAttribute attr)
730  {
731  string text = "{" + Marshal.GetTypeLibGuidForAssembly(assembly).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
732  string text2 = attr.MajorVersion.ToString("x", CultureInfo.InvariantCulture) + "." + attr.MinorVersion.ToString("x", CultureInfo.InvariantCulture);
733  using (RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey("TypeLib", writable: true))
734  {
735  if (registryKey != null)
736  {
737  using (RegistryKey registryKey2 = registryKey.OpenSubKey(text, writable: true))
738  {
739  if (registryKey2 != null)
740  {
741  using (RegistryKey registryKey3 = registryKey2.OpenSubKey(text2, writable: true))
742  {
743  if (registryKey3 != null)
744  {
745  registryKey3.DeleteValue("PrimaryInteropAssemblyName", throwOnMissingValue: false);
746  registryKey3.DeleteValue("PrimaryInteropAssemblyCodeBase", throwOnMissingValue: false);
747  if (registryKey3.SubKeyCount == 0 && registryKey3.ValueCount == 0)
748  {
749  registryKey2.DeleteSubKey(text2);
750  }
751  }
752  }
753  if (registryKey2.SubKeyCount == 0 && registryKey2.ValueCount == 0)
754  {
755  registryKey.DeleteSubKey(text);
756  }
757  }
758  }
759  if (registryKey.SubKeyCount == 0 && registryKey.ValueCount == 0)
760  {
761  Registry.ClassesRoot.DeleteSubKey("TypeLib");
762  }
763  }
764  }
765  }
766 
767  private void EnsureManagedCategoryExists()
768  {
769  if (!ManagedCategoryExists())
770  {
771  using (RegistryKey registryKey = Registry.ClassesRoot.CreateSubKey("Component Categories"))
772  {
773  using (RegistryKey registryKey2 = registryKey.CreateSubKey("{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}"))
774  {
775  registryKey2.SetValue("0", ".NET Category");
776  }
777  }
778  }
779  }
780 
781  private static bool ManagedCategoryExists()
782  {
783  using (RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey("Component Categories", RegistryKeyPermissionCheck.ReadSubTree))
784  {
785  if (registryKey == null)
786  {
787  return false;
788  }
789  using (RegistryKey registryKey2 = registryKey.OpenSubKey("{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}", RegistryKeyPermissionCheck.ReadSubTree))
790  {
791  if (registryKey2 == null)
792  {
793  return false;
794  }
795  object value = registryKey2.GetValue("0");
796  if (value == null || value.GetType() != typeof(string))
797  {
798  return false;
799  }
800  string a = (string)value;
801  if (a != ".NET Category")
802  {
803  return false;
804  }
805  }
806  }
807  return true;
808  }
809 
810  [SecurityCritical]
811  private void CallUserDefinedRegistrationMethod(Type type, bool bRegister)
812  {
813  bool flag = false;
814  Type type2 = null;
815  type2 = ((!bRegister) ? typeof(ComUnregisterFunctionAttribute) : typeof(ComRegisterFunctionAttribute));
816  Type type3 = type;
817  while (!flag && type3 != null)
818  {
819  MethodInfo[] methods = type3.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
820  int num = methods.Length;
821  for (int i = 0; i < num; i++)
822  {
823  MethodInfo methodInfo = methods[i];
824  if (methodInfo.GetCustomAttributes(type2, inherit: true).Length == 0)
825  {
826  continue;
827  }
828  if (!methodInfo.IsStatic)
829  {
830  if (bRegister)
831  {
832  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NonStaticComRegFunction", methodInfo.Name, type3.Name));
833  }
834  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NonStaticComUnRegFunction", methodInfo.Name, type3.Name));
835  }
836  ParameterInfo[] parameters = methodInfo.GetParameters();
837  if (methodInfo.ReturnType != typeof(void) || parameters == null || parameters.Length != 1 || (parameters[0].ParameterType != typeof(string) && parameters[0].ParameterType != typeof(Type)))
838  {
839  if (bRegister)
840  {
841  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_InvalidComRegFunctionSig", methodInfo.Name, type3.Name));
842  }
843  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_InvalidComUnRegFunctionSig", methodInfo.Name, type3.Name));
844  }
845  if (flag)
846  {
847  if (bRegister)
848  {
849  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MultipleComRegFunctions", type3.Name));
850  }
851  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MultipleComUnRegFunctions", type3.Name));
852  }
853  object[] array = new object[1];
854  if (parameters[0].ParameterType == typeof(string))
855  {
856  array[0] = "HKEY_CLASSES_ROOT\\CLSID\\{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
857  }
858  else
859  {
860  array[0] = type;
861  }
862  methodInfo.Invoke(null, array);
863  flag = true;
864  }
865  type3 = type3.BaseType;
866  }
867  }
868 
869  private Type GetBaseComImportType(Type type)
870  {
871  while (type != null && !type.IsImport)
872  {
873  type = type.BaseType;
874  }
875  return type;
876  }
877 
878  private bool IsRegisteredAsValueType(Type type)
879  {
880  if (!type.IsValueType)
881  {
882  return false;
883  }
884  return true;
885  }
886 
887  [MethodImpl(MethodImplOptions.InternalCall)]
888  [SecurityCritical]
889  private static extern void RegisterTypeForComClientsNative(Type type, ref Guid g);
890 
891  [MethodImpl(MethodImplOptions.InternalCall)]
892  [SecurityCritical]
893  private static extern int RegisterTypeForComClientsExNative(Type t, RegistrationClassContext clsContext, RegistrationConnectionType flags);
894 
895  [DllImport("ole32.dll", CharSet = CharSet.Auto, PreserveSig = false)]
896  private static extern void CoRevokeClassObject(int cookie);
897  }
898 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
Discovers the attributes of a parameter and provides access to parameter metadata.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
virtual bool RegisterAssembly(Assembly assembly, AssemblyRegistrationFlags flags)
Registers the classes in a managed assembly to enable creation from COM.
virtual string ImageRuntimeVersion
Gets a string representing the version of the common language runtime (CLR) saved in the file contain...
Definition: Assembly.cs:216
Discovers the attributes of a method and provides access to method metadata.
Definition: MethodInfo.cs:13
virtual Guid GetManagedCategoryGuid()
Returns the GUID of the COM category that contains the managed classes.
virtual int RegisterTypeForComClients(Type type, RegistrationClassContext classContext, RegistrationConnectionType flags)
Registers the specified type with COM using the specified execution context and connection type.
virtual string FullName
Gets the display name of the assembly.
Definition: Assembly.cs:49
bool IsImport
Gets a value indicating whether the T:System.Type has a T:System.Runtime.InteropServices....
Definition: Type.cs:508
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
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
virtual Type ReturnType
Gets the return type of this method.
Definition: MethodInfo.cs:23
override string ToString()
Returns a String representing the name of the current Type.
Definition: Type.cs:2788
static string GenerateProgIdForType(Type type)
Returns a programmatic identifier (ProgID) for the specified type.
Definition: Marshal.cs:2617
virtual string GetProgIdForType(Type type)
Retrieves the COM ProgID for the specified type.
ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
Searches for a constructor whose parameters match the specified argument types and modifiers,...
Definition: Type.cs:1378
Indicates that the attributed assembly is a primary interop assembly.
bool IsClass
Gets a value indicating whether the T:System.Type is a class or a delegate; that is,...
Definition: Type.cs:409
virtual object [] GetCustomAttributes(bool inherit)
Gets all the custom attributes for this assembly.
Definition: Assembly.cs:1041
bool IsStatic
Gets a value indicating whether the method is static.
Definition: MethodBase.cs:227
virtual Type [] GetExportedTypes()
Gets the public types defined in this assembly that are visible outside the assembly.
Definition: Assembly.cs:932
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Represents a globally unique identifier (GUID).To browse the .NET Framework source code for this type...
Definition: Guid.cs:14
Provides a set of services for registering and unregistering managed assemblies for use from COM.
virtual bool TypeRepresentsComType(Type type)
Indicates whether a type is marked with the T:System.Runtime.InteropServices.ComImportAttribute,...
Provides a set of services for registering and unregistering managed assemblies for use from COM.
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
virtual bool UnregisterAssembly(Assembly assembly)
Unregisters the classes in a managed assembly.
Represents an assembly, which is a reusable, versionable, and self-describing building block of a com...
Definition: Assembly.cs:22
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
bool IsCOMObject
Gets a value indicating whether the T:System.Type is a COM object.
Definition: Type.cs:701
virtual void CopyTo(Array array)
Copies the entire T:System.Collections.ArrayList to a compatible one-dimensional T:System....
Definition: ArrayList.cs:2516
MethodImplOptions
Defines the details of how a method is implemented.
CharSet
Dictates which character set marshaled strings should use.
Definition: CharSet.cs:7
object [] GetCustomAttributes(Type attributeType, bool inherit)
Provides COM objects with version-independent access to the M:System.Reflection.MemberInfo....
bool IsValueType
Gets a value indicating whether the T:System.Type is a value type.
Definition: Type.cs:445
virtual Type [] GetRegistrableTypesInAssembly(Assembly assembly)
Retrieves a list of classes in an assembly that would be registered by a call to M:System....
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
RegistrationConnectionType
Defines the types of connections to a class object.
virtual void RegisterTypeForComClients(Type type, ref Guid g)
Registers the specified type with COM using the specified GUID.
The exception that is thrown when one of the arguments provided to a method is not valid.
AssemblyRegistrationFlags
Defines a set of flags used when registering assemblies.
virtual bool ReflectionOnly
Gets a T:System.Boolean value indicating whether this assembly was loaded into the reflection-only co...
Definition: Assembly.cs:182
The exception that is thrown when a method call is invalid for the object's current state.
ClassInterfaceType
Identifies the type of class interface that is generated for a class.
virtual void UnregisterTypeForComClients(int cookie)
Removes references to a type registered with the M:System.Runtime.InteropServices....
string Name
Provides COM objects with version-independent access to the P:System.Reflection.MemberInfo....
Definition: _MethodInfo.cs:24
bool IsAbstract
Gets a value indicating whether the T:System.Type is abstract and must be overridden.
Definition: Type.cs:458
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
virtual bool TypeRequiresRegistration(Type type)
Determines whether the specified type requires registration.
RegistrationClassContext
Specifies the set of execution contexts in which a class object will be made available for requests t...
abstract ParameterInfo [] GetParameters()
When overridden in a derived class, gets the parameters of the specified method or constructor.
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14