各位,你们有谁做过数组参数的传递?

xiaosun 2002-11-18 02:39:02
比方说,客户端是javascript,

var aryText = new String(2);
aryText[1] = "USER"
aryText[0] = "PWD"

var obj =window.external.Item(1);
if(obj != null)
{
obj.InitServiceContext(aryText);

}

而声明的接口函数为BSTR*,
InitServiceContext(BSTR* pbstrText);

为什么我检测
sizeof(pbstrText)/sizeof(BSTR)
总是为1,而且传来的值也不对,
还有,如何将参数传出?
...全文
45 2 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
masterz 2002-11-18
Q1: How can I access JScript Array's within my script host that were created in a script?

A1: To access JScript arrays from your script host, you should use the IActiveScript::GetScriptDispatch() method. Here are the basic steps to do so. Assume that the following code has been passed into
IActiveScriptParse::ParseScriptText with the pstrItemName parameter set to NULL.



var myArray = new Array(10);
for (i = 0; i < 10; i++)
{
myArray[i] = i;
}


1. Call IActiveScript::GetScriptDispatch() (with pstrItemName set to NULL to match the ParseScriptText call), to get an IDispatch pointer to the script.

2. Call IDispatch::GetIDsOfNames/ IDispatch::Invoke for "myArray" to get an IDispatch pointer to the array.

3. Call IDispatch::Invoke on the array's IDispatch pointer with DISPID_NEWENUM to get an IUnknown pointer. Call IUnknown::QueryInterface for IID_IEnumVARIANT to get an IEnumVARIANT interface that can be used to enumerate the array.

- or -

3. Call IDispatch::Invoke on the array's IDispatch pointer with DISPID_GET_SAFEARRAY(-2700L) to get a SAFEARRAY containing the data from the array.
Source: Joel Alley, Microsoft Developer Support, 12/1/99, microsoft.public.scripting.hosting
  • 打赏
  • 举报
回复
masterz 2002-11-18
Here is the code to convert JScript array to SafeArray vector.
This an excerpt from my project and depends on shared library
for auto pointers and error handling stuff:
http://www.geocities.com/~andien/atlaux.htm
Feel free to customize it for your own needs and environment.

-Andrew.

struct CSADtor {
static void Destroy(SAFEARRAY* v) { SafeArrayDestroy(v); }
};
struct CSADataDtor {
SAFEARRAY* m_v;
CSADataDtor(SAFEARRAY* v): m_v(v) {}
template<typename T>
void Destroy(T v) { SafeArrayUnaccessData(m_v); }
};

STDMETHODIMP CJS::VecFromArray(IDispatch* array, SAFEARRAY** val) {
IDispatchExPtr dex;
_S( AuxQI(array, &dex) );

DISPPARAMS dispparams;
ZERO_INIT(dispparams);

DISPID dispid;
_S( dex->GetDispID(CComBSTR(L"length"), fdexNameCaseSensitive, &dispid) );
CComVariant var;
_S( dex->InvokeEx(dispid, 0, DISPATCH_PROPERTYGET, &dispparams, &var, NULL, NULL) );
_S( var.ChangeType(VT_UI4) );

int length = V_UI4(&var);
CAuto<SAFEARRAY*, CSADtor> vec(SafeArrayCreateVector(VT_VARIANT, 0, length));
if ( !vec ) return _HR( E_OUTOFMEMORY );
CAuto<VARIANT*, CSADataDtor> data(NULL, CSADataDtor(vec));
_S( SafeArrayAccessData(vec, (void**)&data) );

for ( int i = 0; i < length; i++ ) {
CComBSTR prop;
_S( VarBstrFromI4(i, 0, 0, &prop) );
DISPID dispid;
if ( dex->GetDispID(prop, fdexNameCaseSensitive, &dispid) == S_OK )
{
_S( dex->InvokeEx(dispid, 0, DISPATCH_PROPERTYGET, &dispparams, &data.v[i], NULL, NULL) );
}
}

*val = vec.Detach();
return S_OK;
}

  • 打赏
  • 举报
回复
相关推荐
发帖
ATL

3233

社区成员

ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
帖子事件
创建了帖子
2002-11-18 02:39
社区公告
暂无公告