1.在COM组件中调用JavaScript函数 // 连接点方式页面javascript脚本 <object classid="CLSID:B568F111-DFE4-4944-B67F-0728AB2AB30F" id="testCom" VIEWASTEXT></object> <script language="JavaScript" for="testCom" event="staTe(s)"> alert("State(" + s + ")"); return 123; </script> <script language="JavaScript"> testCom.FireStateEvent("Hello"); </script> // 事件属性方式页面javascript脚本 function onState(s){ alert("onState(" + s + ")"); return 456; } var o = new ActiveXObject("TestATL.TestCom"); o.onstaTe=onState; o.FireStateEvent("Hello"); // Com组件VC7.1 ATL代码 __interface _ITestComEvents{ [id(1), helpstring("State事件")] HRESULT State([in] BSTR str); }; __event __interface _ITestComEvents; IDispatchPtr m_onState; // 事件属性 STDMETHOD(get_onState)(IDispatch** pVal) { *pVal = m_onState; return S_OK; }; STDMETHOD(put_onState)(IDispatch* newVal) { m_onState = newVal; return S_OK; }; STDMETHOD(FireStateEvent)(BSTR str) { __raise State(str); // 激发连接点事件 CComVariant result; CComVariant avarParams[1] = {str}; DISPPARAMS dispParams = {avarParams, NULL, 1, 0}; EXCEPINFO excepInfo; memset(&excepInfo, 0, sizeof excepInfo); UINT nArgErr = (UINT)-1; // initialize to invalid arg if (m_onState) // 激发属性事件 HRESULT hr = m_onState->Invoke(0, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispParams, &result, &excepInfo, &nArgErr); return S_OK; } 参见: How To Call a Script Function from a VC WebBrowser Application 如何在COM object中使用 Javascript function object? 在COM组件中调用JavaScript函数 2.从页面javascript向Com组件传递结构数组 // 页面脚本 var o = new ActiveXObject("TestATL.TestCom"); o.onstaTe=onState; o.Put("array", {0: 123, 1: "abc"}); o.Put("array", [456, "def"]); o.Put("array", [{name: "tom", age: 8}, {name: "jack", age: 10}]); var a = new Array(789, "ghi"); // has "length" property o.Put("array", a); // Com组件VC7.1 ATL代码 STDMETHODIMP CTestCom::Put(BSTR key, VARIANT value) { WCHAR output[4096] = L""; if(0 == wcsicmp(key, L"array") && VT_DISPATCH == value.vt) { IDispatchPtr spDisp = value.pdispVal; DISPID dispID = 0; DISPPARAMS dispParams = {NULL, NULL, 0, 0}; CComVariant result; EXCEPINFO excepInfo; memset(&excepInfo, 0, sizeof excepInfo); UINT nArgErr = (UINT)-1; // initialize to invalid arg unsigned int length = 0; // 数组长度 或 属性 个数 LPOLESTR func = L"length"; HRESULT hr = spDisp->GetIDsOfNames(GUID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispID); if(S_OK == hr){ // 如果有"length"属性 hr = spDisp->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dispParams, &result, &excepInfo, &nArgErr); if(S_OK == hr && VT_I4 == result.vt) length = result.intVal; // 直接读取数组长度 }else{ unsigned int nTypeInfo = 0; hr = spDisp->GetTypeInfoCount(&nTypeInfo); ATLASSERT(1 == nTypeInfo); ITypeInfoPtr spTypeInfo; hr = spDisp->GetTypeInfo(0, 0, &spTypeInfo); TYPEATTR *pTypeAttr = NULL; hr = spTypeInfo->GetTypeAttr(&pTypeAttr); //ATLASSERT("{C59C6B12-F6C1-11CF-8835-00A0C911E8B2}" == pTypeAttr->guid); // JScript: length = pTypeAttr->cVars; // 从类型信息读取数组长度 spTypeInfo->ReleaseTypeAttr(pTypeAttr); } for(unsigned int i=0; i<length; i++) { WCHAR buf[32]; _itow(i, buf, 10); func = buf; hr = spDisp->GetIDsOfNames(GUID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispID); hr = spDisp->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dispParams, &result, &excepInfo, &nArgErr); if(S_OK != hr) continue; if(VT_DISPATCH == result.vt){ IDispatchPtr spItem = result.pdispVal; func = L"name"; hr = spItem->GetIDsOfNames(GUID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispID); hr = spItem->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dispParams, &result, &excepInfo, &nArgErr); if(S_OK == hr && VT_BSTR == result.vt) swprintf(output + wcslen(output), L"name=%s", result.bstrVal); func = L"age"; hr = spItem->GetIDsOfNames(GUID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispID); hr = spItem->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dispParams, &result, &excepInfo, &nArgErr); if(S_OK == hr && VT_I4 == result.vt) swprintf(output + wcslen(output), L" age=%d\n", result.intVal); }else if(VT_BSTR == result.vt) swprintf(output + wcslen(output), L"BSTR:%s\n", result.bstrVal); else if(VT_I4 == result.vt) swprintf(output + wcslen(output), L"I4:%d\n", result.intVal); else swprintf(output + wcslen(output), L"item.vt=%d\n", result.vt); } } FireStateEvent(output); return S_OK; } 3.枚举IE窗口的内容,并调用其中的脚本 #import <mshtml.tlb> // Internet Explorer 5 #import <shdocvw.dll> SHDocVw::IShellWindowsPtr spSHWinds; spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)); long nCount = spSHWinds->GetCount(); IDispatchPtr spDisp; for (long i = 0; i < nCount; i++) { _variant_t va(i, VT_I4); spDisp = spSHWinds->Item(va); SHDocVw::IWebBrowser2Ptr spBrowser(spDisp); if (spBrowser != NULL) { _bstr_t location = spBrowser->GetLocationName(); if(_bstr_t(L"Test DapCtrl") == location) // 找指定IE窗口 { IHTMLDocument2Ptr spDoc(spBrowser->GetDocument()); if (spDoc != NULL) { _bstr_t exp = m_onState; IDispatch *pdis = NULL; hr = spDoc->get_Script(&pdis); if(pdis){ DISPID tmpDispID = 0; LPOLESTR func = L"Test"; // javascript 函数名 hr = pdis->GetIDsOfNames(GUID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &tmpDispID); if(S_OK == hr) hr = pdis->Invoke(tmpDispID, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispParams, &result, &excepInfo, &nArgErr); } } } } } 参见: HOWTO: Connect to a Running Instance of Internet Explorer ActiveX组件与JavaScript交互 ActiveX组件控制其所在的IE窗口 4.在VC中执行脚本 #import <msscript.ocx> // msscript.ocx using namespace MSScriptControl; IScriptControlPtr pScriptControl(__uuidof(ScriptControl)); LPSAFEARRAY psa; SAFEARRAYBOUND rgsabound[] = { 1, 0 }; // 1 elements, 0-based int i; psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound); if (!psa) { return E_OUTOFMEMORY; } VARIANT vFlavors[1]; for (i = 0; i < 1; i++) { VariantInit(&vFlavors[i]); V_VT(&vFlavors[i]) = VT_BSTR; } V_BSTR(&vFlavors[0]) = SysAllocString(bstr); long lZero = 0; hr = SafeArrayPutElement(psa, &lZero,&vFlavors[0]); for(i=0;i<1;i++) { SysFreeString(vFlavors[i].bstrVal); } pScriptControl->Language = "JScript"; pScriptControl->AllowUI = TRUE; _bstr_t exp = L"1+2+3"; _variant_t outpar = pScriptControl->Eval(exp); //_variant_t outpar = pScriptControl->ExecuteStatement(exp); //_variant_t outpar = pScriptControl->Run("MyStringFunction", &psa); _bstr_t bstrReturn = (_bstr_t)outpar; char *pResult = (char *)bstrReturn; SafeArrayDestroy(psa); 参见: How To Call Run() Method of the Microsoft Script Control in C++
本文研究如何在JS等脚本语言与ActiveX控件之间通信,如何传递各种类型的参数,以及COM的IDispatch接口。使用类似的方法,可以推广到其他所有脚本型语言,如LUA,AutoCad等。
本文将研究以下几个方面:
1.
2.
3.
4.
5.
6.
7.
8.
(一)整形参数
1.
2.
STDMETHODIMP CJsAtl::IntSum(LONG a, LONG b, LONG* retVal)
{
}
3.
<object id="obj" classid="CLSID:AD694878-......"> </object>
function test_int()
{
}
1.
2.
a)
b)
c)
3.
STDMETHODIMP CJsAtl::StringAdd(BSTR str1, BSTR str2, BSTR* retVal)
{
}
4.
function test_str_cat()
{
}
(三)修改传入字符串内容
1.
2.
3.
4.
STDMETHODIMP CJsAtl::StrModify(BSTR str)
{
}
5.
function test_str_modify()
{
}
6.
原字符串:
调用后:
(四)数组参数
1.
2.
3.
4.
下面代码中定义了两个函数 GetArrayNumberOfIndex、GetArrayLength两个函数,功能分别获取数组长度和获取指定序号元素
以下代码含义,请参考下一节 “IDispatch接口介绍”
JS数组在COM中是一个IDispatch对象,获取长度,实际是获取其中名为“length”的属性值。
而获取最后一个数组,实际是获取名为“4”的属性值(假设5个元素)
STDMETHODIMP CJsAtl::GetLastElement(VARIANT vArray, LONG* retVal)
{
}
// ***
// 获取Javascript数组长度
//
// ***
HRESULT GetArrayLength(IDispatch* pDisp, int* pLength)
{
}
// ***
// 获取Javascript数组中指定位置的整数元素值
// ***
HRESULT GetArrayNumberOfIndex(IDispatch* pDisp, int index, int * pValue)
{
}
5.
function test_get_last()
{
}
6.
数组定义:{0,1,2,3}
最后元素:3
(五)IDispatch接口介绍
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
DISPPARAMS dispparams;
a.
b.
c.
d.
11.
DISPPARAMS dispparams;
dispparams.rgvarg[0].intVal = 123;
dispparams.rgvarg[1].vt = VT_BSTR; // 字符串型
dispparams.rgvarg[1].bstrVal = L"abcd";
(六)修改输入数组内容
1.
2.
3.
STDMETHODIMP CJsAtl::ArrayModiy(VARIANT vArray)
{
}
// ***
// 设置Javascript数组中指定位置的整数元素值
// ***
HRESULT SetArrayNumberOfIndex(IDispatch* pDisp, int index, int value)
{
}
4.
function test_set_first()
{
}
5.
原数组:{0,
修改后:{123,1,2,3}
(七)增加数组内容
1.
2.
3.
STDMETHODIMP CJsAtl::AddNewElement(VARIANT vArray)
{
}
// ****************************************************
// 向js数组中增加元素
// ****************************************************
HRESULT AddArrayElement(IDispatch* pDisp, int value)
{
}
4.
function test_add_element()
{
}
5.
原数组:{0,1,2,3}
增加后:{0,1,2,3,123}
(八)以数组传参方式,JS调用S4Execute( )
1.
2.
3.
4.
SENSE4_CONTEXT
// 打开设备,以设备ID作为筛选条件,若设备ID指定为空串,则打开第一把锁
STDMETHODIMP CS4ActiveX::OpenLock(BSTR deviceID, LONG* retVal)
{
cleanup:
}
STDMETHODIMP CS4ActiveX::ChangeDir(BSTR dir, LONG* retVal)
{
}
STDMETHODIMP CS4ActiveX::Execute(BSTR fileID, VARIANT inBuff, VARIANT outBuf, LONG* retVal)
{
}
STDMETHODIMP CS4ActiveX::VerifyPin(BSTR userPin, LONG* retVal)
{
}
STDMETHODIMP CS4ActiveX::Close(LONG* retVal)
{
}