创建一个 C# 类库(在本例中称为 DemoComInterface)并确保未选中“使程序集 COM 可见”。 (提醒一下,以下代码 sn-ps 中的 GUID 应替换为您自己的唯一 GUID。)
向类库添加接口,如下所示:
using System.Runtime.InteropServices;
namespace DemoComInterface
{
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComVisible(true)]
[Guid("01B0A84D-CACE-4EF1-9C4B-6995A71F9AB8")]
public interface ISharedInterface
{
[DispId(0x60030040)]
void Question();
}
}
要演示使用共享接口的 C# 类,请更新 Class1 以实现共享接口,并使用以下属性对其进行装饰:
using System.Runtime.InteropServices;
namespace DemoComInterface
{
[Guid("CC9A9CBC-054A-4C9C-B559-CE39A5EA2742")]
[ProgId("DemoComInterface.Class1")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class Class1 : ISharedInterface
{
public void Question()
{
throw new NotImplementedException();
}
}
}
现在,将您的 AssemblyInfo.cs 文件的 AssemblyDescription 属性修改为有意义的内容,以便可以在 VB6 'References' 浏览器中找到类型库。这可以通过直接编辑文件或填充组装信息对话框的“描述”字段来完成。
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("DemoComInterface")]
// This is what will be seen in VB6's 'References' browser.**
[assembly: AssemblyDescription("Demo C# exported interfaces")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DemoComInterface")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("4294f846-dd61-418d-95cc-63400734c876")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
通过检查项目的“Register for COM interop”构建属性或使用 REGASM 在命令提示符中手动注册来注册此类库。
查看生成的类型库(在项目的 bin 输出文件夹中),您应该会看到如下内容:
// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: DemoComInterface.tlb
[
uuid(4294F846-DD61-418D-95CC-63400734C876),
version(1.0),
helpstring("Demo C# exported interfaces"),
custom(90883F05-3D28-11D2-8F17-00A0C9A6186D, "DemoComInterface, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
]
library DemoComInterface
{
// TLib : // TLib : mscorlib.dll : {BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}
importlib("mscorlib.tlb");
// TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
importlib("stdole2.tlb");
// Forward declare all types defined in this typelib
interface ISharedInterface;
[
uuid(CC9A9CBC-054A-4C9C-B559-CE39A5EA2742),
version(1.0),
custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "DemoComInterface.Class1")
]
coclass Class1 {
interface _Object;
[default] interface ISharedInterface;
};
[
odl,
uuid(01B0A84D-CACE-4EF1-9C4B-6995A71F9AB8),
version(1.0),
dual,
oleautomation,
custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "DemoComInterface.ISharedInterface")
]
interface ISharedInterface : IDispatch {
[id(0x60030040)]
HRESULT Question();
};
};
共享接口现在在 COM 可见的 C# 类中实现。
要在 VB6 项目中实现共享接口,请添加对“演示 C# 导出接口”的引用,并按如下方式实现:
Option Explicit
Implements ISharedInterface
' Implementation of Question.
Public Sub ISharedInterface_Question()
MsgBox ("Who is number one?")
End Sub