VBScript (JScript) is a scripting language that is widely used and easy to learn. It is easier to invoke a COM component using VBScript (JScript). Then, if you can execute the script in your program, it will greatly improve the function of the software on the basis of simplifying the program design. To facilitate the use of this feature, I wrote an EXE wrapper with an automated (Automation/idispatch) function to invoke script execution. This wrapper interface has only 2 functions, as described below:
Void setscriptlanguage (long nlanguage);
Features: Setting types that use scripting languages
Parameters: 0 = use vbscript 1 = use JScript
Note: If you do not call this function, the default is to use VBScript
Void Run (LPCSTR lpszprogram);
Functions: Executing scripts
Parameters: Scripting Programs
Use methods and steps: (The following instructions, is based on VC6, VC7 users please refer to the operation of changes)
Copy the file to any folder, and then execute the VBSENGIN.EXE program at least once. Complete the registration of the component.
Create or Open an MFC program project.
Add code to do COM initialization. Add a call to AfxOleInit () in the InitInstance () function of the app. or call function CoInitialize (NULL) before using COM functionality, and call CoUninitialize () after COM functionality finishes.
Add Vbsengin.tlb to the project. Open the ClassWizard dialog window and execute the from a type library of the Add Class button. Then select the Vbsengin.tlb file and add the wrapper class to the Ivbsengin interface.
In the appropriate place in your program (such as the Onbutton () function), complete the following code and execute the script.
#include "vbsengin.h"
OnButton()
{
IVBSEngin vbsEngin; //声明一个脚本执行代理对象
if(vbsEngin.CreateDispatch("VBSEngin.Api")) //启动脚本执行代理
{
vbsEngin.Run(sProgram); //执行脚本,sProgram中存放的是脚本源程序
vbsEngin.ReleaseDispatch(); //释放脚本执行代理
}
}