简体   繁体   中英

Javascript Com array Component giving me wrong array values

I try to create COM Component for C# method and then try to access this method using javascript.

I have run GACUtil -i and Regasm /Codebase command for create share assembly and also register into Registry successfully. This is my C# Method that return an int[] array for this I create an COM Component for this method. nChannelsCount = 15 which is use in for loop

[Guid("4794D615-BE51-4a1e-B1BA-453F6E9337C4")]  
  [ComVisible(true)]   
 [ClassInterface(ClassInterfaceType.None)]  
  [ComSourceInterfaces(typeof(IComOjbect))] 
   public class MyComObject : IComOjbect 
   {

在此处输入图片说明
}

   [Guid("4B3AE7D8-FB6A-4558-8A96-BF82B54F329C")] 
   [ComVisible(true)]   
 public interface IComOjbect    
    {       
    [DispId(0x10000009)]    
        int[] GetData(int index);   
     }

But when I access this method in javascript it gives me and just count of 15 but I want 5500 count that show in Quick Watch. I dont know how to do this in javascript to achive this code but still i try this javascript code as below

<html>   
<head>   
<title>My Com Component</title>  
<object id="myComComponent" name="myComComponent" classid="clsid:4794D615-BE51-4A1E-B1BA-453F6E9337C4">
</object> 
<script language="javascript" type="text/javascript">        
function MyComComponent_onload() 
    {              
      try {  
           var nAllData = [];   
           for (var index = 0; index < 15; index++) 
             {

               nAllData.push(myComComponent.GetData(index));  
              } 
            alert(nAllData.length);  
          }             
      catch (err) {             
        alert(err.message);    
       }           
  }   
</script> 
</head>  
<body onload="MyComComponent_onload();" onunload="MyComComponent_onunload();"> 
</body>   
</html>

GetData returns an array. The JavaScript code calls it 15 times, and every time pushes the result onto yet another array, called nAllData (the fact that the variable name in JavaScript is the same as in C# is irrelevant - they are unrelated). As a result, you have an array of 15 elements, where each of those elements is in turn an array (presumably of 5500 elements - whatever GetData returns).

The loop within GetData implementation is pointless - the function returns on the very first iteration through that loop. It's not clear what you were trying to achieve there.

Finally, I don't believe JavaScript can directly consume safearrays (which is how I believe the return value of GetData ends up being represented by COM interop). Try this:

var data = new VBArray(myComComponent.GetData(index)).toArray();
alert(data.length);
nAllData.push(data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM