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

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

问题描述

我有一个Windows C#程序,它使用C ++ dll进行数据i / o。我的目标是将应用程序部署为单个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(thx到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作为现有项目,并将Build Action设置为嵌入式资源。要访问其函数,请像往常一样使用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);

就是这样!一旦创建了具有静态ctor的类的第一个实例,嵌入式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.

享受!

a href =http://weblogs.asp.net/ralfw/archive/2007/02/04/single-assembly-deployment-of-managed-and-unmanaged-code.aspx =nofollow noreferrer> http ://weblogs.asp.net/ralfw/archive/2007/02/04/single-assembly-deployment-of-managed-and-unmanaged-code.aspx

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

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

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