LicenseLoader.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace HXX.Scanner.Pop
  7. {
  8. class LicenseLoader
  9. {
  10. [DllImport("kernel32.dll")]
  11. static extern uint GetSystemDirectory([Out] StringBuilder lpBuffer, uint uSize);
  12. [DllImport("kernel32.dll")]
  13. static extern uint GetWindowsDirectory([Out] StringBuilder lpBuffer, uint uSize);
  14. private static string GetLicenseFilePath()
  15. {
  16. StringBuilder sbsSystemDir = new StringBuilder(256);
  17. uint iLength = 0;
  18. iLength = GetSystemDirectory(sbsSystemDir, 256);
  19. if (iLength == 0)
  20. return null;
  21. StringBuilder sbsWindowsDir = new StringBuilder(256);
  22. iLength = GetWindowsDirectory(sbsWindowsDir, 256);
  23. if (iLength == 0)
  24. return null;
  25. if (sbsWindowsDir.Length > 0)
  26. {
  27. if (IntPtr.Size == 8)
  28. {
  29. sbsWindowsDir.Append("\\SysWOW64\\DynamsoftDotNetTwain.lic");
  30. }
  31. else
  32. {
  33. sbsWindowsDir.Append("\\System32\\DynamsoftDotNetTwain.lic");
  34. }
  35. }
  36. return sbsWindowsDir.ToString();
  37. }
  38. private static List<string> ReadSystemLocalLicense(string strlicensePath)
  39. {
  40. List<string> liststringTempListLicense = null;
  41. try
  42. {
  43. if (File.Exists(strlicensePath))
  44. {
  45. string[] stringarrTempAllLines = File.ReadAllLines(strlicensePath);
  46. foreach (string strTemp in stringarrTempAllLines)
  47. {
  48. int iTempIndex = strTemp.IndexOf("SerialNo");
  49. if (iTempIndex != -1)
  50. {
  51. int iIndex1 = strTemp.IndexOf("=");
  52. if (iIndex1 != -1)
  53. {
  54. if (liststringTempListLicense == null)
  55. liststringTempListLicense = new List<string>();
  56. string strTempLicense = strTemp.Substring((iIndex1 + 1), (strTemp.Length - (iIndex1 + 1)));
  57. liststringTempListLicense.Add(strTempLicense);
  58. }
  59. }
  60. }
  61. }
  62. }
  63. catch
  64. { }
  65. return liststringTempListLicense;
  66. }
  67. private static string GetCurrentPath()
  68. {
  69. string strCurrentPath = System.IO.Directory.GetCurrentDirectory();
  70. string strCurrentPathtxt = strCurrentPath + "\\lic.txt";
  71. return strCurrentPathtxt;
  72. }
  73. private static string ReadLicense(string strPath)
  74. {
  75. try
  76. {
  77. string strLicenseMessage = System.IO.File.ReadAllText(strPath);
  78. int beginIndex = strLicenseMessage.IndexOf("=\"");
  79. int endIndex = strLicenseMessage.IndexOf("\";");
  80. string strLicense = null;
  81. if (beginIndex != -1 && endIndex != -1)
  82. {
  83. strLicense = strLicenseMessage.Substring(beginIndex + 2, endIndex - beginIndex - 2);
  84. }
  85. return strLicense;
  86. }
  87. catch (Exception)
  88. {
  89. string strLicense = null;
  90. return strLicense;
  91. }
  92. }
  93. private static string CombineAllLicense(string strCurrentPathLicense)
  94. {
  95. if (strCurrentPathLicense != null)
  96. {
  97. try
  98. {
  99. string strTempLicenseFilePath = GetLicenseFilePath();
  100. List<string> tempListLicense = ReadSystemLocalLicense(strTempLicenseFilePath);
  101. StringBuilder objProductKeys = new StringBuilder();
  102. foreach (string temp in tempListLicense)
  103. {
  104. if (temp != null)
  105. {
  106. objProductKeys.Append(temp);
  107. objProductKeys.Append(";");
  108. }
  109. }
  110. string strTempProductKey = objProductKeys.ToString();
  111. strTempProductKey = strTempProductKey + strCurrentPathLicense + ";";
  112. return strTempProductKey;
  113. }
  114. catch (Exception)
  115. {
  116. string strTempProductKey = strCurrentPathLicense + ";";
  117. return strTempProductKey;
  118. }
  119. }
  120. else
  121. {
  122. try
  123. {
  124. string steTempLicenseFilePath = GetLicenseFilePath();
  125. List<string> liststringTempListLicense = ReadSystemLocalLicense(steTempLicenseFilePath);
  126. StringBuilder objProductKeys = new StringBuilder();
  127. foreach (string temp in liststringTempListLicense)
  128. {
  129. if (temp != null)
  130. {
  131. objProductKeys.Append(temp);
  132. objProductKeys.Append(";");
  133. }
  134. }
  135. string strTempProductKey = objProductKeys.ToString();
  136. return strTempProductKey;
  137. }
  138. catch (Exception)
  139. {
  140. return null;
  141. }
  142. }
  143. }
  144. public static string ReadLocalLicense()
  145. {
  146. string strGetCurrentPath = LicenseLoader.GetCurrentPath();
  147. string strReadLicense = LicenseLoader.ReadLicense(strGetCurrentPath);
  148. string strCombineAllLicense = LicenseLoader.CombineAllLicense(strReadLicense);
  149. return strCombineAllLicense;
  150. }
  151. }
  152. }