使用Native Dll将C#解决方案部署到单个EXE中

时间:2013-08-08 11:27:27

标签: c#

我有一个C#Visual Studio 2012解决方案,它依赖于我使用PInvoke访问的本机dll。部署应用程序时,我必须确保此Dll位于应用程序文件夹中。

无论如何我可以将这个Dll合并到可执行文件中吗?

也许是一种资源?

我听说过ILMerge,但我被告知它无法应对原生代码。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:6)

您可以使用Visual Studio创建一个Setup package项目,将所有文件部署到正确的位置或使用其他第三方打包软件(如完整的InstallShield或alternatives

但是,您的问题提醒我Open Hardware Monitor项目,其中包含驱动程序作为嵌入资源,并在用户启动应用程序时提取它们。它的工作方式如下:他们已将WinRing0.sysWinRing0x64.sys添加到项目中,并将构建操作设置为嵌入式资源,然后将它们设置为有一个从资源中提取驱动程序的方法:

private static bool ExtractDriver(string fileName) {
  string resourceName = "OpenHardwareMonitor.Hardware." +
    (OperatingSystem.Is64BitOperatingSystem() ? "WinRing0x64.sys" : 
    "WinRing0.sys");

  string[] names =
    Assembly.GetExecutingAssembly().GetManifestResourceNames();
  byte[] buffer = null;
  for (int i = 0; i < names.Length; i++) {
    if (names[i].Replace('\\', '.') == resourceName) {
      using (Stream stream = Assembly.GetExecutingAssembly().
        GetManifestResourceStream(names[i])) 
      {
          buffer = new byte[stream.Length];
          stream.Read(buffer, 0, buffer.Length);
      }
    }
  }

  if (buffer == null)
    return false;

  try {
    using (FileStream target = new FileStream(fileName, FileMode.Create)) {
      target.Write(buffer, 0, buffer.Length);
      target.Flush();
    }
  } catch (IOException) { 
    // for example there is not enough space on the disk
    return false; 
  }

  // make sure the file is actually writen to the file system
  for (int i = 0; i < 20; i++) {
    try {
      if (File.Exists(fileName) &&
        new FileInfo(fileName).Length == buffer.Length) 
      {
        return true;
      }
      Thread.Sleep(100);
    } catch (IOException) {
      Thread.Sleep(10);
    }
  }

  // file still has not the right size, something is wrong
  return false;
}

他们将资源读入缓冲区,将该缓冲区写入磁盘并等待文件刷新到磁盘。

答案 1 :(得分:1)

我的解决方案在概念上类似于Wouter提供的解决方案。

这是我们在自己的应用程序中使用的,我们可以使用嵌入在同一.exe中的本机/混合模式和c#dll。

每次运行应用程序时,它都会将dll提取到临时目录中。显然你可能不想在生产版本中这样做,dll将是稳定的;你可以在那里选择一个不同的目录(可能在%AppData%中的某个地方)。它将使用具有相同版本号的现有dll(例如,它仅在启动计算机之间多次打开应用程序时才会完成。)

因为我们正在做

AppDomain.CurrentDomain.AssemblyResolve += (sender, args)

在系统尝试解析dll的任何地方都会调用此函数。并且由于它在静态程序类中被初始化,所以它都是自动运行的。

的Program.cs:

namespace MyApp
{
    internal class Program
    {
        static Program()
        {
            LoadAssemblyResource.Initialize("MyApp");
        }

        //....
    }
}  

LoadAssemblyResource.cs

namespace MyAppStartup
{ 
    public static class LoadAssemblyResource
    {
        private readonly static String _version_string =
            Assembly.GetExecutingAssembly().GetName().Version.ToString();

        private readonly static String _dll_path = Path.GetTempPath()
            + "\\MyApp\\" + _version_string;

        static public String last_error_msg = null;

        public static bool WriteBytesToFile(string filename, byte[] bytes)
        {
            try
            {
                var fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
                fs.Write(bytes, 0, bytes.Length);
                fs.Close();
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine("Writing file failed. Exception: {0}", e.ToString());
            }
            return false;
        }

        public static Assembly LoadUnsafe(String assembly_name, Byte[] assembly)
        {
            if (!Directory.Exists(_dll_path))
            {
                Directory.CreateDirectory(_dll_path);
                Console.WriteLine("Created tmp path '" + _dll_path + "'.");
            }

            String fullpath = _dll_path + "\\" + assembly_name;
            if (!File.Exists(fullpath))
            {
                Console.WriteLine("Assembly location: " + fullpath + ".");
                if (!WriteBytesToFile(fullpath, assembly))
                     return null;
            }

            return Assembly.UnsafeLoadFrom(fullpath);
        }

        public static void Initialize(String exe_name)
        {
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                String assembly_name = new AssemblyName(args.Name).Name + ".dll";
                String resource_name = exe_name + "." + assembly_name;

                using (var stream = 
                    Assembly.GetExecutingAssembly().GetManifestResourceStream(resource_name))
                {
                    if (stream == null)
                        return null;

                    Byte[] assembly_data = new Byte[stream.Length];
                    stream.Read(assembly_data, 0, assembly_data.Length);
                    try
                    {
                        Assembly il_assembly = Assembly.Load(assembly_data);
                        return il_assembly;
                    }
                    catch (System.IO.FileLoadException ex)
                    {
                        // might have failed because it's an mixed-mode dll.
                        last_error_msg = ex.Message;
                    }

                    Assembly mixed_mode_assembly = LoadUnsafe(assembly_name, assembly_data);
                    return mixed_mode_assembly;
                }
            };
        }
    }

}