COM Interop (how to pass an array to the com) via

2019-06-20 22:13发布

问题:

I need to create a com object for my classic asp, since i can create a .net Assembly and have it 'Interop' with com, so i proceeded to create a .net Assembly like this:-

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;
    }


}
}

Next i did the usual, build, ran: gacutil & RegAsm

and in my classic asp page i had this:-

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

and i get error:

Error Type: Microsoft VBScript runtime (0x800A0005) Invalid procedure call or argument: 'Add' /eduservice/test.asp, line 25

But if i modify the assembly not to use an array, it all just work, i can even send normal string or int to and from the assembly to classic asp. i read so many things but i get the same error,

anyone tried this before and was successful, please do share your solution

thanks

回答1:

ASP can only handle arrays that are variant, rather than arrays of strings or ints. So try using an object instead, e.g.,

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;
}