读书人

VC中怎么创建JavaScript的Array对象

发布时间: 2012-04-19 14:36:43 作者: rapoo

求助:VC中如何创建JavaScript的Array对象
请教各位,在VC中如何创建JavaScript的Array对象,COM对象,Array对象的接口在JScript.dll中为JSGlobal::ArrayInstance,但不清楚怎么实现,不希望由JavaScript创建生成Array对象,然后传递进来,那样虽然有一个确切的ArrayInstance接口指针,但不怎么适合我的工程,希望知道的告知一声!

[解决办法]
如果能得到IHTMLWindow2接口指针,就可以这样做:

IDispatchPtr pArray;
__CreateObject(pWin2,L"Array",NULL,&pArray);


/// 摘自: PIMShell
static HRESULT __CreateObject(IDispatch* pWin2,LPCTSTR szObjectName,DISPPARAMS* pdp,IDispatch** ppDisp)
{
//get the Idispatchex of window2
CComPtr<IDispatchEx> pWinEx;
HRESULT hr=__GetWindow2Ex(pWin2,&pWinEx);
if(FAILED(hr))
return hr;

//get dispid
DISPID dispid;
CComBSTR bstrName=szObjectName;
hr=pWinEx->GetDispID(bstrName,0,&dispid);
if(FAILED(hr))
return hr;

//params
DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};

DISPPARAMS* pParams;
if(pdp==NULL)
pParams=&dispparamsNoArgs;
else
pParams=pdp;

//invoke
CComVariant vResult;
hr=pWinEx->InvokeEx(dispid, LOCALE_USER_DEFAULT,
DISPATCH_CONSTRUCT, pParams,&vResult, NULL, NULL);
if(FAILED(hr))
return hr;

return vResult.pdispVal->QueryInterface(IID_IDispatch,(void**)ppDisp);
}


static HRESULT __GetWindow2Ex(IDispatch* pWin2,IDispatchEx** ppWinEx)
{
return pWin2->QueryInterface(IID_IDispatchEx,(void**)ppWinEx);
}

读书人网 >VC/MFC

热点推荐