简体   繁体   English

用于 vb6 的 C# 和 com

[英]C# and com for vb6

I have an issue with C# and COM.我有 C# 和 COM 的问题。

[Guid("f7d936ba-d816-48d2-9bfc-c18be6873b4d")]
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class Process : IProcess
{

    public Process()
    {
    }

    public int UpdateBalance(string accountNumber, string adminEventDescription, decimal curAmount)
    {
        return 10;
    }
}

[ComVisible(true)]
[Guid("5c640a0f-0dce-47d4-87df-07cee3b9a1f9")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IProcess
{
    int UpdateBalance(string accountNumber, string adminEventDescription, decimal curAmount);
}

And the VB code和VB代码

Private Sub Command1_Click()
    Dim test As Object
    Set test = New Forwardslash_PlayerTrackingSystem_Api.Process
End Sub

I get the following,我得到以下信息,

ActiveX component can't create object? ActiveX 组件无法创建对象?

Any ideas on how to fix the issue?关于如何解决问题的任何想法?

您是否勾选了项目属性中的“注册 COM 互操作”框?

Do you have the ProgID Forwardslash_PlayerTrackingSystem_Api.Process defined in the C# source as well?您是否也在 C# 源代码中定义了ProgID Forwardslash_PlayerTrackingSystem_Api.Process Your example code does not seem to include it.您的示例代码似乎不包含它。 (Or are you working with an existing type library and creating the object in VB by GUID somehow?) (或者您是否正在使用现有的类型库并通过 GUID 在 VB 中以某种方式创建对象?)

And is the C# component registered correctly in the registry on the machine where the VB code runs?并且在运行 VB 代码的机器上的注册表中是否正确注册了 C# 组件? See the answer by Paolo for a way to have VisualStudio do this for you when you build and/or register it yourself using the regasm.exe tool.当您使用regasm.exe工具自行构建和/或注册 VisualStudio 时,请参阅Paolo的答案,了解如何让 VisualStudio 为您执行此操作。 This tool is equivalent to regsrv32.exe for "real" COM objects, but then registers an appropriately built .NET assembly in the registry for use from COM.此工具等效于用于“真实”COM 对象的regsrv32.exe ,但随后会在注册表中注册适当构建的 .NET 程序集以供 COM 使用。

Your [InterfaceType] attribute is wrong.您的 [InterfaceType] 属性错误。 VB6 requires an IDispatch interface, it cannot handle an IUnknown interface. VB6 需要 IDispatch 接口,它不能处理 IUnknown 接口。 It likes ComInterfaceType.InterfaceIsDual best, that produces a full type library, enables IntelliSense in the VB6 editor and is roughly a 1000 times faster than the late-bound IDispatch.它最喜欢 ComInterfaceType.InterfaceIsDual,它生成完整的类型库,在 VB6 编辑器中启用 IntelliSense,并且比后期绑定的 IDispatch 快大约 1000 倍。

如果程序集未在 GAC 中注册,则必须使用regasm/codebase开关。

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

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