继Microsoft的Source generation for ComWrappers文章之后,我使用GeneratedComInterfaceAttribute属性在Visual Studio 2022中创建了一个简单的.NET 8 Class Library项目.我的类基于this other Stack Overflow question中描述的类似类,其中使用了.NET 5.
这是默认Class1.cs
的代码:
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
namespace ComHostTest
{
[GeneratedComClass]
[Guid("63C60D62-317F-4BAB-BB20-0AB41F34A345")]
public partial class Class1
{
public string SayHello() => "Hello World from .NET " + RuntimeInformation.FrameworkDescription;
}
}
这是我的项目文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PlatformTarget>x86</PlatformTarget>
<EnableComHosting>true</EnableComHosting>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>AnyCPU;x86</Platforms>
</PropertyGroup>
</Project>
我的构建配置和PlatformTarget设置为x86.该项目正确构建并生成ComHostTest.comhost.dll
:
使用提升的命令提示符,我成功注册了comhost.url:
然后,使用VB 6 IDE的引用窗口或OLViewer,我不会将ComHostTest程序集视为可用的COM类型库:
知道为什么吗?
我读过this very similar question,其中该人也使用.NET 8,但不使用GeneratedComInterfaceProperty.
编辑
我根据西蒙·马希尔的 comments 调整了我的代码:
using System.Runtime.InteropServices;
namespace ComHostTest
{
[ComVisible(true)]
[Guid("72433A8C-941E-4876-8CC2-15F6F4235F32")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IComHostTest
{
public string SayHello();
}
[ComVisible(true)]
[Guid("63C60D62-317F-4BAB-BB20-0AB41F34A345")]
[ClassInterface(ClassInterfaceType.None)]
public class ComHostTest : IComHostTest
{
public string SayHello() => "Hello World from .NET " + RuntimeInformation.FrameworkDescription;
}
}
项目文件没有被触碰,因为它已经包含了<EnableComHosting>true</EnableComHosting>
个.
我从GitHub下载了dscom32版本,然后从Visual Studio中的Developer PowerShell窗口执行了以下命令:
dscom32 tlbexport "D:\.NET Projects\Development\COM host\ComHostTest\bin\x86\Debug\net8.0\ComHostTest.dll"
这生成了一个ComHostTest TSB文件,我将其包含在我的.csproj文件中:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PlatformTarget>x86</PlatformTarget>
<EnableComHosting>true</EnableComHosting>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>AnyCPU;x86</Platforms>
</PropertyGroup>
<ItemGroup>
<ComHostTypeLibrary Include="ComHostTest.tlb" Id="1" />
</ItemGroup>
</Project>
仍然没有运气在VB 6 IDE中看到它,但我一定很接近.