Skip to content

Instantly share code, notes, and snippets.

@iSynaptic
Created July 1, 2014 21:57
Show Gist options
  • Save iSynaptic/0d681605ed360c667b53 to your computer and use it in GitHub Desktop.
Save iSynaptic/0d681605ed360c667b53 to your computer and use it in GitHub Desktop.
Castle.Zmq with embedded libzmq
static Context()
{
IntPtr libPtr = Native.LoadLibrary("libzmq");
if (libPtr == IntPtr.Zero)
{
LoadEmbeddedLibary();
}
}
private static void LoadEmbeddedLibary()
{
var asm = typeof (Context).Assembly;
string resourceName = String.Format(
"Castle.Zmq.Native.lib.x{0}.libzmq.dll",
Environment.Is64BitProcess ? 64 : 86);
var dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(dir);
var tempFile = Path.Combine(dir, "libzmq.dll");
using (var stream = asm.GetManifestResourceStream(resourceName))
using (var file = File.OpenWrite(tempFile))
{
stream.CopyTo(file);
file.Flush(true);
}
IntPtr libPtr = Native.LoadLibrary(tempFile);
if (libPtr == IntPtr.Zero)
{
throw new InvalidOperationException("Unable to load libzmq.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment