1

I have COM function in a disp interface as shown below,

[id(1)] HRESULT MultipleReturn([out]BSTR* arg1, [out, retval] BSTR* arg2);

implemented as

STDMETHODIMP SomeCoolObject::MultipleReturn(BSTR* arg1, BSTR* arg2)
{
    *arg1 = SysAllocString(L"test1");
    *arg2 = SysAllocString(L"test2");
    return S_OK;
}

In Python I can call it as

import comtypes.client as cc

obj = cc.CreateObject('SomeCoolObject')
a = obj.MultipleReturn()
print(a) # gives (u'test1', u'test2'), Python, I see you don't bite :)

same in JavaScript

var obj = new ActiveXObject("SomeCoolObject")

// this gives error, kind of obvious
// 'Wrong number of arguments or invalid property assignment'
// var val = obj.MultipleReturn();

var a = "holaaa!";
var val = obj.MultipleReturn(a);
alert(val); // gives "test2"
alert(a);   // gives "holaaa!", may be could have given "test1"

this proves, JavaScript won't play this ball. Why? If not, then how do you return multiple values from COM to JavaScript. For this particular job, I returned a JSON though.

2
  • I'd think about a SafeArray or in that direction. Commented Jun 10, 2015 at 13:57
  • Yes. I thought about using SAFEARRAY but question is about collecting two [out] variables in JavaScript. Not possible? Commented Jun 10, 2015 at 14:01

1 Answer 1

3

JavaScript/COM binding doesn't support [out] parameters - only [out, retval] (of which, of course, there can only be one). JavaScript generally doesn't have a notion of pass-by-reference.

There are several ways you can get closer to your goal.

  1. Return a SAFEARRAY of two strings. In JavaScript, consume it via VBArray object.

  2. Implement a simple COM object with two BSTR properties, create and return an instance of that object via [out, retval] IDispatch**.

  3. Take IDispatch* as an [in] parameter, set a new property on it via IDispatchEx::GetDispID(fdexNameEnsure). JavaScript would consume it like this:

.

var outParam = {};
var result = obj.MultipleReturn(outParam);
var secondResult = outParam.value;

(where value is the name of the property your method creates on the object).

Sign up to request clarification or add additional context in comments.

5 Comments

It's not JavaScript per se, it's COM automation (late-binding, dispatching, whatever it's called these days) that only supports [out, retval].
@PauloMadeira Nothing in Automation spec precludes [out] parameters. They work fine with VB, VBA and VBScript. So no, it's specifically JavaScript, and its COM binding, that have this limitation.
You're right. But with late binding, you have to inform the language of such arguments (e.g. ByRef). I'm almost sure the op's Python code, as-is, could only return those values with early binding. So, it's not even a fair comparison with the VB* languages.
@PauloMadeira I'm not familiar with Python/COM binding. My guess would be, it's reading the type library, via IDispatch::GetTypeInfo. The information about parameter types and direction of data flow is available there. I don't see any evidence of early binding in the sample shown.
You're right again. I can't bother to check comtypes' code right now, but that's plausible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.