public static void SendKeys(string InSessId, string InKeys)
        {
            int           loopCx = 0;
            StringBuilder sbKeys = null;

            while (true)
            {
                ++loopCx;
                sbKeys = new StringBuilder(InKeys.Length);
                sbKeys.Append(InKeys);
                UInt32 rc = 0;
                UInt32 f  = Ehllapier.InputCodes.HA_SENDKEY;
                UInt32 l  = (UInt32)sbKeys.Length;
                UInt32 rv = PcsDll.hllapi(out f, sbKeys, out l, out rc);

                // 5 = input to the targer session was inhibited or rejected. all of
                //     the keystrokes could not be sent.
                if ((rc == 5) && (loopCx < 5))
                {
                    Thread.Sleep(1);
                    continue;
                }

                else if ((rc != 0) || (rv != 0))
                {
                    throw new EhllapiExcp("SendKeys failed. Session " + InSessId + " failed");
                }
                else
                {
                    break;
                }
            }
        }
        // ------------------------------- Connect -----------------------------
        public static void Connect(string SessId)
        {
            UInt32 rc;
            var    sbData = new StringBuilder(4);

            sbData.Append(SessId);
            UInt32 retc = 0;
            UInt32 f    = InputCodes.CONNECT_PS;
            UInt32 l    = 4;

            rc = PcsDll.hllapi(out f, sbData, out l, out retc);
            if ((rc != 0) && (rc != 4) && (rc != 5))
            {
                throw new EhllapiExcp("connect to session " + SessId + " failed");
            }
        }
        public static void Wait(string InSessId)
        {
            StringBuilder Data = new StringBuilder(0);
            UInt32        rc   = 0;
            UInt32        f    = Ehllapier.InputCodes.HA_WAIT;
            UInt32        l    = 0;
            UInt32        rv   = PcsDll.hllapi(out f, Data, out l, out rc);

            if ((rc != 0) || (rv != 0))
            {
                throw new EhllapiExcp(
                          "Session wait failed. " +
                          " rc: " + rc.ToString() +
                          " SessId: " + InSessId);
            }
        }
        public static void Disconnect(string InSessId)
        {
            UInt32 rc;

            StringBuilder sbData = new StringBuilder(4);

            sbData.Append(InSessId);
            UInt32 retc = 0;
            UInt32 f    = Ehllapier.InputCodes.DISCONNECT_PS;
            UInt32 l    = 4;

            rc = PcsDll.hllapi(out f, sbData, out l, out retc);
            if (rc != 0)
            {
                throw new EhllapiExcp("Disconnect from session " + InSessId + " failed");
            }
        }
        public static DisplayLocation QueryCursorLocation(string InSessId)
        {
            StringBuilder sbKeys = new StringBuilder(10);
            UInt32        rc     = 0;
            UInt32        funcNx = Ehllapier.InputCodes.QueryCursorLocation;
            UInt32        lgth   = 0;
            UInt32        rv     = PcsDll.hllapi(out funcNx, sbKeys, out lgth, out rc);

            if ((rc != 0) || (rv != 0))
            {
                throw new EhllapiExcp(
                          "QueryCursorLocation failed.");
            }

            // cursor location is stored as an int16 in the first two bytes of lgth.
            byte[]          bits  = BitConverter.GetBytes(lgth);
            UInt32          psPos = BitConverter.ToUInt16(bits, 0);
            DisplayLocation loc   = PS_ToDisplayLocation(InSessId, psPos);

            return(loc);
        }
        public static string CopyPresentationSpaceToString(string InSessId)
        {
            UInt32        pos    = 1;
            UInt32        len    = 80 * 24;
            StringBuilder sbData = null;

            while (true)
            {
                sbData = new StringBuilder(3000);
                UInt32 rc = pos;
                UInt32 f  = Ehllapier.InputCodes.HA_COPY_PS_TO_STR;
                UInt32 l  = len;
                UInt32 rv = PcsDll.hllapi(out f, sbData, out l, out rc);

                // waiting for host response. likely, there is a new screen coming from
                // the host. wait until it arrives.
                if (rc == 4)
                {
                    Thread.Sleep(1);
                    continue;
                }
                else if ((rv != 0) || (rc != 0))
                {
                    throw new EhllapiExcp(
                              "copy_ps_to_str failed. Session " + InSessId +
                              "  rv:" + rv.ToString() +
                              "  rc:" + rc.ToString() +
                              "  ps:" + sbData.ToString());
                }
                else
                {
                    break;
                }
            }

            return(sbData.ToString());
        }