Skip to content

Instantly share code, notes, and snippets.

@yavor87
Last active October 25, 2020 17:32
Show Gist options
  • Save yavor87/05f29c269ac9daca7bd693b666b9700a to your computer and use it in GitHub Desktop.
Save yavor87/05f29c269ac9daca7bd693b666b9700a to your computer and use it in GitHub Desktop.
Wix Embedded Sqlite
public class CustomActions
{
[CustomAction]
public static ActionResult BuildIndex(Session session)
{
session.Log("Begin building index");
try
{
EmbeddedAssembly.Load(string.Format("BuildIndexAction.{0}.SQLite.Interop.dll", Environment.Is64BitProcess ? "x64" : "x86"), "SQLite.Interop.dll");
}
catch (Exception e)
{
session.Message(InstallMessage.Error, RecordException(e));
return ActionResult.Failure;
}
// Do the custom action
return ActionResult.Success;
}
}
/// <summary>
/// Load Assembly, DLL from Embedded Resources into memory.
/// </summary>
/// <param name="embeddedResource">Embedded Resource string. Example: WindowsFormsApplication1.SomeTools.dll</param>
/// <param name="fileName">File Name. Example: SomeTools.dll</param>
public static void Load(string embeddedResource, string fileName)
{
byte[] embeddedResourceBytes = null;
Assembly curAsm = Assembly.GetExecutingAssembly();
using (Stream stm = curAsm.GetManifestResourceStream(embeddedResource))
{
// Either the file is not existed or it is not mark as embedded resource
if (stm == null)
throw new Exception(embeddedResource + " is not found in Embedded Resources.");
// Get byte[] from the file from embedded resource
embeddedResourceBytes = new byte[(int)stm.Length];
stm.Read(embeddedResourceBytes, 0, (int)stm.Length);
}
string tempFile = SaveOnDisk(embeddedResourceBytes, fileName);
// Load it into memory
IntPtr result = LoadLibrary(tempFile);
if (result == IntPtr.Zero)
{
Exception e = new Win32Exception();
throw new DllNotFoundException("Unable to load library: " + fileName, e);
}
}
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern IntPtr LoadLibrary(string lpFileName);
// Saves the specified byte array in the temp folder with the specified filename
private static string SaveOnDisk(byte[] data, string fileName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment