Integrated vbs script language application in Delphi

Source: Internet
Author: User
Tags mathematical functions

Luo Yi from Wei Wang zhenghao

Abstract: Using ActiveX Scripting technology, you can integrate and use the scripting language in applications. This article describes how to apply this technology to add VBScript support to Delphi applications.
Keywords: ActiveX script description; Delphi; VBScript

1. Introduction
The Delphi language is a powerful programming language developed by Borland. It is based on serial port and object-oriented, and has the powerful functions of VC and the advantages of quick development, easy to learn and use of VB, it features high development efficiency, convenient UI creation, and various operation functions. Vbs, short for Visual BASIC script, is a simple, easy-to-learn and powerful scripting language and widely used.
This article introduces how to call and operate vbs in Delphi through ActiveX Scripting technology, implements mixed programming of Delphi and vbs, and gives full play to their respective advantages.

2. Basic Principles
2.1 Basic concepts of ActiveX technology
Vbs cannot be called directly in Delphi, but ActiveX technology can solve this problem. ActiveX is a technology proposed by Microsoft in 1996. It is based on COM (Component Object Model, Component Object Model), so that different processes (especially Network processes) can communicate with each other. ActiveX control is a module integration protocol provided by Microsoft. It is a portable software module and is applicable to various development languages and is irrelevant to the development platform. Activexators are composed of executable code that complies with activex, such as an .exe,. dll, or. ocx file. After ActiveX control is added to a program, it becomes part of the development and running environment and provides new functions for the application. ActiveX controls retain the attributes, events, and methods of some common Delphi controls. Their unique methods and attributes greatly enhance the capabilities and flexibility of programmers.
2.2 ActiveX Scripting)
Using ActiveX script description (ActiveX Scripting) technology in ActiveX technology, ActiveX Control and Java programs can be manipulated from the client or server side, data can be transferred, and operations between them can be coordinated.
The ActiveX Scripting system is composed of a COM interface family, which defines a protocol to connect the script engine with the Host Program. In the world of ActiveX Scripting, the script engine is just a component object that can dynamically execute script programs. The Host application can expose its automation interface to the namespace of the script engine, and access the objects of the application in the dynamically executed script as the variables in the program.
The application creates and initializes the Script Engine Based on the script language to be parsed, and then connects the application to the engine. Then, submit the script code to the script engine, and the script code can be executed.
ActiveX Scripting uses wsh, that is, Windows Scripting host, as the working environment of the scripting language. Wsh is a 32-bit Windows platform embedded in the system and a language-independent script running environment. wsh is based on ActiveX, using the scripting engine controller that acts as ActiveX, Windows users are able to take full advantage of the powerful scripting language.

3. Call the implementation of vbs in Delphi
To call vbs in Delphi, you must first install ActiveX controls in the Delphi integration environment.
To import ActiveX control, select component in the Delphi menu, select import ActiveX control, install Microsoft Script control, and then install ActiveX control in the Delphi environment, then add the following statement in the program code: Use msscriptcontrol_tlb to call the vbs code.
For the rest of the code, see the following code example:
Rochelle scriptcontrol: = tscriptcontrol. Create (NiL); // create a script control object
Rochelle scriptcontrol.allowui: = true;
Rochelle scriptcontrol.language: = 'vbscript'; // determine the script language.
V: = vararraycreate ([0, 0], varvariant );
V [0]: = inputvararrays;
Params: = psafearray (tvardata (V). varray );
Rochelle scriptcode: = tstringlist. Create;
Try
Rochelle scriptcode.loadfromfile (vbscript_filename + '. vbs'); // import the vbs file program
If l_scriptcode.count = 0 Then exit;
Rochelle scriptcontrol.addcode (Rochelle scriptcode.text );
Result: = l_scriptcontrol.run (vbscript_filename, Params); // run the vbs Program
Finally
Rochelle scriptcontrol.free; // finally releases all vbs objects.
Rochelle scriptcode.free;
End;
In this way, you can successfully call a vbs program to perform the required operations. However, this is only the most basic operation, and there is still a gap between the actual needs. For example, if you want to improve code reusability and efficiency, you can use some common vbs functions, such as some basic mathematical functions. However, the preceding example can only call one vbs file. The file name is the function name, which has great limitations.
There are corresponding solutions to solve the above problems. The following procedure is shown:
...... // Cyclically check whether the vbs file to be mounted exists
Rochelle scriptcode: = tstringlist. Create;
Rochelle scriptcontrol: = tscriptcontrol. Create (NiL );
Try
Rochelle scriptcontrol.allowui: = true;
Rochelle scriptcontrol.language: = l_vbscript;
V: = vararraycreate ([0, 0], varvariant );
V [0]: = inputvararrays;
Params: = psafearray (tvardata (V). varray );
// Cyclically mount the required vbs file and check whether the file code is empty
For Rochelle I: = 0 to VBScript_filenames.Count-1 do
Begin
Rochelle scriptcode.loadfromfile (vbscript_filenames.strings [L_ I] + '. vbs ');
If l_scriptcode.count = 0 Then exit;
Rochelle scriptcontrol.addcode (Rochelle scriptcode.text );
End;
// Call the vbs program normally
Outputdatasets: = l_scriptcontrol.run (vbscript_filenames.strings [0], Params );
Finally
Rochelle scriptcontrol.free;
Rochelle scriptcode.free;
End;
First, the second program can load multiple files. The first file has the same name as the main function to be executed, the remaining files contain some basic functions that the main function may call. Therefore, the function file and its code check are added.
In this way, you can easily use some programs written in advance, like building blocks, so that users do not have to learn profound programming knowledge and have skilled programming skills, you can easily use vbs to perform the operations you want. At the same time, you can write vbs functions that meet the requirements for function expansion in the future.
4. Summary
With the continuous development of computer and information technology, computer software applications become more and more common, but it is still very difficult to design general-purpose software to adapt to the requirements. This article introduces how to use vbs in Delphi by using the active scripting technology, in order to use the script language to provide secondary programming capabilities for applications, thus greatly improving the applicability of the software. Delphi and VBScript are both very powerful development tools. If we can combine them well, we believe that we can develop more and more powerful and adaptable commercial software.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.