Creating .NET assemblies and instancing them by using the VBScript Driver

1) Introduction

From version 1.0 on, the VBScript Driver has had a global method named CreateDotNetObject, which enables you to instance data configured on a .NET assembly.

First, this article will show you how to create and set a Class Library (DLL) by using Microsoft Visual C# 2008. After that, you can check an example instancing this class on E3 and calling its methods via scripts written in the Driver.

Important: You must install .NET Framework Redistributable Package to execute the scripts. To use VBScript Driver’s CreateDotNetObject method, you also need .NET Framework 2.0 or higher.

2) Creating .NET assemblies on Microsoft Visual C# 2008

In the Microsoft Visual C# 2008, access the FileNewProject menu. In this example, we will create a Class Library project. Write the project’s name, select the directory and click the OK button (Figure 1).

Figure 1. Creating a new Class Library project

To configure the .NET assembly to be compatible to the VBScript Driver, you must enable its COM visibility. To do so, just open the project’s properties, click the Assembly Information button and check the Make assembly COM-Visible option (Figure 2).

Figure 2. Assembly’s properties

The project automatically generates a Class1.cs file with a class named Class1 already created in the TestAssembly namespace. In this example, we will create a method to open a message box when it is called.

You must use the MessageBox.Show method to display the message box, so, you need to add a Windows Forms’ reference to the project. In the Project menu, click the Add Reference item, select the System.Windows.Forms component and click the OK button.

Then, you must add a new method to the class called ShowMessage (with no parameter or return) to create a message box with the text “Hello World!”.

namespace TestAssembly
{
  public class Class1
  {
    public void ShowMessage()
    {
      System.Windows.Forms.MessageBox.Show(“Hello World!”);
    }
  }
}

3) Instancing the object and calling methods via Driver’s scripts

After compiling the DLL file, you can instance a Class1 object via scripts. So, add the VBScript Driver to the E3 project and access its properties. On the VBScript tab, write the script to create a Class1 instance and call the ShowMessage method when starting the Driver.

Figure 3. VBScript Driver’s settings

Dim obj
Sub OnStart()
  Set obj = CreateDotNetObject(“C:\TestAssembly.dll”, “TestAssembly.Class1”)
  obj.ShowMessage()
End Sub
Sub OnStop()
  Set obj = Nothing
End Sub
After adding the script to the corresponding area, click the OK button. In the Driver’s design window, click the Activate/deactivate communication button. If all settings are correctly performed, the message box is displayed when activating the Driver (Figure 4).

Figure 4. Message box displayed by the ShowMessage method

In the CreateDotNetObject method, the first parameter corresponds to the file’s name and path containing the assembly that sets the data type (in this case, it is the TestAssembly.dll file). The second parameter corresponds to the data type’s name, including the namespace (in this example, the Class1 class, which is part of the TestAssembly namespace).

After creating the object by the function’s return, you can call the ShowMessage method. Check the Driver’s documentation for more information about its operation and available functions.


4) Conclusion

In this article, we have presented a simple demonstration on how a class implemented in C# (using the .NET Framework) can be instanced on E3 via VBScript Driver.

However, this feature enables the user to develop very complex classes by using the Framework’s resources, and it is applicable, especially, to cases where you cannot perform a certain task directly on E3.

 

Attachments:

Projeto1.zip
TestAssembly.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 *