如何调用 tlbexp 命令作为 post-build 事件以从 Visual Studio 2019 IDE 的 C# EXE 中导出类型库?

How to invoke the tlbexp command as post-build event to export a type library from a C# EXE from the Visual Studio 2019 IDE?

我开发了一个定义 COM UDT(用户定义类型)的 C# .EXE:

[ComVisible(true)]
[StructLayout(LayoutKind.Sequential)]
[Guid(" ... some GUID ... ")]
public struct MyStructure
{
    ... various fields ...
}

我需要从这个 C# .EXE 创建一个类型库。 我可以使用 Visual Studio 命令提示符来做到这一点:

> tlbexp.exe MyCSharpApp.exe

此命令生成 MyCSharpApp.tlb 文件。

我想使用Visual Studio 2019 自动执行这一步。

因此,我在 post-build 事件中输入了以下行:

"tlbexp.exe $(TargetDir)$(TargetFileName)"

但是,当我从 Visual Studio 2019 构建项目时,出现以下错误:

error MSB3073: The command ""tlbexp.exe C:\Path\To\MyCSharpApp.exe"" exited with code 123.

我做错了什么?我在这里错过了什么?

如何在 post-build 事件中自动执行 tlbexp 调用?

tlbexp.exe 通常不在路径中。你可以做的是改用它(不像你那样包围“”):

call "$(DevEnvDir)..\Tools\VsDevCmd.bat"
tlbexp.exe $(TargetDir)$(TargetFileName)

这将执行与 "Developer Command Prompt for Visual Studio" shell 命令等效的命令,该命令将适当地设置所有路径,然后 运行 tlbexp: