scriptproc.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #ifndef _SCRIPT_H_
  2. #define _SCRIPT_H_
  3. #include <activscp.h>
  4. #include <initguid.h>
  5. #include <AFXPRIV.H>
  6. class MyActiveScriptSite : public IActiveScriptSite {
  7. private:
  8. ULONG m_dwRef; // Reference count
  9. public:
  10. IUnknown *m_pUnkScriptObject; // Pointer to your object that is exposed
  11. // to the script engine in GetItemInfo().
  12. MyActiveScriptSite::MyActiveScriptSite() {m_dwRef = 1;}
  13. MyActiveScriptSite::~MyActiveScriptSite() {}
  14. // IUnknown methods...
  15. virtual HRESULT _stdcall QueryInterface(REFIID riid, void **ppvObject) {
  16. *ppvObject = NULL;
  17. return E_NOTIMPL;
  18. }
  19. virtual ULONG _stdcall AddRef(void) {
  20. return ++m_dwRef;
  21. }
  22. virtual ULONG _stdcall Release(void) {
  23. if(--m_dwRef == 0) return 0;
  24. return m_dwRef;
  25. }
  26. // IActiveScriptSite methods...
  27. virtual HRESULT _stdcall GetLCID(LCID *plcid) {
  28. return S_OK;
  29. }
  30. virtual HRESULT _stdcall GetItemInfo(LPCOLESTR pstrName,
  31. DWORD dwReturnMask, IUnknown **ppunkItem, ITypeInfo **ppti) {
  32. // Is it expecting an ITypeInfo?
  33. if(ppti) {
  34. // Default to NULL.
  35. *ppti = NULL;
  36. // Return if asking about ITypeInfo...
  37. if(dwReturnMask & SCRIPTINFO_ITYPEINFO)
  38. return TYPE_E_ELEMENTNOTFOUND;
  39. }
  40. // Is the engine passing an IUnknown buffer?
  41. if(ppunkItem) {
  42. // Default to NULL.
  43. *ppunkItem = NULL;
  44. // Is Script Engine looking for an IUnknown for our object?
  45. if(dwReturnMask & SCRIPTINFO_IUNKNOWN) {
  46. // Check for our object name...
  47. if (!_wcsicmp(L"MyObject", pstrName)) {
  48. // Provide our object.
  49. *ppunkItem = m_pUnkScriptObject;
  50. // Addref our object...
  51. m_pUnkScriptObject->AddRef();
  52. }
  53. }
  54. }
  55. return S_OK;
  56. }
  57. virtual HRESULT __stdcall GetDocVersionString(BSTR *pbstrVersion) {
  58. return S_OK;
  59. }
  60. virtual HRESULT __stdcall OnScriptTerminate(const VARIANT *pvarResult,
  61. const EXCEPINFO *pexcepInfo) {
  62. return S_OK;
  63. }
  64. virtual HRESULT __stdcall OnStateChange(SCRIPTSTATE ssScriptState) {
  65. return S_OK;
  66. }
  67. virtual HRESULT __stdcall OnScriptError(
  68. IActiveScriptError *pscriptError) {
  69. static BSTR pwcErrorText;
  70. pscriptError->GetSourceLineText(&pwcErrorText);
  71. AfxMessageBox(
  72. CString("脚本存在语法错误,请仔细检查"),
  73. MB_SETFOREGROUND);
  74. ::SysFreeString(pwcErrorText);
  75. return S_OK;
  76. }
  77. virtual HRESULT __stdcall OnEnterScript(void) {
  78. return S_OK;
  79. }
  80. virtual HRESULT __stdcall OnLeaveScript(void) {
  81. return S_OK;
  82. }
  83. };
  84. #endif