Exemple #1
0
        public IntPtr LoadLibrary(string fileName, string platformName = null)
        {
            fileName = this.FixUpLibraryName(fileName);
            lock (this.syncLock) {
                if (!this.loadedAssemblies.ContainsKey(fileName))
                {
                    if (platformName == null)
                    {
                        platformName = SystemManager.GetPlatformName();
                    }
                    LibraryLoaderTrace.TraceInformation("Current platform: " + platformName);
                    var dllHandle = this.CheckExecutingAssemblyDomain(fileName, platformName);
                    if (dllHandle == IntPtr.Zero)
                    {
                        dllHandle = this.CheckCurrentAppDomain(fileName, platformName);
                    }
                    if (dllHandle == IntPtr.Zero)
                    {
                        dllHandle = this.CheckWorkingDirecotry(fileName, platformName);
                    }

                    if (dllHandle != IntPtr.Zero)
                    {
                        this.loadedAssemblies[fileName] = dllHandle;
                    }
                    else
                    {
                        LibraryLoaderTrace.TraceError("Failed to find library \"{0}\" for platform {1}.", fileName, platformName);
                    }
                }
            }
            return(this.loadedAssemblies[fileName]);
        }
Exemple #2
0
 public bool FreeLibrary(string fileName)
 {
     fileName = this.FixUpLibraryName(fileName);
     lock (this.syncLock) {
         if (!this.IsLibraryLoaded(fileName))
         {
             LibraryLoaderTrace.TraceWarning("Failed to free library \"{0}\" because it is not loaded", fileName);
             return(false);
         }
         if (this.logic.FreeLibrary(this.loadedAssemblies[fileName]))
         {
             this.loadedAssemblies.Remove(fileName);
             return(true);
         }
         return(false);
     }
 }
Exemple #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);
     }
 }
Exemple #4
0
        public IntPtr LoadLibrary(string fileName)
        {
            var libraryHandle = IntPtr.Zero;

            try {
                LibraryLoaderTrace.TraceInformation("Trying to load native library \"{0}\"...", fileName);
                libraryHandle = UnixLoadLibrary(fileName, RTLD_NOW);
                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);
        }
Exemple #5
0
        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);
        }
Exemple #6
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);
     }
 }