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

📄 eval.cpp

📁 在程序中加入脚本支持
💻 CPP
字号:
/////////////////////////////////////////////////////////////
//
// eval.cpp - Don Box, 1996-1997
// 
// A program that loads and executes script code.
//

#define _WIN32_DCOM
#include <windows.h>
#include <stdio.h>
#include <activscp.h>
#include <olectl.h>
#include <stddef.h>

// bring in definition of CHostShell (the host application's object)
#include "hostshell.h"

// bring in definition of CActiveScriptSite (the host's site object)
#include "hostsite.h"

// helper function to load a type description from the typelib that
// is bound as a custom resource in this executable
HRESULT LoadTypeInfoFromThisModule(REFIID riid, ITypeInfo **ppti) {
    *ppti = 0;
    char szFileName[MAX_PATH];
    GetModuleFileNameA(0, szFileName, MAX_PATH);
    OLECHAR wszFileName[MAX_PATH];
    mbstowcs(wszFileName, szFileName, MAX_PATH);
    ITypeLib *ptl = 0;
    HRESULT hr = LoadTypeLib(wszFileName, &ptl);
    if (SUCCEEDED(hr)) {
        hr = ptl->GetTypeInfoOfGuid(riid, ppti);
        ptl->Release();
    }
    return hr;
}

// helper function to read a script from a file as OLECHAR-compatible text
OLECHAR *ReadFileAsOLETEXT(LPCSTR szFileName) {
    OLECHAR *pwszResult = 0;
    HANDLE hfile = CreateFileA(szFileName, GENERIC_READ,
                              FILE_SHARE_READ, 0, OPEN_EXISTING,
                              FILE_ATTRIBUTE_NORMAL, 0);
    if (hfile != INVALID_HANDLE_VALUE)
    {
        DWORD cch = GetFileSize(hfile, 0);
        char *psz = (char*)CoTaskMemAlloc(cch + 1);
        if (psz)
        {
            DWORD cb;
            ReadFile(hfile, psz, cch, &cb, 0);
            pwszResult = (OLECHAR*)CoTaskMemAlloc((cch + 1)*sizeof(OLECHAR));
            if (pwszResult)
                mbstowcs(pwszResult, psz, cch + 1);
            pwszResult[cch] = 0;
            CoTaskMemFree(psz);
        }
        CloseHandle(hfile);
    }
    return pwszResult;
}

// helper class to ease the nesting chaos that comes from the lack
// of exception support in the C++ language mapping for COM.
struct HRESULT_EXCEPTION 
{
    HRESULT_EXCEPTION(HRESULT hr)
    { if (FAILED(hr)) throw hr; }

    HRESULT operator = (HRESULT hr) 
    { if (FAILED(hr)) throw hr; return hr; }
};

int main(int argc, char **argv)
{
    if (argc < 3)
    {
        printf("###usage: %s language script1 [script2 [...]]\n", argv[0]);
        return 0;
    }
    CoInitialize(0);
// create host object(s)
    CHostShell *phd = new CHostShell;
// create site object
    CActiveScriptSite *pass = new CActiveScriptSite(phd);
    pass->AddRef();
    IActiveScriptParse *pasp = 0;
    IActiveScript *pas = 0;
    IDispatch *pDisp = 0;

    try
    {
// create script engine
        CLSID clsid;
        OLECHAR wszProgID[256];
        mbstowcs(wszProgID, argv[1], 256);
        HRESULT_EXCEPTION hr = CLSIDFromProgID(wszProgID, &clsid);

        hr = CoCreateInstance(clsid, 0, CLSCTX_ALL,
                              IID_IActiveScriptParse, (void**)&pasp);

        hr = pasp->QueryInterface(IID_IActiveScript, 
                                  (void**)&pas);

// set script state to INITIALIZED 
        hr = pasp->InitNew();
        hr = pas->SetScriptSite(pass);

// add host shell object to engine's namespace and set state to STARTED
        hr = pas->AddNamedItem(OLESTR("shell"), 
                               SCRIPTITEM_ISVISIBLE|SCRIPTITEM_ISSOURCE);
        hr = pas->SetScriptState(SCRIPTSTATE_STARTED);

// add a handler that beeps when the "shell" object sends the OnEndOfLine event
        BSTR bstrName = 0;
        EXCEPINFO ei; ZeroMemory(&ei, sizeof(ei));
        
        hr = pasp->AddScriptlet(L"EOL", 
                                L"shell.beep()",
                                L"shell",
                                L"shell",
                                L"OnEndOfLine",
                                L"", 
                                0, 
                                0,
                                0, 
                                &bstrName,
                                &ei);
        
        SysFreeString(bstrName);

// add supplied script files to the engine's parsed state
        for (int i = 2; i < argc; i++)
        {
            EXCEPINFO ei; ZeroMemory(&ei, sizeof(ei));
            OLECHAR *pwszCode = ReadFileAsOLETEXT(argv[i]);
            if (pwszCode)
            {
                hr = pasp->ParseScriptText(pwszCode, 0, 0, 0, 0, 0, 
                                           SCRIPTTEXT_ISPERSISTENT
                                           |SCRIPTTEXT_ISVISIBLE, 
                                           0, &ei);
                CoTaskMemFree(pwszCode);
            }

        }

// force the engine to connect any outbound interfaces to the 
// host's objects
        hr = pas->SetScriptState(SCRIPTSTATE_CONNECTED);

// get the dispatch interface to the engine and call the procedure named "Main"
        hr = pas->GetScriptDispatch(0, &pDisp);
        LPOLESTR szMain = OLESTR("main");
        DISPID dispid;
        hr = pDisp->GetIDsOfNames(IID_NULL, &szMain, 1, 9, &dispid);
        DISPPARAMS params = { 0, 0, 0, 0 };
        hr = pDisp->Invoke(dispid, IID_NULL, 9, DISPATCH_METHOD,
                           &params, 0, 0, 0);
    }
    catch (HRESULT hresult)
    {
        printf("### error: %x\n", hresult);        
    }

// cleanup    
    if (pDisp)
        pDisp->Release();
    if (pas)
        pas->Release();
    if (pasp)
        pasp->Release();
    pass->Release();

    CoUninitialize();
    return 0;
}

⌨️ 快捷键说明

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