public IntPtr LoadLibrary(string fileName)
        {
            var libraryHandle = IntPtr.Zero;

            try
            {
                LibraryLoaderTrace.TraceInformation("Trying to load native library \"{0}\"...", fileName);
                libraryHandle = UnixLoadLibrary(fileName, RtldNow);
                if (libraryHandle != IntPtr.Zero)
                {
                    LibraryLoaderTrace.TraceInformation("Successfully loaded native library \"{0}\", handle = {1}.", fileName, libraryHandle);
                }
                else
                {
                    LibraryLoaderTrace.TraceError("Failed to load native library \"{0}\".\r\nCheck windows event log.", fileName);
                }
            }
            catch (Exception e)
            {
                var lastError = UnixGetLastError();
                LibraryLoaderTrace.TraceError("Failed to load native library \"{0}\".\r\nLast Error:{1}\r\nCheck inner exception and\\or windows event log.\r\nInner Exception: {2}", fileName, lastError, e.ToString());
            }

            return(libraryHandle);
        }
Example #2
0
 public IntPtr GetProcAddress(IntPtr libraryHandle, string functionName)
 {
     try
     {
         LibraryLoaderTrace.TraceInformation("Trying to load native function \"{0}\" from the library with handle {1}...",
                                             functionName, libraryHandle);
         var functionHandle = WindowsGetProcAddress(libraryHandle, functionName);
         if (functionHandle != IntPtr.Zero)
         {
             LibraryLoaderTrace.TraceInformation("Successfully loaded native function \"{0}\", function handle = {1}.",
                                                 functionName, functionHandle);
         }
         else
         {
             LibraryLoaderTrace.TraceError("Failed to load native function \"{0}\", function handle = {1}",
                                           functionName, functionHandle);
         }
         return(functionHandle);
     }
     catch (Exception e)
     {
         var lastError = WindowsGetLastError();
         LibraryLoaderTrace.TraceError("Failed to free native library with handle {0}.\r\nLast Error:{1}\r\nCheck inner exception and\\or windows event log.\r\nInner Exception: {2}", libraryHandle, lastError, e.ToString());
         return(IntPtr.Zero);
     }
 }
Example #3
0
 public bool FreeLibrary(IntPtr libraryHandle)
 {
     try
     {
         LibraryLoaderTrace.TraceInformation("Trying to free native library with handle {0} ...", libraryHandle);
         var isSuccess = WindowsFreeLibrary(libraryHandle);
         if (isSuccess)
         {
             LibraryLoaderTrace.TraceInformation("Successfully freed native library with handle {0}.", libraryHandle);
         }
         else
         {
             LibraryLoaderTrace.TraceError("Failed to free native library with handle {0}.\r\nCheck windows event log.", libraryHandle);
         }
         return(isSuccess);
     }
     catch (Exception e)
     {
         var lastError = WindowsGetLastError();
         LibraryLoaderTrace.TraceError("Failed to free native library with handle {0}.\r\nLast Error:{1}\r\nCheck inner exception and\\or windows event log.\r\nInner Exception: {2}", libraryHandle, lastError, e.ToString());
         return(false);
     }
 }
        public IntPtr GetProcAddress(IntPtr libraryHandle, string functionName)
        {
            UnixGetLastError(); // Clearing previous errors
            LibraryLoaderTrace.TraceInformation("Trying to load native function \"{0}\" from the library with handle {1}...",
                                                functionName, libraryHandle);
            var functionHandle = UnixGetProcAddress(libraryHandle, functionName);
            var errorPointer   = UnixGetLastError();

            if (errorPointer != IntPtr.Zero)
            {
                throw new Exception("dlsym: " + Marshal.PtrToStringAnsi(errorPointer));
            }
            if (functionHandle != IntPtr.Zero && errorPointer == IntPtr.Zero)
            {
                LibraryLoaderTrace.TraceInformation("Successfully loaded native function \"{0}\", function handle = {1}.",
                                                    functionName, functionHandle);
            }
            else
            {
                LibraryLoaderTrace.TraceError("Failed to load native function \"{0}\", function handle = {1}, error pointer = {2}",
                                              functionName, functionHandle, errorPointer);
            }
            return(functionHandle);
        }