11.在COM组件中调用JavaScript函数
2// 连接点方式页面javascript脚本
3<object classid="CLSID:B568F111-DFE4-4944-B67F-0728AB2AB30F" id="testCom" VIEWASTEXT></object>
4<script language="JavaScript" for="testCom" event="staTe(s)">
5 alert("State(" + s + ")");
6 return 123;
7</script>
8<script language="JavaScript">
9 testCom.FireStateEvent("Hello");
10</script>
11
12// 事件属性方式页面javascript脚本
13function onState(s){
14 alert("onState(" + s + ")");
15 return 456;
16}
17var o = new ActiveXObject("TestATL.TestCom");
18o.onstaTe=onState;
19o.FireStateEvent("Hello");
20
21// Com组件VC7.1 ATL代码
22__interface _ITestComEvents{
23 [id(1), helpstring("State事件")] HRESULT State([in] BSTR str);
24};
25__event __interface _ITestComEvents;
26IDispatchPtr m_onState; // 事件属性
27STDMETHOD(get_onState)(IDispatch** pVal) {
28 *pVal = m_onState;
29 return S_OK;
30};
31STDMETHOD(put_onState)(IDispatch* newVal) {
32 m_onState = newVal;
33 return S_OK;
34};
35STDMETHOD(FireStateEvent)(BSTR str) {
36 __raise State(str); // 激发连接点事件
37 CComVariant result;
38 CComVariant avarParams[1] = {str};
39 DISPPARAMS dispParams = {avarParams, NULL, 1, 0};
40 EXCEPINFO excepInfo;
41 memset(&excepInfo, 0, sizeof excepInfo);
42 UINT nArgErr = (UINT)-1; // initialize to invalid arg
43 if (m_onState) // 激发属性事件
44 HRESULT hr = m_onState->Invoke(0, IID_NULL, LOCALE_USER_DEFAULT,
45 DISPATCH_METHOD, &dispParams, &result, &excepInfo, &nArgErr);
46 return S_OK;
47}
48参见:
49How To Call a Script Function from a VC WebBrowser Application
50如何在COM object中使用 Javascript function object?
51在COM组件中调用JavaScript函数
52
532.从页面javascript向Com组件传递结构数组
54// 页面脚本
55var o = new ActiveXObject("TestATL.TestCom");
56o.onstaTe=onState;
57o.Put("array", {0: 123, 1: "abc"});
58o.Put("array", [456, "def"]);
59o.Put("array", [{name: "tom", age: 8}, {name: "jack", age: 10}]);
60var a = new Array(789, "ghi"); // has "length" property
61o.Put("array", a);
62
63// Com组件VC7.1 ATL代码
64STDMETHODIMP CTestCom::Put(BSTR key, VARIANT value)
65{
66WCHAR output[4096] = L"";
67if(0 == wcsicmp(key, L"array") && VT_DISPATCH == value.vt)
68{
69 IDispatchPtr spDisp = value.pdispVal;
70 DISPID dispID = 0;
71 DISPPARAMS dispParams = {NULL, NULL, 0, 0};
72 CComVariant result;
73 EXCEPINFO excepInfo;
74 memset(&excepInfo, 0, sizeof excepInfo);
75 UINT nArgErr = (UINT)-1; // initialize to invalid arg
76 unsigned int length = 0; // 数组长度 或 属性 个数
77
78 LPOLESTR func = L"length";
79 HRESULT hr = spDisp->GetIDsOfNames(GUID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispID);
80 if(S_OK == hr){ // 如果有"length"属性
81 hr = spDisp->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dispParams, &result, &excepInfo, &nArgErr);
82 if(S_OK == hr && VT_I4 == result.vt)
83 length = result.intVal; // 直接读取数组长度
84 }else{
85 unsigned int nTypeInfo = 0;
86 hr = spDisp->GetTypeInfoCount(&nTypeInfo);
87 ATLASSERT(1 == nTypeInfo);
88 ITypeInfoPtr spTypeInfo;
89 hr = spDisp->GetTypeInfo(0, 0, &spTypeInfo);
90 TYPEATTR *pTypeAttr = NULL;
91 hr = spTypeInfo->GetTypeAttr(&pTypeAttr);
92 //ATLASSERT("{C59C6B12-F6C1-11CF-8835-00A0C911E8B2}" == pTypeAttr->guid); // JScript:
93 length = pTypeAttr->cVars; // 从类型信息读取数组长度
94 spTypeInfo->ReleaseTypeAttr(pTypeAttr);
95 }
96 for(unsigned int i=0; i<length; i++)
97 {
98 WCHAR buf[32];
99 _itow(i, buf, 10);
100 func = buf;
101 hr = spDisp->GetIDsOfNames(GUID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispID);
102 hr = spDisp->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dispParams, &result, &excepInfo, &nArgErr);
103 if(S_OK != hr)
104 continue;
105 if(VT_DISPATCH == result.vt){
106 IDispatchPtr spItem = result.pdispVal;
107 func = L"name";
108 hr = spItem->GetIDsOfNames(GUID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispID);
109 hr = spItem->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dispParams, &result, &excepInfo, &nArgErr);
110 if(S_OK == hr && VT_BSTR == result.vt)
111 swprintf(output + wcslen(output), L"name=%s", result.bstrVal);
112 func = L"age";
113 hr = spItem->GetIDsOfNames(GUID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispID);
114 hr = spItem->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dispParams, &result, &excepInfo, &nArgErr);
115 if(S_OK == hr && VT_I4 == result.vt)
116 swprintf(output + wcslen(output), L" age=%d\n", result.intVal);
117 }else if(VT_BSTR == result.vt)
118 swprintf(output + wcslen(output), L"BSTR:%s\n", result.bstrVal);
119 else if(VT_I4 == result.vt)
120 swprintf(output + wcslen(output), L"I4:%d\n", result.intVal);
121 else
122 swprintf(output + wcslen(output), L"item.vt=%d\n", result.vt);
123 }
124}
125FireStateEvent(output);
126return S_OK;
127}
128
1293.枚举IE窗口的内容,并调用其中的脚本
130#import <mshtml.tlb> // Internet Explorer 5
131#import <shdocvw.dll>
132 SHDocVw::IShellWindowsPtr spSHWinds;
133 spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows));
134 long nCount = spSHWinds->GetCount();
135 IDispatchPtr spDisp;
136 for (long i = 0; i < nCount; i++)
137 {
138 _variant_t va(i, VT_I4);
139 spDisp = spSHWinds->Item(va);
140 SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
141 if (spBrowser != NULL)
142 {
143 _bstr_t location = spBrowser->GetLocationName();
144 if(_bstr_t(L"Test DapCtrl") == location) // 找指定IE窗口
145 {
146 IHTMLDocument2Ptr spDoc(spBrowser->GetDocument());
147 if (spDoc != NULL)
148 {
149 _bstr_t exp = m_onState;
150 IDispatch *pdis = NULL;
151 hr = spDoc->get_Script(&pdis);
152 if(pdis){
153 DISPID tmpDispID = 0;
154 LPOLESTR func = L"Test"; // javascript 函数名
155 hr = pdis->GetIDsOfNames(GUID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &tmpDispID);
156 if(S_OK == hr)
157 hr = pdis->Invoke(tmpDispID, IID_NULL, LOCALE_USER_DEFAULT,
158 DISPATCH_METHOD, &dispParams, &result, &excepInfo, &nArgErr);
159 }
160 }
161 }
162 }
163 }
164参见:
165HOWTO: Connect to a Running Instance of Internet Explorer
166ActiveX组件与JavaScript交互
167ActiveX组件控制其所在的IE窗口
168
1694.在VC中执行脚本
170#import <msscript.ocx> // msscript.ocx
171using namespace MSScriptControl;
172 IScriptControlPtr pScriptControl(__uuidof(ScriptControl));
173 LPSAFEARRAY psa;
174 SAFEARRAYBOUND rgsabound[] = { 1, 0 }; // 1 elements, 0-based
175 int i;
176 psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
177 if (!psa)
178 {
179 return E_OUTOFMEMORY;
180 }
181 VARIANT vFlavors[1];
182 for (i = 0; i < 1; i++)
183 {
184 VariantInit(&vFlavors[i]);
185 V_VT(&vFlavors[i]) = VT_BSTR;
186 }
187 V_BSTR(&vFlavors[0]) = SysAllocString(bstr);
188 long lZero = 0;
189 hr = SafeArrayPutElement(psa, &lZero,&vFlavors[0]);
190 for(i=0;i<1;i++)
191 {
192 SysFreeString(vFlavors[i].bstrVal);
193 }
194 pScriptControl->Language = "JScript";
195 pScriptControl->AllowUI = TRUE;
196 _bstr_t exp = L"1+2+3";
197 _variant_t outpar = pScriptControl->Eval(exp);
198 //_variant_t outpar = pScriptControl->ExecuteStatement(exp);
199 //_variant_t outpar = pScriptControl->Run("MyStringFunction", &psa);
200 _bstr_t bstrReturn = (_bstr_t)outpar;
201 char *pResult = (char *)bstrReturn;
202 SafeArrayDestroy(psa);
203参见:
204How To Call Run() Method of the Microsoft Script Control in C++
205
posted on 2010-10-09 03:21
幽幽 阅读(3401)
评论(2) 编辑 收藏 引用