Search code examples
javascriptarrayscominterop

How do I access a COM byte array from JavaScript


I have a COM interface which has a method which in VB has a signature like this:

Public Function GetData() As Byte()

In C# it shows as

public byte[] GetData();

In javascript it is a strange opaque object. Indexing it returns undefined.

data[1] == undefined
typeof( data ) == "unknown"
data.Item(1) => error

Solution

  • I could not find any way to use the returned object in javascript directly. But since I am in a COM aware environment anyway, I have some microsoft things lying around in the namespace that I can use, namely, the VBArray type. It allows you to create and use VB style arrays from javascript. And VB arrays can understand the COM byte array. So the solution is this:

    var data = thingy.GetData();
    var jsArray = (new VBArray( data )).toArray();