Welcome to the ThinManager Knowledgebase.
Additional knowledgebase articles can be found at Rockwell Automation Tech Support.

If you would like to speak with one of our engineers, please Submit a Question or give us a call at the phone number here. In North America, To route your phone support request directly to a technical support engineer, call toll-free 1-888-382-1583 or 1-440-646-3434, select Option 3 (Technical Support), then select Option 5 (More Options). When prompted, enter the ThinManager Direct Dial Code 201.

Difference between revisions of "TermMon Demo Program Source"

From ThinManager Knowledge Base
Jump to: navigation, search
 
Line 1: Line 1:
Insert source here
 
 
 
<pre>
 
<pre>
 
using System;
 
using System;
Line 9: Line 7:
 
using System.Linq;
 
using System.Linq;
 
using System.Text;
 
using System.Text;
 +
using System.IO;
 
using System.Windows.Forms;
 
using System.Windows.Forms;
 
using System.Reflection;
 
using System.Reflection;
Line 23: Line 22:
 
     public partial class Form1 : Form
 
     public partial class Form1 : Form
 
     {
 
     {
 +
        String[] args = Environment.GetCommandLineArgs().Select(s => s.ToLowerInvariant()).ToArray();
 +
        String[] HelpList = { "-h", "/h", "-?", "/?", "help", "-help", "/help" };
 +
        String[] CommandList = { "reboot", "restart", "calibrate", "gotomainmenu", "switchtonextgroup", "switchtoprevgroup", "switchinstfailover", "changetermsecureuser", "logoffandchangetermsecureuser", "logofftermsecureuser", "disconnecttermsecureuser" };
 +
        int CommandCount = 0;
 +
        string path = "", textOut = Environment.NewLine;
 +
        bool gui = true;
 +
 
         public Form1()
 
         public Form1()
 
         {
 
         {
 
             InitializeComponent();
 
             InitializeComponent();
  
             EnableControl(true);
+
             if (args.Length > 1) gui = false;
            ChangeEnabled(false);
+
 
 +
            if (gui)
 +
            {
 +
                // Enable the OverrideIP and Demo Control setions
 +
                EnableControl(true);
 +
                ChangeEnabled(false);
  
            textBoxConnectionState.Text = "Disconnected";
+
                // Set the connection state display to disconnected
            textBoxConnectionState.BackColor = System.Drawing.Color.LightGray;
+
                textBoxConnectionState.Text = "Disconnected";
 +
                textBoxConnectionState.BackColor = System.Drawing.Color.LightGray;
  
            Assembly a = Assembly.GetExecutingAssembly();
+
                // Get and display prigram version info
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(a.Location);
+
                Assembly a = Assembly.GetExecutingAssembly();
            labelVersion.Text = "Version " + fvi.ProductVersion;
+
                FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(a.Location);
 +
                labelVersion.Text = "Version " + fvi.ProductVersion;
 +
            }
 +
            else
 +
            {
 +
                CommandCount = args.Length - 1;
 +
 
 +
                ParseArgs();
 +
 
 +
                axTermMon.Enable();
 +
 
 +
                timerConnectionTimeout.Enabled = true;
 +
                timerRuntime.Enabled = true;
 +
            }
 
         }
 
         }
  
 +
        // Parse any command line arguments.
 +
        private void ParseArgs()
 +
        {
 +
            for (int i = 0; i < args.Length; i++)
 +
            {
 +
                if (CheckValidIP(args[i]))
 +
                {
 +
                    axTermMon.RunInSession = false;
 +
                    axTermMon.OverrideIP = args[i];
 +
                    CommandCount--;
 +
                }
 +
 +
                if (args[i].EndsWith(".txt"))
 +
                {
 +
                    path = args[i];
 +
                    CommandCount--;
 +
                }
 +
            }
 +
 +
            if (args.Intersect(HelpList).Count() > 0) Help();
 +
        }
 +
 +
        private void Help()
 +
        {
 +
            textOut = Environment.NewLine + "Usage:" + Environment.NewLine + Environment.NewLine
 +
            + "TermMon.exe [IPv4 Address (optional)] [Filename and Path (optional)] [Commands]" + Environment.NewLine + Environment.NewLine
 +
            + "Local Example: TermMon.exe TerminalName TerminalMAC"+ Environment.NewLine
 +
            + "Remote Example: TermMon.exe xxx.xxx.xxx.xxx C:\termmon.txt TerminalName TerminalMAC" + Environment.NewLine + Environment.NewLine
 +
            + "Arguments, Information:" + Environment.NewLine
 +
            + "\tConnectionState" + Environment.NewLine
 +
            + "\tTerminalName" + Environment.NewLine
 +
            + "\tTerminalIP" + Environment.NewLine
 +
            + "\tTerminalMAC" + Environment.NewLine
 +
            + "\tTerminalModel" + Environment.NewLine
 +
            + "\tTerminalFirmwareVersion" + Environment.NewLine
 +
            + "\tTerminalBootloaderVersion" + Environment.NewLine
 +
            + "\tTerminalWindowsUsername" + Environment.NewLine
 +
            + "\tTerminalServerGroupList" + Environment.NewLine
 +
            + "\tCurrentTerminalServerGroup" + Environment.NewLine
 +
            + "\tCurrentWindowsUsername" + Environment.NewLine
 +
            + "\tUserID" + Environment.NewLine
 +
            + "\tTermSecureUsername" + Environment.NewLine
 +
            + "\tTermSecureWindowsUsername" + Environment.NewLine
 +
            + "\tWatchdogTime" + Environment.NewLine
 +
            + "Arguments, Commands" + Environment.NewLine
 +
            + "\tReboot" + Environment.NewLine
 +
            + "\tRestart" + Environment.NewLine
 +
            + "\tCalibrate" + Environment.NewLine
 +
            + "\tGoToMainMenu" + Environment.NewLine
 +
            + "\tSwitchToNextGroup" + Environment.NewLine
 +
            + "\tSwitchToPrevGroup" + Environment.NewLine
 +
            + "\tSwitchInstFailover" + Environment.NewLine
 +
            + "\tChangeTermSecureUser" + Environment.NewLine
 +
            + "\tLogOffAndChangeTermSecureUser" + Environment.NewLine
 +
            + "\tLogOffTermSucureUser" + Environment.NewLine
 +
            + "\tDisconnectTermSecureUser" + Environment.NewLine
 +
            + "\tChangeTerminalServerGroup" + Environment.NewLine
 +
            + "\tTermSecureCheckAccess" + Environment.NewLine;
 +
 +
            Exit();
 +
        }
 +
 +
        // Enable 3 second timer after the demo has been enabled.
 +
        // This provides a timeout in case it can't conenct to the control.
 
         private void WaitForConnection()
 
         private void WaitForConnection()
 
         {
 
         {
Line 43: Line 132:
 
         }
 
         }
  
 +
        // Enables/disables the OverrideIP and demo control sections.
 
         private void EnableControl(bool enabled)
 
         private void EnableControl(bool enabled)
 
         {
 
         {
Line 49: Line 139:
 
         }
 
         }
  
 +
        // Enables/disables all other sections.
 +
        // These sections are only available when connected to the control
 
         private void ChangeEnabled(bool enabled)
 
         private void ChangeEnabled(bool enabled)
 
         {
 
         {
Line 59: Line 151:
 
             groupBoxCameraOverlay.Enabled = enabled;
 
             groupBoxCameraOverlay.Enabled = enabled;
 
             groupBoxActiveScreen.Enabled = enabled;
 
             groupBoxActiveScreen.Enabled = enabled;
 +
            groupBoxLocation.Enabled = enabled;
 
             groupBoxWatchdog.Enabled = enabled;
 
             groupBoxWatchdog.Enabled = enabled;
 +
            groupBoxBiometric.Enabled = enabled;
 
             buttonEnable.Enabled = !enabled;
 
             buttonEnable.Enabled = !enabled;
 
             buttonDisable.Enabled = enabled;
 
             buttonDisable.Enabled = enabled;
 
         }
 
         }
  
 +
        // Checks that the IP address in the OverrideIP section is a valid format
 
         private bool CheckValidIP(string IPAddress)
 
         private bool CheckValidIP(string IPAddress)
 
         {
 
         {
Line 69: Line 164:
 
             if (System.Net.IPAddress.TryParse(IPAddress, out address))
 
             if (System.Net.IPAddress.TryParse(IPAddress, out address))
 
             {
 
             {
                 if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
+
                 if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) return true;
                {
 
                    return true;
 
                }
 
                else
 
                {
 
                    MessageBox.Show("Please use an IPv4 address");
 
                }
 
            }
 
            else
 
            {
 
                MessageBox.Show("Please use xxx.xxx.xxx.xxx format for Override IP function");
 
 
             }
 
             }
 +
            if (gui) MessageBox.Show("Please use a valid (xxx.xxx.xxx.xxx) IPv4 address");
 
             return false;
 
             return false;
 
         }
 
         }
  
 +
        // returns the corresponding string for a result code.
 
         private string ResultLookup(int res)
 
         private string ResultLookup(int res)
 
         {
 
         {
Line 120: Line 206:
 
                     return "No Windows Password";
 
                     return "No Windows Password";
 
                 case (short)TERMMONLib.TermMonConst.NoWindowsUsername:
 
                 case (short)TERMMONLib.TermMonConst.NoWindowsUsername:
                     return "Now Windows Username";
+
                     return "No Windows Username";
 
                 case (short)TERMMONLib.TermMonConst.PasswordChangeReq:
 
                 case (short)TERMMONLib.TermMonConst.PasswordChangeReq:
 
                     return "Password Change Required";
 
                     return "Password Change Required";
Line 128: Line 214:
 
         }
 
         }
  
         private void timer2_Tick(object sender, EventArgs e)
+
        // 3 second timer for connection timeout.
 +
         private void timerWaitForConnection_Tick(object sender, EventArgs e)
 
         {
 
         {
 
             axTermMon.Disable();
 
             axTermMon.Disable();
Line 154: Line 241:
 
         }
 
         }
  
 +
        private void timerConnectionTimeout_Tick(object sender, EventArgs e)
 +
        {
 +
            Console.WriteLine("Connection Failed");
 +
            Application.Exit();
 +
        }
 +
 +
        private void timerRuntime_Tick(object sender, EventArgs e)
 +
        {
 +
            Console.WriteLine("Failed to complete all commands");
 +
            Application.Exit();
 +
        }
 +
 +
        private void timerRunCommands_Tick(object sender, EventArgs e)
 +
        {
 +
            timerRunCommands.Enabled = false;
 +
            ProcessCommands();
 +
        }
 +
 +
        private void ProcessCommands()
 +
        {
 +
            var Commands = args.Intersect(CommandList);
 +
 +
            foreach (string command in Commands)
 +
            {
 +
                axTermMon.Command(GetCommandCode(command));
 +
                if (--CommandCount == 0)
 +
                {
 +
                    Application.Exit();
 +
                }
 +
            }
 +
 +
            ProcessSpecial();
 +
        }
 +
 +
        private void ProcessSpecial()
 +
        {
 +
            string[] Special = { "changeterminalservergroup", "termsecurecheckaccess" };
 +
            string data = "";
 +
 +
            var Commands = args.Intersect(Special);
 +
 +
            foreach (string command in Commands)
 +
            {
 +
                CommandCount--;
 +
                int i = Array.IndexOf(args, command);
 +
 +
                if (i < args.Length - 1 && i != -1) data = args[i + 1].ToLower();
 +
                else
 +
                {
 +
                    Console.WriteLine("That command requires data. Command Data");
 +
                    break;
 +
                }
 +
 +
                if (String.Compare(command, "termsecurecheckaccess") == 0)
 +
                {
 +
                    string[] TSdata = data.Split(',');
 +
                    if (TSdata.Length == 2)
 +
                    {
 +
                        Console.WriteLine(ResultLookup(axTermMon.TermSecureCheckAccess(TSdata[0], TSdata[1])));
 +
                    }
 +
                    else
 +
                    {
 +
                        Console.WriteLine("Use 'username,group' to check TermSecure access");
 +
                    }
 +
                }
 +
                else
 +
                {
 +
                    axTermMon.ChangeTerminalServerGroup(data);
 +
                }
 +
            }
 +
            if (CommandCount == 0) Application.Exit();
 +
        }
 +
 +
        private short GetCommandCode(string command)
 +
        {
 +
            switch (command)
 +
            {
 +
                case "reboot":
 +
                    return (short)TERMMONLib.TermMonCommand.Reboot;
 +
                case "restart":
 +
                    return (short)TERMMONLib.TermMonCommand.Restart;
 +
                case "calibrate":
 +
                    return (short)TERMMONLib.TermMonCommand.Calibrate;
 +
                case "gotomainmenu":
 +
                    return (short)TERMMONLib.TermMonCommand.GotoMainMenu;
 +
                case "swtichtonextgroup":
 +
                    return (short)TERMMONLib.TermMonCommand.SwitchToNextGroup;
 +
                case "switchtopreviousgroup":
 +
                    return (short)TERMMONLib.TermMonCommand.SwitchToPrevGroup;
 +
                case "switchinstantfailover":
 +
                    return (short)TERMMONLib.TermMonCommand.SwitchInstFailover;
 +
                case "changetermsecureuser":
 +
                    return (short)TERMMONLib.TermMonCommand.ChangeTermSecureUser;
 +
                case "loggoffandchangetermsecureuser":
 +
                    return (short)TERMMONLib.TermMonCommand.LogOffAndChangeTermSecureUser;
 +
                case "logofftermsecureuser":
 +
                    return (short)TERMMONLib.TermMonCommand.LogOffTermSecureUser;
 +
                case "disconnecttermsecureuser":
 +
                    return (short)TERMMONLib.TermMonCommand.DisconnectTermSecureUser;
 +
                default:
 +
                    return 0;
 +
            }
 +
        }
 +
 +
        public string ConvertStringToHex(string asciiString)
 +
        {
 +
            string hex = "";
 +
            foreach (char c in asciiString)
 +
            {
 +
                hex += String.Format("{0:x2} ", Convert.ToString(c, 16));
 +
            }
 +
            return hex;
 +
        }
 +
 +
        // Updates demo fields when an event occurs
 
         private void axTermMon_OnEvent(object sender, AxTERMMONLib._DTermMonEvents_OnEventEvent e)
 
         private void axTermMon_OnEvent(object sender, AxTERMMONLib._DTermMonEvents_OnEventEvent e)
 
         {
 
         {
 
             switch (e.eventCode)
 
             switch (e.eventCode)
 
             {
 
             {
 +
                case (short)TERMMONLib.TermMonEvent.ConnectionState:
 +
                    if (gui)
 +
                    {
 +
                        if (axTermMon.ConnectionState == (short)TERMMONLib.TermMonConst.Connected)
 +
                        {
 +
                            textBoxConnectionState.Text = "Connected";
 +
                            textBoxConnectionState.BackColor = System.Drawing.Color.Lime;
 +
                            timerWaitForConnection.Enabled = false;
 +
                            // Enable all sections of the demo
 +
                            ChangeEnabled(true);
 +
                            // disable the [[watchdog]] controls if not running on the client
 +
                            if (checkBoxRunInNonSession.Checked)
 +
                            {
 +
                                groupBoxWatchdog.Enabled = false;
 +
                                groupBoxSessionControl.Enabled = false;
 +
                            }
 +
                        }
 +
                        else if (axTermMon.ConnectionState == (short)TERMMONLib.TermMonConst.Disconnected)
 +
                        {
 +
                            textBoxConnectionState.Text = "Disconnected";
 +
                            textBoxConnectionState.BackColor = System.Drawing.Color.LightGray;
 +
                            ChangeEnabled(false);
 +
                        }
 +
                        else if (axTermMon.ConnectionState == (short)TERMMONLib.TermMonConst.Fail)
 +
                        {
 +
                            textBoxConnectionState.Text = "Failed";
 +
                            textBoxConnectionState.BackColor = System.Drawing.Color.Red;
 +
                            ChangeEnabled(false);
 +
                        }
 +
                    }
 +
                    else
 +
                    {
 +
                        timerConnectionTimeout.Enabled = false;
 +
                        timerRunCommands.Enabled = true;
 +
                        if (args.Contains("ConnectionState"))
 +
                        {
 +
                            textOut = textOut + "Connection State: " + "Connected" + Environment.NewLine;
 +
                            CommandCount--;
 +
                        }
 +
                    }
 +
                    break;
 
                 case (short)TERMMONLib.TermMonEvent.TerminalName:
 
                 case (short)TERMMONLib.TermMonEvent.TerminalName:
                     textBoxName.Text = axTermMon.TerminalName;
+
                     if (gui) textBoxName.Text = axTermMon.TerminalName;
 +
                    else
 +
                    {
 +
                        if (args.Contains("terminalname"))
 +
                        {
 +
                            textOut = textOut + "Terminal Name: " + axTermMon.TerminalName + Environment.NewLine;
 +
                            CommandCount--;
 +
                        }
 +
                    }
 
                     break;
 
                     break;
 
                 case (short)TERMMONLib.TermMonEvent.TerminalIP:
 
                 case (short)TERMMONLib.TermMonEvent.TerminalIP:
                     textBoxIPAddress.Text = axTermMon.TerminalIP;
+
                     if (gui) textBoxIPAddress.Text = axTermMon.TerminalIP;
 +
                    else
 +
                    {
 +
                        if (args.Contains("terminalip"))
 +
                        {
 +
                            textOut = textOut + "Terminal IP: " + axTermMon.TerminalIP + Environment.NewLine;
 +
                            CommandCount--;
 +
                        }
 +
                    }
 
                     break;
 
                     break;
 
                 case (short)TERMMONLib.TermMonEvent.TerminalMAC:
 
                 case (short)TERMMONLib.TermMonEvent.TerminalMAC:
                     textBoxMACAddress.Text = axTermMon.TerminalMAC;
+
                     if (gui) textBoxMACAddress.Text = axTermMon.TerminalMAC;
 +
                    else
 +
                    {
 +
                        if (args.Contains("terminalmac"))
 +
                        {
 +
                            textOut = textOut + "Terminal MAC: " + axTermMon.TerminalMAC + Environment.NewLine;
 +
                            CommandCount--;
 +
                        }
 +
                    }
 
                     break;
 
                     break;
 
                 case (short)TERMMONLib.TermMonEvent.TerminalFirmwareVersion:
 
                 case (short)TERMMONLib.TermMonEvent.TerminalFirmwareVersion:
                     textBoxFirmwareVersion.Text = axTermMon.TerminalFirmwareVersion;
+
                     if (gui) textBoxFirmwareVersion.Text = axTermMon.TerminalFirmwareVersion;
 +
                    else
 +
                    {
 +
                        if (args.Contains("terminalfirmwareversion"))
 +
                        {
 +
                            textOut = textOut + "Terminal Firmware Version: " + axTermMon.TerminalFirmwareVersion + Environment.NewLine;
 +
                            CommandCount--;
 +
                        }
 +
                    }
 
                     break;
 
                     break;
 
                 case (short)TERMMONLib.TermMonEvent.TerminalBootLoaderVersion:
 
                 case (short)TERMMONLib.TermMonEvent.TerminalBootLoaderVersion:
                     textBoxBootRomVersion.Text = axTermMon.TerminalBootLoaderVersion;
+
                     if (gui) textBoxBootRomVersion.Text = axTermMon.TerminalBootLoaderVersion;
 +
                    else
 +
                    {
 +
                        if (args.Contains("terminalbootloaderversion"))
 +
                        {
 +
                            textOut = textOut + "Terminal Boot Loader Version: " + axTermMon.TerminalBootLoaderVersion + Environment.NewLine;
 +
                            CommandCount--;
 +
                        }
 +
                    }
 
                     break;
 
                     break;
 
                 case (short)TERMMONLib.TermMonEvent.TerminalModel:
 
                 case (short)TERMMONLib.TermMonEvent.TerminalModel:
                     textBoxModel.Text = axTermMon.TerminalModel;
+
                     if (gui) textBoxModel.Text = axTermMon.TerminalModel;
 +
                    else
 +
                    {
 +
                        if (args.Contains("terminalmodel"))
 +
                        {
 +
                            textOut = textOut + "Terminal Model: " + axTermMon.TerminalModel + Environment.NewLine;
 +
                            CommandCount--;
 +
                        }
 +
                    }
 
                     break;
 
                     break;
 
                 case (short)TERMMONLib.TermMonEvent.TerminalWindowsUsername:
 
                 case (short)TERMMONLib.TermMonEvent.TerminalWindowsUsername:
                     textBoxTermWinUsername.Text = axTermMon.TerminalWindowsUsername;
+
                     if (gui) textBoxTermWinUsername.Text = axTermMon.TerminalWindowsUsername;
 +
                    else
 +
                    {
 +
                        if (args.Contains("terminalwindowsusername"))
 +
                        {
 +
                            textOut = textOut + "Windows Username: " + axTermMon.TerminalWindowsUsername + Environment.NewLine;
 +
                            CommandCount--;
 +
                        }
 +
                    }
 
                     break;
 
                     break;
 
                 case (short)TERMMONLib.TermMonEvent.TerminalServerGroupList:
 
                 case (short)TERMMONLib.TermMonEvent.TerminalServerGroupList:
                     textBoxTSGroupList.Text = axTermMon.TerminalServerGroupList;
+
                     if (gui) textBoxTSGroupList.Text = axTermMon.TerminalServerGroupList;
 +
                    else
 +
                    {
 +
                        if (args.Contains("TerminalServerGroupList"))
 +
                        {
 +
                            textOut = textOut + "[[Display Clients]]: " + axTermMon.TerminalServerGroupList + Environment.NewLine;
 +
                            CommandCount--;
 +
                        }
 +
                    }
 
                     break;
 
                     break;
 
                 case (short)TERMMONLib.TermMonEvent.CurrentTerminalServerGroup:
 
                 case (short)TERMMONLib.TermMonEvent.CurrentTerminalServerGroup:
                     textBoxCurrentTSGroup.Text = axTermMon.CurrentTerminalServerGroup;
+
                     if (gui) textBoxCurrentTSGroup.Text = axTermMon.CurrentTerminalServerGroup;
 +
                    else
 +
                    {
 +
                        if (args.Contains("CurrentTerminalServerGroup"))
 +
                        {
 +
                            textOut = textOut + "Current [[Display Client]]: " + axTermMon.CurrentTerminalServerGroup + Environment.NewLine;
 +
                            CommandCount--;
 +
                        }
 +
                    }
 +
                    break;
 +
                case (short)TERMMONLib.TermMonEvent.CurrentWindowsUsername:
 +
                    if (gui) textBoxSessionWinUsername.Text = axTermMon.CurrentWindowsUsername;
 +
                    else
 +
                    {
 +
                        if (args.Contains("CurrentWindowsUsername"))
 +
                        {
 +
                            textOut = textOut + "Current Windows Username: " + axTermMon.CurrentWindowsUsername + Environment.NewLine;
 +
                            CommandCount--;
 +
                        }
 +
                    }
 +
                    break;
 +
                case (short)TERMMONLib.TermMonEvent.TerminalServerName:
 +
                    if (gui) textBoxTermServerName.Text = axTermMon.TerminalServerName;
 +
                    else
 +
                    {
 +
                        if (args.Contains("TerminalServerName"))
 +
                        {
 +
                            textOut = textOut + "Terminal Server: " + axTermMon.TerminalServerName + Environment.NewLine;
 +
                            CommandCount--;
 +
                        }
 +
                    }
 
                     break;
 
                     break;
                 case (short)TERMMONLib.TermMonEvent.ConnectionState:
+
                 case (short)TERMMONLib.TermMonEvent.UserID:
                     if (axTermMon.ConnectionState == (short)TERMMONLib.TermMonConst.Connected)
+
                     if (gui) textBoxTSUserID.Text = axTermMon.UserID;
 +
                    else
 
                     {
 
                     {
                         textBoxConnectionState.Text = "Connected";
+
                         if (args.Contains("UserID"))
                         textBoxConnectionState.BackColor = System.Drawing.Color.Lime;
+
                         {
                        timerWaitForConnection.Enabled = false;
+
                            textOut = textOut + "User ID: " + axTermMon.UserID + Environment.NewLine;
                         ChangeEnabled(true);
+
                            CommandCount--;
                        if (checkBoxRunInNonSession.Checked) groupBoxWatchdog.Enabled = false;
+
                         }
 
                     }
 
                     }
                     else if (axTermMon.ConnectionState == (short)TERMMONLib.TermMonConst.Disconnected)
+
                     break;
 +
                case (short)TERMMONLib.TermMonEvent.TermSecureUsername:
 +
                    if (gui) textBoxTSUsername.Text = axTermMon.TermSecureUsername;
 +
                    else
 
                     {
 
                     {
                         textBoxConnectionState.Text = "Disconnected";
+
                         if (args.Contains("TermSecureUsername"))
                         textBoxConnectionState.BackColor = System.Drawing.Color.LightGray;
+
                         {
                         ChangeEnabled(false);
+
                            textOut = textOut + "TermSecure Username: " + axTermMon.TermSecureUsername + Environment.NewLine;
 +
                            CommandCount--;
 +
                         }
 
                     }
 
                     }
                     else if (axTermMon.ConnectionState == (short)TERMMONLib.TermMonConst.Fail)
+
                     break;
 +
                case (short)TERMMONLib.TermMonEvent.TermSecureWindowsUsername:
 +
                    if (gui) textBoxTSWinUsername.Text = axTermMon.TermSecureWindowsUsername;
 +
                    else
 
                     {
 
                     {
                         textBoxConnectionState.Text = "Failed";
+
                         if (args.Contains("TermSecureWindowsUsername"))
                         textBoxConnectionState.BackColor = System.Drawing.Color.Red;
+
                         {
                         ChangeEnabled(false);
+
                            textOut = textOut + "TermSecure Windows Username: " + axTermMon.TermSecureWindowsUsername + Environment.NewLine;
 +
                            CommandCount--;
 +
                         }
 
                     }
 
                     }
 
                     break;
 
                     break;
                 case (short)TERMMONLib.TermMonEvent.CurrentWindowsUsername:
+
                 case (short)TERMMONLib.TermMonEvent.RelevanceLocationName:
                     textBoxSessionWinUsername.Text = axTermMon.CurrentWindowsUsername;
+
                     if (gui) textBoxLocationName.Text = axTermMon.RelevanceLocationName;
 +
                    else
 +
                    {
 +
                        if (args.Contains("RelevanceLocationName"))
 +
                        {
 +
                            textOut = textOut + "Location Name: " + axTermMon.RelevanceLocationName + Environment.NewLine;
 +
                            CommandCount--;
 +
                        }
 +
                    }
 
                     break;
 
                     break;
                 case (short)TERMMONLib.TermMonEvent.TerminalServerName:
+
                 case (short)TERMMONLib.TermMonEvent.ScanResult:
                     textBoxTermServerName.Text = axTermMon.TerminalServerName;
+
                     if (gui) textBoxScanResponse.Text = axTermMon.ScanResult;
 +
                    else
 +
                    {
 +
                        if (args.Contains("ScanResult"))
 +
                        {
 +
                            textOut = textOut + "Scan Result: " + axTermMon.ScanResult + Environment.NewLine;
 +
                            CommandCount--;
 +
                        }
 +
                    }
 
                     break;
 
                     break;
                 case (short)TERMMONLib.TermMonEvent.UserID:
+
                 case (short)TERMMONLib.TermMonEvent.BiometricData:
                     textBoxTSUserID.Text = axTermMon.UserID;
+
                     textBoxBiometricData.Text = ConvertStringToHex(axTermMon.BiometricData);
 +
                    labelBiometricCount.Text = Convert.ToString(Convert.ToInt32(labelBiometricCount.Text) + 1);
 
                     break;
 
                     break;
                 case (short)TERMMONLib.TermMonEvent.TermSecureUsername:
+
                 case (short)TERMMONLib.TermMonEvent.BiometricLookupResult:
                     textBoxTSUsername.Text = axTermMon.TermSecureUsername;
+
                     textBoxBiometricResult.Text = ResultLookup(axTermMon.BiometricLookupResult);
 
                     break;
 
                     break;
                 case (short)TERMMONLib.TermMonEvent.TermSecureWindowsUsername:
+
                 case (short)TERMMONLib.TermMonEvent.BiometricLookupUsername:
                     textBoxTSWinUsername.Text = axTermMon.TermSecureWindowsUsername;
+
                     textBoxBiometricUsername.Text = axTermMon.BiometricLookupUsername;
 
                     break;
 
                     break;
 
                 case (short)TERMMONLib.TermMonEvent.WatchdogTime:
 
                 case (short)TERMMONLib.TermMonEvent.WatchdogTime:
                     labelWatchdogCount.Text = axTermMon.WatchdogTime.ToString();
+
                     if (gui) labelWatchdogCount.Text = axTermMon.WatchdogTime.ToString();
 +
                    else
 +
                    {
 +
                        if (args.Contains("WatchdogTime"))
 +
                        {
 +
                            textOut = textOut + "[[Watchdog]] Time: " + axTermMon.WatchdogTime.ToString() + Environment.NewLine;
 +
                            CommandCount--;
 +
                        }
 +
                    }
 
                     break;
 
                     break;
 
                 default:
 
                 default:
 
                     break;
 
                     break;
 
             }
 
             }
 +
            if (CommandCount == 0 && !gui) Exit();
 
         }
 
         }
 +
 +
        private void Exit()
 +
        {
 +
            Console.WriteLine(textOut);
 +
            if (path.Length > 0)
 +
            {
 +
                textOut = Environment.NewLine + DateTime.Now.ToString() + Environment.NewLine + textOut;
 +
                if (!File.Exists(path))
 +
                {
 +
                    // Create a file to write to if it doesn't already exist.
 +
                    File.WriteAllText(path, textOut);
 +
                }
 +
                else
 +
                {
 +
                    // Add to existing file
 +
                    File.AppendAllText(path, textOut);
 +
                }
 +
            }
 +
            System.Environment.Exit(0);
 +
        }
 +
 +
        // (All functions below) Handles button and check box clicks.
  
 
         private void buttonCalibrate_Click(object sender, EventArgs e)
 
         private void buttonCalibrate_Click(object sender, EventArgs e)
Line 379: Line 775:
 
         private void buttonCamMove_Click(object sender, EventArgs e)
 
         private void buttonCamMove_Click(object sender, EventArgs e)
 
         {
 
         {
 +
            if (textBoxCamX.Text.Length == 0) textBoxCamX.Text = "100";
 +
            if (textBoxCamY.Text.Length == 0) textBoxCamY.Text = "100";
 
             axTermMon.CameraOverlayMove(textBoxCamGroupName.Text, textBoxCamOverlayName.Text, short.Parse(textBoxCamX.Text), short.Parse(textBoxCamY.Text));
 
             axTermMon.CameraOverlayMove(textBoxCamGroupName.Text, textBoxCamOverlayName.Text, short.Parse(textBoxCamX.Text), short.Parse(textBoxCamY.Text));
 
         }
 
         }
Line 384: Line 782:
 
         private void buttonCamResize_Click(object sender, EventArgs e)
 
         private void buttonCamResize_Click(object sender, EventArgs e)
 
         {
 
         {
 +
            if (textBoxCamW.Text.Length == 0) textBoxCamW.Text = "100";
 +
            if (textBoxCamH.Text.Length == 0) textBoxCamH.Text = "100";
 
             axTermMon.CameraOverlayResize(textBoxCamGroupName.Text, textBoxCamOverlayName.Text, short.Parse(textBoxCamW.Text), short.Parse(textBoxCamH.Text));
 
             axTermMon.CameraOverlayResize(textBoxCamGroupName.Text, textBoxCamOverlayName.Text, short.Parse(textBoxCamW.Text), short.Parse(textBoxCamH.Text));
 
         }
 
         }
Line 389: Line 789:
 
         private void buttonCamMoveResize_Click(object sender, EventArgs e)
 
         private void buttonCamMoveResize_Click(object sender, EventArgs e)
 
         {
 
         {
 +
            if (textBoxCamX.Text.Length == 0) textBoxCamX.Text = "100";
 +
            if (textBoxCamY.Text.Length == 0) textBoxCamY.Text = "100";
 +
            if (textBoxCamW.Text.Length == 0) textBoxCamW.Text = "100";
 +
            if (textBoxCamH.Text.Length == 0) textBoxCamH.Text = "100";
 
             axTermMon.CameraOverlayResizeMove(textBoxCamGroupName.Text, textBoxCamOverlayName.Text, short.Parse(textBoxCamX.Text), short.Parse(textBoxCamY.Text), short.Parse(textBoxCamW.Text), short.Parse(textBoxCamH.Text));
 
             axTermMon.CameraOverlayResizeMove(textBoxCamGroupName.Text, textBoxCamOverlayName.Text, short.Parse(textBoxCamX.Text), short.Parse(textBoxCamY.Text), short.Parse(textBoxCamW.Text), short.Parse(textBoxCamH.Text));
 
         }
 
         }
Line 426: Line 830:
 
         private void buttonSetActiveScreen_Click(object sender, EventArgs e)
 
         private void buttonSetActiveScreen_Click(object sender, EventArgs e)
 
         {
 
         {
 +
            // Checks to make sure the screen number given is valid. 0 through 5.
 
             short i = -1;
 
             short i = -1;
 
             if (textBoxSetActiveScreen.Text.Length > 0) i = short.Parse(textBoxSetActiveScreen.Text);
 
             if (textBoxSetActiveScreen.Text.Length > 0) i = short.Parse(textBoxSetActiveScreen.Text);
Line 434: Line 839:
 
             else
 
             else
 
             {
 
             {
                 MessageBox.Show("To set active screet use 0 through 5");
+
                 MessageBox.Show("To set active screen use 0 through 5");
 
             }
 
             }
 
         }
 
         }
Line 440: Line 845:
 
         private void buttonWatchdogSet_Click(object sender, EventArgs e)
 
         private void buttonWatchdogSet_Click(object sender, EventArgs e)
 
         {
 
         {
 +
            // checks to make sure the [[watchdog]] time given is valid.
 
             if (short.Parse(textBoxWatchdogSeconds.Text) > 32767)
 
             if (short.Parse(textBoxWatchdogSeconds.Text) > 32767)
 
             {
 
             {
Line 470: Line 876:
 
                 checkBoxTSDisconnectUserSessions.Checked = true;
 
                 checkBoxTSDisconnectUserSessions.Checked = true;
 
             }
 
             }
 +
        }
 +
 +
        private void buttonScanQuery_Click(object sender, EventArgs e)
 +
        {
 +
            short i;
 +
            textBoxScanResponse.Text = "Requesting Scan...";
 +
            textBoxScanResponse.Update();
 +
            i = axTermMon.Command((short)TERMMONLib.TermMonCommand.ScanCodeAndQueryLocation);
 +
            if (i != (short)TERMMONLib.TermMonConst.Success)
 +
                textBoxScanResponse.Text = "Request Failed";
 +
        }
 +
 +
        private void buttonLocLogon_Click(object sender, EventArgs e)
 +
        {
 +
            short i;
 +
 +
            if (comboLocAction.Text == "transfer")
 +
                i = (short)TERMMONLib.TermMonRelevance.ActionTransfer;
 +
            else if (comboLocAction.Text == "clone")
 +
                i = (short )TERMMONLib.TermMonRelevance.ActionClone;
 +
            else if (comboLocAction.Text == "shadow")
 +
                i = (short)TERMMONLib.TermMonRelevance.ActionShadow;
 +
            else
 +
                i = (short)TERMMONLib.TermMonRelevance.ActionNone;
 +
 +
            textBoxScanResponse.Text = "Requesting Logon...";
 +
            textBoxScanResponse.Update();
 +
            textBoxScanResponse.Text = ResultLookup(axTermMon.RelevanceLocationLogon(textBoxLocPathName.Text, i, 1));
 +
        }
 +
 +
        private void buttonScanData_Click(object sender, EventArgs e)
 +
        {
 +
            short i;
 +
            textBoxScanResponse.Text = "Requesting Scan...";
 +
            textBoxScanResponse.Update();
 +
            i = axTermMon.Command((short)TERMMONLib.TermMonCommand.ScanCodeReturnData);
 +
            if (i != (short)TERMMONLib.TermMonConst.Success)
 +
                textBoxScanResponse.Text = "Request Failed";
 +
        }
 +
 +
        private void buttonLocLogoff_Click(object sender, EventArgs e)
 +
        {
 +
            textBoxScanResponse.Text = "Requesting Logoff...";
 +
            textBoxScanResponse.Update();
 +
            textBoxScanResponse.Text = ResultLookup(axTermMon.RelevanceLocationLogoff(1));
 +
        }
 +
 +
        private void buttonBiometricScanAndQuery_Click(object sender, EventArgs e)
 +
        {
 +
            short i;
 +
            textBoxBiometricResult.Text = "Requesting Scan...";
 +
            textBoxBiometricUsername.Text = "";
 +
            i = axTermMon.Command((short)TERMMONLib.TermMonCommand.ScanBiometricAndQueryUser);
 +
            if (i != (short)TERMMONLib.TermMonConst.Success)
 +
                textBoxBiometricResult.Text = "Request Failed";
 
         }
 
         }
 
     }
 
     }
 
}
 
}
 
