⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scriptengineex.cpp

📁 VB Script脚本引擎及例子程序
💻 CPP
字号:
#include "stdafx.h"
#include "ScriptEngineEx.h"
#include "mydlg.h"

#include "Utils.h"
#include "STDIO.H"
#include <SYS\STAT.H>

IMPLEMENT_SERIAL(CScriptEngineEx, CScriptEngine, 1)

CScriptEngineEx::CScriptEngineEx() //: m_pDoc(NULL)
{
	TRACE_IN("CScriptEngineEx::CScriptEngineEx");
}

/*CScriptEngineEx::CScriptEngineEx(CScriptControlDoc* pDoc) : m_pDoc(pDoc)
{
	TRACE_IN("CScriptEngineEx::CScriptEngineEx");

	ASSERT_VALID(m_pDoc);
}*/


CScriptEngineEx::CScriptEngineEx(CMyDlg* pDlg):m_pDlg(pDlg)
{
	TRACE_IN("CScriptEngineEx::CScriptEngineEx");
	ASSERT_VALID(m_pDlg);
}

/*CScriptEngineEx::CScriptEngineEx(CScriptControlDoc* pDoc,
																								 REFCLSID clsidScriptEngine) :
  CScriptEngine(clsidScriptEngine),
  m_pDoc(pDoc)
{
	TRACE_IN("CScriptEngineEx::CScriptEngineEx");

	ASSERT_VALID(m_pDoc);
}*/

CScriptEngineEx::~CScriptEngineEx()
{
	TRACE_IN("CScriptEngineEx::~CScriptEngineEx");
}

void CScriptEngineEx::Serialize(CArchive& ar)
{
/*	CScriptEngine::Serialize(ar);

  if (ar.IsStoring()) {
    ar << m_pDoc;
  } else {
		CObject* pObj;
		ar >> pObj;
		m_pDoc = (CScriptControlDoc*)pObj;
  }*/
}

HRESULT CScriptEngineEx::OnGetItemInfo(
		/* [in] */ LPCOLESTR   pstrName,
		/* [in] */ DWORD       dwReturnMask,
		/* [out]*/ IUnknown**  ppUnknownItem,
		/* [out]*/ ITypeInfo** ppTypeInfo)
{
	TRACE_IN("CScriptEngineEx::OnGetItemInfo");

	HRESULT hr = S_OK;

	// get the name as a CString
	CString strName(pstrName);

	// if the ppUnknownItem pointer is valid ...
	if (ppUnknownItem != NULL) {
		// initialize the return value
		*ppUnknownItem = NULL;
	}

	// if the ppTypeInfo pointer is valid ...
	if (ppTypeInfo != NULL) {
		// initialize the return value
		*ppTypeInfo = NULL;
	}
/*
	if(wcsicmp(pstrName,L"BUTTON")==0){
		IDispatch* pDis=GetDlgItem(IDC_BUTTON1)->GetIDispatch(false);
		*ppUnknownItem = (IUnknown*)GetDlgItem(IDC_BUTTON1)->GetIDispatch(TRUE); // TRUE => does AddRef()
	}
*/
	// if the script engine wants the document object ...
	if (wcsicmp(pstrName, L"Document") == 0) {
		// get the document's dispatch interface
		IDispatch* pDispatch = m_pDlg->GetIDispatch(FALSE);//m_pDoc->GetIDispatch(FALSE); // FALSE -> don't AddRef()
		ASSERT(pDispatch != NULL);

		// if the script engine wants the object's IUnknown pointer ...
		if (dwReturnMask & SCRIPTINFO_IUNKNOWN) {
			ASSERT(ppUnknownItem != NULL);
			//ASSERT_VALID(m_pDoc);

			// return the document's IDispatch interface as its IUnknown
			*ppUnknownItem = (IUnknown*)m_pDlg->GetIDispatch(TRUE); // TRUE => does AddRef()
			ASSERT(*ppUnknownItem != NULL);
		}

		// if the script engine wants the object's ITypeInfo pointer ...
		if (dwReturnMask & SCRIPTINFO_ITYPEINFO) {
			ASSERT(ppTypeInfo != NULL);

			// just return a NULL pointer
			*ppTypeInfo = NULL;
		}
	} else {
		MESSAGE_BOX(("The script engine asked for information\nfor an unknown object named \"%s\".", (LPCSTR)strName));
		ASSERT(FALSE);
		hr = E_UNEXPECTED;
	}

	return hr;
}

/////////////////////////////////////////////////////////////////////////////
// CScriptEngineEx diagnostics

#ifdef _DEBUG
void CScriptEngineEx::AssertValid() const
{
/*	CScriptEngine::AssertValid();

	ASSERT_VALID(m_pDoc);
*/
}

void CScriptEngineEx::Dump(CDumpContext& dc) const
{
/*	CScriptEngine::Dump(dc);

	dc << "m_pDoc = " << m_pDoc;

	m_pDoc->Dump(dc);*/
}

HRESULT CScriptEngineEx::ParseFile(const char * pszFileName, 
							   LPCOLESTR pstrItemName)
{
   HRESULT hr = S_OK;
   
   struct _stat stat;
   size_t cch;
   EXCEPINFO ei;
   FILE *pfile; 
   if(_stat(pszFileName,&stat)) return E_FAIL;
   cch = stat.st_size;
   char* pszAlloc = new char[cch + 1];
   pszAlloc[cch] = '\0';// this is vitally important
   if(pszAlloc == NULL)   return E_OUTOFMEMORY;
   memset(pszAlloc,0,cch);
   // get the script text into a memory block
   pfile = fopen(pszFileName,"rb");
   if ( !pfile ) 
   {
	   hr = E_FAIL;
	   return hr;
   }
   fread(pszAlloc,cch,1,pfile);
   fclose(pfile);
   LPOLESTR pwszCode;
   
   int CharCount = MultiByteToWideChar(CP_ACP,0,pszAlloc,-1,NULL,0);
   pwszCode = new WCHAR[CharCount];
   MultiByteToWideChar(CP_ACP,0,pszAlloc,-1,pwszCode,CharCount);
   size_t t = wcslen(pwszCode);
   hr = ParseScriptText(pwszCode, pstrItemName,NULL, NULL, 0,0,0L,NULL,&ei);
   delete []pwszCode;
   delete []pszAlloc;
   return hr;
}

#endif //_DEBUG

void CScriptEngineEx::TestPointer(int p, void **result)
{
	*result=&p;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -