Pass Array From C # COM Object to JavaScript?

Like this How to return an array of strings from an ActiveX object in JScript , but in C #.

I have a COM control that passes an array of strings in javascript. It seems that javascript cannot understand that it is me, and the array in javascript is always undefined.

JavaScript:

try
 {
  keystore.openKeyStore("MY", true, false);
  var fNames = new Array();
  fNames = keystore.getAllFriendlyNames();
  document.getElementById('par').innerHTML = fNames[0];
 }
 catch(err)
 {
  document.getElementById('err').innerHTML = err.description;
 }

This outputs 'undefined' for fNames[0];

WITH#:

    public object[] getAllFriendlyNames()
    {
        if (!keystoreInitialized)
            throw new Exception("Key store has not been initialized");

        X509Certificate2Collection allCerts = certificateStore.Certificates;

        int storeSize = allCerts.Count;

        if (storeSize == 0)
            throw new Exception("Empty Key Store, could have opened using the wrong keystore name.");

        object[] friendlyNames = new object[storeSize];

        for (int i = 0; i < storeSize; i++)
        {
            string friendlyName = allCerts[i].FriendlyName;

            if (friendlyName == "")
                friendlyName = allCerts[i].Subject;

            friendlyNames[i] = (object) friendlyName;
        }

        return friendlyNames;
  }

I tried to return both arrays of objects and arrays of strings to no avail.

+3
source share
2 answers

json . jQuery json. , , , .

+2

JavaScript activeX, :

public ArrayObject getAllFriendlyNames()
{
    //.... the same ...... 
    return Microsoft.JScript.GlobalObject.Array.ConstructArray(friendlyNames);
}

Microsoft.JScript .


MSDN: ArrayConstructor.ConstructArray Method

+3

Source: https://habr.com/ru/post/1756974/


All Articles