简体   繁体   English

Javascript Com数组组件给我错误的数组值

[英]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. 我尝试为C#方法创建COM组件,然后尝试使用javascript访问此方法。

I have run GACUtil -i and Regasm /Codebase command for create share assembly and also register into Registry successfully. 我已经运行GACUtil -i和Regasm / Codebase命令来创建共享程序集,并且还成功注册到注册表中。 This is my C# Method that return an int[] array for this I create an COM Component for this method. 这是我的C#方法,为此它返回一个int []数组,为此方法我创建了一个COM组件。 nChannelsCount = 15 which is use in for loop nChannelsCount = 15 ,用于for循环

[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. 但是,当我在javascript中访问此方法时,它给了我15个计数,但我想在“快速监视”中显示5500个计数。 I dont know how to do this in javascript to achive this code but still i try this javascript code as below 我不知道如何在javascript中做到这一点,但是我仍然尝试使用以下javascript代码

<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. GetData返回一个数组。 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). JavaScript代码将其调用15次,并且每次将结果压入另一个数组nAllData (JavaScript中的变量名与C#中的变量名无关紧要-它们是不相关的)。 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). 结果,您有一个由15个元素组成的数组,其中每个元素又是一个数组(大概是5500个元素-无论GetData返回什么)。

The loop within GetData implementation is pointless - the function returns on the very first iteration through that loop. GetData实现中的循环是没有意义的-函数在该循环的第一次迭代中返回。 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). 最后,我不相信JavaScript可以直接使用safearrays(这就是我认为GetData的返回值最终由COM interop表示的方式)。 Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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