views:

189

answers:

2

Hello there, 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("NEW Sends APDU to Card and Reads the Response")]  HRESULT  NewSendAPDU( [in] VARIANT pAPDU, [out, retval]VARIANT* pAPDUResponse);

Any help is greatly appreciated. Thanks in advance!

+2  A: 

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.

Georg Fritzsche
Yup, looks like IDispatch is the way to go seeing as how my Javascript code does indeed create a VARIANT of type IDispatch. Thanks for the heads up.
Voulnet
+1  A: 

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.

jdv
Your solution can work as a proof-of-concept that the two end-nodes are correctly exchanging data, but it's not the optimal solution in terms of automation. Thanks a lot!
Voulnet