123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace IMCS.Device.HeiDenHainBase
- {
- public static class MyHelper
- {
- #region "public static methods"
- public static string CheckDncVersion(int mayor = 0, int minor = 0, int revision = 0, int build = 0)
- {
- int[] requiredVersion = new int[] { mayor, minor, revision, build };
- int[] installedVersion = null;
- string strVersionComInterface = null;
- HeidenhainDNCLib.JHMachine machine = null;
- try
- {
- machine = new HeidenhainDNCLib.JHMachine();
- strVersionComInterface = machine.GetVersionComInterface();
- if (strVersionComInterface.Contains("."))
- installedVersion = strVersionComInterface.Split('.').Select(n => Convert.ToInt32(n)).ToArray();
- else
- installedVersion = strVersionComInterface.Split(',').Select(n => Convert.ToInt32(n)).ToArray();
- // check if installed COM component meets minimum application requirement
- if (!CheckVersion(requiredVersion, installedVersion))
- {
- string strRequiredVersionComInterface = requiredVersion[0] + "." +
- requiredVersion[1] + "." +
- requiredVersion[2] + "." +
- requiredVersion[3];
- string strInstalledVersionComInterface = installedVersion[0] + "." +
- installedVersion[1] + "." +
- installedVersion[2] + "." +
- installedVersion[3];
- // --- close application, because HeidenhainDNC version dos not meer minimum application requirement
- MessageBox.Show(
- "Your HeidenhainDNC version does not meet minimum application requirement!" + Environment.NewLine + Environment.NewLine +
- "Installed version\t: " + strInstalledVersionComInterface + Environment.NewLine +
- "Required version\t: " + strRequiredVersionComInterface + Environment.NewLine + Environment.NewLine +
- "The application has to be closed." + Environment.NewLine,
- "Verify HeidenhainDNC version",
- MessageBoxButtons.OK,
- MessageBoxIcon.Error);
- Application.Exit();
- }
- }
- catch (COMException)
- {
- // --- close application, because can't use HeidenhainDNC COM component (reinstall ?)
- MessageBox.Show(
- "Can't use HeidenhainDNC COM interface!" + Environment.NewLine +
- "The application has to be closed." + Environment.NewLine +
- "Please reinstall HeidenhainDNC.",
- "Verify HeidenhainDNC version",
- MessageBoxButtons.OK,
- MessageBoxIcon.Error);
- Application.Exit();
- }
- catch (Exception)
- {
- // --- something went wrong by verifying HeidenhainDNC version --------
- DialogResult dr;
- dr = MessageBox.Show(
- "Problem verifying HeidenhainDNC version!" + Environment.NewLine +
- "Possibly the application can't be executed properly." + Environment.NewLine +
- "Do you want to proceed anyway?" + Environment.NewLine,
- "Verify HeidenhainDNC version",
- MessageBoxButtons.YesNo,
- MessageBoxIcon.Question);
- if (dr == System.Windows.Forms.DialogResult.No)
- Application.Exit();
- }
- finally
- {
- if (machine != null)
- Marshal.ReleaseComObject(machine);
- }
- return strVersionComInterface;
- }
- public static void ShowComException(int ErrorCode, string ClassName = null, string MethodName = null)
- {
- // --- build caption for COM exception message box ----------------------
- string strMessageBoxCaption = "COM Exception";
- if (!String.IsNullOrEmpty(ClassName) && !String.IsNullOrEmpty(MethodName))
- strMessageBoxCaption = ClassName + " --> " + MethodName;
- // --- build text for COM exception message box -------------------------
- string strExceptionMessage = "HRESULT: 0x" + Convert.ToString(ErrorCode, 16) + Environment.NewLine;
- HeidenhainDNCLib.DNC_HRESULT hr = (HeidenhainDNCLib.DNC_HRESULT)ErrorCode;
- strExceptionMessage += Enum.GetName(typeof(HeidenhainDNCLib.DNC_HRESULT), hr);
- // --- show message box -------------------------------------------------
- MessageBox.Show(strExceptionMessage, strMessageBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- public static void ShowException(Exception ex, string ClassName = null, string MethodName = null)
- {
- // --- build caption for COM exception message box ----------------------
- string strMessageBoxCaption = "Exception";
- if (!String.IsNullOrEmpty(ClassName) && !String.IsNullOrEmpty(MethodName))
- strMessageBoxCaption = ClassName + " --> " + MethodName;
- // --- build text for COM exception message box -------------------------
- string strExceptionMessage = ".NET Exception:" + Environment.NewLine +
- ex.Message + Environment.NewLine;
- // --- show message box -------------------------------------------------
- MessageBox.Show(strExceptionMessage, strMessageBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- #endregion
- #region "private static methods"
- private static bool CheckVersion(int[] requiredVersion, int[] installedVersion)
- {
- if (requiredVersion.Length != 4 || installedVersion.Length != 4)
- throw (new Exception());
- for (int i = 0; i < installedVersion.Length; i++)
- {
- if (installedVersion[i] > requiredVersion[i])
- break;
- if (installedVersion[i] == requiredVersion[i])
- continue;
- return false;
- }
- return true;
- }
- #endregion
- }
- public class MyDirectoryEntry
- {
- public string Name { get; set; }
- public DateTime TimeStamp { get; set; }
- public double FileSize { get; set; }
- // directory entry attributes
- public bool ReadOnly { get; set; }
- public bool Hidden { get; set; }
- public bool Dir { get; set; }
- public bool System { get; set; }
- public bool Modified { get; set; }
- public bool Locked { get; set; }
- }
- }
|