1. Import library, the instruction is generally added to the stdafx.h
#import "MSScript.ocx" No_namespace
2. Create a Script object
Script engine
Iscriptcontrolptr M_pscript;
M_pscript.createinstance (__uuidof (ScriptControl));
m_pscript->timeout = 1000 * ntimeout; Timeout setting
M_pscript->putallowui (VARIANT_FALSE);
M_pscript->putlanguage (_bstr_t (_t ("JScript"));
3. Read script files
Wstring js = read_file ("C:/1.js");//read_file function omitted here
4. Add code and Execute
Add code
M_pscript->addcode (_bstr_t (Js.c_str ()));
Execution code (there is a main function in 1.js)
M_pscript-> (_bstr_t (_t ("Main ()"));
As for the interaction between the script and VC, you can create COM components to implement, in the VC project, add a COM component, such as Ivrengine, and then add a method, before executing the script, add the object to the script engine, the code is as follows:
Secondary objects
Ccomptr<iivrengine> Pivrengine;
Pivrengine.cocreateinstance (__uuidof (ivrengine));
M_pscript->addobject (_bstr_t ("IVR"), Pivrengine, VARIANT_TRUE);
At this point, in the script (1.js), you can directly invoke the method provided by Iivrengine, such as IVR. Playfile ("C:/1.wav");
Attention
The corresponding class Cscriptobject is already available
BOOL cscriptdemoapp::initinstance ()
{
Initialize COM
CoInitialize (NULL);
.......
}