Support script programs in the VC program (-)

zhaozj2021-02-08  236

VBScript in the program

The following is the basic method I have summarized from several routines, with no event processing functions, (the source code has been lost, I have no use of this technology to develop products), 1. Head file contains #include

#include

2. declared in the class two members IActiveScript * m_pIActiveScript; IActiveScriptParse * m_pIActiveScriptParse; 3. DECLARE_MESSAGE_MAP CMainFrame class declaration in the plus (after) the following code DECLARE_INTERFACE_MAP () BEGIN_INTERFACE_PART (ScriptSite, IActiveScriptSite) STDMETHOD (GetLCID) (LCID * plcid); STDMETHOD (GetItemInfo) (LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown ** ppiunkItem, ITypeInfo ** ppti); STDMETHOD (GetDocVersionString) (BSTR * pszVersion); STDMETHOD (RequestItems) (void); STDMETHOD (RequestTypeLibs) (void) ; STDMETHOD (OnScriptTerminate) (const VARIANT * pvarResult, const EXCEPINFO * pexcepinfo); STDMETHOD (OnStateChange) (SCRIPTSTATE ssScriptState); STDMETHOD (OnScriptError) (IActiveScriptError * pscripterror); STDMETHOD (OnEnterScript) (void); STDMETHOD (OnLeaveScript) (void ); END_INTERFACE_PART (ScriptSite) BEGIN_INTERFACE_PART (ScriptSiteWindow, IActiveScriptSiteWindow) STDMETHOD (GetWindow) (HWND * phwnd); STDMETHOD (EnableModeless) (BOOL fEnable); END_INTERFACE_PART (ScriptSiteWindow) / ************** ************************* After adding the above code in CMAINFRA Multi-two members M_xscriptsite and m_xscriptsiteWindow *************************************************************************************************************************** ******* / // the following are shifted down from above IActiveScript * m_pIActiveScript; IActiveScriptParse * m_pIActiveScriptParse; 4. implementation code 1 in 3) add the following code in END_MESSAGE_MAP BEGIN_INTERFACE_MAP (CMainFrame, CFrameWnd) (after) INTERFACE_PART (CMainFrame, IID_IActiveScriptSite, ScriptSite) INTERFACE_PART (CMainFrame, IID_IActiveScriptSiteWindow, ScriptSiteWindow) END_INTERFACE_MAP () 2) implemented in the member methods m_xScriptSite // COM interface implementation ULONG FAR EXPORT CMainFrame :: XScriptSite :: AddRef () {METHOD_PROLOGUE (CMainFrame, ScriptSite) Return PTHIS-> EXTERNALADDREF ();} Ulong Far Export CMAINFRAME :: xscriptsite :: release () {method_prologue (cmainframe, scriptsite) Return PTHIS-> ExternalRelease ();

} HRESULT FAR EXPORT CMainFrame :: XScriptSite :: QueryInterface (REFIID iid, void FAR * FAR * ppvObj) {METHOD_PROLOGUE (CMainFrame, ScriptSite) return (HRESULT) pThis-> ExternalQueryInterface (& iid, ppvObj);} ULONG FAR EXPORT CMainFrame :: XScriptSiteWindow :: AddRef () {METHOD_PROLOGUE (CMainFrame, ScriptSiteWindow) return pThis-> ExternalAddRef ();} ULONG FAR EXPORT CMainFrame :: XScriptSiteWindow :: Release () {METHOD_PROLOGUE (CMainFrame, ScriptSiteWindow) return pThis-> ExternalRelease ();} HRESULT FAR EXPORT CMainFrame :: XScriptSiteWindow :: QueryInterface (REFIID iid, void FAR * FAR * ppvObj) {METHOD_PROLOGUE (CMainFrame, ScriptSiteWindow) return (HRESULT) pThis-> ExternalQueryInterface (& iid, ppvObj);} / // ActiveScript hosting // #### ******************************************* ************************************************** // IactiveScriptsite Interface // *** *********************************************************** *************************** Stdmethodimp CMAINFRAME :: Xscriptsite :: getlcid LCID * plcid) {return E_NOTIMPL; // Use system settings} STDMETHODIMP CMainFrame :: XScriptSite :: GetItemInfo (LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown ** ppunkItemOut, ITypeInfo ** pptinfoOut) {return TYPE_E_ELEMENTNOTFOUND;} STDMETHODIMP CMainFrame :: XScriptSite: : GetDocVersionString (BSTR * pbstrVersion) {return E_NOTIMPL; // UNDONE: Implement this method} STDMETHODIMP CMainFrame :: XScriptSite :: RequestItems (void) {return E_NOTIMPL;} STDMETHODIMP CMainFrame :: XScriptSite :: RequestTypeLibs (void) {METHOD_PROLOGUE (CMainFrame , ScriptSite); return E_NOTIMPL;} STDMETHODIMP CMainFrame :: XScriptSite :: OnScriptTerminate (const VARIANT * pvarResult, const EXCEPINFO * pexcepinfo) {// UNDONE: Put up error dlg here return S_OK;

} Stdmethodimp cmainframe :: xscriptsite :: onStateChange (scriptstate ssscriptState) {// don't care about notification return s_ok;} // ---------------------------------------------------------------------------------------------- -------------------------------------------------- --- // display the error // ---------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------- stdmethodimp cmainframe :: xscriptsite :: onscripterror (IactivescripterRor * PSE {Method_Prologue (CMainFrame, ScriptSite); CString strError; CString strArrow; CString strDesc; CString strLine; EXCEPINFO ei; DWORD dwSrcContext; ULONG ulLine; LONG ichError; BSTR bstrLine = NULL; HRESULT hr; pse-> GetExceptionInfo (& ei); pse-> GetSourcePosition (& dwSrcContext, & ulLine, & ichError); hr = pse-> GetSourceLineText (& bstrLine); if (hr) hr = S_OK; // Ignore this error, there may not be source available if (hr!) {strError = ei.bstrSource; STRDESC = Ei.BSTRDESCRIPTION; Strline = BSTRLINE; IF (ICHERROR> 0 && icherRor <255) {strrrow = cstring (_t ('-'), icherror); strarrow.set (icherror-1, _t ('v')) } CSTRING STR ErrorCopy = strerror; strerror.format (_T ("Source: '% s' / nfile: '% s' line:% D char:% ld / nerror:% d '% s' / n% s / n% s" ), LPCTSTR (STRERRORCOPY), _T ("file"), ulline, icherror, (int) ei.wcode, lpctstr (strdesc), lpctstr (strord), lpctstr (strline); afxMessageBox (strerror);} if (bstrline ) SysFreeString (bstrLine); return hr;} STDMETHODIMP CMainFrame :: XScriptSite :: OnEnterScript (void) {// No need to do anything return S_OK;} STDMETHODIMP CMainFrame :: XScriptSite :: OnLeaveScript (void) {// No need to Do Anything Return S_OK;

} // ***************************************************************** ************************************************************************************************************************************************************************************************************************TION *********************************************************** ********* STDMETHODIMP CMainFrame :: XScriptSiteWindow :: GetWindow (HWND * phwndOut) {if (! phwndOut) return E_INVALIDARG; METHOD_PROLOGUE (CMainFrame, ScriptSiteWindow); * phwndOut = pThis-> GetSafeHwnd (); return S_OK ;} STDMETHODIMP CMainFrame :: XScriptSiteWindow :: EnableModeless (BOOL fEnable) {return S_OK;} // ##### END ACTIVEX SCRIPTING SUPPORT ##### 5. Add two global variables const IID CLSID_VBScript = {0xb54f3741, 0x5b07 0x11cf, {0xA4, 0x0, 0x4a, 0x55, 0x0, 0x4a, 0x55, 0xE8}}; const tchar * szitemname = _t ("document"); szitemname refers to an object that can be created directly in the script, such as the top Value can be used as follows: Document.MessageBox "SLDFK" 'MessageBox is the method of document If SZITEMNAME = _T ("Document1") is written as follows: Document1.MessageBox "SLDFK"' MessageBox is a method 6 of Document1 6. Create a script object HRESULT CMAINFRAME :: CreateScripTENGINE (LPCOLESTR PSTRITEMNAME) {HRESULT HR; if (M_PIACTIVEScript) Return S_FALSE; // Already created it // Create the ActiveX Scripting Engine hr = CoCreateInstance (CLSID_VBScript, NULL, CLSCTX_INPROC_SERVER, IID_IActiveScript, (void **) & m_pIActiveScript); if (FAILED (hr)) {return E_FAIL;} // Script Engine must support IActiveScriptParse for us to use it hr = m_pIActiveScript-> QueryInterface (IID_IActiveScriptParse, (void **) & m_pIActiveScriptParse); if (hr) {return hr;} hr = m_pIActiveScript-> SetScriptSite (& m_xScriptSite); if (hr) return hr ; // initnew the object: hr = m_piactivescriptpival-> initnew (); // joining host object name Uses_conversion; lpcolestr lpostrapp = t2cole (szitemname); hr = m_piactivescript->

