Using functions and classes created with Visual Basic 2005 in E3.

1) Introduction

In order to optimize reuse of existing code and integration with libraries already developed by integrators, Elipse E3 allows the use of libraries created on VB or external tools, ported using COM technology in a simple and accessible way to any VB programmer. In the example described here we will use Visual Studio 2005 but this procedure can also be done with classes and functions developed with earlier VB tools from Microsoft although always using the COM technology.

The source code used in this article can be found on the sites cited at the References section at the end of the document.

2) Creating E3 compatible libraries

To use existing libraries we need to make small changes on them in order to turn them into COM libraries. A COM library is different from a normal library in two important aspects: they must be registered in the system where they will be called and they must implement a standard set of access functions. Fortunately, this is done automatically on Visual Studio 2005 and also in a simple way with earlier versions of this tool. For this procedure it is enough to add some parameters and headers to the project.

To create a COM library on Visual Studio in a simple way and using Visual Basic we indicate the article Walkthrough: creating COM objects with Visual Basic 2005, available at MSDN website. This document from Microsoft explains how to create a COM interface compatible project and how to make your classes generate COM objects.

The rest of this article considers that the libraries were already adapted to the COM model and analogous procedure can be used in COM libraries created on other programming languages.

To register COM libraries there are two ways, each one used for a different case. In this article’s example the COM library is generated using the .NET Framework and it contains .NET extensions, so it must be registered in a different way from conventional COM libraries. It must be registered using regasm application, included on .NET Framework 2.0, supplied by Microsoft and it must be stored on the same directory where resides the executable that will access it, for example C:\Program Files\Elipse E3\bin, or it must be stored on GAC (Global Assembly Cache) but this second option involves signing the library with a strong name. For developers that will develop COM objects without .NET extensions, we must register these objects with regsvr32 tool, supplied with Microsoft Windows. See more information on using regasm in an article from Microsoft entitled Assembly Registration Tool in References section.

3) Inserting COM objects in E3

To use a COM object in E3 it is enough to create an internal tag, associate it with a system registered COM library and access this library through this tag. Now we will see, in a step-by-step example, how this association can be made in a quick and simple way.

Before start the following steps, be sure that the COM library was registered.

1) Open Studio and create a new application.
2) In the Organizer, right-click Data, select Insert–Panel and add an internal tag.


Figure 1: Inserting an internal tag

3) Open this internal tag, select the Scripts tab and, at OnStartRunning() event, insert the link’s script.


Figure 2: Selecting Scripts tab

 


Figure 3: Link script to COM object

This script will link the tag to a COM library class, thus creating an object. In this case, we have a library named ComLibrary.dll containing VBClass class, which is linked to the tag.

Next we have the script’s code:

Sub InternalTag1_OnStartRunning()
 Value = CreateObject(“ComLibrary.VBClass”)
End Sub

 

Now the link is ready.

4) Accessing COM objects

After inserting the object, we must have a way of accessing its methods. This example shows how this is done.

Consider, for this example, that we have a method called AddValues(Integer Value1, Integer Value2) in class ComLibrary.VBClass that receives two values and returns their sum.

We will add a graphical interface to test the link:

1) Create a graphical interface on the main screen to insert two values. We suggest using Labels to indicate where to type the values, TextBoxes to type the values, and a button to do the sum between the values.

 


Figure 4: Graphical interface to use COM object

 

2) Double click the button that sums the values and edit the button’s script for the Click() event. Add the script that executes the sum method of the COM object and shows the result in a dialog box.

 


Figure 5: Button script to link with COM object

The source code:

Sub CommandButton1_Click()
set comObj = Application.GetObject(“Data.InternalTag1”).Value
MsgBox comObj.AddValues(Parent.Item(“TextBox1”).Value, Parent.Item(“TextBox2”).Value)
End Sub

By analyzing this code we have link from InternalTag1 to a local variable. As this tag points to a COM object, we can see that the comObj variable represents this object directly and we can call its methods, just like the next line of code, where the values on both text fields are summed and the result is attributed to a result field.

The next figure shows the result of executing this project, showing the input fields and the resulting message box.

 


Figure 6: Running this example

 

5) Final Remarks

Using external libraries in E3 allows more customization of the supervisory being developed and also more control over the process that best adapts to development. Another important question is the encapsulation of classes and functions within libraries, allowing the abstraction of procedures for the integrator and reuse of these libraries.

Since we are dealing with COM libraries, this represents great advantage on programming because E3 becomes compatible with a technology that is broadly used and recommended by Microsoft, allowing the development of libraries separated from the supervisory but widely compatible. This separation of components facilitates the organization of complex projects for it permits splitting the functionalities implemented in modules.

With this model, we can have COM servers serving the supervisory and any other program that needs or wants to interact with these servers, and the development and maintenance can be separated.

In short, the apparently simple procedure seen in this article represents a very useful programming tool on the development of modular projects using E3.

 

6) References

Microsoft Corporation. Assembly Registration Tool (Regasm.exe).
Available at http://msdn2.microsoft.com/en-us/library/tzat5yw6(VS.80).aspx

Microsoft Corporation. Walkthrough: creating COM objects with Visual Basic 2005.
Available at http://msdn2.microsoft.com/en-us/library/x66s8zcd(VS.80).aspx

Attachments:

ComLibrary.zip
E3Project.zip

Print Friendly, PDF & Email

Este artigo foi útil? Was this helpful?

Classificação média - Average rating 0 / 5. Count: 0

Leave a Reply

Your email address will not be published.Required fields are marked *