tfzxyinhao
tfzxyinhao

Reputation: 354

how to pass c array to javascript from C++

i found some definition in "wtypes.h"

VT_PTR  = 26,
VT_SAFEARRAY    = 27,
VT_CARRAY   = 28,
VT_USERDEFINED  = 29,
VT_LPSTR    = 30,
VT_LPWSTR   = 31,


*  VT_PTR                 [T]        pointer type
*  VT_SAFEARRAY           [T]        (use VT_ARRAY in VARIANT)
*  VT_CARRAY              [T]        C style array
*  VT_USERDEFINED         [T]        user defined type
*  VT_LPSTR               [T][P]     null terminated string
*  VT_LPWSTR              [T][P]     wide null terminated string

In my opinion,this definition show that a Variant can as a c array,ptr or c point. but can't get the type of arg when i use follow code to pass c array to javascript

STDMETHODIMP CFileSystemObject::Invoke( DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr ){
    pVarResult->vt = VT_CARRAY|VT_I4;
    pVarResult->pintVal = new int[4];
}

how to pass c array to javascript from c++?

Upvotes: 2

Views: 2367

Answers (2)

Igor Tandetnik
Igor Tandetnik

Reputation: 52471

There are several ways you can achieve something close to your goal.

A) Have your method return a SAFEARRAY (see SafeArrayCreate, SafeArrayAccessData et al). You can then pack that into a variant of type VT_ARRAY | VT_I4. JavaScript cannot consume safearrays directly, but can with the help of VBArray object. On JavaScript side, the call would look something like this:

  var retArr = new VBArray(yourObject.yourMethod()).toArray()

Note that, if you want your method to be callable from VBScript in addition to JavaScript, VBScript only understands safearrays of variants (VT_ARRAY | VT_VARIANT), not of any other type. So you would need to create a safearray of variants, and have each of those variants wrap an int. JavaScript would work with such an arrangement, too.

B) Implement a separate COM collection object wrapping the array. That object would expose, say, length and item() properties for access to individual elements; and optionally _NewEnum property to play well with VBScript's for each syntax, if that's a concern. Your method would then create and return an instance of that object.

I think, though am not 100% sure, that a default property (one with DISPID of DISPID_VALUE) is accessible in JavaScript via objectName[i] syntax, in addition to the regular objectName.item(i) syntax. If that works, that would make the collection object look almost like a native JavaScript array.

C) Create, populate and return a native JavaScript Array object:

http://www.codeproject.com/Articles/88753/Creating-JavaScript-arrays-and-other-objects-from

I wouldn't personally recommend this, as it ties your component intimately to a particular host. I mention it for completeness.

Upvotes: 2

Roman Ryltsov
Roman Ryltsov

Reputation: 69652

VT_CARRAY | VT_I4

Has no chances to work out for obvious reason: the values 28 and 3 are not even OR'rable. The resulting value of 31 is nothing but VT_LPWSTR... Compare this to VT_ARRAY 0x2000 residing in separate bit position to not interfere with base type.

VT_CARRAY is a variant type on its own and I would not even expect it to be supported on JS side. Use safe arrays to have your data delivered to JS, this is a reliable alternate option.

Upvotes: 1

Related Questions