COM互操作(如何通过经典ASP将数组传递给COM)

6

我需要为我的经典asp创建一个com对象。由于我可以创建一个.net程序集并使其与com“Interop”,因此我按照以下方式创建了一个.net程序集:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web;


namespace LMS
{

[ComVisible(true)]
public class Calc
{

    public int Add(int val1, int val2, out string[] outputz)
    {
        int total = val1 + val2;
        outputz = new string[5];
        outputz[1] = "test2";
        outputz[2] = "test3";
        outputz[3] = "test4";
        outputz[4] = "test5";
        return total;
    }


}
}

接下来我做了通常的操作,构建并运行:gacutil & RegAsm

在我的经典asp页面中,我有以下内容:

Dim params  
dim objPassport3
set objPassport3 = Server.CreateObject("LMS.Calc")
comTest2 = objPassport3.Add(1,1,params)

我遇到了错误:

错误类型: Microsoft VBScript runtime (0x800A0005) 无效的过程调用或参数:'Add' /eduservice/test.asp,第25行

但是如果我修改程序集不使用数组,它就可以正常工作,我甚至可以在程序集和经典asp之间发送普通字符串或整数。 我阅读了很多东西,但是仍然出现同样的错误。

如果有人尝试过并且成功,请分享您的解决方案。

谢谢

1个回答

10

ASP只能处理变体数组,而不能处理字符串或整数数组。因此建议使用对象代替,例如:

public int Add(int val1, int val2, out object outputz)
{
    int total = val1 + val2;
    outputz = new object[5]
      {
          "test1",
          "test2",
          "test3",
          "test4",
          "test5"
      };

    return total;
}

非常感谢您的回复,我迫不及待地想尝试一下。我会及时向您更新进展情况。 - visual
OrbMan,谢谢,你解决了我的问题,希望我能请你吃午餐。 - visual
1
这对于VBScript也是一样的。即使您可以从VBScript创建公开通过COM的复杂类型,但除非该方法的复杂参数为对象类型,否则无法将它们传回.NET程序集中的方法。原语(例如字符串、整数等)是可以的。 - obsoleter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接