How do I combine an unmanaged dll and a managed as

2019-04-27 01:28发布

问题:

SQLite from PHX Software has combined a managed assembly (System.Data.SQLite) with an unmanaged dll (the SQLite 32- or 64-bit dll) into one file, and managed to link them together.

How do I do this? Do I need to embed the managed assembly into the unmanaged dll, or vice versa?

ie. my questions are:

  1. In which order do I need to do this?
  2. What tools or knowledge do I need in order to do this?
  3. How (if different) do I link to the exported functions from the unmanaged dll in my managed code?

The reason I ask this is that I want to build a managed zLib wrapper. I know there is managed classes in .NET but from experience they're a bit limited (and a bit boneheaded in that they don't do proper buffering), so I'd like to create my own copy, also because I want to learn how to do this.

So does anyone know what I need to do and how?

I've found the following:

  • How to merge several managed and unmanaged DLLs into a single DLL?
  • Sample: Mixing Unmanaged C++, C++/CLI, and C# code

I'm going to try this, but any additional information available would be nice as well.

回答1:

Have you tried running reflector on System.Data.SQLite to see how they do it?

I'd imagine you could:

  • Include the 32 and 64 bit dlls as resources in a managed assembly
  • Extract the correct one depending on bitness to somewhere
  • Call SetDllDirectory() via PInvoke if necessary so that windows can find the extracted dll
  • Instantiate a 32 or 64 bit managed wrapper class which has references to the dll

However because zlib is in C you could also wrap the calls you need in a C++ implemented CLR assembly using the zlib source. But I'm not sure how you would handle bitness in that case.



回答2:

look up mergebin from sqlite it should be what you are looking for.

it can merge both managed and unmanaged assemblies togeteher .

MS also seems to have somthing to say about using it here