RevitProductUtility.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* ==============================================================================
  2. * 功能描述:stringUtility
  3. * 创 建 者:Garrett
  4. * 创建日期:2019/8/1 11:14:34
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using Microsoft.Win32;
  14. namespace SAGA.DotNetUtils.Revit
  15. {
  16. /// <summary>
  17. /// Revit版本信息
  18. /// </summary>
  19. public class RevitProductUtility
  20. {
  21. [DllImport("msi.dll", CharSet = CharSet.Unicode)]
  22. public static extern uint MsiEnumClients(string szComponent, uint iProductIndex, string lpProductBuf);
  23. [DllImport("msi.dll", CharSet = CharSet.Unicode)]
  24. public static extern int MsiGetProductInfo(string product, string property, string valueBuf, ref int len);
  25. [DllImport("advapi32.dll", SetLastError = true)]
  26. public static extern int RegCloseKey(UIntPtr hKey);
  27. [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
  28. public static extern int RegOpenKeyEx(UIntPtr hKey, string subKey, int ulOptions, int samDesired, ref UIntPtr hkResult);
  29. [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
  30. public static extern int RegQueryValueEx(UIntPtr hKey, string lpValueName, int lpReserved, ref uint lpType, ref uint lpData, ref uint lpcbData);
  31. [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
  32. public static extern int RegQueryValueEx(UIntPtr hKey, string lpValueName, int lpReserved, ref uint lpType, StringBuilder lpData, ref uint lpcbData);
  33. /// <summary>
  34. /// 获取所有安装的Revit版本
  35. /// </summary>
  36. /// <returns>
  37. /// 2018,E:\Program Files\Autodesk\Revit 2018\
  38. /// </returns>
  39. public static List<Tuple<string, string>> GetAllInstalledRevitProducts()
  40. {
  41. List<Tuple<string, string>> list = new List<Tuple<string, string>>();
  42. initializeProductsDictionary();
  43. try
  44. {
  45. List<string>.Enumerator enumerator = GetInstalledCodes().GetEnumerator();
  46. while (enumerator.MoveNext())
  47. {
  48. Tuple<string, string> item = GetProductVersionInfo(enumerator.Current);
  49. if (null != item)
  50. {
  51. list.Add(item);
  52. }
  53. }
  54. }
  55. catch (Exception)
  56. {
  57. }
  58. return list;
  59. }
  60. private static Tuple<string, string> getInstalledProductInfo(string productRegGUID)
  61. {
  62. Tuple<string, string> tuple = null;
  63. string installPath, version;
  64. uint num4 = 0;
  65. if (!m_productsHashtable.ContainsKey(productRegGUID))
  66. {
  67. return null;
  68. }
  69. RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall").OpenSubKey(productRegGUID);
  70. if (null != key)
  71. {
  72. installPath = (string)key.GetValue("InstallLocation");
  73. version = (string)key.GetValue("DisplayName");
  74. tuple = new Tuple<string, string>(version, installPath);
  75. goto Label_0365;
  76. }
  77. UIntPtr hkResult = new UIntPtr();
  78. uint lpcbData = 0x400;
  79. uint lpData = 0;
  80. StringBuilder builder = new StringBuilder(0x400);
  81. RegQueryValueEx(hkResult, "InstallLocation", 0, ref num4, builder, ref lpcbData);
  82. installPath = builder.ToString();
  83. RegQueryValueEx(hkResult, "DisplayName", 0, ref num4, builder, ref lpcbData);
  84. version = builder.ToString();
  85. tuple = new Tuple<string, string>(version, installPath);
  86. return tuple;
  87. Label_0365:
  88. key.Close();
  89. return tuple;
  90. }
  91. private static List<string> GetInstalledCodes()
  92. {
  93. List<string> list = new List<string>();
  94. string lpProductBuf = new string('0', 0x26);
  95. string szComponent = "{DF7D485F-B8BA-448E-A444-E6FB1C258912}";
  96. uint num = 1;
  97. if (MsiEnumClients(szComponent, 0, lpProductBuf) == 0)
  98. {
  99. uint num2;
  100. do
  101. {
  102. string item = string.Copy(lpProductBuf);
  103. list.Add(item);
  104. num2 = num;
  105. num++;
  106. }
  107. while (MsiEnumClients(szComponent, num2, lpProductBuf) == 0);
  108. }
  109. return list;
  110. }
  111. private static Tuple<string, string> GetProductVersionInfo(string productCode)
  112. {
  113. Match match = new Regex(@"^(\{{0,1}(7346B4A[0-9a-fA-F])-(?<Majorversion>([0-9a-fA-F]){2})(?<Subversion>([0-9a-fA-F]){2})-(?<Discipline>([0-9a-fA-F]){2})(?<Platform>([0-9a-fA-F]){1})[0-9a-fA-F]-(?<Language>([0-9a-fA-F]){4})-705C0D862004\}{0,1})$").Match(productCode);
  114. if (!match.Success)
  115. {
  116. return getInstalledProductInfo(productCode);
  117. }
  118. string version = "20" + match.Result("${Majorversion}");
  119. string installPath = new string(' ', 260);
  120. int num4 = 260;
  121. MsiGetProductInfo(productCode, "InstallLocation", installPath, ref num4);
  122. return new Tuple<string, string>(version, installPath.Substring(0,num4));
  123. }
  124. private static Hashtable m_productsHashtable;
  125. private static void initializeProductsDictionary()
  126. {
  127. if (null == m_productsHashtable)
  128. {
  129. m_productsHashtable = new Hashtable();
  130. }
  131. else
  132. {
  133. m_productsHashtable.Clear();
  134. }
  135. }
  136. }
  137. }