1

EXE通过IScriptControlPtr调用jscript,喜欢

obj.dec = myobject_dec

但 jscript 无法附加 COM 事件,抛出“对象不支持此属性或方法”消息

以下 jscript 的代码:

var obj1;

function myobject_dec()
{
    obj1.messagebox("xxx");
}

function myobject_main(obj)
{
    obj1 = obj;
    obj.dec=myobject_dec; //it's report "Object doesn't support this property or method"
    obj.test("bbb");//it's fire dec event
}

以下com代码:

 interface IScript : IDispatch {
        [id(1)] HRESULT test([in] BSTR aaa);
        [id(3)] HRESULT get_script([out,retval] IDispatch** jscript);
    };
    [
        uuid(702C44D6-6649-4FA7-8B19-2E59DB7116EF),
        version(1.0),
        helpstring("testScript 1.0")
    ]
    library testScriptLib
    {
        importlib("stdole2.tlb");
        [
            uuid(2BF42BF6-457E-4923-9DFF-59BAD961EE6C),
            helpstring("_IScriptEvents ")
        ]
        dispinterface _IScriptEvents
        {
            properties:
            methods:
                [id(1)] HRESULT add([in] BSTR a, BSTR b);
                [id(2)] HRESULT dec(void);
        };
        [
            uuid(6ED96EBA-4144-47FA-9467-7C41A6BFA155),
            helpstring("Script Class")
        ]
        coclass Script
        {
            [default] interface IScript;
            [default, source] dispinterface _IScriptEvents;
        };
    };

以下实现代码

class ATL_NO_VTABLE CScript :
    public CComObjectRootEx<CComMultiThreadModel>,
    public CComCoClass<CScript, &CLSID_Script>,
    public IProvideClassInfo2Impl<&CLSID_Script, NULL, &LIBID_testScriptLib>,
    public IConnectionPointContainerImpl<CScript>,
    public CProxy_IScriptEvents<CScript>,
    public IDispatchImpl<IScript, &IID_IScript, &LIBID_testScriptLib, /*wMajor =*/ 1, /*wMinor =*/ 0>


BEGIN_COM_MAP(CScript)
    COM_INTERFACE_ENTRY(IProvideClassInfo2)
    COM_INTERFACE_ENTRY(IProvideClassInfo)
    COM_INTERFACE_ENTRY(IScript)
    COM_INTERFACE_ENTRY(IDispatch)
    //COM_INTERFACE_ENTRY(IDispatchEx) 
    COM_INTERFACE_ENTRY(IConnectionPointContainer)
END_COM_MAP()

BEGIN_CONNECTION_POINT_MAP(CScript)
    CONNECTION_POINT_ENTRY(__uuidof(_IScriptEvents))
END_CONNECTION_POINT_MAP()

以下exe代码:

CComSafeArray<VARIANT> sfHelper;
IDispatchPtr dispatch(script_);//IScriptPtr script_
sfHelper.Add(CComVariant(dispatch.GetInterfacePtr()));
LPSAFEARRAY* sa =  sfHelper.GetSafeArrayPtr();
_variant_t varRet;
varRet = m_pScript->Run(_T("myobject_main"), sa );

如何在 jscript 中附加 IScript::dec 事件???

4

0 回答 0