如何将 .tlb 作为资源文件嵌入到 .NET 程序集 DLL 中?

发布于 2024-07-10 21:14:18 字数 155 浏览 5 评论 0原文

我们通过 COM (CCW) 在本机 C++ 中使用 .NET Assembly DLL。 每当我制作新版本的 DLL 时,我都必须向在代码中使用它的人员发送两个文件(.dll 和相应的 .tlb)。

是否可以将 .tlb 文件作为资源嵌入到 .NET DLL 文件中?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2024-07-17 21:14:18

使用 Visual Studio .NET 执行此操作并不十分简单,但可以完成。 在基本层面上,您需要做的是:

  1. 生成您的 TLB 文件,例如“YourLibrary.tlb”。

  2. 使用文本编辑器(例如记事本或 Visual Studio 中的 File/New/File.../Text File)创建一个名为“YourLibrary.rc”的 Win32 资源脚本文件。

  3. 在脚本文件中,逐字键入以下文本(当然请替换您的实际 TLB 文件名):

    1 typelib "YourLibrary.tlb"

  4. 保存脚本文件将脚本文件保存到与 TLB 文件相同的文件夹中。

  5. 从 Visual Studio 命令提示符中,更改到包含脚本文件的文件夹并使用以下命令编译它:

    rc YourLibrary.rc

    这将在名为“YourLibrary.res”的同一文件夹中生成一个 Win32 资源文件。

  6. 在 Visual Studio 中,右键单击解决方案资源管理器中的项目节点(例如“YourLibrary”),然后选择“属性”。

    在 Visual Studio 中,右键单击

  7. 在“应用程序”选项卡的“资源”下,选择“资源文件”选项,然后浏览到步骤 5 中的“YourLibrary.res”文件。

  8. 保存并重建项目。

TLB 现在将作为资源嵌入到 DLL 中,以便其他 COM 应用程序可以读取它。

如果稍后重新生成 TLB 文件,则需要重复步骤 5 重新编译资源文件,并重复步骤 8 将新版本嵌入 DLL 中。

话虽如此,您也许可以通过构建事件或将自定义 MSBuild 目标放入项目文件中来自动执行其中的一些操作,但这是一个完全不同的讨论。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文