简体   繁体   English

具有VB6 / asp错误的C#COM对象

[英]C# COM objects with VB6/asp error

I'm trying to expose a C# class library via COM so that I can use it in a classic asp web site. 我正在尝试通过COM公开C#类库,以便可以在经典的ASP网站中使用它。

I've used sn - k, regasm and gacutil. 我用过sn-k,regasm和gacutil。 About all I can do now though is echo back strings. 我现在所能做的就是回显字符串。

Methods which take Class variables as input are not working for me. 将Class变量作为输入的方法对我不起作用。 ie my test method EchoPerson(Person p) which returns a string of the first and last name doesn't work. 即我的测试方法EchoPerson(Person p)返回名字和姓氏的字符串不起作用。 I get a runtime error 5 - Invalid procedure call or argument. 我收到运行时错误5-无效的过程调用或参数。

Please let me know what I am missing. 请让我知道我想念的东西。 Also I have no intellisence in VB. 我在VB中也没有智慧。 What do I need to do to get the intellisence working. 我需要做什么才能使智能发挥作用。

Below is my C# test code 以下是我的C#测试代码

namespace MrdcToFastCom
{

    public class Person : MrdcToFastCom.IPerson
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }


    public class ComTester : MrdcToFastCom.IComTester
    {
        public string EchoString(string s)
        {
            return ("Echo: " + s);
        }

        public string Hello()
        {
            return "Hello";
        }


        public string EchoPerson(ref Person p)
        {
            return string.Format("{0} {1}", p.FirstName, p.LastName);
        }

    }

}

and VB6 call 和VB6通话

Private Sub btnClickMe_Click() 

    Dim ct
    Set ct = New MrdcToFastCom.ComTester

    Dim p
    Set p = New MrdcToFastCom.Person
    p.FirstName = "Joe"
    p.LastName = "Test"

    Dim s
    s = ct.EchoPerson(p) ''#Error on this line
    tbx1.Text = s


End Sub

Here is the pattern you should consider using:- 这是您应该考虑使用的模式:-

[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("B4CAC74B-ADE0-4ac7-AD0E-26E6439F9CF7")]
public interface _IPerson
{
    string FirstName { get; set; }     
    string LastName { get; set; }     
}

[ClassInterface(ClassInterfaceType.None)]
[Guid("A3C553DC-A348-43e4-957A-F94D23E3300E")]
public class Person :  _IPerson      
{      
    public string FirstName { get; set; }      
    public string LastName { get; set; }      
}

[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("4B527235-6738-4853-BEA0-FB3087C89291")]
public interface _ComTester
{
     string EchoPerson(Person person);
}

[ClassInterface(ClassInterfaceType.None)]
[Guid("C753D72B-C802-44ae-946A-E3F6D7C5D14B")]
public class ComTester : _ComTester
{
    public string EchoPerson(Person person)
    {
        return person.FirstName + " " + person.LastName;
    }
}

This approach gives you much more control over the COM Interface exposed by the .NET components. 这种方法使您可以更好地控制.NET组件公开的COM接口。 Only the members defined by interface are exposed for consumption by COM clients. 仅由接口定义的成员公开以供COM客户端使用。

When you are using complex types in COM interfaces, you need to use structs that are attributed with [StructLayout(LayoutKind.Sequential)] . 在COM接口中使用复杂类型时,需要使用[StructLayout(LayoutKind.Sequential)]结构。 You can find more information here on MSDN: Exported Type Conversion . 您可以在MSDN上找到更多信息: 导出类型转换 Since COM has to marshal the types across the boundary, you have to make sure all your types can be copied across into unmanaged land successfully. 由于COM必须封送跨边界的类型,因此您必须确保可以将所有类型成功复制到非托管区域。 Reference types won't make it. 引用类型无法实现。

 public string EchoPerson(ref Person p)

You are getting an error because you declared the argument with the ref keyword. 由于使用ref关键字声明了参数,因此出现错误。 That's not correct, Person is already a reference type and the object that VB6 is using is a variant, not a Person. 这是不正确的,Person已经是引用类型,VB6使用的对象是变量,而不是Person。 Just omit "ref". 只是省略“ ref”。 Using Option Explicit On is a good practice in VB6 btw. 在VB6 btw中,使用Option Explicit On是一个好习惯。

You are not getting IntelliSense because you probably didn't declare the interfaces with the [InterfaceType(ComInterfaceType.InerfaceIsDual)]. 您没有获得IntelliSense,因为您可能没有使用[InterfaceType(ComInterfaceType.InerfaceIsDual)]声明接口。 Microsoft recommends against this because of the DLL Hell problems with dual interfaces. Microsoft建议不要这样做,因为双接口存在DLL Hell问题。 Required though to get a type library to help VB6 display the IS you want. 尽管获得类型库来帮助VB6显示所需的IS是必需的。

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

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