📄 scriptengine.cpp
字号:
/********************************************************************/
/* */
/* ScriptEngine.cpp */
/* */
/* Implementation of the CScriptEngine class. */
/* */
/* Programmed by Pablo van der Meer */
/* http://www.pablovandermeer.nl */
/* */
/* Last updated: 22 february 2003 */
/* */
/********************************************************************/
#include "stdafx.h"
#include "demo.h"
#include "ScriptEngine.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNCREATE(CScriptEngine, CCmdTarget)
BEGIN_INTERFACE_MAP(CScriptEngine, CCmdTarget)
INTERFACE_PART(CScriptEngine, IID_IActiveScriptSite, ActiveScriptSite)
END_INTERFACE_MAP()
/********************************************************************/
/* */
/* Function name : CScriptEngine::CScriptEngine */
/* Description : Constructor */
/* */
/********************************************************************/
CScriptEngine::CScriptEngine()
{
m_hrScriptError = S_OK;
m_pActiveScript = NULL;
m_pActiveScriptParse = NULL;
}
/********************************************************************/
/* */
/* Function name : CScriptEngine::~CScriptEngine */
/* Description : Destructor */
/* */
/********************************************************************/
CScriptEngine::~CScriptEngine()
{
CleanUp();
}
/********************************************************************/
/* */
/* Function name : Create */
/* Description : Create an instance of a scripting engine */
/* */
/********************************************************************/
BOOL CScriptEngine::Create(REFCLSID clsidScriptEngine)
{
// Start inproc script engine, for example VBSCRIPT.DLL
HRESULT hResult = CoCreateInstance(clsidScriptEngine, NULL, CLSCTX_INPROC_SERVER, IID_IActiveScript, (void **)&m_pActiveScript);
if (SUCCEEDED(hResult))
{
// Get engine's IActiveScriptParse interface
hResult = m_pActiveScript->QueryInterface(IID_IActiveScriptParse, (void **)&m_pActiveScriptParse);
if (SUCCEEDED(hResult))
{
// Give engine our IActiveScriptSite interface
hResult = m_pActiveScript->SetScriptSite(&m_xActiveScriptSite);
if (SUCCEEDED(hResult))
{
// Give the engine a chance to initialize itself
hResult = m_pActiveScriptParse->InitNew();
if (SUCCEEDED(hResult))
{
return TRUE;
}
else
{
m_strLastError = "Failed to initialize script engine.";
}
}
else
{
m_strLastError = "Failed to set script engine's site.";
}
}
else
{
m_strLastError = "Failed to obtain IActiveScriptParse interface.";
}
}
else
{
m_strLastError = "Failed to create script engine.";
}
// release script engine
CleanUp();
return FALSE;
}
/********************************************************************/
/* */
/* Function name : GetScriptState */
/* Description : Determine scripting engine's state */
/* */
/********************************************************************/
SCRIPTSTATE CScriptEngine::GetScriptState()
{
SCRIPTSTATE scriptState = SCRIPTSTATE_UNINITIALIZED;
m_pActiveScript->GetScriptState(&scriptState);
return scriptState;
}
/********************************************************************/
/* */
/* Function name : AddNamedItem */
/* Description : Add a new item to the script's namespace */
/* */
/********************************************************************/
BOOL CScriptEngine::AddNamedItem(LPCOLESTR lpstrName, DWORD dwFlags)
{
HRESULT hResult = m_pActiveScript->AddNamedItem(lpstrName, dwFlags);
return SUCCEEDED(hResult);
}
/********************************************************************/
/* */
/* Function name : AddTypeLib */
/* Description : Add an entire typelib to the script's namespace. */
/* */
/********************************************************************/
BOOL CScriptEngine::AddTypeLib(REFGUID guidTypeLib, DWORD dwMaj, DWORD dwMin, DWORD dwFlags)
{
HRESULT hResult = m_pActiveScript->AddTypeLib(guidTypeLib, dwMaj, dwMin, dwFlags);
return SUCCEEDED(hResult);
}
/********************************************************************/
/* */
/* Function name : GetScriptDispatch */
/* Description : Get the scripting engine's automation interface. */
/* */
/********************************************************************/
IDispatch* CScriptEngine::GetScriptDispatch(LPCOLESTR pstrItemName)
{
IDispatch* pDispatch = NULL;
HRESULT hResult = m_pActiveScript->GetScriptDispatch(pstrItemName, &pDispatch);
return pDispatch;
}
/********************************************************************/
/* */
/* Function name : ParseScriptText */
/* Description : Give the scripting engine the script. */
/* */
/********************************************************************/
BOOL CScriptEngine::ParseScriptText(LPCOLESTR pstrCode,
LPCOLESTR pstrItemName,
IUnknown* punkContext,
LPCOLESTR pstrEndDelimiter,
DWORD dwSourceContextCookie,
ULONG ulStartingLineNumber,
DWORD dwFlags,
VARIANT* pvarResult,
EXCEPINFO* pExcepInfo)
{
HRESULT hResult = m_pActiveScriptParse->ParseScriptText(
pstrCode,
pstrItemName,
punkContext,
pstrEndDelimiter,
dwSourceContextCookie,
ulStartingLineNumber,
dwFlags,
pvarResult,
pExcepInfo);
return SUCCEEDED(hResult);
}
/********************************************************************/
/* */
/* Function name : Connect */
/* Description : Start execution of the script */
/* */
/********************************************************************/
BOOL CScriptEngine::Connect()
{
m_hrScriptError = S_OK;
// Set the engine state. This line actually triggers the execution of the script.
HRESULT hResult = m_pActiveScript->SetScriptState(SCRIPTSTATE_CONNECTED);
// if success
if (SUCCEEDED(hResult))
{
// return any script error caught by OnScriptError()
hResult = m_hrScriptError;
}
return SUCCEEDED(hResult);
}
/********************************************************************/
/* */
/* Function name : Disconnect */
/* Description : Stop execution of the script */
/* */
/********************************************************************/
BOOL CScriptEngine::Disconnect()
{
// Set the engine state.
HRESULT hResult = m_pActiveScript->SetScriptState(SCRIPTSTATE_DISCONNECTED);
return SUCCEEDED(hResult);
}
/********************************************************************/
/* */
/* Function name : Close */
/* Description : Causes the scripting engine to abandon any */
/* currently loaded script, lose its state, and */
/* release any interface pointers it has to other */
/* objects, thus entering a closed state. */
/* */
/********************************************************************/
BOOL CScriptEngine::Close()
{
// call the script's method
HRESULT hResult = m_pActiveScript->Close();
return SUCCEEDED(hResult);
}
/********************************************************************/
/* */
/* Function name : OnGetLCID */
/* Description : Retrieves the locale identifier associated with */
/* the host's user interface. */
/* */
/********************************************************************/
HRESULT CScriptEngine::OnGetLCID(LCID* pLCID)
{
return E_NOTIMPL;
}
/********************************************************************/
/* */
/* Function name : OnGetItemInfo */
/* Description : Allows the scripting engine to obtain */
/* information about an item added with the */
/* IActiveScript::AddNamedItem method. */
/* */
/********************************************************************/
HRESULT CScriptEngine::OnGetItemInfo(LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown** ppUnknownItem, ITypeInfo** ppTypeInfo)
{
return E_NOTIMPL;
}
/********************************************************************/
/* */
/* Function name : OnGetDocVersionString */
/* Description : Retrieves a host-defined string that uniquely */
/* identifies the current document version. */
/* */
/********************************************************************/
BSTR CScriptEngine::OnGetDocVersionString()
{
return NULL;
}
/********************************************************************/
/* */
/* Function name : OnScriptTerminate */
/* Description : The script has completed execution. */
/* */
/********************************************************************/
void CScriptEngine::OnScriptTerminate(const VARIANT* pVarResult, const EXCEPINFO* pExcepInfo)
{
}
/********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -