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

📄 mydlg.cpp

📁 VB Script脚本引擎及例子程序
💻 CPP
字号:
// MyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "test2.h"
#include "MyDlg.h"
#include "ScriptEngineEx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog


CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(IDD_DIALOG1, pParent)
{
	EnableAutomation();

	//{{AFX_DATA_INIT(CMyDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CMyDlg::OnFinalRelease()
{
	// When the last reference for an automation object is released
	// OnFinalRelease is called.  The base class will automatically
	// deletes the object.  Add additional cleanup required for your
	// object before calling the base class.

	CDialog::OnFinalRelease();
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CMyDlg, CDialog)
	//{{AFX_DISPATCH_MAP(CMyDlg)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

// Note: we add support for IID_IMyDlg to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {BE2FE5D8-24E2-11D5-AECF-0050BACEC5F7}
static const IID IID_IMyDlg =
{ 0xbe2fe5d8, 0x24e2, 0x11d5, { 0xae, 0xcf, 0x0, 0x50, 0xba, 0xce, 0xc5, 0xf7 } };

BEGIN_INTERFACE_MAP(CMyDlg, CDialog)
	INTERFACE_PART(CMyDlg, IID_IMyDlg, Dispatch)
END_INTERFACE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

void CMyDlg::OnButton1() 
{
//	CString str="CLSID_VBScript";
	void* v=(void*)&CLSID_VBScript;
	CLSID* pCLSID = (CLSID*)v;
		// TODO: Add your control notification handler code here
	//m_strScript = GetScript();
	HRESULT hr = S_OK;
	CString str="baby!";
	m_strScript="msgbox\""+str+"\"";

	// if the user entered a script ...
	if (!m_strScript.IsEmpty()) {
		// get the script engine's CLSID
//		const CLSID* pCLSID = GetScriptEngineCLSID();//(CLSID*)"CLSID_VBScript";
			//GetScriptEngineCLSID();
		ASSERT(pCLSID != NULL);

		// create the script engine proxy on heap (already AddRef'd upon return)
		CScriptEngineEx* pScriptEngine = new CScriptEngineEx(this);//m_pDoc);
//		ASSERT(pScriptEngine != NULL);

		// create the actual scripting engine
		hr = pScriptEngine->Create(*pCLSID);

		// if success ...
		if (SUCCEEDED(hr)) {
			// give the engine the script
//			USES_CONVERSION; // needed by the T2COLE macro
			BSTR bstrScriptTxt=m_strScript.AllocSysString();
//			hr=pScriptEngine->ParseScriptText(bstrScriptTxt);
			hr=pScriptEngine->ParseFile("test.txt",NULL);
//			hr = pScriptEngine->ParseScriptText(m_strScript.AllocSysString());

			// if success ...
			if (SUCCEEDED(hr)) {
				// add the document as a named item
				hr = pScriptEngine->AddNamedItem(L"Document");
//				pScriptEngine->AddNamedItem(L"Button");

				// if success ...
				if (SUCCEEDED(hr)) {
					// run the script
					hr = pScriptEngine->Run();

					// if failure ...
					if (FAILED(hr)) {
//						MESSAGE_BOX(("Script failed to run.\nReturn code = %x", hr));
					}
				} else {
//					MESSAGE_BOX(("Failed to add document to script's name space.\nReturn code = %x", hr));
				}
			} else {
//				MESSAGE_BOX(("Failed to parse script.\nReturn code = %x", hr));
			}
		} else {
//			MESSAGE_BOX(("Failed to create script engine.\nReturn code = %x", hr));
		}
//		IDispatch* pDis=m_pDoc->GetIDispatch(false);

		// close the script engine
		pScriptEngine->Close();

		// release the script engine
		pScriptEngine->ExternalRelease();

		//test pointer's pointer
		int t=2;
		int* test;
		pScriptEngine->TestPointer(t,(void**)&test);
		int i=*test;
	}		
}

⌨️ 快捷键说明

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