Integrated VBS scripting language application in Delphi

xiaoxiao2021-03-06  55

Integrated VBS scripting language application in Delphi

?

Luo Wei? From Wei ?? Wang Zhenghao

Shanghai East China Normal University Information College

Summary: Using ActiveX Scripting technology, you can integrate using scripting languages ​​in your application. This article describes how to apply this technology to add VBScript support in the Delphi application.

Keywords: ActiveX script description; Delphi; VBScript

?

1. introduction

Delphi language is a powerful programming language developed by Borland. It is based on serial port and object-oriented, and has the powerful function of VC and the rapid development of VC. It is easy to use. It has high development efficiency. It is very beautiful. It can achieve various operational functions. VBS full name Visual Basic Script is an easy-to-learn, powerful scripting language, is extremely wide.

This article introduces the method of calling and operating VBS through ActiveX Scripting Technology in Delphi, achieving mixing programming of Delphi and VBS, which has fully played their respective advantages.

?

2. Basic principle

2.1 Basic Concept of ActiveX Technology

The VBS cannot be called directly in Delphi, and this problem can be solved using ActiveX technology. ActiveX is a technology proposed in 1996, which is based on COM (Component Object Model, component object model), so that different processes (especially network processes) can communicate with each other. The ActiveX control is a protocol for module integration for Microsoft, which is a portable software module for a variety of development languages, so unrelated to the development platform. The ActiveX control consists of some executable code that follows the ActiveX specification, such as a .exe, .dll, or .ocx file. After joining the ActiveX control in the program, it will become part of the development and operational environment and provide new features for the application. The ActiveX control retains some of the property, events, and methods of ordinary Delphi controls, which greatly enhances the ability and flexibility of program designers.

2.2 ActiveX script description (ActiveX scripting)

Using the ActiveX script description technology in ActiveX technology, you can manipulate ActiveX control and Java programs from the client or server side, pass data, and coordinate the operations between them.

The ActiveX Scripting system consists of a COM interface family that defines a protocol that connects the scripting engine and the host program. In the ActiveX Scripting world, the script engine is just a component object, which can dynamically perform scripts. The host application can expose its automation interface to the namespace of the script engine, which can access the application objects like the variables in the program in the dynamically executed script.

??? The application creates and initializes the script engine based on the Script language to be parsed, and then connect the application to the engine. The script code is then submitted to the script engine, and the script code can be executed.

ActiveX Scripting uses WSH, which is Windows Scripting Host to do a working environment for scripting languages. WSH is a 32-bit Windows platform based on the system, and is independent of the language script running environment. The WSH architecture is above ActiveX, and the WINDOWS user is fully utilized for Windows users to make the Windows users. Sweep the obstacles.

?

3. Call the implementation of VBS in Delphi

To call VBS in Delphi First, you need to install the ActiveX control in the Delphi integration environment.

To import an ActiveX control, first select Component in the Delphi menu, select Import ActiveX Control, Install Microsoft Script Control, install the ActiveX control in the Delphi environment, then add such a sentence in the program code: USE MSScriptControl_TLB can call VBS code . See the following code demonstration:

l_scriptcontrol: = TscriptControl.create (nil); // Create a Script control object

? l_scriptcontrol.allowui: = true;

? l_scriptcontrol.language: = 'vbscript'; // Determine Script language

? v: = VararrayCreate ([0, 0], Varvariant);

? v [0]: = INPUTVARARRAYS;

PARAMS: = PsafeArray (Tvardata (v) .vaRray);

? l_scriptcode: = TSTRINGLIST.CREATE;

? Try

l_scriptcode.loadFromfile (VBScript_filename '. VBS'); // Import VBS file program

IF l_scriptcode.count = 0.

l_scriptcontrol.addcode (l_scriptcode.text);

Result: = l_scriptcontrol.run (vbscript_filename, params); // Run VBS program

Finally

????? l_scriptcontrol.free; // finally release all VBS objects

??? l_scriptcode.free;

? End;

This allows you to successfully call a VBS program for the required operation. But this is just the most basic operation, and there is still a distance from the actual needs. For example, if you want to improve the code reuse and efficiency, some common VBS functions written in advance, such as some basic mathematical functions. However, the above example can only call a VBS file, the file name is a function name, which has great limitations.

In order to solve the above problems, there is a corresponding solution. As shown below:

?? ... // Cycle if the VBS file to be loaded is existing

? l_scriptcode: = TSTRINGLIST.CREATE;

? l_scriptcontrol: = TscriptControl.create (nil);

? Try

??? l_scriptcontrol.allowui: = true;

??? l_scriptcontrol.language: = l_vbscript;

??? v: = varVarray ([0,0], varvariant);

??? v [0]: = INPUTVARARRAYS;

??? params: = psafearray (tvardata (v) .vaRray);

// loop to load the required VBS file and check if the file code is empty

??? for l_i: = 0 to vbscript_filenames.count-1 do

??? begin

????? l_scriptcode.loadfromfile (vbscript_filenames.strings [l_i] '. VBS');

????? if l_scriptcode.count = 0 THEN? EXIT;

????? l_scriptcontrol.addcode (l_scriptcode.text);

???

?? // Normal call VBS program

OutputDataSets: = l_scriptcontrol.run (vbscript_filenames.strings [0], params);

Finally

??? L_ScriptControl.Free;

??? l_scriptcode.free;

? End;

To compare the differences in two programs, first, the second program can load multiple files, where the first file is the same name, the rest of the file contains some basic functions that the main function may call. In order to increase the work of checking function files and their code.

This will easily use some programs written in advance, like a wooden, so that users don't have to learn the profound programming knowledge, skilled programming skills, can easily use the VBS to do what they want. At the same time, it is encountered in the future functional extension, and it is also possible to prepare the VBS function that meets the requirements for the operation we need.

4. to sum up

With the continuous development of computer and information technology, computer software applications become more common, but universal software to design adaptation requirements is still very difficult. This article describes how to integrate Using VBS using Active Scripting technology in Delphi, the purpose is to use scripting language to provide secondary programming capabilities for applications, thereby greatly improving software applicability. Delphi and VBScript are very powerful development tools. If you can combine them well, I believe that you can develop more and more powerful, applicable commercial software.

?

references:

[1] Lu Siwei, Pan Aimin. ActiveX Scriptng Technology (1) One (3). Microcomputer 1999-01 ~ 03

[2]? Steve Teixeora, Xavier Pacheco, Ren Xuzhen, Wang Yongsheng, Feng Translation, Delphi5 Developer Guide Machinery Industry Press, July 2000, July

[3]. Introduction to ActiveX Controls, Microsoft Corp., MSDN, August 1998

[4] http://www.microsoft.com/, Microsoft, including MSDN, MSJ and other technical resources

?

?

转载请注明原文地址:https://www.9cbs.com/read-116941.html

New Post(0)