Technical Notes |
|
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.
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.
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.
In the Visual Studio .NET editing environment, add a reference to the Reflection product you wish to control in your application.
Note: Ensure that you do not select the corresponding ActiveX Control Library.
After you have added a reference to the proper Reflection library, you must instantiate the Reflection session object.
Public RHP as New Reflection1.SessionPublic RIBM as New Reflection.SessionPublic RRG as New Reflection4.SessionPublic RUO as New Reflection2.Sessionpublic Reflection1.Session RHP = new Reflection1.Session();public Reflection.Session RIBM = new Reflection.Session();public Reflection4.Session RRG = new Reflection4.Session();public Reflection2.Session RUO = new Reflection2.Session();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.
Reflection2.Constants.rcAllowKeystrokesReflection2.TerminalKeys.rcVTPF1keyExamples for error handling are provided for both VB.NET and C#.
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 hereCatch ReflectionError As ExceptionMsgBox(ReflectionError.Message)End TryIf 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 ErrorHandlerWith Session ‘Reflection code Exit SubErrorHandler:.MsgBox Err.Description, vbExclamation + vbOKOnlyEnd With End SubEdited Code:
On Error GoTo ErrorHandlerWith Session‘Reflection code End With Exit SubErrorHandler:MsgBox Err.Description, vbExclamation + vbOKOnlyEnd SubTo 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, Reflection.NET_Samples.zip, is available for you to download from the Download Library.
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.
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.
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.
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/.
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.
Public RHP As Reflection1.SessionRHP = Me.AxR1winCtrl1.GetActiveSession()Public RIBM as Reflection.SessionRIBM = Me.AxRibmCtrl1.GetActiveSession()Public RRG As Reflection4.SessionRRG = Me.AxR4winCtrl1.GetActiveSession()Public RUO As Reflection2.SessionRUO = Me.AxR2winCtrl1.GetActiveSession()In C#, you need to cast the Reflection Session object explicitly when instantiating it.
public Reflection1.Session RHPReflection1.Session RHP = (Reflection1.Session)axR1winCtrl1.GetActiveSession();public RIBM as Reflection.SessionReflection.Session RIBM = (Reflection.Session)axRibmCtrl1.GetActiveSession()public Reflection4.Session RRGReflection4.Session RRG = (Reflection4.Session)axR4winCtrl1.GetActiveSession();public Reflection2.Session RUOReflection2.Session RUO = (Reflection2.Session)axR2winCtrl1.GetActiveSession();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/.
'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 SelectEnd Sub |