/// <summary>
 /// Create the hidden form to handle the open trust window, and RawInput
 /// </summary>
 public HiddenForm()
 {
     RawInput rawinput = new RawInput(Handle, false);
     rawinput.KeyPressed += OnKeyPressed;
     openTrust.Tick += timeUp;
     HF = this;
     Verifiers.currentUser = WindowsIdentity.GetCurrent().User.ToString();
 }
        static void Main()
        {
            if (AlreadyRunning())
            {
                return;
            }
            Application.EnableVisualStyles();
            AppDomain.CurrentDomain.ProcessExit += new EventHandler(Cleanup);
            ni = SetupNI();
            pipeClient = new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut, PipeOptions.Asynchronous, TokenImpersonationLevel.Impersonation);
            HeartBeatLock = false;
            System.Timers.Timer HeartBeat = new System.Timers.Timer();
            HeartBeat.Interval = 1000;
            HeartBeat.Elapsed += beat;
            HeartBeat.Start();
            service = new ServiceController("ActiveAuthenticationService", Environment.MachineName);
            HF = new HiddenForm();
            if (!UserConfigurations.Registered) // User is new to active authentication, or has been invalidated by the owner.
            {
                while (!pipeClient.IsConnected)
                {
                    // Wait for connection.
                }
                Enrollment EnrollmentForm = new Enrollment("", "", "");
                while (!EnrollmentForm.Registered) // Keep popping up new enrollment forms until it is properly filled out.
                {
                    EnrollmentForm.ShowDialog();
                    if (!EnrollmentForm.Registered)
                    {
                        // If the user tries to avoid giving contact information we explain why it is needed.
                        MessageBox.Show("This system requires a valid set of contact information to function properly and securly." +
                            " If you can not provide a valid set of contact information we appologize for the inconvienience, and this application can be uninstalled either with the control panel " +
                            "uninstall applications function or by running the Installer.msi executable that you used to install the program, and selecting the remove option.");
                    }
                }
                HF.openTrustWindow(); // Start training mode without the initial lock screen.
            }
            else if (!File.Exists(Configuration.ownerFile)) // If there is not an owner than this is the original installation of the program.
            {                                               // In that case we set the user that installed the program (this user) as the owner.
                try
                {
                    ss.WriteString("ChangeOwner");
                    if (ss.ReadString().Equals("Ready"))
                    {
                        ss.WriteString(Environment.UserName);
                    }
                    if (!ss.ReadString().Equals("Success"))
                        MessageBox.Show("Error");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            IDverify = new IdentityVerifier();
            failCount = 0;

            //Start the keyboard hook then bind the eventhandler(s).
            KeyboardHook.Start();
            Highlander = new Mutex();
            KeyboardHook.KeyboardAction += new EventHandler<KeyboardHookEventArgs>(KeyboardEventCollect);
            #if DEBUG
            // KeyboardHook.KeyboardAction += new EventHandler<KeyboardHookEventArgs>(KeyboardEventBalloon);
            #endif
            #if Kahona
            DataSet TBC = new DataSet();
            #if Nick1
            TBC.ReadXml(Path.GetPathRoot(Environment.SystemDirectory)+@"Workspace\ActiveAuthenticationDesktopClient\KeyStrokeData\TheBigKahona.xml");
            #elif Nick2
            TBC.ReadXml(Path.GetPathRoot(Environment.SystemDirectory)+@"Workspace\ActiveAuthenticationDesktopClient\KeyStrokeData\KSData.xml");
            #elif Nick3
            TBC.ReadXml(Path.GetPathRoot(Environment.SystemDirectory)+@"Workspace\ActiveAuthenticationDesktopClient\KeyStrokeData\TheBigKahona2.xml");
            #elif AZ1
            TBC.ReadXml(Path.GetPathRoot(Environment.SystemDirectory)+@"Workspace\ActiveAuthenticationDesktopClient\KeyStrokeData\AZKahona.xml");
            #endif
            DataTable BIG_ONE = TBC.Tables[0];
            foreach (DataRow row in BIG_ONE.Rows)
            {
                IDverify.FileEvent(row);
            }
            #endif
            //Setup other hooks to catch events
              //  ContextHook.Start();
              //  ContextHook.ContextChange += new EventHandler<ContextHookEventArgs>(ContextEvent);   used for context specific version --N
            RawKeyboard.NewKeyboard += new EventHandler<RawKeyboardEventArgs>(New_Keyboard);

            NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkAvailabilityChange);
            Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SessionSwitch);
            Application.Run();
        }