2

「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

私の意見では、この定義は、Variant が ac 配列、ptr、または c ポイントとして使用できることを示しています。しかし、次のコードを使用してc配列をjavascriptに渡すと、argのタイプを取得できません

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];
}

c++からjavascriptにc配列を渡す方法は?

4

2 に答える 2

2

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.

于 2013-07-15T13:58:48.120 に答える