试图建立在C#中的COM组件,可以被引用,并从VB5 / 6中使用 [英] Trying to build a COM Component in C# that can be referenced and used from VB5/6

查看:151
本文介绍了试图建立在C#中的COM组件,可以被引用,并从VB5 / 6中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看我是否能够建立在C#(.NET 4),我可以使用从VB5程序(线索嘲笑的话)来访问Web服务的COM组件。随着所有的指示,我已经能够找到MSDN和$ C $的CProject如下:

I am trying to see if I can build a COM component in C# (.NET 4) which I can use from a VB5 program (cue derisive remarks) to access a web service. Following all the instructions I have been able to find on MSDN and CodeProject as follows:

  • Building COM Objects in C#
  • Example COM Class (C# Programming Guide)

我写了下面的:

[Guid("7A715F02-D349-45DC-B0AE-9925FD3B943C")]
public interface ARCOM_Interface
{
    [DispId(1)]
    string GetServiceResponse();
}

[Guid("5130F041-619E-41F9-84B6-8332642228F6")
    , InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ARCOM_Events
{
}

[Guid("0A77754F-34CF-4E0E-AAC2-85FD686758E0")
    , ClassInterface(ClassInterfaceType.None)
    , ComSourceInterfaces(typeof(ARCOM_Events))]
[ComVisible(true)]
public class ARCOM_Class : ARCOM_Interface
{
    public string GetServiceResponse()
    {
        string response = string.Empty;

        ARWebService.ARWebService svc = new ARWebService.ARWebService();

        response = svc.PingMeBack();

        return response;
    }
}

大会的问题是使用强名称签署,并输出注册为COM互操作。建设后,我已经申请RegAsm它并产生与tlbexp.exe类型库。

The Assembly in question is signed with a strong name and the output is registered for COM Interop. After building, I have applied RegAsm to it and generated a type library with tlbexp.exe.

在VB6,当我打开从项目属性引用的列表,我可以在列表中的程序集,我可以检查它。我甚至可以做的VB6 code以下内容:

In VB6, when I open the list of references from the Project properties, I can find the assembly in the list, and I can check it. I can even do the following in the VB6 code:

Private Sub HitWebService()

    Dim arcom As ARCOMObject.ARCOM_Class

    arcom. <== Intellisense doesn't sense anything!

End Sub

的智能感知看到ARCOMObject和类,但没有什么是ARCOM_Class本身内(除了通常的GetType,等于等通用Object方法/属性)。最具体,它似乎并没有看到GetServiceResponse()方法,所以我不能调用它。

The Intellisense sees the ARCOMObject and the class, but nothing that is within the ARCOM_Class itself (except for the usual "GetType", "Equals" and other generic Object methods/properties). Most specifically, it does not seem to see the GetServiceResponse() method, so I can't call it.

我是谁留下的?

推荐答案

我看你已经制定了一个办法让你想要的东西,而我打字这个答复,但我会继续吗,因为它可能会告诉你有些东西你不知道......

I see you've worked out a way to get what you want while I was typing up this reply, but I'll continue anyway because it might tell you some stuff you don't know...

您已经应用了ClassInterface(ClassInterfaceType.None)属性的类。这告诉COM互操作不能定义一个明确的接口类,所以你的客户端必须使用的IDispatch(后期绑定)。这意味着你的客户端必须有接口的类实现的先验知识。也就是说,你(程序员)知道什么方法都可以,但像智能感知工具没有办法发现的信息。

You've applied the ClassInterface(ClassInterfaceType.None) attribute to the class. This tells COM interop not to define an explicit interface for the class, and so your client must use IDispatch (late binding). This means that your client must have a-priori knowledge of the interfaces that the class implements. That is, you (the programmer) know what methods are available but tools like IntelliSense have no way of finding that information.

只是继续前进,调用方法:

Just go ahead and call the method:

Dim response As String
response = arcom.GetServiceResponse

这篇关于试图建立在C#中的COM组件,可以被引用,并从VB5 / 6中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