如何能在C ++ Windows动态链接库合并成一个C#应用程序EXE? [英] How can a C++ windows dll be merged into a C# application exe?

查看:618
本文介绍了如何能在C ++ Windows动态链接库合并成一个C#应用程序EXE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用的数据I / O一个C ++ DLL一个Windows C#程序。我的目标是将应用程序部署作为一个单一的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?

推荐答案

托管和非托管code单大会部署
周日,2007年2月4日

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

.NET开发人员喜欢XCOPY部署。他们喜欢单一的组装部件。至少我总觉得有点不安,如果我要使用一些组件,需要记住的文件列表还包括与该组件的主要部件。所以,当我最近不得不制定一个管理code成分,并与从C DLL一些非托管code(感谢马库斯Heege帮助我这个!),以加强它,我想过如何使它更容易部署的两个DLL。如果这只是两个组件我可以使用ILmerge收拾起来,在短短一个文件。但是,随着管理的混合code成分本doesn't工作,以及托管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.

所以here's什么,我想出了一个解决方案:

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

我包括我想用我的component's部署主要部件嵌入任何资源的DLL。
然后,我建立了一个类的构造函数来提取这些DLL像下面。类构造函数被调用仅一次中的每个应用程序域这样一个汇入作业开销可以忽略不计,我想。

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作为资源,其中之一是一个非托管code DLL,而且一个是管理code DLL(只用于演示目的),以示,这种技术是如何为这两种的code。

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.

在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);
                }
    }
}

通过这样的管理code大会工作是和往常一样 - 差不多。在component's:(ManagedService.dll这里)主体工程就引用它(此处MyLib中),但本地地产设置复制到假的。此外,您在装配链接作为现有项目,并设置生成操作嵌入的资源。

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.

有关的非托管code(此处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);

That's吧!只要你创建静态构造函数嵌入的DLL得到提取到自己的文件类的第一个实例,并准备,如果你部署它们作为单独的文件来使用。只要您有执行目录的写权限这应该很好地工作适合你。至少对于原型code,我认为单一组装这种部署方式是相当方便的。

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.

享受!

<一个href=\"http://weblogs.asp.net/ralfw/archive/2007/02/04/single-assembly-deployment-of-managed-and-unmanaged-$c$c.aspx\" rel=\"nofollow\">http://weblogs.asp.net/ralfw/archive/2007/02/04/single-assembly-deployment-of-managed-and-unmanaged-$c$c.aspx

这篇关于如何能在C ++ Windows动态链接库合并成一个C#应用程序EXE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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