How to embed .tlb as a resource file into .NET Ass

2020-05-20 09:35发布

问题:

We're using our .NET Assembly DLL within native C++ through COM (CCW). Whenever I make new version of my DLL, I have to send two files (.dll and corresponding .tlb) to crew that's using it in their code.

Is it possible to embed .tlb file as a resource in .NET DLL file?

回答1:

It is not exactly straightforward to do this with Visual Studio .NET, but it can be done. At a basic level, what you have to do is this:

  1. Generate your TLB file, e.g., "YourLibrary.tlb".

  2. Create a Win32 resource script file called, for example, "YourLibrary.rc" using a text editor (such as Notepad, or File/New/File.../Text File in Visual Studio).

  3. In the script file, type the following text verbatim (but substitute your actual TLB file name of course):

    1 typelib "YourLibrary.tlb"

  4. Save the script file to the same folder as the TLB file.

  5. From a Visual Studio Command Prompt, change to the folder with the script file and compile it using the following command:

    rc YourLibrary.rc

    This will generate a Win32 resource file in the same folder called "YourLibrary.res".

  6. In Visual Studio, right click the project node (e.g., "YourLibrary") in the Solution Explorer and select Properties.

  7. On the Application tab, under "Resources", select the "Resource File" option and browse to the "YourLibrary.res" file from step 5.

  8. Save and rebuild the project.

The TLB will now be embedded as a resource in the DLL such that other COM applications can read it.

If you regenerate the TLB file later you will need to repeat step 5 to recompile the resource file, and step 8 to embed the new version in the DLL.

All that said, you may be able to automate some of this with Build Events or by putting custom MSBuild targets into your project file, but that is a whole other discussion.



标签: c# .net com dll ccw