Search code examples
c#visual-studiomsbuildcomtypelib

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?


I developed a C# .EXE that defines a COM UDT (User Defined Type):

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

I need to create a type library from this C# .EXE. I am able to do that using the Visual Studio Command Prompt:

> tlbexp.exe MyCSharpApp.exe

This command produces the MyCSharpApp.tlb file.

I'd like to automate this step using Visual Studio 2019.

So, I entered the following line in the post-build events:

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

Post-build event settings in VS

But, when I build the project from Visual Studio 2019, I get the following error:

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

What am I doing wrong? What am I missing here?

How can I automate the tlbexp invocation in the post-build events?


Solution

  • tlbexp.exe is usually not in the path. What you can do is use this instead (without surrounding " " as you did):

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

    This will execute the equivalent to the "Developer Command Prompt for Visual Studio" shell command which will set all paths appropriately, and then run tlbexp:

    enter image description here