</pre>
 
</pre>

Latest revision as of 19:54, 16 February 2015

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;

namespace TermMon_Demo
{
    public enum CommandConst
    {
        TermSecureCheckAccess = -2,
        ChangeTerminalServerGroup = -1,
    }

    public partial class Form1 : Form
    {
        String[] args = Environment.GetCommandLineArgs().Select(s => s.ToLowerInvariant()).ToArray();
        String[] HelpList = { "-h", "/h", "-?", "/?", "help", "-help", "/help" };
        String[] CommandList = { "reboot", "restart", "calibrate", "gotomainmenu", "switchtonextgroup", "switchtoprevgroup", "switchinstfailover", "changetermsecureuser", "logoffandchangetermsecureuser", "logofftermsecureuser", "disconnecttermsecureuser" };
        int CommandCount = 0;
        string path = "", textOut = Environment.NewLine;
        bool gui = true;

        public Form1()
        {
            InitializeComponent();

            if (args.Length > 1) gui = false;

            if (gui)
            {
                // Enable the OverrideIP and Demo Control setions
                EnableControl(true);
                ChangeEnabled(false);

                // Set the connection state display to disconnected
                textBoxConnectionState.Text = "Disconnected";
                textBoxConnectionState.BackColor = System.Drawing.Color.LightGray;

                // Get and display prigram version info
                Assembly a = Assembly.GetExecutingAssembly();
                FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(a.Location);
                labelVersion.Text = "Version " + fvi.ProductVersion;
            }
            else
            {
                CommandCount = args.Length - 1;

                ParseArgs();

                axTermMon.Enable();

                timerConnectionTimeout.Enabled = true;
                timerRuntime.Enabled = true;
            }
        }

        // Parse any command line arguments.
        private void ParseArgs()
        {
            for (int i = 0; i < args.Length; i++)
            {
                if (CheckValidIP(args[i]))
                {
                    axTermMon.RunInSession = false;
                    axTermMon.OverrideIP = args[i];
                    CommandCount--;
                }

                if (args[i].EndsWith(".txt"))
                {
                    path = args[i];
                    CommandCount--;
                }
            }

            if (args.Intersect(HelpList).Count() > 0) Help();
        }

        private void Help()
        {
            textOut = Environment.NewLine + "Usage:" + Environment.NewLine + Environment.NewLine
            + "TermMon.exe [IPv4 Address (optional)] [Filename and Path (optional)] [Commands]" + Environment.NewLine + Environment.NewLine
            + "Local Example: TermMon.exe TerminalName TerminalMAC"+ Environment.NewLine
            + "Remote Example: TermMon.exe xxx.xxx.xxx.xxx C:\termmon.txt TerminalName TerminalMAC" + Environment.NewLine + Environment.NewLine
            + "Arguments, Information:" + Environment.NewLine
            + "\tConnectionState" + Environment.NewLine
            + "\tTerminalName" + Environment.NewLine
            + "\tTerminalIP" + Environment.NewLine
            + "\tTerminalMAC" + Environment.NewLine
            + "\tTerminalModel" + Environment.NewLine
            + "\tTerminalFirmwareVersion" + Environment.NewLine
            + "\tTerminalBootloaderVersion" + Environment.NewLine
            + "\tTerminalWindowsUsername" + Environment.NewLine
            + "\tTerminalServerGroupList" + Environment.NewLine
            + "\tCurrentTerminalServerGroup" + Environment.NewLine
            + "\tCurrentWindowsUsername" + Environment.NewLine
            + "\tUserID" + Environment.NewLine
            + "\tTermSecureUsername" + Environment.NewLine
            + "\tTermSecureWindowsUsername" + Environment.NewLine
            + "\tWatchdogTime" + Environment.NewLine
            + "Arguments, Commands" + Environment.NewLine
            + "\tReboot" + Environment.NewLine
            + "\tRestart" + Environment.NewLine 
            + "\tCalibrate" + Environment.NewLine 
            + "\tGoToMainMenu" + Environment.NewLine 
            + "\tSwitchToNextGroup" + Environment.NewLine 
            + "\tSwitchToPrevGroup" + Environment.NewLine 
            + "\tSwitchInstFailover" + Environment.NewLine 
            + "\tChangeTermSecureUser" + Environment.NewLine 
            + "\tLogOffAndChangeTermSecureUser" + Environment.NewLine 
            + "\tLogOffTermSucureUser" + Environment.NewLine 
            + "\tDisconnectTermSecureUser" + Environment.NewLine
            + "\tChangeTerminalServerGroup" + Environment.NewLine
            + "\tTermSecureCheckAccess" + Environment.NewLine;

            Exit();
        }

