views:

690

answers:

3

Hi all,

I need to pass data (byte array, i.e char*) from ActiveX object (using Visual C++ with ATL) to my javascript code (and vice versa). I've digged the Web for such problem and tried lots of solutions but have not succeeded. I've tried the followings:

  • Converting char* to BSTR and pass it to javascript (JS), but my result in JS is "", due to the nature of my data is not string.
//in C++:
STDMETHODIMP CActiveXObj::f(BSTR* msg) // msg is the return value in ATL automation function
{
    char *buffer; // byte data is stored in buffer
    *msg = SysAllocStringByteLen((LPCSTR)buffer, bufferLen+1);
}
//////////////////////////////////////////////////////////////////////////
//in JavaScript:
var myobj= new ActiveXObject("IGCE.ActiveXObj");
var result = myobj.f(); // result = ""
  • Pass safe array of byte data from C++

Could anyone please give me the working code in its simplest form?

Thank you very much!

Kristin

A: 

are you defining a function that takes in parameter but not supplying them during runtime?

YeenFei
A: 

You probably need to use a SAFEARRAY to pass the data. There is an ATL wrapper for this called CComSafeArray. Hopefully that will be enough for you to get started, if not then I'll dig out some code.

Rob
A: 

To the best of my knowledge (and in my experience), you can only use the following basic datatypes when talking to javascript:

  • String
  • Int
  • Double
  • Bool
  • IDispatch*

Anything else doesn't seem to work. I've never tried using a SAFEARRAY, but I can suggest a possible alternative.

If you get a reference to the DOM window (I won't cover that here; if you don't know how, search and/or submit a new question and I can answer it there), you can use IDispatch to Invoke the method "Array" on the window, and it will return an IDispatch* for an empty javascript array. You can then Invoke "push" on the Array IDispatch* for each byte that you want to send to javascript as an int, and then return the IDispatch* of the Array as a return value from the method or property in question. You'll get the data in javascript as an array of integers, but each element is a byte and you can use it that way.

If you can suggest another way to use binary data in javascript (forget the activex control for a moment), I might be able to tell you how to return the data from the control that way.

This is essentially the method that FireBreath (open source plugin framework for IE and Firefox; http://firebreath.googlecode.com) uses when you return a vector from a JSAPI (javascript scripted object) method to return the data to javascript as an array. You can use a similar (almost identical) method on NPAPI compatible browsers.

Taxilian