HeiDenHainB.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. namespace IMCS.Device.HeiDenHainBase
  9. {
  10. public static class MyHelper
  11. {
  12. #region "public static methods"
  13. public static string CheckDncVersion(int mayor = 0, int minor = 0, int revision = 0, int build = 0)
  14. {
  15. int[] requiredVersion = new int[] { mayor, minor, revision, build };
  16. int[] installedVersion = null;
  17. string strVersionComInterface = null;
  18. HeidenhainDNCLib.JHMachine machine = null;
  19. try
  20. {
  21. machine = new HeidenhainDNCLib.JHMachine();
  22. strVersionComInterface = machine.GetVersionComInterface();
  23. if (strVersionComInterface.Contains("."))
  24. installedVersion = strVersionComInterface.Split('.').Select(n => Convert.ToInt32(n)).ToArray();
  25. else
  26. installedVersion = strVersionComInterface.Split(',').Select(n => Convert.ToInt32(n)).ToArray();
  27. // check if installed COM component meets minimum application requirement
  28. if (!CheckVersion(requiredVersion, installedVersion))
  29. {
  30. string strRequiredVersionComInterface = requiredVersion[0] + "." +
  31. requiredVersion[1] + "." +
  32. requiredVersion[2] + "." +
  33. requiredVersion[3];
  34. string strInstalledVersionComInterface = installedVersion[0] + "." +
  35. installedVersion[1] + "." +
  36. installedVersion[2] + "." +
  37. installedVersion[3];
  38. // --- close application, because HeidenhainDNC version dos not meer minimum application requirement
  39. MessageBox.Show(
  40. "Your HeidenhainDNC version does not meet minimum application requirement!" + Environment.NewLine + Environment.NewLine +
  41. "Installed version\t: " + strInstalledVersionComInterface + Environment.NewLine +
  42. "Required version\t: " + strRequiredVersionComInterface + Environment.NewLine + Environment.NewLine +
  43. "The application has to be closed." + Environment.NewLine,
  44. "Verify HeidenhainDNC version",
  45. MessageBoxButtons.OK,
  46. MessageBoxIcon.Error);
  47. Application.Exit();
  48. }
  49. }
  50. catch (COMException)
  51. {
  52. // --- close application, because can't use HeidenhainDNC COM component (reinstall ?)
  53. MessageBox.Show(
  54. "Can't use HeidenhainDNC COM interface!" + Environment.NewLine +
  55. "The application has to be closed." + Environment.NewLine +
  56. "Please reinstall HeidenhainDNC.",
  57. "Verify HeidenhainDNC version",
  58. MessageBoxButtons.OK,
  59. MessageBoxIcon.Error);
  60. Application.Exit();
  61. }
  62. catch (Exception)
  63. {
  64. // --- something went wrong by verifying HeidenhainDNC version --------
  65. DialogResult dr;
  66. dr = MessageBox.Show(
  67. "Problem verifying HeidenhainDNC version!" + Environment.NewLine +
  68. "Possibly the application can't be executed properly." + Environment.NewLine +
  69. "Do you want to proceed anyway?" + Environment.NewLine,
  70. "Verify HeidenhainDNC version",
  71. MessageBoxButtons.YesNo,
  72. MessageBoxIcon.Question);
  73. if (dr == System.Windows.Forms.DialogResult.No)
  74. Application.Exit();
  75. }
  76. finally
  77. {
  78. if (machine != null)
  79. Marshal.ReleaseComObject(machine);
  80. }
  81. return strVersionComInterface;
  82. }
  83. public static void ShowComException(int ErrorCode, string ClassName = null, string MethodName = null)
  84. {
  85. // --- build caption for COM exception message box ----------------------
  86. string strMessageBoxCaption = "COM Exception";
  87. if (!String.IsNullOrEmpty(ClassName) && !String.IsNullOrEmpty(MethodName))
  88. strMessageBoxCaption = ClassName + " --> " + MethodName;
  89. // --- build text for COM exception message box -------------------------
  90. string strExceptionMessage = "HRESULT: 0x" + Convert.ToString(ErrorCode, 16) + Environment.NewLine;
  91. HeidenhainDNCLib.DNC_HRESULT hr = (HeidenhainDNCLib.DNC_HRESULT)ErrorCode;
  92. strExceptionMessage += Enum.GetName(typeof(HeidenhainDNCLib.DNC_HRESULT), hr);
  93. // --- show message box -------------------------------------------------
  94. MessageBox.Show(strExceptionMessage, strMessageBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
  95. }
  96. public static void ShowException(Exception ex, string ClassName = null, string MethodName = null)
  97. {
  98. // --- build caption for COM exception message box ----------------------
  99. string strMessageBoxCaption = "Exception";
  100. if (!String.IsNullOrEmpty(ClassName) && !String.IsNullOrEmpty(MethodName))
  101. strMessageBoxCaption = ClassName + " --> " + MethodName;
  102. // --- build text for COM exception message box -------------------------
  103. string strExceptionMessage = ".NET Exception:" + Environment.NewLine +
  104. ex.Message + Environment.NewLine;
  105. // --- show message box -------------------------------------------------
  106. MessageBox.Show(strExceptionMessage, strMessageBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
  107. }
  108. #endregion
  109. #region "private static methods"
  110. private static bool CheckVersion(int[] requiredVersion, int[] installedVersion)
  111. {
  112. if (requiredVersion.Length != 4 || installedVersion.Length != 4)
  113. throw (new Exception());
  114. for (int i = 0; i < installedVersion.Length; i++)
  115. {
  116. if (installedVersion[i] > requiredVersion[i])
  117. break;
  118. if (installedVersion[i] == requiredVersion[i])
  119. continue;
  120. return false;
  121. }
  122. return true;
  123. }
  124. #endregion
  125. }
  126. public class MyDirectoryEntry
  127. {
  128. public string Name { get; set; }
  129. public DateTime TimeStamp { get; set; }
  130. public double FileSize { get; set; }
  131. // directory entry attributes
  132. public bool ReadOnly { get; set; }
  133. public bool Hidden { get; set; }
  134. public bool Dir { get; set; }
  135. public bool System { get; set; }
  136. public bool Modified { get; set; }
  137. public bool Locked { get; set; }
  138. }
  139. }