public static PresentationSpace GetPresentationSpace(DisplaySession Sess)
        {
            PresentationSpace ps = null;

            byte[] charBuf = null;
            int    bufSx   = 0;

            // get display characters and char attributes.
            while (true)
            {
                Ehllapier.SetSessionParameters("ATTRB,EAB,NOXLATE");
                bufSx   = (Sess.Dim.Width * Sess.Dim.Height) * 2;
                charBuf = CopyPresentationSpace(bufSx);

                // presentation space contains nulls.
                if ((charBuf[0] == 0x20) && (charBuf[1] == 0x00))
                {
                    Thread.Sleep(1);
                    continue;
                }

                // got some valid data
                break;
            }

            // get the color attributes of presentation space characters.
            Ehllapier.SetSessionParameters("ATTRB,EAB,XLATE");
            byte[] colorBuf = CopyPresentationSpace(bufSx);

            var lowPs = new LowPresentationSpace(Sess, charBuf, colorBuf, 2);

            ps = new PresentationSpace(lowPs);

            return(ps);
        }
        /// <summary>
        /// send keystrokes intended to signoff the display session to the Settings.SessId
        /// identified display session window.
        /// </summary>
        /// <param name="InSess"></param>
        public void Play_Signoff(DisplaySession InSess)
        {
            while (true)
            {
                PresentationSpace          ps  = InSess.GetPresentationSpace();
                CommonScreens.SignonScreen sos = new CommonScreens.SignonScreen(ps);
                if (sos.IsScreen( ))
                {
                    throw new EhllapiExcp("At signon screen. cant signoff from this screen.");
                }

                InSess.SendKeys(Ehllapier.Key.SystemRequest + Ehllapier.Key.Enter);
                InSess.Wait();

                ps = InSess.GetPresentationSpace();
                var sysrqs = new CommonScreens.SystemRequestMenuScreen(ps);
                if (sysrqs.IsScreen( ))
                {
                    break;
                }
            }

            InSess.SendKeys("90" + Ehllapier.Key.Enter);
            InSess.Wait();

            // signoff prompt
            InSess.SendKeys(Ehllapier.Key.Enter);
            InSess.Wait();

            return;

            InSess.SendKeys(Ehllapier.Key.SystemRequest + "90" + Ehllapier.Key.Enter);
            Play_Signoff_WaitUntilSignedOff(InSess);
        }
        public void Play_Signon(EhllapiSettings Settings)
        {
            try
            {
                using (DisplaySession sess = new DisplaySession())
                {
                    sess.Connect(Settings.SessId);

                    sess.SendKeys(Settings.UserName);
                    if (Settings.UserName.Length < 10)
                    {
                        sess.SendKeys(Ehllapier.Key.FieldExit);
                    }

                    sess.SendKeys(Settings.Password);
                    if (Settings.Password.Length < 10)
                    {
                        sess.SendKeys(Ehllapier.Key.FieldExit);
                    }

                    sess.SendKeys(Ehllapier.Key.Enter);
                    sess.Wait();

                    // display messages screen breaks on screen. Press enter.
                    if (DisplayMessagesScreen.IsScreen(sess))
                    {
                        sess.SendKeys(Ehllapier.Key.Enter);
                        sess.Wait();
                    }

                    // display program messages screen is displayed. Screen displays on
                    // signon to say "message queue allocated to another job".
                    // Press enter.
                    if (DisplayProgramMessagesScreen.IsScreen(sess))
                    {
                        sess.SendKeys(Ehllapier.Key.Enter);
                        sess.Wait();
                    }

                    // handle the "attempt to recover interactive job" display by running the signoff
                    // option and signing on again.
                    if (RecoverInteractiveJobScreen.IsScreen(sess))
                    {
                        sess.SendKeys("90" + Ehllapier.Key.Enter);
                        sess.Wait();
                        Play_Signon(Settings);
                    }
                }
            }
            catch (ApplicationException excp)
            {
                throw new EhllapiExcp("Play_Signon script failed", excp);
            }
        }
        public static DisplaySession StartSession(string SessId)
        {
            var settings = EhllapiSettings.RecallSettings();

            var sess = new DisplaySession()
            {
                mActiveSessId = SessId
            };



            return(sess);
        }
 private void Play_Signoff_WaitUntilSignedOff(DisplaySession InSess)
 {
     while (true)
     {
         InSess.Wait( );
         PresentationSpace          ps  = InSess.GetPresentationSpace();
         CommonScreens.SignonScreen sos = new CommonScreens.SignonScreen(ps);
         if (sos.IsScreen( ))
         {
             break;
         }
         Thread.Sleep(1);
     }
 }
 /// <summary>
 /// send keystrokes intended to signoff the display session to the Settings.SessId
 /// identified display session window.
 /// </summary>
 /// <param name="InSettings"></param>
 public void Play_Signoff(EhllapiSettings InSettings)
 {
     try
     {
         using (DisplaySession sess = new DisplaySession())
         {
             sess.Connect(InSettings.SessId);
             Play_Signoff(sess);
         }
     }
     catch (ApplicationException excp)
     {
         throw new EhllapiExcp("Play_Signoff script failed", excp);
     }
 }
        /// <summary>
        /// Scrape the screen to determine if the Samuel signon screen is displayed.
        /// </summary>
        /// <param name="InSess"></param>
        /// <returns></returns>
        public bool IsAtxSignon(DisplaySession InSess)
        {
            PresentationSpace ps = null;

            ps = InSess.GetPresentationSpace();
            if ((ps.IndexOf("Sign On") != null) &&
                (ps.IndexOf("User  . . .") != null) &&
                (ps.IndexOf("Password  .") != null))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public static PresentationSpace ReadPresentationSpace(
            EhllapiSettings InSettings)
        {
            PresentationSpace ps = null;

            try
            {
                using (DisplaySession sess = new DisplaySession())
                {
                    sess.Connect(InSettings.SessId);

                    ps = sess.GetPresentationSpace();
                }
            }
            catch (ApplicationException excp)
            {
                throw new EhllapiExcp("ReadPresentationSpace failed", excp);
            }
            return(ps);
        }
        public void AssureSignoff(EhllapiSettings Settings)
        {
            SessionScript script = new SessionScript();

            try
            {
                using (DisplaySession sess = new DisplaySession())
                {
                    sess.Connect(Settings.SessId);
                    var sos = new CommonScreens.SignonScreen(sess.GetPresentationSpace( ));
                    if (sos.IsScreen( ) == true)
                    {
                        Play_Signoff(sess);
                    }
                }
            }
            catch (ApplicationException excp)
            {
                throw new EhllapiExcp("AssureSignoff failed. " + excp.Message, excp);
            }
        }
        public static void Play_ItemLocatorInquiry(EhllapiSettings InSettings)
        {
            try
            {
                PresentationSpace ps     = null;
                SessionScript     script = new SessionScript();

                script.AssureSignoff(InSettings);

                // signon the display
                script.Play_Signon(InSettings);

                using (DisplaySession sess = new DisplaySession())
                {
                    sess.Connect(InSettings.SessId);

                    // tab to the menu option input field
                    sess.SendKeys(Ehllapier.Key.TabRight);

                    // option i
                    sess.SendKeys("i");
                    sess.Wait();

                    // 6174054
                    sess.SendKeys("6174054" + Ehllapier.Key.Enter);
                    sess.Wait();

                    // option 10
                    sess.SendKeys(Ehllapier.Key.TabRight + Ehllapier.Key.NewLine + "10" + Ehllapier.Key.Enter);
                    sess.Wait();
                }
            }
            catch (ApplicationException excp)
            {
                throw new EhllapiExcp("Play_ItemLocatorInquiry script failed", excp);
            }
        }
        public static void Launch_ClientAccessSession(
            EhllapiSettings InSettings, string ProfilePath, string SessId,
            LaunchOptions InLaunchOptions)
        {
            int sessCx = 0;

            if (InLaunchOptions == LaunchOptions.WaitForReady)
            {
                sessCx = pcsapi.QueryActiveSessionCount();
            }

            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = InSettings.Path_pcsws;

            // enclose the wrkstn profile path in quotes ( in case spaces in the path )
            string profilePath = ProfilePath;

            if (profilePath.IndexOf('"') == -1)
            {
                profilePath = '"' + profilePath + '"';
            }
            proc.StartInfo.Arguments = profilePath;

            //      proc.StartInfo.Arguments =
            //        '"' +@"c:\Program Files\IBM\Client Access\Emulator\private\pc04a.ws" + '"';
            //      proc.StartInfo.Arguments = "'" + InWrkstnProfilePath + "'";
            //      proc.StartInfo.Arguments = @"..\private\pc04a.ws";
            if (SessId.IsNullOrEmpty() == false)
            {
                proc.StartInfo.Arguments = @"/S=" + SessId + " " + proc.StartInfo.Arguments;
            }

            proc.Start();

            proc.WaitForInputIdle(5000);

            // wait until session count increments. that tells us the new session
            // is completely started.
            if (InLaunchOptions == LaunchOptions.WaitForReady)
            {
                int newSessCx = 0;
                while (newSessCx <= sessCx)
                {
                    Thread.Sleep(100);
                    newSessCx = pcsapi.QueryActiveSessionCount();
                }

                // wait until the SessId identified session can be connected to.
                bool isConnected = false;
                int  cummWait    = 0;
                while (isConnected == false)
                {
                    using (DisplaySession sess = new DisplaySession())
                    {
                        try
                        {
                            isConnected = true;
                            sess.Connect(SessId);
                            sess.Wait();
                        }
                        catch (EhllapiExcp)
                        {
                            cummWait += 300;
                            if (cummWait > 10000)
                            {
                                throw new ApplicationException("client access session connection timeout");
                            }
                            isConnected = false;
                            Thread.Sleep(300);
                        }
                    }
                }

                // wait for SystemAvailable.
                int tx = 0;
                while (true)
                {
                    ++tx;
                    if (tx > 1000)
                    {
                        throw new EhllapiExcp("Wait for system available timeout");
                    }
                    using (DisplaySession sess = new DisplaySession())
                    {
                        sess.Connect(SessId);
                        if (sess.SystemAvailable == true)
                        {
                            break;
                        }
                        Thread.Sleep(100);
                    }
                }
            }
        }