Programming Reflection 14.x with .NET

  • 7021688
  • 25-Mar-2004
  • 01-Apr-2018

Environment

Reflection for HP version 14.x
Reflection for IBM version 14.x
Reflection for UNIX and OpenVMS version 14.x

Situation

This technical note provides basic information to help programmers who are new to Reflection, or experienced VB programmers who are new to .NET, work with Reflection in a .NET environment. Information about using ActiveX control in this environment is also included.

Note the following:

  • Starting in version 12.0, the Reflection macro recorder will record commands using C# or VB.NET syntax. See KB 7021690 for details.

Resolution

The Reflection COM Object

The Reflection API provides a COM object that is available to .NET programmers. COM objects can cause memory leaks in a .NET environment; however, Attachmate optimized the Reflection COM object to prevent memory leaks in both C# and VB.NET in Reflection version 11.0.3 or higher.

Getting Started with Reflection 14.x in .NET

To successfully program with Reflection in a .NET environment, you must add a reference to Reflection, instantiate the Reflection session object, and properly name the Reflection constants, and handle errors.

Add a Reference to the Reflection Library

In the Visual Studio .NET editing environment, add a reference to the Reflection product you wish to control in your application.

  1. In the Add Reference dialog box, click the COM tab. Reflection emulation products appear as follows:
Reflection for HP
Reflection for IBM 12.0 (or higher) Object Library
Reflection for ReGIS Graphics
Reflection for UNIX and OpenVMS
  1. Select one or more of the above applications.
    Note:
    Ensure that you do not select the corresponding ActiveX Control Library.

Instantiate the Reflection Session Object

After you have added a reference to the proper Reflection library, you must instantiate the Reflection session object.

VB.NET Syntax

Reflection for HP
Public RHP as New Reflection1.Session

Reflection for IBM
Public RIBM as New Reflection.Session

Reflection for ReGIS Graphics
Public RRG as New Reflection4.Session

Reflection for UNIX and OpenVMS
Public RUO as New Reflection2.Session

C# Syntax

Reflection for HP
public Reflection1.Session RHP = new Reflection1.Session();

Reflection for IBM
public Reflection.Session RIBM = new Reflection.Session();

Reflection for ReGIS Graphics
public Reflection4.Session RRG = new Reflection4.Session();

Reflection for UNIX and OpenVMS
public Reflection2.Session RUO = new Reflection2.Session();

Naming the Reflection Constants

In Visual Basic 6 or VBA, after adding a reference to Reflection, the compiler recognizes Reflection constants such as rcAllowKeystrokes or rcVTPF1key. To access Reflection constants in C# and VB.NET, all Reflection constants must be preceded by <Object>.Constants, <Object>.ControlCodes, or <Object>.TerminalKeys, as appropriate.

Example Using Reflection for UNIX and OpenVMS

Reflection2.Constants.rcAllowKeystrokes
Reflection2.TerminalKeys.rcVTPF1key

Handling Reflection Errors

Examples for error handling are provided for both VB.NET and C#.

VB.NET Error Handling

In VB.NET, you can use either a Try…Catch block or the Visual Basic 6 On Error GoTo method for error handling.

The following example shows the Try…Catch block:

Try
‘Your Reflection code here
Catch ReflectionError As Exception
MsgBox(ReflectionError.Message)
End Try

If you want to use the On Error GoTo method, and you used the Reflection macro recorder to record programs in Visual Basic 6, then you will need to edit the code. (Note: Reflection for RIBM does not record error handling code, so you must edit your script manually to include the error handling code.) In VB.NET, labels cannot be inside a With block, so you must move the With block and use MsgBox rather than .MsgBox. Compare the recorded code to the edited code in the examples below:

Recorded Code:

On Error GoTo ErrorHandler

With Session
‘Reflection code
Exit Sub

ErrorHandler:
.MsgBox Err.Description, vbExclamation + vbOKOnly

End With

End Sub

Edited Code:

On Error GoTo ErrorHandler

With Session
‘Reflection code
End With

Exit Sub

ErrorHandler:
MsgBox Err.Description, vbExclamation + vbOKOnly

End Sub

C# Error Handling

To handle errors in C#, execute Reflection code in a try…catch block, as shown in the following example:

try
{
//Your Reflection code here
}
catch(Exception ReflectionError)
{
MessageBox.Show (ReflectionError.Message);
}

Sample Code

Sample code, Reflection.NET_Samples.zip, is available for you to download from the Download Library.

Known Issue – Windows 7 64-bit

When opening the .NET sample in Visual Studio 2010 on Windows 7 64-bit, you may receive an error message:

"Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))"

To resolve this issue, you must create an "x86" Build Configuration Platform setting in Visual Studio 2010 and then change the configuration setting from "Any CPU" to "x86." Follow these steps to create the "x86" configuration:

  1. Open the Visual Studio 2010 Configuration Manager.
  2. Click Platform > New.
  3. Select x86 as the "New platform."
  4. Click OK.

Now you can change the configuration from "Any CPU" to "x86."

Known Issue – Undefined Reflection Types and Constants

The code samples in Reflection.NET_Samples.zip were originally created for use with Reflection 13.0 and contain a reference to the Reflection type library included in Reflection 13.0. If you are using Reflection 14.0 or 14.1, Visual Studio may not be able to resolve the reference to the earlier version of the Reflection type library when the project is upgraded to Visual Studio 2010 format. Reflection types and constants may appear "undefined".

To fix this, on the Project menu in Visual Studio 2010, select the <project name> Properties, and on the References tab, remove the reference to the missing Reflection type library (Reflection 13.0). Then, add a reference to the current version of Reflection, and the project should build properly.

Using ActiveX Control

Reflection also provides an ActiveX control. The ActiveX control is useful when a programmer needs to provide a terminal screen on a Windows form or web page along with another application or a different data source. An example of this is a document viewer that opens a specific document on the form when a user clicks on that document in the terminal screen.

Add Reference to both Session and ActiveX Control

To use the Reflection ActiveX control you need to add a reference to the Reflection Library, as described above, and add a reference the Reflection ActiveX Control 1.0 Type Library appropriate for your application, for example Reflection for HP ActiveX Control 1.0 Type Library.

Add ActiveX Control to the Toolbox

To add Reflection's ActiveX control to your project, first, you need to add it to your Toolbox. In your .NET project, right-click the Toolbox and select Customize Toolbox. Select the appropriate ActiveX control to add to the toolbox.

Add ActiveX Control to the Form

Once added to the Toolbox, the Reflection ActiveX control can be added to the form like any other control.

Note: When you add the Reflection ActiveX control to a Windows form in your VB.NET project in Microsoft Visual Studio 2008 or 2005, you may experience one of the following problems:

  • When attempting to add the control, it may not display in the form. You may also see an “Unable to get the window handle” error. To resolve this issue, update to Visual Studio 2008 Service Pack 1 or higher.
  • After successfully adding the control, when you click on the control, the design environment may become nonresponsive. To workaround this issue, temporarily click a different application window (such as Notepad). When you return to Visual Studio, responsiveness is restored.

Instantiate the Reflection Session within the ActiveX Control

The Reflection ActiveX control has only a few methods and properties that control Reflection. Reflection is controlled by instantiating the Reflection Session object within the ActiveX control.

VB.NET Syntax

Reflection for HP
Public RHP As Reflection1.Session
RHP = Me.AxR1winCtrl1.GetActiveSession()

Reflection for IBM
Public RIBM as Reflection.Session
RIBM = Me.AxRibmCtrl1.GetActiveSession()

Reflection for ReGIS Graphics
Public RRG As Reflection4.Session
RRG = Me.AxR4winCtrl1.GetActiveSession()

Reflection for UNIX and OpenVMS
Public RUO As Reflection2.Session
RUO = Me.AxR2winCtrl1.GetActiveSession()

C# Syntax

In C#, you need to cast the Reflection Session object explicitly when instantiating it.

Reflection for HP
public Reflection1.Session RHP
Reflection1.Session RHP = (Reflection1.Session)axR1winCtrl1.GetActiveSession();

Reflection for IBM
public RIBM as Reflection.Session
Reflection.Session RIBM = (Reflection.Session)axRibmCtrl1.GetActiveSession()

Reflection for ReGIS Graphics
public Reflection4.Session RRG
Reflection4.Session RRG = (Reflection4.Session)axR4winCtrl1.GetActiveSession();

Reflection for UNIX and OpenVMS
public Reflection2.Session RUO
Reflection2.Session RUO = (Reflection2.Session)axR2winCtrl1.GetActiveSession();

Events

In VB.NET you can use the Reflection OnEvent method to set up events in Reflection that call the .RaiseControlEvent method. RaiseControlEvent raises an event that can be processed in VB.NET in your form code.

Note: Use of Automation events in C# is supported in version 14.0.2 (service pack 2 or 2.1). For more information, please contact Attachmate Technical Support, https://support.microfocus.com/contact/.

Example Using Reflection for UNIX and OpenVMS

'Set up an Event when Reflection disconnects.
.OnEvent(1, Reflection2.Constants.rcEvDisconnected, Reflection2.Constants.rcVBCommand, _
    ".RaiseControlEvent 1, ""Lost our Connection""", _
    Reflection2.Constants.rcEnable, _ Reflection2.Constants.rcEventReenable)

'Process events raised by RaiseControlEvent.
Private Sub AxR2winCtrl1_OnReflectionEvent(ByVal sender _
    As Object, ByVal e As AxR2AXCTRLLib. _
    _R2winEvents_OnReflectionEventEvent) 
    Handles AxR2winCtrl1.OnReflectionEvent

    Select Case e.v1 'This is event number
       Case 1
           'Session was disconnected
           RUO.MsgBox(e.v2)
       Case Else
    End Select
End Sub

Additional Information

Legacy KB ID

This document was originally published as Attachmate Technical Note 1799.

Feedback service temporarily unavailable. For content questions or problems, please contact Support.