AddNamedItem (lpostrApp, SCRIPTITEM_ISVISIBLE); return hr;} // ##### END ACTIVEX SCRIPTING SUPPORT ##### procedure calls: the general call USES_CONVERSION OnCreate function in the CMainFrame; LPCOLESTR lpszT = T2COLE (szItemName); if ( CreateScripTENGINE (LPSZT)! = S_OK) AFXMessageBox (_T ("Can't load the vbscript engine."))); 7. Loading script bool cmainframe :: loadingTextFile () {cfiledialog dlg (True, _t ("*. Txt" ), _T ("*. Txt"), OFN_HIDEREADOONLY | OFN_OVERWRITEPROMPT, _T ("Text Files (* .txt)"); if (DLG.Domodal () == iDok) {m_strcode.empty (); tchar szbuffer [ 256]; IFSTREAM IFS (DLG.GetPathname ()); while (IFS) {ifs.getline (szbuffer, 256); m_strcode = szbuffer; m_strcode = _ t ("/ r / n");} // #### # BEGIN ACTIVEX SCRIPTING SUPPORT ##### USES_CONVERSION; EXCEPINFO ei; LPCOLESTR lpszCode = T2COLE (LPCTSTR (m_strCode)); HRESULT hr = m_pIActiveScriptParse-> ParseScriptText (lpszCode, NULL, NULL, NULL, 0, 0, 0L, NULL, & ei); // ##### End ActiveX Scripting Support ##### IF (HR) {AFXMessageBox (_T ("can't compile the program"); ​​return false;} return true;} Return true; } 8. Run the stop script m_pIActiveScript-> SetScriptState (SCRIPTSTATE_CONNECTED); m_pIActiveScript-> SetScriptState (SCRIPTSTATE_DISCONNECTED); 9. Script Host Support in STDMETHODIMP CMainFrame :: XScriptSite :: GetItemInfo was currently supported target host: LPCOLESTR pstrName object name, const TCHAR * szItemName as defined in 5 = _T ( "Document") DWORD dwReturnMask IUnknown ** ppunkItemOut ITypeInfo ** pptinfoOut return object code that implements STDMETHODIMP CMainFrame :: XScriptSite :: GetItemInfo (LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown ** ppunkItemOut {Method_Prologue (cmainframe, scriptsite) Uses_conversion; lpctstr lpstrapplication = szitemname; if (dwreturnmask &

转载请注明原文地址:https://www.9cbs.com/read-2631.html

New Post(0)