        // Enable 3 second timer after the demo has been enabled.
        // This provides a timeout in case it can't conenct to the control.
        private void WaitForConnection()
        {
            timerWaitForConnection.Enabled = true;
        }

        // Enables/disables the OverrideIP and demo control sections.
        private void EnableControl(bool enabled)
        {
            groupBoxDemoControl.Enabled = enabled;
            groupBoxOverrideIP.Enabled = enabled;
        }

        // Enables/disables all other sections.
        // These sections are only available when connected to the control
        private void ChangeEnabled(bool enabled)
        {
            groupBoxTerminalInfo.Enabled = enabled;
            groupBoxTerminalControl.Enabled = enabled;
            groupBoxSessionControl.Enabled = enabled;
            groupBoxNavigation.Enabled = enabled;
            groupBoxGetDisplayClientScreen.Enabled = enabled;
            groupBoxTermSecure.Enabled = enabled;
            groupBoxCameraOverlay.Enabled = enabled;
            groupBoxActiveScreen.Enabled = enabled;
            groupBoxLocation.Enabled = enabled;
            groupBoxWatchdog.Enabled = enabled;
            groupBoxBiometric.Enabled = enabled;
            buttonEnable.Enabled = !enabled;
            buttonDisable.Enabled = enabled;
        }

