如何将ActiveX对象中的字符串数组返回给JScript

时间:2021-12-10 06:53:26

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 JavaScript. This is for script that runs on local machine for administration scripts, not for the web browser.

我需要调用Win32 API来获取一系列字符串,我想将这些字符串的数组返回给JavaScript。这适用于在本地计算机上运行管理脚本而不是Web浏览器的脚本。

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

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

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

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

该函数返回正确,但是当它们被分配给JavaScript中的变量时,字符串会“丢失”。

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

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

2 个解决方案

#1


6  

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:

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

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

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

then

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

#2


1  

Shog9 is correct. COM scripting requires that all outputs be VARIANTS.

Shog9是正确的。 COM脚本要求所有输出都是VARIANTS。

In fact, it also requires that all the INPUTs be VARIANTS as well -- see the nasty details of IDispatch in your favorite help file. It's only thought the magic of the Dual Interface implementation by ATL and similar layers (which most likely is what you are using) that you don't have to worry about that. The input VARIANTs passed by the calling code are converted to match your method signature before your actual method is called.

实际上,它还要求所有INPUT都是VARIANTS - 在您最喜欢的帮助文件中查看IDispatch的令人讨厌的细节。它只是认为ATL和类似层(你很可能正在使用它)的双接口实现的神奇之处,你不必担心这一点。在调用实际方法之前,将调用由调用代码传递的输入VARIANT以匹配您的方法签名。

#1


6  

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:

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

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

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

then

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

#2


1  

Shog9 is correct. COM scripting requires that all outputs be VARIANTS.

Shog9是正确的。 COM脚本要求所有输出都是VARIANTS。

In fact, it also requires that all the INPUTs be VARIANTS as well -- see the nasty details of IDispatch in your favorite help file. It's only thought the magic of the Dual Interface implementation by ATL and similar layers (which most likely is what you are using) that you don't have to worry about that. The input VARIANTs passed by the calling code are converted to match your method signature before your actual method is called.

实际上,它还要求所有INPUT都是VARIANTS - 在您最喜欢的帮助文件中查看IDispatch的令人讨厌的细节。它只是认为ATL和类似层(你很可能正在使用它)的双接口实现的神奇之处,你不必担心这一点。在调用实际方法之前,将调用由调用代码传递的输入VARIANT以匹配您的方法签名。