7
votes

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:

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

2
Think twice before doing this, especially with your "core" assemblies. If this is not the essential feature of you code, it is better to make such assemblies dynamically loaded. In other case anyone who uses such assembly will have to do some additional steps to make sure that it uses proper version (x86 vs x64), even if he didn't use any of your unmanaged code. It is reasonable for you example of System.Data.SQLite (essential feature) but might be not reasonable in your case.SergGr

2 Answers

0
votes

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.

0
votes

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