        // Checks that the IP address in the OverrideIP section is a valid format
        private bool CheckValidIP(string IPAddress)
        {
            System.Net.IPAddress address;
            if (System.Net.IPAddress.TryParse(IPAddress, out address))
            {
                if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) return true;
            }
            if (gui) MessageBox.Show("Please use a valid (xxx.xxx.xxx.xxx) IPv4 address");
            return false;
        }

        // returns the corresponding string for a result code.
        private string ResultLookup(int res)
        {
            switch(res)
            {
                case (short)TERMMONLib.TermMonConst.Busy:
                    return "Busy";
                case (short)TERMMONLib.TermMonConst.Connected:
                    return "Connected";
                case (short)TERMMONLib.TermMonConst.Disconnected:
                    return "Disconnected";
                case (short)TERMMONLib.TermMonConst.Fail:
                    return "Failed";
                case (short)TERMMONLib.TermMonConst.GroupNotFound:
                    return "Group Not Found";
                case (short)TERMMONLib.TermMonConst.InvalidMember:
                    return "Invalid Member";
                case (short)TERMMONLib.TermMonConst.RequestFailed:
                    return "Request Failed";
                case (short)TERMMONLib.TermMonConst.Success:
                    return "Success";
                case (short)TERMMONLib.TermMonConst.Timeout:
                    return "Timeout";
                case (short)TERMMONLib.TermMonConst.Updating:
                    return "Updating";
                case (short)TERMMONLib.TermMonConst.UserNotFound:
                    return "User Not Found";
                case (short)TERMMONLib.TermMonConst.ValidMember:
                    return "Valid Member";
                case (short)TERMMONLib.TermMonConst.BadPassword:
                    return "Bad Password";
                case (short)TERMMONLib.TermMonConst.NoPermission:
                    return "No Permission";
                case (short)TERMMONLib.TermMonConst.NoWindowsPassword:
                    return "No Windows Password";
                case (short)TERMMONLib.TermMonConst.NoWindowsUsername:
                    return "No Windows Username";
                case (short)TERMMONLib.TermMonConst.PasswordChangeReq:
                    return "Password Change Required";
                default:
                    return "Error: Other";
            }
        }

        // 3 second timer for connection timeout.
        private void timerWaitForConnection_Tick(object sender, EventArgs e)
        {
            axTermMon.Disable();
            timerWaitForConnection.Enabled = false;

            string message = "";
            string location = "this terminal";
            if (axTermMon.RunInSession == false)
            {
                location = axTermMon.OverrideIP;
                message = "\n- Is 'Only allow connections from session' set to 'NO' in the module?";
            }
            message = "The demo application could not connect to the ActiveX control on " + location + "\n\n- Check that TermMon.ocx is registered on the terminal server.\n- Check that the terminal has the TermMon ActiveX module" + message;

            int res = (int)MessageBox.Show(message, "TermMon ActiveX Demo", MessageBoxButtons.RetryCancel, MessageBoxIcon.Asterisk);

            if (res == 4)
            {
                WaitForConnection();
            }
            else
            {
                EnableControl(true);
            }
        }

        private void timerConnectionTimeout_Tick(object sender, EventArgs e)
        {
            Console.WriteLine("Connection Failed");
            Application.Exit();
        }

        private void timerRuntime_Tick(object sender, EventArgs e)
        {
            Console.WriteLine("Failed to complete all commands");
            Application.Exit();
        }

        private void timerRunCommands_Tick(object sender, EventArgs e)
        {
            timerRunCommands.Enabled = false;
            ProcessCommands();
        }

        private void ProcessCommands()
        {
            var Commands = args.Intersect(CommandList);

            foreach (string command in Commands)
            {
                axTermMon.Command(GetCommandCode(command));
                if (--CommandCount == 0)
                {
                    Application.Exit();
                }
            }

            ProcessSpecial();
        }

        private void ProcessSpecial()
        {
            string[] Special = { "changeterminalservergroup", "termsecurecheckaccess" };
            string data = "";

            var Commands = args.Intersect(Special);

            foreach (string command in Commands)
            {
                CommandCount--;
                int i = Array.IndexOf(args, command);

                if (i < args.Length - 1 && i != -1) data = args[i + 1].ToLower();
                else
                {
                    Console.WriteLine("That command requires data. Command Data");
                    break;
                }

                if (String.Compare(command, "termsecurecheckaccess") == 0)
                {
                    string[] TSdata = data.Split(',');
                    if (TSdata.Length == 2)
                    {
                        Console.WriteLine(ResultLookup(axTermMon.TermSecureCheckAccess(TSdata[0], TSdata[1])));
                    }
                    else
                    {
                        Console.WriteLine("Use 'username,group' to check TermSecure access");
                    }
                }
                else
                {
                    axTermMon.ChangeTerminalServerGroup(data);
                }
            }
            if (CommandCount == 0) Application.Exit();
        }

        private short GetCommandCode(string command)
        {
            switch (command)
            {
                case "reboot":
                    return (short)TERMMONLib.TermMonCommand.Reboot;
                case "restart":
                    return (short)TERMMONLib.TermMonCommand.Restart;
                case "calibrate":
                    return (short)TERMMONLib.TermMonCommand.Calibrate;
                case "gotomainmenu":
                    return (short)TERMMONLib.TermMonCommand.GotoMainMenu;
                case "swtichtonextgroup":
                    return (short)TERMMONLib.TermMonCommand.SwitchToNextGroup;
                case "switchtopreviousgroup":
                    return (short)TERMMONLib.TermMonCommand.SwitchToPrevGroup;
                case "switchinstantfailover":
                    return (short)TERMMONLib.TermMonCommand.SwitchInstFailover;
                case "changetermsecureuser":
                    return (short)TERMMONLib.TermMonCommand.ChangeTermSecureUser;
                case "loggoffandchangetermsecureuser":
                    return (short)TERMMONLib.TermMonCommand.LogOffAndChangeTermSecureUser;
                case "logofftermsecureuser":
                    return (short)TERMMONLib.TermMonCommand.LogOffTermSecureUser;
                case "disconnecttermsecureuser":
                    return (short)TERMMONLib.TermMonCommand.DisconnectTermSecureUser;
                default:
                    return 0;
            }
        }

        public string ConvertStringToHex(string asciiString)
        {
            string hex = "";
            foreach (char c in asciiString)
            {
                hex += String.Format("{0:x2} ", Convert.ToString(c, 16));
            }
            return hex;
        }

