如何将一个字符串数组从ActiveX对象返回到JScript [英] How do I return an array of strings from an ActiveX object to JScript

查看:495
本文介绍了如何将一个字符串数组从ActiveX对象返回到JScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用一个Win32 API来获取一系列字符串,并且我想将这些字符串的数组返回到JScript。

I need to call into a Win32 API to get a series of strings, and I would like to return an array of those strings to JScript. This is for script that runs on local machine for administration scripts, not for the web browser.

我的COM对象的IDL文件具有我调用的接口:

My IDL file for the COM object has the interface that I am calling into as:


HRESULT GetArrayOfStrings([out, retval] SAFEARRAY(BSTR) * rgBstrStringArray);

函数返回correcty,但是当字符串被分配给JScript中的变量时,字符串将会丢失。

The function returns correcty, but the strings are getting 'lost' when they are being assigned to a variable in JScript.

问题是:
将字符串数组返回给JScript变量的正确方法是什么?

The question is: What is the proper way to get the array of strings returned to a JScript variable?

推荐答案

如果我没记错,你需要将 SAFEARRAY 包裹在 VARIANT ,以便它通过,然后使用 VBArray对象在JS端解压缩它:

If i recall correctly, you'll need to wrap the SAFEARRAY in a VARIANT in order for it to get through, and then use a VBArray object to unpack it on the JS side of things:

HRESULT GetArrayOfStrings(/*[out, retval]*/ VARIANT* pvarBstrStringArray)
{
   // ...

   _variant_t ret;
   ret.vt = VT_ARRAY|VT_VARIANT;
   ret.parray = rgBstrStringArray;
   *pvarBstrStringArray = ret.Detach();
   return S_OK;
}

然后

var jsFriendlyStrings = new VBArray( axOb.GetArrayOfStrings() ).toArray();

这篇关于如何将一个字符串数组从ActiveX对象返回到JScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