Source code of Windows XP (NT5)
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.

1128 lines
31 KiB

  1. //+------------------------------------------------------------------------
  2. //
  3. // Microsoft Forms
  4. // Copyright (C) Microsoft Corporation, 1996
  5. //
  6. // File: bsssite.hxx
  7. //
  8. // Contents: CBServerScriptSite
  9. //
  10. //-------------------------------------------------------------------------
  11. #include "headers.hxx"
  12. #undef ASSERT
  13. // ************************************************************************
  14. //
  15. // CConnectionPoint
  16. //
  17. // ************************************************************************
  18. CConnectionPoint::CConnectionPoint(CScriptSite *pSite)
  19. {
  20. _ulRefs = 1;
  21. _pSite = pSite;
  22. _pSite->AddRef();
  23. }
  24. CConnectionPoint::~CConnectionPoint()
  25. {
  26. _pSite->Release();
  27. }
  28. HRESULT
  29. CConnectionPoint::QueryInterface(REFIID iid, void **ppv)
  30. {
  31. if (iid == IID_IUnknown || iid == IID_IConnectionPoint)
  32. {
  33. *ppv = (IConnectionPoint *)this;
  34. }
  35. else
  36. {
  37. *ppv = NULL;
  38. return E_NOINTERFACE;
  39. }
  40. ((IUnknown *)*ppv)->AddRef();
  41. return S_OK;
  42. }
  43. HRESULT
  44. CConnectionPoint::GetConnectionInterface(IID * pIID)
  45. {
  46. *pIID = DIID_DLocalMTScriptEvents;
  47. return S_OK;
  48. }
  49. HRESULT
  50. CConnectionPoint::GetConnectionPointContainer(IConnectionPointContainer ** ppCPC)
  51. {
  52. *ppCPC = _pSite;
  53. (*ppCPC)->AddRef();
  54. return S_OK;
  55. }
  56. HRESULT
  57. CConnectionPoint::Advise(IUnknown *pUnkSink, DWORD *pdwCookie)
  58. {
  59. *pdwCookie = 0;
  60. ClearInterface(&_pSite->_pDispSink);
  61. RRETURN(THR(pUnkSink->QueryInterface(IID_IDispatch, (void **)&_pSite->_pDispSink)));
  62. }
  63. HRESULT
  64. CConnectionPoint::Unadvise(DWORD dwCookie)
  65. {
  66. ClearInterface(&_pSite->_pDispSink);
  67. return S_OK;
  68. }
  69. HRESULT
  70. CConnectionPoint::EnumConnections(LPENUMCONNECTIONS * ppEnum)
  71. {
  72. *ppEnum = NULL;
  73. RRETURN(E_NOTIMPL);
  74. }
  75. // ************************************************************************
  76. //
  77. // CScriptSite
  78. //
  79. // ************************************************************************
  80. STDMETHODIMP
  81. CScriptSite::QueryInterface(REFIID iid, LPVOID * ppv)
  82. {
  83. if (iid == IID_IActiveScriptSite ||
  84. iid == IID_IUnknown)
  85. {
  86. *ppv = (IActiveScriptSite *) this;
  87. }
  88. else if (iid == IID_IGlobalMTScript || iid == IID_IDispatch)
  89. {
  90. *ppv = (IGlobalMTScript *)this;
  91. }
  92. else if (iid == IID_IActiveScriptSiteDebug)
  93. {
  94. *ppv = (IActiveScriptSiteDebug *)this;
  95. }
  96. else if (iid == IID_IConnectionPointContainer)
  97. {
  98. *ppv = (IConnectionPointContainer *)this;
  99. }
  100. else if (iid == IID_IProvideClassInfo ||
  101. iid == IID_IProvideClassInfo2 ||
  102. iid == IID_IProvideMultipleClassInfo)
  103. {
  104. *ppv = (IProvideMultipleClassInfo *)this;
  105. }
  106. else if (iid == IID_IActiveScriptSiteWindow)
  107. {
  108. *ppv = (IActiveScriptSiteWindow *) this;
  109. }
  110. else
  111. {
  112. *ppv = NULL;
  113. RRETURN(E_NOINTERFACE);
  114. }
  115. AddRef();
  116. return S_OK;
  117. }
  118. //---------------------------------------------------------------------------
  119. //
  120. // Method: CScriptSite constructor
  121. //
  122. //---------------------------------------------------------------------------
  123. CScriptSite::CScriptSite(CScriptHost *pScriptHost)
  124. : _cstrName(CSTR_NOINIT)
  125. {
  126. _pSH = pScriptHost;
  127. _ulRefs = 1;
  128. }
  129. //---------------------------------------------------------------------------
  130. //
  131. // Method: CScriptSite destructor
  132. //
  133. //---------------------------------------------------------------------------
  134. CScriptSite::~CScriptSite()
  135. {
  136. VariantClear(&_varParam);
  137. ClearInterface(&_pDispSink);
  138. AssertSz(_ulRefs <= 1, "Object not properly released on destruction!");
  139. if (_pDDH)
  140. {
  141. _pDDH->Detach();
  142. ClearInterface(&_pDDH);
  143. }
  144. }
  145. //---------------------------------------------------------------------------
  146. //
  147. // Method: CScriptSite::AddRef, Release
  148. //
  149. //---------------------------------------------------------------------------
  150. ULONG
  151. CScriptSite::AddRef()
  152. {
  153. _ulRefs += 1;
  154. return _ulRefs;
  155. }
  156. ULONG
  157. CScriptSite::Release()
  158. {
  159. if (--_ulRefs == 0)
  160. {
  161. delete this;
  162. return 0;
  163. }
  164. return _ulRefs;
  165. }
  166. //---------------------------------------------------------------------------
  167. //
  168. // Method: CScriptSite::Init
  169. //
  170. //---------------------------------------------------------------------------
  171. HRESULT
  172. CScriptSite::Init(LPWSTR pszName)
  173. {
  174. HRESULT hr;
  175. IActiveScriptParse *pParse = NULL;
  176. static const CLSID CLSID_JSCRIPT = { 0xf414c260, 0x6ac0, 0x11cf, 0xb6, 0xd1, 0x00, 0xaa, 0x00, 0xbb, 0xbb, 0x58};
  177. WCHAR *pch;
  178. _cstrName.Set(pszName);
  179. hr = THR(ScriptHost()->_pMT->HackCreateInstance(CLSID_JSCRIPT,
  180. NULL,
  181. CLSCTX_INPROC_SERVER,
  182. IID_IActiveScript,
  183. (void **)&_pScript));
  184. if (hr)
  185. {
  186. hr = THR(CoCreateInstance(CLSID_JSCRIPT,
  187. NULL,
  188. CLSCTX_INPROC_SERVER,
  189. IID_IActiveScript,
  190. (void **)&_pScript));
  191. }
  192. if (hr)
  193. goto Cleanup;
  194. if (ScriptHost()->_pMT->_pPDM)
  195. {
  196. Assert(_pDDH == NULL);
  197. hr = ScriptHost()->_pMT->_pPDM->CreateDebugDocumentHelper(NULL, &_pDDH);
  198. if (SUCCEEDED(hr))
  199. {
  200. hr = THR(_pDDH->Init(ScriptHost()->_pMT->_pDA,
  201. _cstrName,
  202. _cstrName,
  203. 0));
  204. if (hr)
  205. goto Cleanup;
  206. hr = THR(_pDDH->Attach((_pScriptSitePrev)
  207. ? _pScriptSitePrev->_pDDH
  208. : NULL));
  209. if (hr)
  210. goto Cleanup;
  211. }
  212. }
  213. for (pch = _cstrName; *pch; pch++)
  214. {
  215. if (*pch == L'.')
  216. {
  217. *pch = L'_';
  218. }
  219. }
  220. hr = THR(_pScript->QueryInterface(IID_IActiveScriptParse, (void **)&pParse));
  221. if (hr)
  222. goto Cleanup;
  223. hr = THR(_pScript->SetScriptSite(this));
  224. if (hr)
  225. goto Cleanup;
  226. hr = THR(pParse->InitNew());
  227. if (hr)
  228. goto Cleanup;
  229. hr = THR(_pScript->AddNamedItem(_cstrName, SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISSOURCE | SCRIPTITEM_GLOBALMEMBERS));
  230. if (hr)
  231. goto Cleanup;
  232. Cleanup:
  233. ReleaseInterface(pParse);
  234. RRETURN(hr);
  235. }
  236. //---------------------------------------------------------------------------
  237. //
  238. // Method: CScriptSite::Close
  239. //
  240. //---------------------------------------------------------------------------
  241. void
  242. CScriptSite::Close()
  243. {
  244. if (_pScript)
  245. {
  246. _pScript->Close();
  247. ClearInterface(&_pScript);
  248. }
  249. }
  250. //---------------------------------------------------------------------------
  251. //
  252. // Method: CScriptSite::Abort
  253. //
  254. //---------------------------------------------------------------------------
  255. void
  256. CScriptSite::Abort()
  257. {
  258. if (_pScript)
  259. {
  260. HRESULT hr;
  261. hr = THR(_pScript->InterruptScriptThread(SCRIPTTHREADID_CURRENT,
  262. NULL,
  263. 0));
  264. }
  265. }
  266. //---------------------------------------------------------------------------
  267. //
  268. // Member: CScriptSite::GetClassInfo, IProvideClassInfo
  269. //
  270. //---------------------------------------------------------------------------
  271. HRESULT
  272. CScriptSite::GetClassInfo(ITypeInfo **ppTypeInfo)
  273. {
  274. HRESULT hr;
  275. hr = ScriptHost()->LoadTypeLibrary();
  276. if (hr)
  277. goto Cleanup;
  278. *ppTypeInfo = ScriptHost()->_pTypeInfoCMTScript;
  279. (*ppTypeInfo)->AddRef();
  280. Cleanup:
  281. RRETURN(hr);
  282. }
  283. //---------------------------------------------------------------------------
  284. //
  285. // Member: CScriptSite::GetGUID, IProvideClassInfo2
  286. //
  287. //---------------------------------------------------------------------------
  288. HRESULT
  289. CScriptSite::GetGUID(DWORD dwGuidKind, GUID * pGUID)
  290. {
  291. if (dwGuidKind == GUIDKIND_DEFAULT_SOURCE_DISP_IID)
  292. {
  293. *pGUID = DIID_DLocalMTScriptEvents;
  294. }
  295. else
  296. {
  297. return E_NOTIMPL;
  298. }
  299. return S_OK;
  300. }
  301. //---------------------------------------------------------------------------
  302. //
  303. // Member: CScriptSite::GetMultiTypeInfoCount, IProvideMultipleClassInfo
  304. //
  305. //---------------------------------------------------------------------------
  306. HRESULT
  307. CScriptSite::GetMultiTypeInfoCount(ULONG *pc)
  308. {
  309. *pc = 1;
  310. return S_OK;
  311. }
  312. //---------------------------------------------------------------------------
  313. //
  314. // Member: CScriptSite::GetInfoOfIndex, IProvideMultipleClassInfo
  315. //
  316. //---------------------------------------------------------------------------
  317. HRESULT
  318. CScriptSite::GetInfoOfIndex(
  319. ULONG itinfo,
  320. DWORD dwFlags,
  321. ITypeInfo** pptinfoCoClass,
  322. DWORD* pdwTIFlags,
  323. ULONG* pcdispidReserved,
  324. IID* piidPrimary,
  325. IID* piidSource)
  326. {
  327. AssertSz(itinfo == 0, "itinfo == 0");
  328. if (dwFlags & MULTICLASSINFO_GETTYPEINFO)
  329. {
  330. *pptinfoCoClass = ScriptHost()->_pTypeInfoCMTScript;
  331. (*pptinfoCoClass)->AddRef();
  332. if (pdwTIFlags)
  333. *pdwTIFlags = 0;
  334. }
  335. if (dwFlags & MULTICLASSINFO_GETNUMRESERVEDDISPIDS)
  336. {
  337. *pcdispidReserved = 100;
  338. }
  339. if (dwFlags & MULTICLASSINFO_GETIIDPRIMARY)
  340. {
  341. *piidPrimary = IID_IGlobalMTScript;
  342. }
  343. if (dwFlags & MULTICLASSINFO_GETIIDSOURCE)
  344. {
  345. *piidSource = DIID_DLocalMTScriptEvents;
  346. }
  347. return S_OK;
  348. }
  349. //---------------------------------------------------------------------------
  350. //
  351. // Member: CScriptSite::EnumConnectionPoints, IConnectionPointContainer
  352. //
  353. //---------------------------------------------------------------------------
  354. HRESULT
  355. CScriptSite::EnumConnectionPoints(LPENUMCONNECTIONPOINTS *)
  356. {
  357. return E_NOTIMPL;
  358. }
  359. //---------------------------------------------------------------------------
  360. //
  361. // Member: CScriptSite::FindConnectionPoint, IConnectionPointContainer
  362. //
  363. //---------------------------------------------------------------------------
  364. HRESULT
  365. CScriptSite::FindConnectionPoint(REFIID iid, LPCONNECTIONPOINT* ppCpOut)
  366. {
  367. HRESULT hr;
  368. if (iid == DIID_DLocalMTScriptEvents || iid == IID_IDispatch)
  369. {
  370. *ppCpOut = new CConnectionPoint(this);
  371. hr = *ppCpOut ? S_OK : E_OUTOFMEMORY;
  372. }
  373. else
  374. {
  375. hr = E_NOINTERFACE;
  376. }
  377. RRETURN(hr);
  378. }
  379. //---------------------------------------------------------------------------
  380. //
  381. // Method: CScriptSite::GetLCID, IActiveScriptSite
  382. //
  383. //---------------------------------------------------------------------------
  384. HRESULT
  385. CScriptSite::GetLCID(LCID *plcid)
  386. {
  387. return E_NOTIMPL; // Use system settings
  388. }
  389. //---------------------------------------------------------------------------
  390. //
  391. // Method: CScriptSite::GetItemInfo, IActiveScriptSite
  392. //
  393. //---------------------------------------------------------------------------
  394. HRESULT
  395. CScriptSite::GetItemInfo(
  396. LPCOLESTR pstrName,
  397. DWORD dwReturnMask,
  398. IUnknown** ppunkItemOut,
  399. ITypeInfo** pptinfoOut)
  400. {
  401. if (dwReturnMask & SCRIPTINFO_ITYPEINFO)
  402. {
  403. if (!pptinfoOut)
  404. return E_INVALIDARG;
  405. *pptinfoOut = NULL;
  406. }
  407. if (dwReturnMask & SCRIPTINFO_IUNKNOWN)
  408. {
  409. if (!ppunkItemOut)
  410. return E_INVALIDARG;
  411. *ppunkItemOut = NULL;
  412. }
  413. if (!_wcsicmp(_cstrName, pstrName))
  414. {
  415. if (dwReturnMask & SCRIPTINFO_ITYPEINFO)
  416. {
  417. *pptinfoOut = ScriptHost()->_pTypeInfoCMTScript;
  418. (*pptinfoOut)->AddRef();
  419. }
  420. if (dwReturnMask & SCRIPTINFO_IUNKNOWN)
  421. {
  422. *ppunkItemOut = (IGlobalMTScript *)this;
  423. (*ppunkItemOut)->AddRef();
  424. }
  425. return S_OK;
  426. }
  427. return TYPE_E_ELEMENTNOTFOUND;
  428. }
  429. //---------------------------------------------------------------------------
  430. //
  431. // Method: CScriptSite::GetDocVersionString, IActiveScriptSite
  432. //
  433. //---------------------------------------------------------------------------
  434. HRESULT
  435. CScriptSite::GetDocVersionString(BSTR *pbstrVersion)
  436. {
  437. return E_NOTIMPL;
  438. }
  439. //---------------------------------------------------------------------------
  440. //
  441. // Method: CScriptSite::, IActiveScriptSite
  442. //
  443. //---------------------------------------------------------------------------
  444. HRESULT
  445. CScriptSite::RequestItems()
  446. {
  447. return _pScript->AddNamedItem(_cstrName, SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISSOURCE);
  448. }
  449. //---------------------------------------------------------------------------
  450. //
  451. // Method: CScriptSite::RequestTypeLibs, IActiveScriptSite
  452. //
  453. //---------------------------------------------------------------------------
  454. HRESULT
  455. CScriptSite::RequestTypeLibs()
  456. {
  457. return _pScript->AddTypeLib(LIBID_MTScriptEngine, 1, 0, 0);
  458. }
  459. //---------------------------------------------------------------------------
  460. //
  461. // Method: CScriptSite::OnScriptTerminate, IActiveScriptSite
  462. //
  463. //---------------------------------------------------------------------------
  464. HRESULT
  465. CScriptSite::OnScriptTerminate(const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
  466. {
  467. // UNDONE: Put up error dlg here
  468. return S_OK;
  469. }
  470. //---------------------------------------------------------------------------
  471. //
  472. // Method: CScriptSite::OnStateChange, IActiveScriptSite
  473. //
  474. //---------------------------------------------------------------------------
  475. HRESULT
  476. CScriptSite::OnStateChange(SCRIPTSTATE ssScriptState)
  477. {
  478. // Don't care about notification
  479. return S_OK;
  480. }
  481. //---------------------------------------------------------------------------
  482. //
  483. // Method: CScriptSite::OnScriptError, IActiveScriptSite
  484. //
  485. //---------------------------------------------------------------------------
  486. HRESULT
  487. CScriptSite::OnScriptError(IActiveScriptError *pse)
  488. {
  489. long nScriptErrorResult = 0;
  490. BSTR bstrLine = NULL;
  491. TCHAR * pchDescription;
  492. TCHAR achDescription[256];
  493. TCHAR achMessage[1024];
  494. EXCEPINFO ei;
  495. DWORD dwSrcContext;
  496. ULONG ulLine;
  497. LONG ichError;
  498. HRESULT hr;
  499. LONG iRet = IDYES;
  500. TraceTag((tagError, "OnScriptError!"));
  501. if (ScriptHost()->_fMustExitThread)
  502. {
  503. return S_OK;
  504. }
  505. hr = THR(pse->GetExceptionInfo(&ei));
  506. if (hr)
  507. goto Cleanup;
  508. hr = THR(pse->GetSourcePosition(&dwSrcContext, &ulLine, &ichError));
  509. if (hr)
  510. goto Cleanup;
  511. hr = THR(pse->GetSourceLineText(&bstrLine));
  512. if (hr)
  513. hr = S_OK; // Ignore this error, there may not be source available
  514. if (ei.bstrDescription)
  515. {
  516. pchDescription = ei.bstrDescription;
  517. }
  518. else
  519. {
  520. achDescription[0] = 0;
  521. FormatMessage(
  522. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  523. NULL,
  524. ei.scode,
  525. LANG_SYSTEM_DEFAULT,
  526. achDescription,
  527. ARRAY_SIZE(achDescription),
  528. NULL);
  529. pchDescription = achDescription;
  530. }
  531. // First, lets format a message nice for OUTPUTDEBUGSTRING
  532. _snwprintf(achMessage, ARRAY_SIZE(achMessage),
  533. _T("Thread: %s")
  534. _T("File: %s ")
  535. _T("Line: %d ")
  536. _T("Char: %d ")
  537. _T("Text: %s ")
  538. _T("Scode: %x ")
  539. _T("Source: %s ")
  540. _T("Description: %s"),
  541. (LPTSTR)_cstrName,
  542. _achPath,
  543. (long)(ulLine + 1),
  544. (long)(ichError),
  545. bstrLine ? bstrLine : _T(""),
  546. ei.scode,
  547. ei.bstrSource ? ei.bstrSource : _T(""),
  548. pchDescription
  549. );
  550. achMessage[ARRAY_SIZE(achMessage) - 1] = 0;
  551. OUTPUTDEBUGSTRING(achMessage);
  552. //Assert(!_fInScriptError);
  553. if (!_fInScriptError)
  554. {
  555. _fInScriptError = true;
  556. nScriptErrorResult = _pSH->FireScriptErrorEvent(
  557. _achPath,
  558. ulLine + 1,
  559. ichError,
  560. bstrLine ? bstrLine : _T(""),
  561. ei.scode,
  562. ei.bstrSource ? ei.bstrSource : _T(""),
  563. pchDescription);
  564. _fInScriptError = false;
  565. }
  566. if (nScriptErrorResult == 0)
  567. {
  568. _snwprintf(achMessage, ARRAY_SIZE(achMessage),
  569. _T("Thread: %s\r\n")
  570. _T("File: %s\r\n")
  571. _T("Line: %d\r\n")
  572. _T("Char: %d\r\n")
  573. _T("Text: %s\r\n")
  574. _T("Scode: %x\r\n")
  575. _T("Source: %s\r\n")
  576. _T("Description: %s\r\n")
  577. _T("%s"),
  578. (LPTSTR)_cstrName,
  579. _achPath,
  580. (long)(ulLine + 1),
  581. (long)(ichError),
  582. bstrLine ? bstrLine : _T(""),
  583. ei.scode,
  584. ei.bstrSource ? ei.bstrSource : _T(""),
  585. pchDescription,
  586. ((_fInDebugError) ? _T("\r\nDo you wish to debug?\r\n") : _T(""))
  587. );
  588. achMessage[ARRAY_SIZE(achMessage) - 1] = 0;
  589. MSGBOXPARAMS mbp;
  590. memset(&mbp, 0, sizeof(mbp));
  591. mbp.cbSize = sizeof(mbp);
  592. //mbp.hwndOwner = ScriptHost()->_pDoc->_hwnd;
  593. mbp.hwndOwner = NULL;
  594. mbp.lpszText = achMessage;
  595. mbp.lpszCaption = _T("Script Error");
  596. mbp.dwStyle = MB_APPLMODAL | MB_ICONERROR | ((_fInDebugError) ? MB_YESNO : MB_OK);
  597. iRet = MessageBoxIndirect(&mbp);
  598. }
  599. if (nScriptErrorResult == 1)
  600. hr = S_FALSE;
  601. Cleanup:
  602. if(ei.bstrSource)
  603. SysFreeString(ei.bstrSource);
  604. if(ei.bstrDescription)
  605. SysFreeString(ei.bstrDescription);
  606. if(ei.bstrHelpFile)
  607. SysFreeString(ei.bstrHelpFile);
  608. if (bstrLine)
  609. SysFreeString(bstrLine);
  610. RRETURN1((_fInDebugError && !hr && iRet == IDYES) ? S_FALSE : hr, S_FALSE);
  611. }
  612. //---------------------------------------------------------------------------
  613. //
  614. // Method: CScriptSite::OnEnterScript, IActiveScriptSite
  615. //
  616. //---------------------------------------------------------------------------
  617. HRESULT CScriptSite::OnEnterScript()
  618. {
  619. // No need to do anything
  620. return S_OK;
  621. }
  622. //---------------------------------------------------------------------------
  623. //
  624. // Method: CScriptSite::OnLeaveScript, IActiveScriptSite
  625. //
  626. //---------------------------------------------------------------------------
  627. HRESULT CScriptSite::OnLeaveScript()
  628. {
  629. // No need to do anything
  630. return S_OK;
  631. }
  632. //---------------------------------------------------------------------------
  633. //
  634. // Method: CScriptSite::, IActiveScriptSiteWindow
  635. //
  636. //---------------------------------------------------------------------------
  637. HRESULT CScriptSite::GetWindow(HWND *phwndOut)
  638. {
  639. //*phwndOut = ScriptHost()->_pDoc->_hwnd;
  640. *phwndOut = NULL;
  641. return S_OK;
  642. }
  643. //---------------------------------------------------------------------------
  644. //
  645. // Method: CScriptSite::EnableModeless, IActiveScriptSiteWindow
  646. //
  647. //---------------------------------------------------------------------------
  648. HRESULT CScriptSite::EnableModeless(BOOL fEnable)
  649. {
  650. return S_OK;
  651. }
  652. //---------------------------------------------------------------------------
  653. //
  654. // Member: CScriptSite::ExecuteScriptFile
  655. //
  656. // Load and execute script file
  657. //
  658. //---------------------------------------------------------------------------
  659. HRESULT
  660. CScriptSite::ExecuteScriptFile(TCHAR *pchPath)
  661. {
  662. HRESULT hr;
  663. HANDLE hFile = INVALID_HANDLE_VALUE;
  664. DWORD cchFile;
  665. DWORD cbRead;
  666. char * pchBuf = 0;
  667. TCHAR * pchBufWide = 0;
  668. TCHAR * pchFile;
  669. // Attempt to find the file. First, we try to use the path as given. If
  670. // that doesn't work, then we prefix the script path and try to find it.
  671. GetFullPathName(pchPath, ARRAY_SIZE(_achPath), _achPath, &pchFile);
  672. if (GetFileAttributes(_achPath) == 0xFFFFFFFF)
  673. {
  674. // The file apparently doesn't exist. Try pre-pending the ScriptPath
  675. // onto it.
  676. CStr cstrPath;
  677. ScriptHost()->GetScriptPath(&cstrPath);
  678. cstrPath.Append(L"\\");
  679. cstrPath.Append(pchPath);
  680. GetFullPathName(cstrPath, ARRAY_SIZE(_achPath), _achPath, &pchFile);
  681. }
  682. // Load script file
  683. hFile = CreateFile(_achPath,
  684. GENERIC_READ,
  685. FILE_SHARE_READ,
  686. NULL,
  687. OPEN_EXISTING,
  688. 0,
  689. NULL);
  690. if (hFile == INVALID_HANDLE_VALUE)
  691. {
  692. hr = HRESULT_FROM_WIN32(GetLastError());
  693. goto Cleanup;
  694. }
  695. cchFile = GetFileSize(hFile, NULL);
  696. if (cchFile == 0xFFFFFFFF)
  697. {
  698. hr = HRESULT_FROM_WIN32(GetLastError());
  699. goto Cleanup;
  700. }
  701. pchBuf = new char[cchFile + 1];
  702. pchBufWide = new TCHAR[cchFile + 1];
  703. if (!pchBuf || !pchBufWide)
  704. {
  705. hr = E_OUTOFMEMORY;
  706. goto Cleanup;
  707. }
  708. if (!ReadFile(hFile, pchBuf, cchFile, &cbRead, 0))
  709. {
  710. hr = HRESULT_FROM_WIN32(GetLastError());
  711. goto Cleanup;
  712. }
  713. pchBuf[cbRead] = 0;
  714. MultiByteToWideChar(CP_ACP, 0, pchBuf, -1, pchBufWide, cchFile + 1);
  715. if (_pDDH)
  716. {
  717. pchFile = wcsrchr(pchPath, L'\\');
  718. if (!pchFile)
  719. {
  720. pchFile = pchPath;
  721. }
  722. else
  723. pchFile++;
  724. hr = THR(_pDDH->SetLongName(_achPath));
  725. if (hr)
  726. goto Cleanup;
  727. hr = THR(_pDDH->AddUnicodeText(pchBufWide));
  728. if (hr)
  729. goto Cleanup;
  730. hr = THR(_pDDH->DefineScriptBlock(0,
  731. cchFile,
  732. _pScript,
  733. TRUE,
  734. &_dwSourceContext));
  735. //if (hr)
  736. // goto Cleanup;
  737. }
  738. // Execute script
  739. hr = ExecuteScriptStr(pchBufWide);
  740. if(hr)
  741. goto Cleanup;
  742. Cleanup:
  743. delete pchBuf;
  744. delete pchBufWide;
  745. if (hFile != INVALID_HANDLE_VALUE)
  746. CloseHandle(hFile);
  747. RRETURN(hr);
  748. }
  749. //---------------------------------------------------------------------------
  750. //
  751. // Method: CScriptSite::ExecuteScriptStr
  752. //
  753. //---------------------------------------------------------------------------
  754. HRESULT
  755. CScriptSite::ExecuteScriptStr(TCHAR * pchScript)
  756. {
  757. HRESULT hr;
  758. IActiveScriptParse * pParse = NULL;
  759. hr = THR(_pScript->QueryInterface(IID_IActiveScriptParse, (void **)&pParse));
  760. if (hr)
  761. goto Cleanup;
  762. hr = THR(pParse->ParseScriptText(pchScript, _cstrName, NULL, NULL, 0, 0, 0L, NULL, NULL));
  763. if (hr)
  764. goto Cleanup;
  765. Cleanup:
  766. ReleaseInterface(pParse);
  767. RRETURN(hr);
  768. }
  769. //---------------------------------------------------------------------------
  770. //
  771. // Method: CScriptSite::SetScriptState
  772. //
  773. //---------------------------------------------------------------------------
  774. HRESULT
  775. CScriptSite::SetScriptState(SCRIPTSTATE ss)
  776. {
  777. return _pScript->SetScriptState(ss);
  778. }
  779. HRESULT
  780. CScriptSite::GetDocumentContextFromPosition(DWORD dwSourceContext,
  781. ULONG uCharacterOffset,
  782. ULONG uNumChars,
  783. IDebugDocumentContext **ppsc)
  784. {
  785. HRESULT hr;
  786. if (_pDDH)
  787. {
  788. ULONG ulStartPos;
  789. hr = _pDDH->GetScriptBlockInfo(dwSourceContext, NULL, &ulStartPos, NULL);
  790. if (hr)
  791. goto Cleanup;
  792. hr = _pDDH->CreateDebugDocumentContext(ulStartPos+uCharacterOffset,
  793. uNumChars,
  794. ppsc);
  795. }
  796. else
  797. hr = E_UNEXPECTED;
  798. Cleanup:
  799. RRETURN(hr);
  800. }
  801. HRESULT
  802. CScriptSite::GetApplication(IDebugApplication **ppda)
  803. {
  804. *ppda = ScriptHost()->_pMT->_pDA;
  805. return (*ppda) ? S_OK : E_UNEXPECTED;
  806. }
  807. HRESULT
  808. CScriptSite::GetRootApplicationNode(IDebugApplicationNode **ppdanRoot)
  809. {
  810. if (!ScriptHost()->_pMT->_pDA || !_pDDH)
  811. {
  812. return E_UNEXPECTED;
  813. }
  814. return _pDDH->GetDebugApplicationNode(ppdanRoot);
  815. }
  816. HRESULT
  817. CScriptSite::OnScriptErrorDebug(IActiveScriptErrorDebug *pErrorDebug,
  818. BOOL *pfEnterDebugger,
  819. BOOL *pfCallOnScriptErrorWhenContinuing)
  820. {
  821. HRESULT hr;
  822. IActiveScriptError * pError;
  823. TraceTag((tagError, "OnScriptErrorDebug!"));
  824. *pfEnterDebugger = FALSE;
  825. *pfCallOnScriptErrorWhenContinuing = TRUE;
  826. if (ScriptHost()->_fMustExitThread)
  827. {
  828. return S_OK;
  829. }
  830. hr = pErrorDebug->QueryInterface(IID_IActiveScriptError, (LPVOID*)&pError);
  831. if (SUCCEEDED(hr))
  832. {
  833. _fInDebugError = TRUE;
  834. hr = OnScriptError(pError);
  835. _fInDebugError = FALSE;
  836. if (hr == S_FALSE)
  837. {
  838. *pfEnterDebugger = TRUE;
  839. }
  840. *pfCallOnScriptErrorWhenContinuing = TRUE;
  841. pError->Release();
  842. }
  843. return S_OK;
  844. }
  845. //---------------------------------------------------------------------------
  846. //
  847. // Method: CScriptSite::xxx, IBServer
  848. //
  849. // The implementation of IBServer passed to the script engine
  850. // cannot be the same as that of the CBServerDoc because this
  851. // causes a reference count loop with the script engine.
  852. //
  853. //---------------------------------------------------------------------------
  854. HRESULT CScriptSite::GetTypeInfoCount(UINT FAR* pctinfo)
  855. { return ScriptHost()->GetTypeInfoCount(pctinfo); }
  856. HRESULT CScriptSite::GetTypeInfo(
  857. UINT itinfo,
  858. LCID lcid,
  859. ITypeInfo FAR* FAR* pptinfo)
  860. { return ScriptHost()->GetTypeInfo(itinfo, lcid, pptinfo); }
  861. HRESULT CScriptSite::GetIDsOfNames(
  862. REFIID riid,
  863. OLECHAR FAR* FAR* rgszNames,
  864. UINT cNames,
  865. LCID lcid,
  866. DISPID FAR* rgdispid)
  867. { return ScriptHost()->GetIDsOfNames(riid, rgszNames, cNames, lcid, rgdispid); }
  868. HRESULT CScriptSite::Invoke(
  869. DISPID dispidMember,
  870. REFIID riid,
  871. LCID lcid,
  872. WORD wFlags,
  873. DISPPARAMS FAR* pdispparams,
  874. VARIANT FAR* pvarResult,
  875. EXCEPINFO FAR* pexcepinfo,
  876. UINT FAR* puArgErr)
  877. { return ScriptHost()->Invoke(dispidMember, riid, lcid, wFlags, pdispparams, pvarResult, pexcepinfo, puArgErr); }
  878. HRESULT CScriptSite::get_PublicData(VARIANT *pvData)
  879. { return ScriptHost()->get_PublicData(pvData); }
  880. HRESULT CScriptSite::put_PublicData(VARIANT vData)
  881. { return ScriptHost()->put_PublicData(vData); }
  882. HRESULT CScriptSite::get_PrivateData(VARIANT *pvData)
  883. { return ScriptHost()->get_PrivateData(pvData); }
  884. HRESULT CScriptSite::put_PrivateData(VARIANT vData)
  885. { return ScriptHost()->put_PrivateData(vData); }
  886. HRESULT CScriptSite::ExitProcess()
  887. { return ScriptHost()->ExitProcess(); }
  888. HRESULT CScriptSite::Restart()
  889. { return ScriptHost()->Restart(); }
  890. HRESULT CScriptSite::get_LocalMachine(BSTR *pbstrName)
  891. { return ScriptHost()->get_LocalMachine(pbstrName); }
  892. HRESULT CScriptSite::Include(BSTR bstrPath)
  893. { return ScriptHost()->Include(bstrPath); }
  894. HRESULT CScriptSite::CallScript(BSTR Path, VARIANT *ScriptParam)
  895. { return ScriptHost()->CallScript(Path, ScriptParam); }
  896. HRESULT CScriptSite::SpawnScript(BSTR Path, VARIANT *ScriptParam)
  897. { return ScriptHost()->SpawnScript(Path, ScriptParam); }
  898. HRESULT CScriptSite::get_ScriptParam(VARIANT *ScriptParam)
  899. { return ScriptHost()->get_ScriptParam(ScriptParam); }
  900. HRESULT CScriptSite::get_ScriptPath(BSTR *bstrPath)
  901. { return ScriptHost()->get_ScriptPath(bstrPath); }
  902. HRESULT CScriptSite::CallExternal(BSTR bstrDLLName, BSTR bstrFunctionName, VARIANT *pParam, long *plRetVal)
  903. { return ScriptHost()->CallExternal(bstrDLLName, bstrFunctionName, pParam, plRetVal); }
  904. HRESULT CScriptSite::ResetSync(const BSTR bstrName)
  905. { return ScriptHost()->ResetSync(bstrName); }
  906. HRESULT CScriptSite::WaitForSync(BSTR bstrName, long nTimeout, VARIANT_BOOL *pfTimedout)
  907. { return ScriptHost()->WaitForSync(bstrName, nTimeout, pfTimedout); }
  908. HRESULT CScriptSite::WaitForMultipleSyncs(const BSTR bstrNameList, VARIANT_BOOL fWaitForAll, long nTimeout, long *plSignal)
  909. { return ScriptHost()->WaitForMultipleSyncs(bstrNameList, fWaitForAll, nTimeout, plSignal); }
  910. HRESULT CScriptSite::SignalThreadSync(BSTR bstrName)
  911. { return ScriptHost()->SignalThreadSync(bstrName); }
  912. HRESULT CScriptSite::TakeThreadLock(BSTR bstrName)
  913. { return ScriptHost()->TakeThreadLock(bstrName); }
  914. HRESULT CScriptSite::ReleaseThreadLock(BSTR bstrName)
  915. { return ScriptHost()->ReleaseThreadLock(bstrName); }
  916. HRESULT CScriptSite::DoEvents()
  917. { return ScriptHost()->DoEvents(); }
  918. HRESULT CScriptSite::MessageBoxTimeout(BSTR msg, long cBtn, BSTR btnText, long time, long intrvl, VARIANT_BOOL cancel, VARIANT_BOOL confirm, long *plSel)
  919. { return ScriptHost()->MessageBoxTimeout(msg, cBtn, btnText, time, intrvl, cancel, confirm, plSel); }
  920. HRESULT CScriptSite::RunLocalCommand(BSTR bstrCmd, BSTR bstrDir, BSTR bstrTitle, VARIANT_BOOL fMin, VARIANT_BOOL fGetOut, VARIANT_BOOL fWait, VARIANT_BOOL fNoPopup, VARIANT_BOOL fNoEnviron, long *plError)
  921. { return ScriptHost()->RunLocalCommand(bstrCmd, bstrDir, bstrTitle, fMin, fGetOut, fWait, fNoPopup, fNoEnviron, plError); }
  922. HRESULT CScriptSite::CopyOrAppendFile(BSTR bstrSrc, BSTR bstrDst, long nSrcOffset, long nSrcLength, VARIANT_BOOL fAppend, long *nSrcFilePosition)
  923. { return ScriptHost()->CopyOrAppendFile(bstrSrc, bstrDst, nSrcOffset, nSrcLength, fAppend, nSrcFilePosition); }
  924. HRESULT CScriptSite::GetLastRunLocalError(long *plErrorCode)
  925. { return ScriptHost()->GetLastRunLocalError(plErrorCode); }
  926. HRESULT CScriptSite::GetProcessOutput(long lProcessID, BSTR *pbstrData)
  927. { return ScriptHost()->GetProcessOutput(lProcessID, pbstrData); }
  928. HRESULT CScriptSite::GetProcessExitCode(long lProcessID, long *plExitCode)
  929. { return ScriptHost()->GetProcessExitCode(lProcessID, plExitCode); }
  930. HRESULT CScriptSite::TerminateProcess(long lProcessID)
  931. { return ScriptHost()->TerminateProcess(lProcessID); }
  932. HRESULT CScriptSite::SendToProcess(long lProcessID, BSTR bstrType, BSTR bstrData, long *plReturn)
  933. { return ScriptHost()->SendToProcess(lProcessID, bstrType, bstrData, plReturn); }
  934. HRESULT CScriptSite::SendMail(BSTR to, BSTR cc, BSTR bcc, BSTR subject, BSTR msg, BSTR attach, BSTR user, BSTR pass, long *plError)
  935. { return ScriptHost()->SendMail(to, cc, bcc, subject, msg, attach, user, pass, plError); }
  936. HRESULT CScriptSite::SendSMTPMail(BSTR from, BSTR to, BSTR cc, BSTR subject, BSTR msg, BSTR host, long *plError)
  937. { return ScriptHost()->SendSMTPMail(from, to, cc, subject, msg, host, plError); }
  938. HRESULT CScriptSite::OUTPUTDEBUGSTRING(BSTR LogMsg)
  939. { return ScriptHost()->OUTPUTDEBUGSTRING(LogMsg); }
  940. HRESULT CScriptSite::UnevalString(BSTR in, BSTR *out)
  941. { return ScriptHost()->UnevalString(in, out); }
  942. HRESULT CScriptSite::ASSERT(VARIANT_BOOL Assertion, BSTR LogMsg)
  943. { return ScriptHost()->ASSERT(Assertion, LogMsg); }
  944. HRESULT CScriptSite::Sleep (int nTimeout)
  945. { return ScriptHost()->Sleep (nTimeout); }
  946. HRESULT CScriptSite::Reboot ()
  947. { return ScriptHost()->Reboot (); }
  948. HRESULT CScriptSite::NotifyScript (BSTR bstrEvent, VARIANT vData)
  949. { return ScriptHost()->NotifyScript (bstrEvent, vData); }
  950. HRESULT CScriptSite::RegisterEventSource (IDispatch *pDisp, BSTR bstrProgID)
  951. { return ScriptHost()->RegisterEventSource (pDisp, bstrProgID); }
  952. HRESULT CScriptSite::UnregisterEventSource (IDispatch *pDisp)
  953. { return ScriptHost()->UnregisterEventSource (pDisp); }
  954. HRESULT CScriptSite::get_HostMajorVer(long *plMajorVer)
  955. { return ScriptHost()->get_HostMajorVer (plMajorVer); }
  956. HRESULT CScriptSite::get_HostMinorVer(long *plMinorVer)
  957. { return ScriptHost()->get_HostMinorVer (plMinorVer); }
  958. HRESULT CScriptSite::get_StatusValue(long nIndex, long *pnStatus)
  959. { return ScriptHost()->get_StatusValue(nIndex, pnStatus); }
  960. HRESULT CScriptSite::put_StatusValue(long nIndex, long nStatus)
  961. { return ScriptHost()->put_StatusValue(nIndex, nStatus); }