开发者

Dilemma of Sending a Byte Array from JavaScript to COM

开发者 https://www.devze.com 2022-12-22 17:37 出处:网络
I\'m having a bit of a problem because neither Javascript nor ActiveX (written in C++) are behaving like good little children. All I\'m asking them to do is for Javascript to send a byte array and for

I'm having a bit of a problem because neither Javascript nor ActiveX (written in C++) are behaving like good little children. All I'm asking them to do is for Javascript to send a byte array and for the ActiveX to receive the byte array correctly in order to do more computation.

This is how 开发者_开发知识库I declared my byte array in JS, and I verified it inside JS:

var arr = new Array(0x00, 0xA4, 0x04, 0x00, 0x10,0xA0, 0x00, 00, 00, 0x18, 0x30, 0x03, 0x01, 00, 00, 00, 00, 00, 00, 00, 00);

Javascript sends this array as an argument to an ActiveX method. Here is the tricky part; I want the ActiveX method to receive the byte array as a SAFEARRAY or VARIANT, but I can't get it to work for the life of me.

I tried debugging and seeing the contents received inside the ActiveX as both SAFEARRAY or VARIANT, but to no avail. Here is the IDL segment:

[id(7), helpstring("blah blah blah")]  HRESULT  Blah( [in] VARIANT blah1, [out, retval]VARIANT* blah2);

Any help is greatly appreciated. Thanks in advance!


Arrays in JScript are no distinct types, they are just objects that have a property length and allow access to their contents via properties.
In your Invoke()/InvokeEx() method, the VARIANT argument you receive should contain an IDispatch, which represents the scriptable object. On that retrieve the property length and get the contents via the property names 0 to length-1.

As an implementation example, see e.g. FireBreaths IDispatchAPI::GetProperty() (IDispatchAPI wraps the scriptable browser object there). dispApi->GetProperty("length") would get the size of the array, while dispApi->GetProperty("0") to length-1 would get the actual content of the array.


I have been struggling with this in a project 5 years ago. We ended up exchanging strings between JavaScript and COM, because they have low overhead and can easily be exchanged. If the data is truly binary, you will have to encode it. We used base64, but you could also add 0x100 to all byte values, because strings are unicode (16bit character) on either side anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号