InfoConnect API Guide
Get Started / Connect to a Session
In This Topic
    Connect to a Session
    In This Topic

    You can connect to running sessions. In this sample, the code gets a control object from an existing session (identified by its file path) and writes some data from that session to the Console.

    To connect to a running session

    1. Open the InfoConnect workspace and create a demo session as follows:

      If you are using IBM, create an IBM3270 session with a host name of "demo:ibm3270.sim" and save the session as demoSession.rd3x in the default folder (...\myDocuments\Micro Focus\InfoConnect.)

      If you are using Open Systems, create a VT session with a host name of "demo:UNIX" and save the session as demoSession.rdox in the default folder.

    2. In Visual Studio, create a new Console Application project and add references for the following InfoConnect assemblies. (Depending on your version of Visual Studio, these can be found either on the .NET tab or under Assemblies | Extensions.)
      Attachmate.Reflection.Framework
      Attachmate.Reflection.Emulation.IbmHosts
      Attachmate.Reflection.Emulation.OpenSystems
    3. Replace all the code in the Program.cs file with the following code for the terminal you are using.  Then change the file name in the sessionPath variable to that of the session you created and run the program.

                              

      Connect to a session
      Copy Code
      //This sample gets the Terminal control of a running session.
      //Before you run this sample, make sure the session used in GetControlsByFilePath()
      //is running in an InfoConnect workspace. If more than one instance of InfoConnect is running,
      //the session must be running in the first instance that was started.
      using System;
      using System.Collections.Generic;
      using System.Text;
      using Attachmate.Reflection.Framework;
      using Attachmate.Reflection.Emulation.IbmHosts;
      namespace ConnectToASession
      {
          class Program
          {
              static void Main(string[] args)
              {
                  //Get a handle to an application that represents the first instance of InfoConnect started manually.
                  //For production code, use a try catch block here to handle a System Application Exception thrown
                  //if the app isn't running.
                  Application app = MyReflection.CreateApplication();
      
                  //Get the terminal from the session document file path.
                  string sessionPath = Environment.GetEnvironmentVariable("USERPROFILE") + @"\Documents\Micro Focus\InfoConnect\demoSession.rd3x";
                  object[] terminals = app.GetControlsByFilePath(sessionPath);
      
                  //Check to make sure the session is running.
                  if (terminals != null && terminals.Length > 0)
                  {
                      //Get a screen and then get some text from the screen.
                      IIbmTerminal terminal = (IIbmTerminal)terminals[0];
                      IIbmScreen screen = terminal.Screen;
                      string text = screen.GetText(18, 2, 48);
                      Console.WriteLine(text);
                  }
                  else
                      Console.WriteLine("No such control exists. Check to make sure that the session from the file is running.");
      
              }
          }
      }
      

                              

      Connect to a session
      Copy Code
      //This sample gets the Terminal control of a running session.
      //Before you run this sample, make sure the session used in GetControlsByFilePath()
      //is running in a workspace. If more than one instance of is running,
      //the session must be running in the first instance that was started.
      using System;
      using System.Collections.Generic;
      using System.Text;
      using Attachmate.Reflection.Framework;
      using Attachmate.Reflection.Emulation.OpenSystems;
      namespace ConnectToASession
      {
          class Program
          {
              static void Main(string[] args)
              {
      
                  //Get a handle to an application that represents the first instance of started manually.
                  //For production code, use a try catch block here to handle a System Application Exception thrown
                  //if the app isn't running.
                  Application app = MyReflection.CreateApplication();
      
                  //Get the terminal from the session document file path.
                  string sessionPath = Environment.GetEnvironmentVariable("USERPROFILE") + @"\Documents\Micro Focus\InfoConnect\demoSession.rdox";
                  object[] terminals = app.GetControlsByFilePath(sessionPath);
      
                  //Make sure the session is running.
                  if (terminals != null && terminals.Length > 0)
                  {
                      //Get a screen and then get some text from the screen.
                      ITerminal terminal = (ITerminal)terminals[0];
                      IScreen screen = terminal.Screen;
                      string text = screen.GetText(1, 1, screen.DisplayRows, screen.DisplayColumns);
                      Console.WriteLine(text);
                  }
                  else
                  {
                      Console.WriteLine("No such control exists. Check to make sure that the session from the file is running.");
                  }
      
              }
          }
      }
                                             
      
                                              
      

    To test this project

    Press F5 to run the project and verify that text from the screen is written to the Console.