如何将 C++ windows dll 合并到 C# 应用程序 exe 中? [英] How can a C++ windows dll be merged into a C# application exe?

查看:54
本文介绍了如何将 C++ windows dll 合并到 C# 应用程序 exe 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows C# 程序,它使用 C++ dll 进行数据输入/输出.我的目标是将应用程序部署为单个 EXE.

I have a Windows C# program that uses a C++ dll for data i/o. My goal is to deploy the application as a single EXE.

创建这样一个可执行文件的步骤是什么?

What are the steps to create such an executable?

推荐答案

托管和非托管代码的单一程序集部署2007 年 2 月 4 日,星期日

Single Assembly Deployment of Managed and Unmanaged Code Sunday, February 4, 2007

.NET 开发人员喜欢 XCOPY 部署.他们喜欢单个装配组件.至少我总是觉得有点不安,如果我必须使用某个组件并且需要记住一个文件列表,该列表也包含在该组件的主要组件中.因此,当我最近不得不开发一个托管代码组件,并且不得不使用来自 C DLL 的一些非托管代码对其进行扩充时(感谢 Marcus Heege 帮助我解决这个问题!),我想到了如何更轻松地部署这两个 DLL.如果这只是两个程序集,我可以使用 ILmerge 将它们打包到一个文件中.但这不适用于具有托管和非托管 DLL 的混合代码组件.

.NET developers love XCOPY deployment. And they love single assembly components. At least I always feel kinda uneasy, if I have to use some component and need remember a list of files to also include with the main assembly of that component. So when I recently had to develop a managed code component and had to augment it with some unmanaged code from a C DLL (thx to Marcus Heege for helping me with this!), I thought about how to make it easier to deploy the two DLLs. If this were just two assemblies I could have used ILmerge to pack them up in just one file. But this doesn´t work for mixed code components with managed as well as unmanaged DLLs.

所以这是我想出的解决方案:

So here´s what I came up with for a solution:

我将我想与组件的主程序集一起部署的任何 DLL 作为嵌入式资源包括在内.然后我设置了一个类构造函数来提取这些 DLL,如下所示.类 ctor 在每个 AppDomain 中只调用一次,所以我认为这是一个可以忽略不计的开销.

I include whatever DLLs I want to deploy with my component´s main assembly as embedded resources. Then I set up a class constructor to extract those DLLs like below. The class ctor is called just once within each AppDomain so it´s a neglible overhead, I think.

namespace MyLib
{
    public class MyClass
    {
        static MyClass()
        {
            ResourceExtractor.ExtractResourceToFile("MyLib.ManagedService.dll", "managedservice.dll");
            ResourceExtractor.ExtractResourceToFile("MyLib.UnmanagedService.dll", "unmanagedservice.dll");
        }

        ...

在这个例子中,我包含了两个 DLL 作为资源,一个是非托管代码 DLL,一个是托管代码 DLL(仅用于演示目的),以展示这种技术如何适用于这两种代码.

In this example I included two DLLs as resources, one being an unmanaged code DLL, and one being a managed code DLL (just for demonstration purposes), to show, how this technique works for both kinds of code.

将 DLL 提取到它们自己的文件中的代码很简单:

The code to extract the DLLs into files of their own is simple:

public static class ResourceExtractor
{
    public static void ExtractResourceToFile(string resourceName, string filename)
    {
        if (!System.IO.File.Exists(filename))
            using (System.IO.Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
                using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create))
                {
                    byte[] b = new byte[s.Length];
                    s.Read(b, 0, b.Length);
                    fs.Write(b, 0, b.Length);
                }
    }
}

使用像这样的托管代码程序集与往常一样 - 几乎.您在组件的主项目(此处:MyLib)中引用它(此处:ManagedService.dll),但将 Copy Local 属性设置为 false.此外,您将程序集中作为现有项进行链接,并将构建操作设置为嵌入式资源.

Working with a managed code assembly like this is the same as usual - almost. You reference it (here: ManagedService.dll) in your component´s main project (here: MyLib), but set the Copy Local property to false. Additionally you link in the assembly as an Existing Item and set the Build Action to Embedded Resource.

对于非托管代码(此处:UnmanagedService.dll),您只需将 DLL 作为现有项目链接,并将构建操作设置为嵌入式资源.要访问其功能,请像往常一样使用 DllImport 属性,例如

For the unmanaged code (here: UnmanagedService.dll) you just link in the DLL as an Existing Item and set the Build Action to Embedded Resource. To access its functions use the DllImport attribute as usual, e.g.

[DllImport("unmanagedservice.dll")] public extern static int Add(int a, int b);

就是这样!一旦您使用静态构造函数创建类的第一个实例,嵌入的 DLL 就会被提取到它们自己的文件中,并准备好使用,就像您将它们部署为单独的文件一样.只要您对执行目录有写权限,这应该适合您.至少对于原型代码我觉得这种单程序集部署的方式还是挺方便的.

That´s it! As soon as you create the first instance of the class with the static ctor the embedded DLLs get extracted into files of their own and are ready to use as if you deployed them as separate files. As long as you have write permissions for the execution directory this should work fine for you. At least for prototypical code I think this way of single assembly deployment is quite convenient.

享受吧!

http://weblogs.asp.net/ralfw/archive/2007/02/04/single-assembly-deployment-of-managed-and-unmanaged-code.aspx

这篇关于如何将 C++ windows dll 合并到 C# 应用程序 exe 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