        // Updates demo fields when an event occurs
        private void axTermMon_OnEvent(object sender, AxTERMMONLib._DTermMonEvents_OnEventEvent e)
        {
            switch (e.eventCode)
            {
                case (short)TERMMONLib.TermMonEvent.ConnectionState:
                    if (gui)
                    {
                        if (axTermMon.ConnectionState == (short)TERMMONLib.TermMonConst.Connected)
                        {
                            textBoxConnectionState.Text = "Connected";
                            textBoxConnectionState.BackColor = System.Drawing.Color.Lime;
                            timerWaitForConnection.Enabled = false;
                            // Enable all sections of the demo
                            ChangeEnabled(true);
                            // disable the [[watchdog]] controls if not running on the client
                            if (checkBoxRunInNonSession.Checked)
                            {
                                groupBoxWatchdog.Enabled = false;
                                groupBoxSessionControl.Enabled = false;
                            }
                        }
                        else if (axTermMon.ConnectionState == (short)TERMMONLib.TermMonConst.Disconnected)
                        {
                            textBoxConnectionState.Text = "Disconnected";
                            textBoxConnectionState.BackColor = System.Drawing.Color.LightGray;
                            ChangeEnabled(false);
                        }
                        else if (axTermMon.ConnectionState == (short)TERMMONLib.TermMonConst.Fail)
                        {
                            textBoxConnectionState.Text = "Failed";
                            textBoxConnectionState.BackColor = System.Drawing.Color.Red;
                            ChangeEnabled(false);
                        }
                    }
                    else
                    {
                        timerConnectionTimeout.Enabled = false;
                        timerRunCommands.Enabled = true;
                        if (args.Contains("ConnectionState"))
                        {
                            textOut = textOut + "Connection State: " + "Connected" + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.TerminalName:
                    if (gui) textBoxName.Text = axTermMon.TerminalName;
                    else
                    {
                        if (args.Contains("terminalname"))
                        {
                            textOut = textOut + "Terminal Name: " + axTermMon.TerminalName + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.TerminalIP:
                    if (gui) textBoxIPAddress.Text = axTermMon.TerminalIP;
                    else
                    {
                        if (args.Contains("terminalip"))
                        {
                            textOut = textOut + "Terminal IP: " + axTermMon.TerminalIP + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.TerminalMAC:
                    if (gui) textBoxMACAddress.Text = axTermMon.TerminalMAC;
                    else
                    {
                        if (args.Contains("terminalmac"))
                        {
                            textOut = textOut + "Terminal MAC: " + axTermMon.TerminalMAC + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.TerminalFirmwareVersion:
                    if (gui) textBoxFirmwareVersion.Text = axTermMon.TerminalFirmwareVersion;
                    else
                    {
                        if (args.Contains("terminalfirmwareversion"))
                        {
                            textOut = textOut + "Terminal Firmware Version: " + axTermMon.TerminalFirmwareVersion + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.TerminalBootLoaderVersion:
                    if (gui) textBoxBootRomVersion.Text = axTermMon.TerminalBootLoaderVersion;
                    else
                    {
                        if (args.Contains("terminalbootloaderversion"))
                        {
                            textOut = textOut + "Terminal Boot Loader Version: " + axTermMon.TerminalBootLoaderVersion + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.TerminalModel:
                    if (gui) textBoxModel.Text = axTermMon.TerminalModel;
                    else
                    {
                        if (args.Contains("terminalmodel"))
                        {
                            textOut = textOut + "Terminal Model: " + axTermMon.TerminalModel + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.TerminalWindowsUsername:
                    if (gui) textBoxTermWinUsername.Text = axTermMon.TerminalWindowsUsername;
                    else
                    {
                        if (args.Contains("terminalwindowsusername"))
                        {
                            textOut = textOut + "Windows Username: " + axTermMon.TerminalWindowsUsername + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.TerminalServerGroupList:
                    if (gui) textBoxTSGroupList.Text = axTermMon.TerminalServerGroupList;
                    else
                    {
                        if (args.Contains("TerminalServerGroupList"))
                        {
                            textOut = textOut + "[[Display Clients]]: " + axTermMon.TerminalServerGroupList + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.CurrentTerminalServerGroup:
                    if (gui) textBoxCurrentTSGroup.Text = axTermMon.CurrentTerminalServerGroup;
                    else
                    {
                        if (args.Contains("CurrentTerminalServerGroup"))
                        {
                            textOut = textOut + "Current [[Display Client]]: " + axTermMon.CurrentTerminalServerGroup + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.CurrentWindowsUsername:
                    if (gui) textBoxSessionWinUsername.Text = axTermMon.CurrentWindowsUsername;
                    else
                    {
                        if (args.Contains("CurrentWindowsUsername"))
                        {
                            textOut = textOut + "Current Windows Username: " + axTermMon.CurrentWindowsUsername + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.TerminalServerName:
                    if (gui) textBoxTermServerName.Text = axTermMon.TerminalServerName;
                    else
                    {
                        if (args.Contains("TerminalServerName"))
                        {
                            textOut = textOut + "Terminal Server: " + axTermMon.TerminalServerName + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.UserID:
                    if (gui) textBoxTSUserID.Text = axTermMon.UserID;
                    else
                    {
                        if (args.Contains("UserID"))
                        {
                            textOut = textOut + "User ID: " + axTermMon.UserID + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.TermSecureUsername:
                    if (gui) textBoxTSUsername.Text = axTermMon.TermSecureUsername;
                    else
                    {
                        if (args.Contains("TermSecureUsername"))
                        {
                            textOut = textOut + "TermSecure Username: " + axTermMon.TermSecureUsername + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.TermSecureWindowsUsername:
                    if (gui) textBoxTSWinUsername.Text = axTermMon.TermSecureWindowsUsername;
                    else
                    {
                        if (args.Contains("TermSecureWindowsUsername"))
                        {
                            textOut = textOut + "TermSecure Windows Username: " + axTermMon.TermSecureWindowsUsername + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.RelevanceLocationName:
                    if (gui) textBoxLocationName.Text = axTermMon.RelevanceLocationName;
                    else
                    {
                        if (args.Contains("RelevanceLocationName"))
                        {
                            textOut = textOut + "Location Name: " + axTermMon.RelevanceLocationName + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.ScanResult:
                    if (gui) textBoxScanResponse.Text = axTermMon.ScanResult;
                    else
                    {
                        if (args.Contains("ScanResult"))
                        {
                            textOut = textOut + "Scan Result: " + axTermMon.ScanResult + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                case (short)TERMMONLib.TermMonEvent.BiometricData:
                    textBoxBiometricData.Text = ConvertStringToHex(axTermMon.BiometricData);
                    labelBiometricCount.Text = Convert.ToString(Convert.ToInt32(labelBiometricCount.Text) + 1);
                    break;
                case (short)TERMMONLib.TermMonEvent.BiometricLookupResult:
                    textBoxBiometricResult.Text = ResultLookup(axTermMon.BiometricLookupResult);
                    break;
                case (short)TERMMONLib.TermMonEvent.BiometricLookupUsername:
                    textBoxBiometricUsername.Text = axTermMon.BiometricLookupUsername;
                    break;
                case (short)TERMMONLib.TermMonEvent.WatchdogTime:
                    if (gui) labelWatchdogCount.Text = axTermMon.WatchdogTime.ToString();
                    else
                    {
                        if (args.Contains("WatchdogTime"))
                        {
                            textOut = textOut + "[[Watchdog]] Time: " + axTermMon.WatchdogTime.ToString() + Environment.NewLine;
                            CommandCount--;
                        }
                    }
                    break;
                default:
                    break;
            }
            if (CommandCount == 0 && !gui) Exit();
        }

        private void Exit()
        {
            Console.WriteLine(textOut);
            if (path.Length > 0)
            {
                textOut = Environment.NewLine + DateTime.Now.ToString() + Environment.NewLine + textOut;
                if (!File.Exists(path))
                {
                    // Create a file to write to if it doesn't already exist. 
                    File.WriteAllText(path, textOut);
                }
                else
                {
                    // Add to existing file 
                    File.AppendAllText(path, textOut);
                }
            }
            System.Environment.Exit(0);
        }

        // (All functions below) Handles button and check box clicks.

        private void buttonCalibrate_Click(object sender, EventArgs e)
        {
            axTermMon.Command((short)TERMMONLib.TermMonCommand.Calibrate);
        }

        private void buttonReboot_Click(object sender, EventArgs e)
        {
            axTermMon.Command((short)TERMMONLib.TermMonCommand.Reboot);
        }

        private void buttonRestart_Click(object sender, EventArgs e)
        {
            axTermMon.Command((short)TERMMONLib.TermMonCommand.Restart);
        }

        private void buttonDisconnect_Click(object sender, EventArgs e)
        {
            axTermMon.Command((short)TERMMONLib.TermMonCommand.DisconnectSession);
        }

        private void buttonLogoff_Click(object sender, EventArgs e)
        {
            axTermMon.Command((short)TERMMONLib.TermMonCommand.LogOffSession);
        }

        private void buttonPrevious_Click(object sender, EventArgs e)
        {
            axTermMon.Command((short)TERMMONLib.TermMonCommand.SwitchToPrevGroup);
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            axTermMon.Command((short)TERMMONLib.TermMonCommand.SwitchToNextGroup);
        }

        private void buttonStartTiling_Click(object sender, EventArgs e)
        {
            axTermMon.Command((short)TERMMONLib.TermMonCommand.TileStart);
        }

        private void buttonStopTiling_Click(object sender, EventArgs e)
        {
            axTermMon.Command((short)TERMMONLib.TermMonCommand.TileEnd);
        }

        private void buttonMainMenu_Click(object sender, EventArgs e)
        {
            axTermMon.Command((short)TERMMONLib.TermMonCommand.GotoMainMenu);
        }

        private void buttonInstantFailover_Click(object sender, EventArgs e)
        {
            axTermMon.Command((short)TERMMONLib.TermMonCommand.SwitchInstFailover);
        }

        private void buttonGoToDisplayClient_Click(object sender, EventArgs e)
        {
            axTermMon.ChangeTerminalServerGroup(textBoxDisplayClientName.Text);
        }

        private void buttonGetScreen_Click(object sender, EventArgs e)
        {
            textBoxScreenNumber.Text = "";
            textBoxScreenNumber.Text = axTermMon.GetGroupScreen(textBoxGetDisplayClientName.Text).ToString();
        }

        private void buttonTSLogOn_Click(object sender, EventArgs e)
        {
            if (checkBoxTSLogOffUserSessions.Checked)
            {
                axTermMon.Command((short)TERMMONLib.TermMonCommand.LogOffAndChangeTermSecureUser);
            }
            else
            {
                axTermMon.Command((short)TERMMONLib.TermMonCommand.ChangeTermSecureUser);
            }
        }

        private void buttonTSLogOff_Click(object sender, EventArgs e)
        {
            if (checkBoxTSLogOffUserSessions.Checked)
            {
                axTermMon.Command((short)TERMMONLib.TermMonCommand.LogOffTermSecureUser);
            }
            else
            {
                axTermMon.Command((short)TERMMONLib.TermMonCommand.DisconnectTermSecureUser);
            }
        }

        private void buttonTSCheckAccess_Click(object sender, EventArgs e)
        {
            textBoxTSCheckAccessResponse.Text = "Updating...";
            textBoxTSCheckAccessResponse.Text = ResultLookup(axTermMon.TermSecureCheckAccess(textBoxTSCheckAccessUsername.Text, textBoxTSCheckAccessGroup.Text));
        }

        private void buttonTSLogOnUser_Click(object sender, EventArgs e)
        {
            textBoxTSLogOnResponse.Text = "Updating...";
            textBoxTSLogOnResponse.Text = ResultLookup(axTermMon.TermSecureLogonUser(textBoxTSLogOnUsername.Text, textBoxTSLogOnPassword.Text));
        }

        private void buttonCamEnable_Click(object sender, EventArgs e)
        {
            axTermMon.CameraOverlayEnable(textBoxCamGroupName.Text, textBoxCamOverlayName.Text);
        }

        private void buttonCamDisable_Click(object sender, EventArgs e)
        {
            axTermMon.CameraOverlayDisable(textBoxCamGroupName.Text, textBoxCamOverlayName.Text);
        }

        private void buttonCamCycleStart_Click(object sender, EventArgs e)
        {
            axTermMon.CameraOverlayCycleStart(textBoxCamGroupName.Text, textBoxCamOverlayName.Text);
        }

        private void buttonCamCycleStop_Click(object sender, EventArgs e)
        {
            axTermMon.CameraOverlayCycleStop(textBoxCamGroupName.Text, textBoxCamOverlayName.Text);
        }

        private void buttonCamPrevious_Click(object sender, EventArgs e)
        {
            axTermMon.CameraOverlaySwitchPrev(textBoxCamGroupName.Text, textBoxCamOverlayName.Text);
        }

        private void buttonCamNext_Click(object sender, EventArgs e)
        {
            axTermMon.CameraOverlaySwitchNext(textBoxCamGroupName.Text, textBoxCamOverlayName.Text);
        }

        private void buttonCamSwitchByName_Click(object sender, EventArgs e)
        {
            axTermMon.CameraOverlaySwitchByName(textBoxCamGroupName.Text, textBoxCamOverlayName.Text, textBoxCamName.Text);
        }

        private void buttonCamEnterFullscreen_Click(object sender, EventArgs e)
        {
            axTermMon.CameraOverlayFullscreenEnter(textBoxCamGroupName.Text, textBoxCamOverlayName.Text);
        }

        private void buttonCamExitFullscreen_Click(object sender, EventArgs e)
        {
            axTermMon.CameraOverlayFullscreenExit(textBoxCamGroupName.Text, textBoxCamOverlayName.Text);
        }

        private void buttonCamMove_Click(object sender, EventArgs e)
        {
            if (textBoxCamX.Text.Length == 0) textBoxCamX.Text = "100";
            if (textBoxCamY.Text.Length == 0) textBoxCamY.Text = "100";
            axTermMon.CameraOverlayMove(textBoxCamGroupName.Text, textBoxCamOverlayName.Text, short.Parse(textBoxCamX.Text), short.Parse(textBoxCamY.Text));
        }

        private void buttonCamResize_Click(object sender, EventArgs e)
        {
            if (textBoxCamW.Text.Length == 0) textBoxCamW.Text = "100";
            if (textBoxCamH.Text.Length == 0) textBoxCamH.Text = "100";
            axTermMon.CameraOverlayResize(textBoxCamGroupName.Text, textBoxCamOverlayName.Text, short.Parse(textBoxCamW.Text), short.Parse(textBoxCamH.Text));
        }

        private void buttonCamMoveResize_Click(object sender, EventArgs e)
        {
            if (textBoxCamX.Text.Length == 0) textBoxCamX.Text = "100";
            if (textBoxCamY.Text.Length == 0) textBoxCamY.Text = "100";
            if (textBoxCamW.Text.Length == 0) textBoxCamW.Text = "100";
            if (textBoxCamH.Text.Length == 0) textBoxCamH.Text = "100";
            axTermMon.CameraOverlayResizeMove(textBoxCamGroupName.Text, textBoxCamOverlayName.Text, short.Parse(textBoxCamX.Text), short.Parse(textBoxCamY.Text), short.Parse(textBoxCamW.Text), short.Parse(textBoxCamH.Text));
        }

        private void buttonEnable_Click(object sender, EventArgs e)
        {
            axTermMon.RunInSession = true;

            if (checkBoxRunInNonSession.Checked)
            {
                axTermMon.RunInSession = false;
                if (CheckValidIP(textBoxOverrideIPAddress.Text))
                {
                    axTermMon.OverrideIP = textBoxOverrideIPAddress.Text;
                    
                }
                else
                {
                    return;
                }
            }

            axTermMon.Enable();
            WaitForConnection();
        }

        private void buttonDisable_Click(object sender, EventArgs e)
        {
            short res = axTermMon.Disable();

            if (res == (short)TERMMONLib.TermMonConst.Fail)
            {
                MessageBox.Show("Not Enabled");
            }
        }

        private void buttonSetActiveScreen_Click(object sender, EventArgs e)
        {
            // Checks to make sure the screen number given is valid. 0 through 5.
            short i = -1;
            if (textBoxSetActiveScreen.Text.Length > 0) i = short.Parse(textBoxSetActiveScreen.Text);
            if (i >= 0 && i < 6)
            {
                axTermMon.ActiveScreen = i;
            }
            else
            {
                MessageBox.Show("To set active screen use 0 through 5");
            }
        }

        private void buttonWatchdogSet_Click(object sender, EventArgs e)
        {
            // checks to make sure the [[watchdog]] time given is valid.
            if (short.Parse(textBoxWatchdogSeconds.Text) > 32767)
            {
                textBoxWatchdogSeconds.Text = "32767";
            }
            labelWatchdogCount.Text = textBoxWatchdogSeconds.Text;
            axTermMon.WatchdogTime = short.Parse(textBoxWatchdogSeconds.Text);
        }

        private void checkBoxTSDisconnectUserSessions_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxTSDisconnectUserSessions.Checked)
            {
                checkBoxTSLogOffUserSessions.Checked = false;
            }
            else
            {
                checkBoxTSLogOffUserSessions.Checked = true;
            }
        }

        private void checkBoxTSLogOffUserSessions_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxTSLogOffUserSessions.Checked)
            {
                checkBoxTSDisconnectUserSessions.Checked = false;
            }
            else
            {
                checkBoxTSDisconnectUserSessions.Checked = true;
            }
        }

        private void buttonScanQuery_Click(object sender, EventArgs e)
        {
            short i;
            textBoxScanResponse.Text = "Requesting Scan...";
            textBoxScanResponse.Update();
            i = axTermMon.Command((short)TERMMONLib.TermMonCommand.ScanCodeAndQueryLocation);
            if (i != (short)TERMMONLib.TermMonConst.Success)
                textBoxScanResponse.Text = "Request Failed";
        }

        private void buttonLocLogon_Click(object sender, EventArgs e)
        {
            short i;

            if (comboLocAction.Text == "transfer")
                i = (short)TERMMONLib.TermMonRelevance.ActionTransfer;
            else if (comboLocAction.Text == "clone")
                i = (short )TERMMONLib.TermMonRelevance.ActionClone;
            else if (comboLocAction.Text == "shadow")
                i = (short)TERMMONLib.TermMonRelevance.ActionShadow;
            else
                i = (short)TERMMONLib.TermMonRelevance.ActionNone;

            textBoxScanResponse.Text = "Requesting Logon...";
            textBoxScanResponse.Update();
            textBoxScanResponse.Text = ResultLookup(axTermMon.RelevanceLocationLogon(textBoxLocPathName.Text, i, 1));
        }

        private void buttonScanData_Click(object sender, EventArgs e)
        {
            short i;
            textBoxScanResponse.Text = "Requesting Scan...";
            textBoxScanResponse.Update();
            i = axTermMon.Command((short)TERMMONLib.TermMonCommand.ScanCodeReturnData);
            if (i != (short)TERMMONLib.TermMonConst.Success)
                textBoxScanResponse.Text = "Request Failed";
        }

        private void buttonLocLogoff_Click(object sender, EventArgs e)
        {
            textBoxScanResponse.Text = "Requesting Logoff...";
            textBoxScanResponse.Update();
            textBoxScanResponse.Text = ResultLookup(axTermMon.RelevanceLocationLogoff(1));
        }

        private void buttonBiometricScanAndQuery_Click(object sender, EventArgs e)
        {
            short i;
            textBoxBiometricResult.Text = "Requesting Scan...";
            textBoxBiometricUsername.Text = "";
            i = axTermMon.Command((short)TERMMONLib.TermMonCommand.ScanBiometricAndQueryUser);
            if (i != (short)TERMMONLib.TermMonConst.Success)
                textBoxBiometricResult.Text = "Request Failed";
        }
    }
}