static Interop() { if (LibraryLoader.CustomLoadLibrary != null) { LibraryLoader.SymbolLookupDelegate symbolLookup; var hiredisxAddr = LibraryLoader.CustomLoadLibrary("Hiredisx", out symbolLookup); InitializeDelegates(hiredisxAddr, symbolLookup); } }
private static void InitializeDelegates(IntPtr hiredisxAddr, LibraryLoader.SymbolLookupDelegate lookup) { // When running under mono and the native hiredisx libraries are in a non-standard location (e.g. are placed in application_dir/x86|x64), // we cannot just load the native libraries and have everything automatically work. Hence all these delegates. // TODO: The performance impact of this over conventional P/Invoke is evidently not good - there is about a 50% increase in overhead. // http://ybeernet.blogspot.com/2011/03/techniques-of-calling-unmanaged-code.html // There appears to be an alternative "dllmap" approach: // http://www.mono-project.com/Interop_with_Native_Libraries // but this requires config file entries. // Perhaps the method of calling native methods would better depend on which platform is being used. // anyway, delegates work everywhere so that's what we'll use for now. get it working first, optimize later. redisConnectWithTimeoutX = (redisConnectWithTimeoutX_delegate)Marshal.GetDelegateForFunctionPointer(lookup(hiredisxAddr, "redisConnectWithTimeoutX"), typeof(redisConnectWithTimeoutX_delegate)); redisCommandX = (redisCommandX_delegate)Marshal.GetDelegateForFunctionPointer(lookup(hiredisxAddr, "redisCommandX"), typeof(redisCommandX_delegate)); retrieveElementX = (retrieveElementX_delegate)Marshal.GetDelegateForFunctionPointer(lookup(hiredisxAddr, "retrieveElementX"), typeof(retrieveElementX_delegate)); freeReplyObjectX = (freeReplyObjectX_delegate)Marshal.GetDelegateForFunctionPointer(lookup(hiredisxAddr, "freeReplyObjectX"), typeof(freeReplyObjectX_delegate)); retrieveStringAndFreeReplyObjectX = (retrieveStringAndFreeReplyObjectX_delegate)Marshal.GetDelegateForFunctionPointer(lookup(hiredisxAddr, "retrieveStringAndFreeReplyObjectX"), typeof(retrieveStringAndFreeReplyObjectX_delegate)); retrieveElementStringX = (retrieveElementStringX_delegate)Marshal.GetDelegateForFunctionPointer(lookup(hiredisxAddr, "retrieveElementStringX"), typeof(retrieveElementStringX_delegate)); setupArgumentArrayX = (setupArgumentArrayX_delegate)Marshal.GetDelegateForFunctionPointer(lookup(hiredisxAddr, "setupArgumentArrayX"), typeof(setupArgumentArrayX_delegate)); setArgumentX = (setArgumentX_delegate)Marshal.GetDelegateForFunctionPointer(lookup(hiredisxAddr, "setArgumentX"), typeof(setArgumentX_delegate)); }