Yavor Ivanov

Yavor Ivanov

Creating SQLite database from WiX MSI installer

MSI installers are usually used to install applications on desktop machines, but sometimes you need more than just the application files. Sometimes you need to create or update a SQLite database as a part of the installation process and then you encounter the famous missing SQLte.Interop.dll exception.

Why does it happen

SQLite support on .NET comes in the form of a couple of managed assemblies and 2 unmanaged dlls. In a regular application these unmanaged Interop files are copied to the output folder and picked up from there, no drama at all. But in a WiX custom action the resulting assembly is embedded inside of the msi file (or whatever you are building) and when the custom action is executed, these files are in fact missing.

The solution

To resolve this issue, you need to basically carry the SQLte.Interop.dll with your msi setup file and load it on the user’s pc when executed. To do this, include the dll in the custom action project and set the build action to Embedded Resource. This way it will be put right inside the custom action assembly and into the msi installer.

Next, some code is needed that gets the raw bytes of the embedded resource and writes them down on the user’s disk. Now you can use the win32 function LoadLibrary() to load the dll in memory. This is enough to resolve the missing file error. I have made one utility class that does just that.

Now using it is as simple as calling that utility function with the correct embedded resource name.

Voila, the exception is gone. Now you can create a database using WiX custom action.
You can find a complete buildable project in this GitHub repo. Happy coding!