class CMyScriptHost : public IActiveScriptSite,
public IActiveScriptSiteWindow
* This source code was highlighted with Source Code Highlighter .
STDMETHOD(QueryInterface)(REFIID riid, void * * ppvObj);
STDMETHOD_(ULONG, AddRef)();
STDMETHOD_(ULONG, Release)();
* This source code was highlighted with Source Code Highlighter .
STDMETHOD(GetLCID)(
LCID *plcid ); // address of variable for language identifier
STDMETHOD(GetItemInfo)(
LPCOLESTR pstrName, // address of item name
DWORD dwReturnMask, // bit mask for information retrieval
IUnknown **ppunkItem, // address of pointer to item's IUnknown
ITypeInfo **ppTypeInfo); // address of pointer to item's ITypeInfo
STDMETHOD(GetDocVersionString)(
BSTR *pbstrVersionString); // address of document version string
STDMETHOD(OnScriptTerminate)(
const VARIANT *pvarResult, // address of script results
const EXCEPINFO *pexcepinfo); // address of structure with exception information
STDMETHOD(OnStateChange)(
SCRIPTSTATE ssScriptState); // new state of engine
STDMETHOD(OnScriptError)(
IActiveScriptError *pase); // address of error interface
STDMETHOD(OnEnterScript)( void );
STDMETHOD(OnLeaveScript)( void );
* This source code was highlighted with Source Code Highlighter .
STDMETHODIMP CMyScriptHost::OnScriptError(IActiveScriptError *pase)
{
#ifdef _DEBUG
EXCEPINFO Exception;
HRESULT hr = pase->GetExceptionInfo(&Exception);
if (SUCCEEDED(hr))
{
CString sErrLog = _T( "" );
sErrLog += _T( "EXCEPINFO" );
sErrLog += _T( "\n\rDescription: " );
sErrLog += Exception.bstrDescription;
sErrLog += _T( "\n\rSource: " );
sErrLog += Exception.bstrSource;
CComBSTR bsSrcLineText;
hr = pase->GetSourceLineText(&bsSrcLineText);
if (SUCCEEDED(hr))
{
sErrLog += _T( "\n\rSource line text: " );
sErrLog += bsSrcLineText;
}
DWORD dwSourceContext = 0;
ULONG ulLineNumber = 0;
LONG lCharacterPosition = 0;
hr = pase->GetSourcePosition(&dwSourceContext, &ulLineNumber, &lCharacterPosition);
if (SUCCEEDED(hr))
{
CString sSourceContext;
sErrLog += _T( "\n\rSource context: " );
sSourceContext.Format(_T( "%d" ), dwSourceContext);
sErrLog += sSourceContext;
CString sLineNumber;
sErrLog += _T( "\n\rLine number: " );
sLineNumber.Format(_T( "%d" ), ulLineNumber);
sErrLog += sLineNumber;
CString sCharPos;
sErrLog += _T( "\n\rCharacterPosition: " );
sCharPos.Format(_T( "%d" ), lCharacterPosition);
sErrLog += sCharPos;
}
::MessageBox(0, sErrLog, COLE2T(Exception.bstrSource), 0);
}
#endif // _DEBUG
return S_OK;
}
* This source code was highlighted with Source Code Highlighter .
CComPtr<IActiveScript> m_pEngine; // reference to the scripting engine <br>CComQIPtr<IActiveScriptParse> m_pParser; // reference to the IActiveScriptParse interface of the scripting engine <br><br> * This source code was highlighted with Source Code Highlighter .
HRESULT Initialize();<br>HRESULT Close();<br>HRESULT PutScript(CString sScriptText);<br>HRESULT CallJSFunction(CString sFuncName, VARIANT *varResult); <br><br> * This source code was highlighted with Source Code Highlighter .
HRESULT CMyScriptHost::Initialize()<br>{<br> HRESULT hr = E_FAIL;<br><br> //First, create the scripting engine with a call to CoCreateInstance, <br> //placing the created engine in m_Engine. <br><br> hr = m_pEngine.CoCreateInstance(CComBSTR(_T( "JScript" )));<br> if (SUCCEEDED(hr) && m_pEngine)<br> {<br> m_pParser = m_pEngine;<br> if (m_pParser)<br> {<br> //The engine needs to know the host it runs on. <br> hr = m_pEngine->SetScriptSite( this );<br> ATLASSERT(SUCCEEDED(hr));<br><br> //Initialize the script engine so it's ready to run. <br> hr = m_pParser->InitNew();<br> ATLASSERT(SUCCEEDED(hr));<br> }<br> }<br><br> return hr;<br>} <br><br> * This source code was highlighted with Source Code Highlighter .
HRESULT CMyScriptHost::Close()
{
HRESULT hr = E_FAIL;
if (m_pEngine)
{
if (m_pParser)
m_pParser.Release();
// Disconnect the host application from the engine. This will prevent the
// further firing of events. Event sinks that are in progress are
// completed before the state changes.
m_pEngine->SetScriptState(SCRIPTSTATE_DISCONNECTED);
// Call to InterruptScriptThread to abandon any running scripts and force
// a cleanup of all script elements.
m_pEngine->InterruptScriptThread(SCRIPTTHREADID_ALL, NULL, 0 );
m_pEngine->Close();
m_pEngine.Release();
hr = S_OK;
}
return hr;
}
* This source code was highlighted with Source Code Highlighter .
HRESULT CMyScriptHost::PutScript( CString sScriptText )
{
HRESULT hr = E_FAIL;
//Pass the script to be run to the script engine with a call to ParseScriptText
hr = m_pParser->ParseScriptText(sScriptText, NULL, NULL, NULL, 0, 0, 0, NULL, NULL);
hr = m_pEngine->SetScriptState(SCRIPTSTATE_CONNECTED);
return hr;
}
* This source code was highlighted with Source Code Highlighter .
HRESULT CMyScriptHost::CallJSFunction( CString sFuncName, VARIANT *varResult )
{
HRESULT hr;
CComPtr<IDispatch> pDispScript;
hr = m_pEngine->GetScriptDispatch( NULL, &pDispScript );
if ( SUCCEEDED(hr) && pDispScript )
{
hr = pDispScript.Invoke0(sFuncName, varResult);
}
return hr;
}
* This source code was highlighted with Source Code Highlighter .
int _tmain( int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
CMyScriptHost* myScriptHost = new CMyScriptHost(); // Script host`
myScriptHost->AddRef();
HRESULT hr = E_FAIL;
hr = myScriptHost->Initialize();
if (SUCCEEDED(hr))
{
// test
// - "Hello, World!"
CString sScriptText = _T( "function test() { \
return 'Hello, World!'; \
}" );
hr = myScriptHost->PutScript(sScriptText);
if (SUCCEEDED(hr))
{
CComVariant varResult; //
hr = myScriptHost->CallJSFunction(_T( "test" ), &varResult); // test
if (SUCCEEDED(hr))
{
_tprintf(_T( "Result: %s\n\r" ), COLE2T(varResult.bstrVal)); //
_tprintf(_T( "\n\rPress any key to exit..." ));
_gettch();
}
}
myScriptHost->Close(); //
}
myScriptHost->Release();
CoUninitialize();
return 0;
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/74548/
All Articles