Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

886 lines
24 KiB

  1. //____________________________________________________________________________
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997 - 1999
  5. //
  6. // File: ScriptHost.cpp
  7. //
  8. // Contents: CScriptHostMgr & CScriptHost implementation.
  9. //
  10. // History: 11/05/1999 AnandhaG Created
  11. //____________________________________________________________________________
  12. //
  13. #include "stdafx.h"
  14. #include "scripthost.h"
  15. //+-------------------------------------------------------------------
  16. // MMCObjectName is the name scripts will use to refer to mmc object.
  17. //
  18. // Example:
  19. //
  20. // Dim doc
  21. // Dim snapins
  22. // Set doc = MMCApplication.Document
  23. // Set snapins = doc.snapins
  24. // snapins.Add "{58221c66-ea27-11cf-adcf-00aa00a80033}" ' the services snap-in
  25. //
  26. //+-------------------------------------------------------------------
  27. const LPOLESTR MMCObjectName = OLESTR("MMCApplication");
  28. //+-------------------------------------------------------------------
  29. //
  30. // Member: CScriptHostMgr::ScInitScriptHostMgr
  31. //
  32. // Synopsis: Get the ITypeInfo of this instance of mmc.
  33. //
  34. // Arguments: [pDispatch]
  35. //
  36. // Returns: SC
  37. //
  38. //--------------------------------------------------------------------
  39. CScriptHostMgr::CScriptHostMgr(LPDISPATCH pDispatch)
  40. {
  41. m_spMMCObjectDispatch = pDispatch;
  42. // It is ok if below fails. These interfaces (dispatch & typeinfo) are required
  43. // in CScriptHost::GetItemInfo method, so that this object can be given to engine.
  44. pDispatch->GetTypeInfo(1, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &m_spMMCObjectTypeInfo);
  45. }
  46. CScriptHostMgr::~CScriptHostMgr()
  47. {
  48. DECLARE_SC(sc, _T("CScriptHostMgr::~CScriptHostMgr"));
  49. // The script manager is going away so ask all the script
  50. // hosts to finish their scripts & then destroy them.
  51. sc = ScDestroyScriptHosts();
  52. }
  53. SC CScriptHostMgr::ScGetMMCObject(LPUNKNOWN *ppunkItem)
  54. {
  55. DECLARE_SC(sc, TEXT("CScriptHostMgr::ScGetMMCObject"));
  56. sc = ScCheckPointers(ppunkItem);
  57. if (sc)
  58. return sc;
  59. if (m_spMMCObjectDispatch)
  60. {
  61. *ppunkItem = m_spMMCObjectDispatch;
  62. return sc;
  63. }
  64. return (sc = E_FAIL);
  65. }
  66. SC CScriptHostMgr::ScGetMMCTypeInfo(LPTYPEINFO *ppTypeInfo)
  67. {
  68. DECLARE_SC(sc, TEXT("CScriptHostMgr::ScGetMMCObject"));
  69. sc = ScCheckPointers(ppTypeInfo);
  70. if (sc)
  71. return sc;
  72. if (m_spMMCObjectDispatch)
  73. {
  74. *ppTypeInfo = m_spMMCObjectTypeInfo;
  75. return sc;
  76. }
  77. return (sc = E_FAIL);
  78. }
  79. //+-------------------------------------------------------------------
  80. //
  81. // Member: ScGetScriptEngineFromExtn
  82. //
  83. // Synopsis: Using the file extension get the script engine & Clsid.
  84. //
  85. // Arguments: [strFileExtn] - Script extension.
  86. // [strScriptEngine] - Type script.
  87. // [rClsid] - CLSID of engine.
  88. //
  89. // Returns: SC
  90. //
  91. //--------------------------------------------------------------------
  92. SC CScriptHostMgr::ScGetScriptEngineFromExtn(const tstring& strFileExtn,
  93. tstring& strScriptEngine,
  94. CLSID& rClsid)
  95. {
  96. DECLARE_SC(sc, _T("CScriptHostMgr::ScGetScriptEngine"));
  97. CRegKey regKey;
  98. // Open the extension.
  99. LONG lRet = regKey.Open(HKEY_CLASSES_ROOT, strFileExtn.data(), KEY_READ);
  100. if (ERROR_SUCCESS != lRet)
  101. {
  102. sc.FromWin32(lRet);
  103. return sc;
  104. }
  105. TCHAR szTemp[MAX_PATH];
  106. DWORD dwLen = MAX_PATH;
  107. tstring strTemp;
  108. // Read the default value, the location of file association data.
  109. lRet = regKey.QueryValue(szTemp, NULL, &dwLen);
  110. if (ERROR_SUCCESS != lRet)
  111. {
  112. sc.FromWin32(lRet);
  113. return sc;
  114. }
  115. ASSERT(dwLen > 0);
  116. // Open the HKCR/FileAssocLoc/ScriptEngine.
  117. strTemp = szTemp;
  118. strTemp += _T("\\");
  119. strTemp += SCRIPT_ENGINE_KEY;
  120. lRet = regKey.Open(HKEY_CLASSES_ROOT, strTemp.data(), KEY_READ);
  121. if (ERROR_SUCCESS != lRet)
  122. {
  123. sc.FromWin32(lRet);
  124. return sc;
  125. }
  126. // Now read the ScriptEngine default value.
  127. dwLen = MAX_PATH;
  128. lRet = regKey.QueryValue(szTemp, NULL, &dwLen);
  129. if (ERROR_SUCCESS != lRet)
  130. {
  131. sc.FromWin32(lRet);
  132. return sc;
  133. }
  134. ASSERT(dwLen > 0);
  135. strScriptEngine = szTemp;
  136. // Read HKCR/ScriptEngine/CLSID for ScriptEngine clsid.
  137. strTemp = strScriptEngine + _T("\\");
  138. strTemp += CLSIDSTR;
  139. lRet = regKey.Open(HKEY_CLASSES_ROOT, strTemp.data(), KEY_READ);
  140. if (ERROR_SUCCESS != lRet)
  141. {
  142. sc.FromWin32(lRet);
  143. return sc;
  144. }
  145. // Read the CLSID value.
  146. dwLen = MAX_PATH;
  147. lRet = regKey.QueryValue(szTemp, NULL, &dwLen);
  148. if (ERROR_SUCCESS != lRet)
  149. {
  150. sc.FromWin32(lRet);
  151. return sc;
  152. }
  153. ASSERT(dwLen > 0);
  154. USES_CONVERSION;
  155. LPOLESTR lpClsid = T2OLE(szTemp);
  156. sc = CLSIDFromString(lpClsid, &rClsid);
  157. if (sc)
  158. return sc;
  159. return (sc);
  160. }
  161. //+-------------------------------------------------------------------
  162. //
  163. // Member: ScGetScriptEngine
  164. //
  165. // Synopsis: [strFileName] - Script file name.
  166. // [eScriptType] - Type script.
  167. // [rClsid] - CLSID of engine.
  168. //
  169. // Arguments:
  170. //
  171. // Returns: SC
  172. //
  173. //--------------------------------------------------------------------
  174. SC CScriptHostMgr::ScGetScriptEngine(const tstring& strFileName,
  175. tstring& strScriptEngine,
  176. CLSID& rClsid)
  177. {
  178. DECLARE_SC(sc, _T("CScriptHostMgr::ScGetScriptEngine"));
  179. // Is this required, the file is already read.
  180. // It is a file & it exists.
  181. DWORD dwAttr = GetFileAttributes(strFileName.data());
  182. if (-1 == dwAttr)
  183. {
  184. // What if lasterror is overwritten?
  185. sc.FromWin32(::GetLastError());
  186. return sc;
  187. }
  188. // Get the extension (look for . from end).
  189. int iPos = strFileName.rfind(_T('.'));
  190. tstring strExtn;
  191. if (-1 != iPos)
  192. {
  193. strExtn = strFileName.substr(iPos, strFileName.length());
  194. }
  195. else
  196. {
  197. sc = E_UNEXPECTED;
  198. return sc;
  199. }
  200. sc = ScGetScriptEngineFromExtn(strExtn, strScriptEngine, rClsid);
  201. if (sc)
  202. return sc;
  203. return (sc);
  204. }
  205. //+-------------------------------------------------------------------
  206. //
  207. // Member: ScLoadScriptFromFile
  208. //
  209. // Synopsis: Allocate memory & Load the script from the given file.
  210. //
  211. // Arguments: [strFileName] - File to be loaded.
  212. // [pszScriptContents] - Memory buffer containing the script
  213. // contents (See note).
  214. //
  215. // Note: The caller should call HeapFree() to free the pszScriptContents.
  216. //
  217. // Returns: SC
  218. //
  219. //--------------------------------------------------------------------
  220. SC CScriptHostMgr::ScLoadScriptFromFile (const tstring& strFileName, LPOLESTR* pszScriptText)
  221. {
  222. DECLARE_SC(sc, _T("CScriptHostMgr::ScLoadScriptFromFile"));
  223. sc = ScCheckPointers(pszScriptText);
  224. if (sc)
  225. return sc;
  226. *pszScriptText = NULL;
  227. // Open the file.
  228. HANDLE hFile = ::CreateFile(strFileName.data(),
  229. GENERIC_READ,
  230. FILE_SHARE_READ,
  231. NULL,
  232. OPEN_EXISTING,
  233. FILE_ATTRIBUTE_NORMAL,
  234. NULL );
  235. if (hFile == INVALID_HANDLE_VALUE)
  236. {
  237. sc.FromWin32(::GetLastError());
  238. return sc;
  239. }
  240. HANDLE hFileMap = NULL;
  241. LPSTR pszMBCS = NULL;
  242. DWORD dwFileSize = ::GetFileSize(hFile, NULL);
  243. if (dwFileSize == 0xFFFFFFFF)
  244. {
  245. sc.FromWin32(::GetLastError());
  246. goto FileError;
  247. }
  248. if (dwFileSize == 0)
  249. {
  250. sc = E_UNEXPECTED;
  251. goto FileError;
  252. }
  253. // Create a file mapping object.
  254. hFileMap = ::CreateFileMapping(hFile,
  255. NULL,
  256. PAGE_READONLY,
  257. 0, dwFileSize,
  258. NULL );
  259. if (hFileMap == NULL)
  260. {
  261. sc.FromWin32(::GetLastError());
  262. goto FileError;
  263. }
  264. // Dummy block.
  265. {
  266. // Map the file into memory.
  267. pszMBCS = (LPSTR) ::MapViewOfFile(hFileMap,
  268. FILE_MAP_READ,
  269. 0, 0,
  270. 0 );
  271. if (pszMBCS == NULL)
  272. {
  273. sc.FromWin32(::GetLastError());
  274. goto FileMapError;
  275. }
  276. // Get the size of buffer needed.
  277. int n = ::MultiByteToWideChar(CP_ACP,
  278. 0,
  279. pszMBCS, dwFileSize,
  280. NULL, 0 );
  281. //
  282. // Allocate script text buffer. +1 for EOS.
  283. //
  284. LPOLESTR pszText;
  285. pszText = (LPOLESTR) ::HeapAlloc(::GetProcessHeap(),
  286. 0,
  287. (n + 2) * sizeof(wchar_t) );
  288. if (pszText == NULL)
  289. {
  290. sc.FromWin32(::GetLastError());
  291. goto FileAllocError;
  292. }
  293. // Store file as WCHAR inthe buffer.
  294. ::MultiByteToWideChar(CP_ACP,
  295. 0,
  296. pszMBCS, dwFileSize,
  297. pszText, n );
  298. //
  299. // Remove legacy EOF character.
  300. //
  301. if (pszText[n - 1] == 0x1A)
  302. {
  303. pszText[n - 1] = '\n';
  304. }
  305. pszText[n] = '\n';
  306. pszText[n + 1] = '\0';
  307. *pszScriptText = pszText;
  308. }
  309. FileAllocError:
  310. ::UnmapViewOfFile(pszMBCS);
  311. FileMapError:
  312. ::CloseHandle(hFileMap);
  313. FileError:
  314. ::CloseHandle(hFile);
  315. //NoError:
  316. return sc;
  317. }
  318. //+-------------------------------------------------------------------
  319. //
  320. // Member: ScExecuteScript
  321. //
  322. // Synopsis: Execute given script file.
  323. //
  324. // Arguments: [strFileName] - The script file.
  325. //
  326. // Returns: SC
  327. //
  328. //--------------------------------------------------------------------
  329. SC CScriptHostMgr::ScExecuteScript(const tstring& strFileName)
  330. {
  331. DECLARE_SC(sc, _T("CScriptHostMgr::ScExecuteScript"));
  332. CHeapAllocMemPtr<OLECHAR> spszFileContents;
  333. sc = ScLoadScriptFromFile(strFileName, &spszFileContents);
  334. if (sc)
  335. return sc;
  336. tstring strScriptEngine;
  337. CLSID EngineClsid;
  338. // Validate the file, get the script engine and script type.
  339. sc = ScGetScriptEngine(strFileName, strScriptEngine, EngineClsid);
  340. if (sc)
  341. return sc;
  342. sc = ScExecuteScriptHelper(spszFileContents, strScriptEngine, EngineClsid);
  343. return sc;
  344. }
  345. //+-------------------------------------------------------------------
  346. //
  347. // Member: ScExecuteScript
  348. //
  349. // Synopsis: Execute given script.
  350. //
  351. // Arguments: [pszScriptText] - The script itself.
  352. // [strExtn] - The script file extension.
  353. //
  354. // Note: The extension is used to determine the script
  355. // engine (as shell does).
  356. //
  357. // Returns: SC
  358. //
  359. //--------------------------------------------------------------------
  360. SC CScriptHostMgr::ScExecuteScript(LPOLESTR pszScriptText, const tstring& strExtn)
  361. {
  362. DECLARE_SC(sc, _T("CScriptHostMgr::ScExecuteScript"));
  363. tstring strScriptEngine;
  364. CLSID EngineClsid;
  365. // Validate the file, get the script engine and script type.
  366. sc = ScGetScriptEngineFromExtn(strExtn, strScriptEngine, EngineClsid);
  367. if (sc)
  368. return sc;
  369. sc = ScExecuteScriptHelper(pszScriptText, strScriptEngine, EngineClsid);
  370. return sc;
  371. }
  372. //+-------------------------------------------------------------------
  373. //
  374. // Member: ScExecuteScriptHelper
  375. //
  376. // Synopsis: Helper for ScExecuteScript, Create the Script Host &
  377. // asks it to run the script.
  378. //
  379. // Arguments: [pszScriptText] - The script contents.
  380. // [strScriptEngine] - The script engine name.
  381. // [EngineClsid]
  382. //
  383. // Returns: SC
  384. //
  385. //--------------------------------------------------------------------
  386. SC CScriptHostMgr::ScExecuteScriptHelper (LPCOLESTR pszScriptText,
  387. const tstring strScriptEngine,
  388. const CLSID& EngineClsid)
  389. {
  390. DECLARE_SC(sc, _T("ScExecuteScriptHelper"));
  391. // Create CScriptHost and ask it to run the script.
  392. CComObject<CScriptHost>* pScriptHost = NULL;
  393. sc = CComObject<CScriptHost>::CreateInstance(&pScriptHost);
  394. if (sc)
  395. return sc;
  396. if (NULL == pScriptHost)
  397. return (sc = E_FAIL);
  398. IUnknownPtr spUnknown = pScriptHost;
  399. if (NULL == spUnknown)
  400. return (sc = E_UNEXPECTED);
  401. m_ArrayOfHosts.push_back(spUnknown);
  402. sc = pScriptHost->ScRunScript(this, pszScriptText, strScriptEngine, EngineClsid);
  403. if (sc)
  404. return sc;
  405. return (sc);
  406. }
  407. //+-------------------------------------------------------------------
  408. //
  409. // Member: ScDestroyScriptHosts
  410. //
  411. // Synopsis: Stop all the running scripts and destroy all
  412. // script hosts.
  413. //
  414. // Arguments:
  415. //
  416. // Returns: SC
  417. //
  418. //--------------------------------------------------------------------
  419. SC CScriptHostMgr::ScDestroyScriptHosts()
  420. {
  421. DECLARE_SC(sc, _T("CScriptHostMgr::ScStopAllScripts"));
  422. // Ask each script host created to stop its script.
  423. ArrayOfScriptHosts::iterator it = m_ArrayOfHosts.begin();
  424. for (;it != m_ArrayOfHosts.end(); ++it)
  425. {
  426. CScriptHost* pScriptHost = dynamic_cast<CScriptHost*>(it->GetInterfacePtr());
  427. sc = ScCheckPointers(pScriptHost, E_UNEXPECTED);
  428. if (sc)
  429. return sc;
  430. sc = pScriptHost->ScStopScript();
  431. }
  432. // This clear will call release on the IUnknown smart-pointers (that are in this array).
  433. m_ArrayOfHosts.clear();
  434. return sc;
  435. }
  436. CScriptHost::CScriptHost() :
  437. m_pScriptHostMgr(NULL)
  438. {
  439. }
  440. CScriptHost::~CScriptHost()
  441. {
  442. }
  443. //+-------------------------------------------------------------------
  444. //
  445. // Member: ScRunScript
  446. //
  447. // Synopsis: Run the given script
  448. //
  449. // Arguments: [pMgr] - Object that manages all CScriptHosts.
  450. // [strScript] - The script itself.
  451. // [strEngineName] - Script engine name.
  452. // [rEngineClsid] - The script engine that runs this script.
  453. //
  454. // Returns: SC
  455. //
  456. //--------------------------------------------------------------------
  457. SC CScriptHost::ScRunScript(CScriptHostMgr* pMgr, LPCOLESTR pszScriptText,
  458. const tstring& strEngineName, const CLSID& rEngineClsid)
  459. {
  460. DECLARE_SC(sc, _T("CScriptHost::ScRunScript"));
  461. m_pScriptHostMgr = pMgr;
  462. sc = ScCheckPointers(m_pScriptHostMgr, E_UNEXPECTED);
  463. if (sc)
  464. return sc;
  465. m_strScriptEngine = strEngineName;
  466. m_EngineClsid = rEngineClsid;
  467. // Now create the script engine.
  468. LPUNKNOWN* pUnknown = NULL;
  469. sc = CoCreateInstance(m_EngineClsid, NULL, CLSCTX_INPROC_SERVER,
  470. IID_IActiveScript, (void **)&m_spActiveScriptEngine);
  471. if (sc)
  472. return sc;
  473. m_spActiveScriptParser = m_spActiveScriptEngine;
  474. if (NULL == m_spActiveScriptParser)
  475. {
  476. m_spActiveScriptEngine = NULL; // Release the engine.
  477. return (sc = E_FAIL);
  478. }
  479. sc = m_spActiveScriptEngine->SetScriptSite(this);
  480. if (sc)
  481. return sc;
  482. sc = m_spActiveScriptParser->InitNew();
  483. if (sc)
  484. return sc;
  485. // Add MMC objects to the top-level.
  486. sc = m_spActiveScriptEngine->AddNamedItem(MMCObjectName,
  487. SCRIPTITEM_ISSOURCE |
  488. SCRIPTITEM_GLOBALMEMBERS |
  489. SCRIPTITEM_ISVISIBLE);
  490. if (sc)
  491. {
  492. m_spActiveScriptEngine = NULL;
  493. m_spActiveScriptParser = NULL;
  494. return sc;
  495. }
  496. sc = m_spActiveScriptParser->ParseScriptText(pszScriptText, NULL, NULL, NULL,
  497. 0, 0, 0L, NULL, NULL);
  498. if (sc)
  499. {
  500. m_spActiveScriptEngine = NULL;
  501. m_spActiveScriptParser = NULL;
  502. return sc;
  503. }
  504. sc = m_spActiveScriptEngine->SetScriptState(SCRIPTSTATE_CONNECTED);
  505. if (sc)
  506. {
  507. m_spActiveScriptEngine = NULL;
  508. m_spActiveScriptParser = NULL;
  509. return sc;
  510. }
  511. return sc;
  512. }
  513. //+-------------------------------------------------------------------
  514. //
  515. // Member: ScStopScript
  516. //
  517. // Synopsis: Stop the script engine.
  518. //
  519. // Arguments:
  520. //
  521. // Returns: SC
  522. //
  523. //--------------------------------------------------------------------
  524. SC CScriptHost::ScStopScript ()
  525. {
  526. DECLARE_SC(sc, _T("CScriptHost::ScStopScript"));
  527. sc = ScCheckPointers(m_spActiveScriptEngine, E_UNEXPECTED);
  528. if (sc)
  529. return sc;
  530. sc = m_spActiveScriptEngine->SetScriptState(SCRIPTSTATE_DISCONNECTED);
  531. sc = m_spActiveScriptEngine->Close();
  532. return (sc);
  533. }
  534. //+-------------------------------------------------------------------
  535. //
  536. // Member: GetLCID
  537. //
  538. // Synopsis: Return the Lang Id to Script engine.
  539. //
  540. // Arguments: [plcid] - Language Identifier.
  541. //
  542. // Returns: HRESULT
  543. //
  544. //--------------------------------------------------------------------
  545. STDMETHODIMP CScriptHost::GetLCID( LCID *plcid)
  546. {
  547. DECLARE_SC(sc, _T("CScriptHost::GetLCID"));
  548. sc = ScCheckPointers(plcid);
  549. if (sc)
  550. return sc.ToHr();
  551. *plcid = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
  552. return sc.ToHr();
  553. }
  554. //+-------------------------------------------------------------------
  555. //
  556. // Member: GetItemInfo
  557. //
  558. // Synopsis: Return IUnknown or ITypeInfo of the item added using
  559. // IActiveScript::AddNamedItem. Called by script engine.
  560. //
  561. // Arguments: [pstrName] - The item that was added.
  562. // [dwReturnMask] - Request (IUnknown or ITypeInfo).
  563. // [ppunkItem] - IUnknown returned if requested.
  564. // [ppTypeInfo] - ITypeInfo returned if requested.
  565. //
  566. // Returns: HRESULT
  567. //
  568. //--------------------------------------------------------------------
  569. STDMETHODIMP CScriptHost::GetItemInfo( LPCOLESTR pstrName, DWORD dwReturnMask,
  570. IUnknown **ppunkItem, ITypeInfo **ppTypeInfo)
  571. {
  572. DECLARE_SC(sc, _T("CScriptHost::GetItemInfo"));
  573. // The IUnknown** & ITypeInfo** can be NULL.
  574. if (ppunkItem)
  575. *ppunkItem = NULL;
  576. if (ppTypeInfo)
  577. *ppTypeInfo = NULL;
  578. // Make sure it is our object being requested.
  579. if (_wcsicmp(MMCObjectName, pstrName))
  580. return (sc = TYPE_E_ELEMENTNOTFOUND).ToHr();
  581. sc = ScCheckPointers(m_pScriptHostMgr, E_UNEXPECTED);
  582. if (sc)
  583. return sc.ToHr();
  584. if (dwReturnMask & SCRIPTINFO_IUNKNOWN)
  585. {
  586. if (ppunkItem)
  587. {
  588. sc = m_pScriptHostMgr->ScGetMMCObject(ppunkItem);
  589. if (sc)
  590. return sc.ToHr();
  591. (*ppunkItem)->AddRef();
  592. }
  593. else
  594. return (sc = E_POINTER).ToHr();
  595. }
  596. if (dwReturnMask & SCRIPTINFO_ITYPEINFO)
  597. {
  598. if (ppTypeInfo)
  599. {
  600. sc = m_pScriptHostMgr->ScGetMMCTypeInfo(ppTypeInfo);
  601. if (sc)
  602. return sc.ToHr();
  603. (*ppTypeInfo)->AddRef();
  604. }
  605. else
  606. return (sc = E_POINTER).ToHr();
  607. }
  608. return sc.ToHr();
  609. }
  610. //+-------------------------------------------------------------------
  611. //
  612. // Member: GetDocVersionString
  613. //
  614. // Synopsis: This retrieves a host-defined string that uniquely
  615. // identifies the current script (document) version from
  616. // the host's point of view. Called by script engine.
  617. //
  618. // Arguments: [pbstrVersionString] - The doc version string.
  619. //
  620. // Returns: HRESULT
  621. //
  622. //--------------------------------------------------------------------
  623. STDMETHODIMP CScriptHost::GetDocVersionString( BSTR *pbstrVersionString)
  624. {
  625. DECLARE_SC(sc, _T("CScriptHost::GetDocVersionString"));
  626. return E_NOTIMPL;
  627. }
  628. //+-------------------------------------------------------------------
  629. //
  630. // Member: OnScriptTerminate
  631. //
  632. // Synopsis: Called by engine when the script has completed execution.
  633. //
  634. // Arguments: [pvarResult] - Script results.
  635. // [pexcepinfo] - Any exceptions generated.
  636. //
  637. // Returns: HRESULT
  638. //
  639. //--------------------------------------------------------------------
  640. STDMETHODIMP CScriptHost::OnScriptTerminate( const VARIANT *pvarResult,
  641. const EXCEPINFO *pexcepinfo)
  642. {
  643. DECLARE_SC(sc, _T("CScriptHost::OnScriptTerminate"));
  644. return sc.ToHr();
  645. }
  646. //+-------------------------------------------------------------------
  647. //
  648. // Member: OnStateChange
  649. //
  650. // Synopsis: Called by engine when its state changes.
  651. //
  652. // Arguments: [ssScriptState] - New state.
  653. //
  654. // Returns: HRESULT
  655. //
  656. //--------------------------------------------------------------------
  657. STDMETHODIMP CScriptHost::OnStateChange(SCRIPTSTATE ssScriptState)
  658. {
  659. DECLARE_SC(sc, _T("CScriptHost::OnStateChange"));
  660. return sc.ToHr();
  661. }
  662. //+-------------------------------------------------------------------
  663. //
  664. // Member: OnScriptError
  665. //
  666. // Synopsis: Engine informs that an execution error occurred
  667. // while it was running the script.
  668. //
  669. // Arguments: [pase ] - Host can obtain info about execution
  670. // error using this interface.
  671. //
  672. // Returns: HRESULT
  673. //
  674. //--------------------------------------------------------------------
  675. STDMETHODIMP CScriptHost::OnScriptError(IActiveScriptError *pase)
  676. {
  677. DECLARE_SC(sc, _T("CScriptHost::OnScriptError"));
  678. sc = ScCheckPointers(pase);
  679. if (sc)
  680. return sc.ToHr();
  681. // For test purposes. We need to provide much better debug info,
  682. // We will hookup the ScriptDebugger for this.
  683. BSTR bstrSourceLine;
  684. sc = pase->GetSourceLineText(&bstrSourceLine);
  685. EXCEPINFO exinfo;
  686. ZeroMemory(&exinfo, sizeof(exinfo));
  687. sc = pase->GetExceptionInfo(&exinfo);
  688. DWORD dwSourceContext = 0;
  689. ULONG ulLineNumber = -1;
  690. LONG lCharPos = -1;
  691. sc = pase->GetSourcePosition(&dwSourceContext, &ulLineNumber, &lCharPos);
  692. return sc.ToHr();
  693. }
  694. //+-------------------------------------------------------------------
  695. //
  696. // Member: OnEnterScript
  697. //
  698. // Synopsis: Engine informs that it has begun executing script.
  699. //
  700. // Arguments:
  701. //
  702. // Returns: HRESULT
  703. //
  704. //--------------------------------------------------------------------
  705. STDMETHODIMP CScriptHost::OnEnterScript(void)
  706. {
  707. DECLARE_SC(sc, _T("CScriptHost::OnEnterScript"));
  708. return sc.ToHr();
  709. }
  710. //+-------------------------------------------------------------------
  711. //
  712. // Member: OnEnterScript
  713. //
  714. // Synopsis: Engine informs that it has returned from executing script.
  715. //
  716. // Arguments:
  717. //
  718. // Returns: HRESULT
  719. //
  720. //--------------------------------------------------------------------
  721. STDMETHODIMP CScriptHost::OnLeaveScript(void)
  722. {
  723. DECLARE_SC(sc, _T("CScriptHost::OnLeaveScript"));
  724. return sc.ToHr();
  725. }
  726. //+-------------------------------------------------------------------
  727. //
  728. // Member: GetWindow
  729. //
  730. // Synopsis: Engine asks for window that can be parent of a popup
  731. // it can display.
  732. //
  733. // Arguments: [phwnd ] - Parent window.
  734. //
  735. // Returns: HRESULT
  736. //
  737. //--------------------------------------------------------------------
  738. STDMETHODIMP CScriptHost::GetWindow(HWND *phwnd)
  739. {
  740. DECLARE_SC(sc, _T("CScriptHost::GetWindow"));
  741. return sc.ToHr();
  742. }
  743. //+-------------------------------------------------------------------
  744. //
  745. // Member: EnableModeless
  746. //
  747. // Synopsis: Enables/Disables modelessness of parent window.
  748. //
  749. // Arguments: [fEnable ] - Enable/Disable.
  750. //
  751. // Returns: HRESULT
  752. //
  753. //--------------------------------------------------------------------
  754. STDMETHODIMP CScriptHost::EnableModeless(BOOL fEnable)
  755. {
  756. DECLARE_SC(sc, _T("CScriptHost::EnableModeless"));
  757. return sc.ToHr();
  758. }