Attachmate Worldwide  |   Contact Us  |   NetIQ.com
Home » Support » Solution Library

Technical Notes

Programming Reflection with .NET
Technical Note 1799
Last Reviewed 06-Jul-2007
Applies To
Reflection for HP version 11.0.3 through 14.x
Reflection for IBM version 11.0.3 through 14.x
Reflection for UNIX and OpenVMS version 11.0.3 through 14.x
Summary

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:

  • This technical note does not apply to Reflection 2007. For information on using Reflection 2007 in a .NET environment, see Technical Note 2225.
  • Starting in version 12.0, the Reflection macro recorder will record commands using C# or VB.NET syntax. See Technical Note 1809 for details.

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 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.

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: If you experience problems after dragging the Reflection ActiveX control to a form in your VB.NET project in Visual Studio 2005, please contact Attachmate Technical Support, http://support.attachmate.com/contact/.

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, http://support.attachmate.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

Related Technical Notes
1809 Reflection Macro Recorder Supports .NET
2183 Developer Tools for Attachmate Products
2225 Programming Reflection 2007 with .NET
9993 Index of Reflection Scripting Technical Notes

Did this technical note answer your question?

Yes    No    Somewhat     Not sure yet

Additional comments about this tech note:

Need further help? For technical support, please contact Support.