嵌入 C++库到 .Net 库

发布于 2024-09-12 10:09:18 字数 164 浏览 5 评论 0原文

在我的 .Net 程序集中,我必须使用一些本机 (C++) dll。通常我们需要将C++ dll复制到bin文件夹中并使用PInvoke来调用它。为了节省分发成本,我想将 C++ 直接嵌入到我的 .Net dll 中,这样分发的程序集数量就会更少。

知道如何做到这一点吗?

In my .Net assemblies I would have to make use of some native (C++ ) dlls. Usually we need to copy the C++ dlls into the bin folder and use PInvoke to call it. To save the distribution cost, I want to embed the C++ into my .Net dll direct, so that the number of assemblies distributed would be less.

Any idea how to do this?

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

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

发布评论

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

评论(2

隔岸观火 2024-09-19 10:09:18

您可以将本机 DLL 作为资源嵌入。

然后在运行时,您必须将这些本机 DLL 提取到临时文件夹中;当您的应用程序启动时,您不一定具有对应用程序文件夹的写入权限:想想 Windows Vista 或 Windows 7 和 UAC。因此,您将使用此类代码从特定路径加载它们:

public static class NativeMethods {

  [DllImport("kernel32")]
  private unsafe static extern void* LoadLibrary(string dllname);

  [DllImport("kernel32")]
  private unsafe static extern void FreeLibrary(void* handle);

  private sealed unsafe class LibraryUnloader
  {
    internal LibraryUnloader(void* handle)
    {
      this.handle = handle;
    }

    ~LibraryUnloader()
    {
      if (handle != null)
        FreeLibrary(handle);
    }

    private void* handle;

  } // LibraryUnloader


  private static readonly LibraryUnloader unloader;

  static NativeMethods()
  {
    string path;

    // set the path according to some logic
    path = "somewhere/in/a/temporary/directory/Foo.dll";    

    unsafe
    {
      void* handle = LoadLibrary(path);

      if (handle == null)
        throw new DllNotFoundException("unable to find the native Foo library: " + path);

      unloader = new LibraryUnloader(handle);
    }
  }
}

You would embed your native DLLs as resources.

Then at runtime, you would have to extract those native DLLs into a temporary folder; you don't necessarily have write access to the application folder when your application launches: think windows vista or windows 7 and UAC. As a consequence, you would use this kind of code to load them from a specific path:

public static class NativeMethods {

  [DllImport("kernel32")]
  private unsafe static extern void* LoadLibrary(string dllname);

  [DllImport("kernel32")]
  private unsafe static extern void FreeLibrary(void* handle);

  private sealed unsafe class LibraryUnloader
  {
    internal LibraryUnloader(void* handle)
    {
      this.handle = handle;
    }

    ~LibraryUnloader()
    {
      if (handle != null)
        FreeLibrary(handle);
    }

    private void* handle;

  } // LibraryUnloader


  private static readonly LibraryUnloader unloader;

  static NativeMethods()
  {
    string path;

    // set the path according to some logic
    path = "somewhere/in/a/temporary/directory/Foo.dll";    

    unsafe
    {
      void* handle = LoadLibrary(path);

      if (handle == null)
        throw new DllNotFoundException("unable to find the native Foo library: " + path);

      unloader = new LibraryUnloader(handle);
    }
  }
}
赠我空喜 2024-09-19 10:09:18

您可以将 dll 作为资源嵌入。

在运行时,将它们解压到与 exe 相同的文件夹中,并使用 P/Invoke 调用其中的方法。

You can embed your dlls as resources.

At runtime, extract them to the same folder as your exe and use P/Invoke to call methods inside them.

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