Ejemplo n.º 1
0
        /// <summary>
        /// Get's the current process architecture while keeping track of any assumptions or possible errors.
        /// </summary>
        /// <returns></returns>
        private ProcessArchitectureInfo GetProcessArchitecture()
        {
            ProcessArchitectureInfo processInfo = new ProcessArchitectureInfo();

            // BUGBUG: Will this always be reliable?
            string processArchitecture = Environment.GetEnvironmentVariable(PROCESSOR_ARCHITECTURE);

            if (!String.IsNullOrEmpty(processArchitecture))
            {
                // Sanity check
                processInfo.Architecture = processArchitecture;
            }
            else
            {
                processInfo.Architecture = "x86";
                processInfo.Warnings.Add("Failed to detect processor architecture, falling back to x86.");
            }


            var addressWidth = processorArchitectureAddressWidthPlatforms[processInfo.Architecture];

            if (addressWidth != IntPtr.Size)
            {
                processInfo.Warnings.Add(String.Format("Expected the detected processing architecture of {0} to have an address width of {1} Bytes but was {2} Bytes.", processInfo.Architecture, addressWidth, IntPtr.Size));
            }

            return(processInfo);
        }
        private IntPtr LoadLibraryInternal(string dllName, string baseDirectory, ProcessArchitectureInfo processArchInfo)
        {
            var platformName         = GetPlatformName(processArchInfo.Architecture);
            var expectedDllDirectory = Path.Combine(
                Path.Combine(baseDirectory, DllDirectory), platformName);

            return(this.LoadLibraryRaw(dllName, expectedDllDirectory));
        }
Ejemplo n.º 3
0
        private IntPtr LoadLibraryRaw(string dllName, string baseDirectory)
        {
            var libraryHandle = IntPtr.Zero;
            var fileName      = FixUpDllFileName(Path.Combine(baseDirectory, dllName));

#if WINRT && false
            // MP! Note: This is a hack, needs refinement. We don't need to carry payload of both binaries for WinRT because the appx is platform specific.
            ProcessArchitectureInfo processInfo = GetProcessArchitecture();

            string cpu = "x86";
            if (processInfo.Architecture == "AMD64")
            {
                cpu = "x64";
            }

            string dllpath = baseDirectory.Replace($"dll\\{cpu}", "");
            fileName = $"{dllpath}{dllName}.dll";

            // Show where we're trying to load the file from
            Debug.WriteLine($"Trying to load native library \"{fileName}\"...");
#endif

            if (File.Exists(fileName))
            {
                // Attempt to load dll
                try
                {
                    libraryHandle = Win32Api.LoadLibrary(fileName);
                    if (libraryHandle != IntPtr.Zero)
                    {
                        // library has been loaded
                        Debug.WriteLine($"Successfully loaded native library \"{fileName}\".");
                        loadedAssemblies.Add(dllName);
                    }
                    else
                    {
                        Debug.WriteLine($"Failed to load native library \"{fileName}\".\r\nCheck windows event log.");
                    }
                }
                catch (Exception e)
                {
                    // ReSharper disable once RedundantAssignment
                    var lastError = Marshal.GetLastWin32Error();
                    Debug.WriteLine(
                        $"Failed to load native library \"{fileName}\".\r\nLast Error:{lastError}\r\nCheck inner exception and\\or windows event log.\r\nInner Exception: {e}");
                }
            }
            else
            {
                Debug.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                              "The native library \"{0}\" does not exist.",
                                              fileName));
            }

            return(libraryHandle);
        }
Ejemplo n.º 4
0
        private IntPtr LoadLibraryInternal(string dllName, string baseDirectory, ProcessArchitectureInfo processArchInfo)
        {
            //IntPtr libraryHandle = IntPtr.Zero;
            var platformName         = GetPlatformName(processArchInfo.Architecture);
            var expectedDllDirectory = Path.Combine(
                Path.Combine(baseDirectory, DllDirectory), platformName);

            //var fileName = FixUpDllFileName(Path.Combine(expectedDllDirectory, dllName));

            return(LoadLibraryRaw(dllName, expectedDllDirectory));
        }
Ejemplo n.º 5
0
        private IntPtr LoadLibraryInternal(string dllName, string baseDirectory, ProcessArchitectureInfo processArchInfo)
        {
            IntPtr libraryHandle        = IntPtr.Zero;
            var    platformName         = GetPlatformName(processArchInfo.Architecture);
            var    expectedDllDirectory = Path.Combine(
                Path.Combine(baseDirectory, DllDirectory), platformName);
            var fileName = FixUpDllFileName(Path.Combine(expectedDllDirectory, dllName));

            if (File.Exists(fileName))
            {
                // Attempt to load dll
                try
                {
                    // Show where we're trying to load the file from
                    Trace.TraceInformation(String.Format(CultureInfo.CurrentCulture,
                                                         "Trying to load native library \"{0}\"...",
                                                         fileName));

                    libraryHandle = Win32LoadLibrary(fileName);
                    if (libraryHandle != IntPtr.Zero)
                    {
                        // library has been loaded
                        Trace.TraceInformation(String.Format(CultureInfo.CurrentCulture,
                                                             "Successfully loaded native library \"{0}\".",
                                                             fileName));
                        loadedAssemblies.Add(dllName);
                    }
                    else
                    {
                        Trace.TraceError(
                            "Failed to load native library \"{0}\".\r\nCheck windows event log.",
                            fileName);
                    }
                }
                catch (Exception e)
                {
                    var lastError = Marshal.GetLastWin32Error();
                    Trace.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);
                }
            }
            else
            {
                Trace.TraceWarning(String.Format(CultureInfo.CurrentCulture,
                                                 "The native library \"{0}\" does not exist.",
                                                 fileName));
            }

            return(libraryHandle);
        }
Ejemplo n.º 6
0
        /// <summary>
        ///     Get's the current process architecture while keeping track of any assumptions or possible errors.
        /// </summary>
        /// <returns></returns>
        private ProcessArchitectureInfo GetProcessArchitecture()
        {
            // BUGBUG: Will this always be reliable?
            var processArchitecture = Environment.GetEnvironmentVariable(ProcessorArchitecture);
            var processInfo         = new ProcessArchitectureInfo();

            if (!string.IsNullOrEmpty(processArchitecture))
            {
                // Sanity check
                processInfo.Architecture = processArchitecture;
            }
            else
            {
                processInfo.AddWarning("Failed to detect processor architecture, falling back to x86.");

                processInfo.Architecture = (IntPtr.Size == 8) ? "x64" : "x86";
            }

            var addressWidth = processorArchitectureAddressWidthPlatforms[processInfo.Architecture];

            if (addressWidth != IntPtr.Size)
            {
                if (string.Equals(processInfo.Architecture, "AMD64", StringComparison.OrdinalIgnoreCase) &&
                    IntPtr.Size == 4)
                {
                    // fall back to x86 if detected x64 but has an address width of 32 bits.
                    processInfo.Architecture = "x86";
                    processInfo.AddWarning(
                        "Expected the detected processing architecture of {0} to have an address width of {1} Bytes but was {2} Bytes, falling back to x86.",
                        processInfo.Architecture, addressWidth, IntPtr.Size);
                }
                else
                {
                    // no fallback possible
                    processInfo.AddWarning(
                        "Expected the detected processing architecture of {0} to have an address width of {1} Bytes but was {2} Bytes.",
                        processInfo.Architecture, addressWidth, IntPtr.Size);
                }
            }

            return(processInfo);
        }
Ejemplo n.º 7
0
        private IntPtr LoadLibraryInternal(string dllName, string baseDirectory, ProcessArchitectureInfo processArchInfo)
        {
            //IntPtr libraryHandle = IntPtr.Zero;
            var platformName = GetPlatformName(processArchInfo.Architecture);
            var expectedDllDirectory = Path.Combine(
                Path.Combine(baseDirectory, DllDirectory), platformName);
            //var fileName = FixUpDllFileName(Path.Combine(expectedDllDirectory, dllName));

            return LoadLibraryRaw(dllName, expectedDllDirectory);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Get's the current process architecture while keeping track of any assumptions or possible errors.
        /// </summary>
        /// <returns></returns>
        private ProcessArchitectureInfo GetProcessArchitecture()
        {
            // BUGBUG: Will this always be reliable?
            string processArchitecture = Environment.GetEnvironmentVariable(ProcessorArchitecture);
            var processInfo = new ProcessArchitectureInfo();
            if (!String.IsNullOrEmpty(processArchitecture))
            {
                // Sanity check
                processInfo.Architecture = processArchitecture;
            }
            else
            {
                processInfo.AddWarning("Failed to detect processor architecture, falling back to x86.");

                processInfo.Architecture = (IntPtr.Size == 8) ? "x64" : "x86";
            }

            var addressWidth = processorArchitectureAddressWidthPlatforms[processInfo.Architecture];
            if (addressWidth != IntPtr.Size)
            {
                if (String.Equals(processInfo.Architecture, "AMD64", StringComparison.OrdinalIgnoreCase) && IntPtr.Size == 4)
                {
                    // fall back to x86 if detected x64 but has an address width of 32 bits.
                    processInfo.Architecture = "x86";
                    processInfo.AddWarning("Expected the detected processing architecture of {0} to have an address width of {1} Bytes but was {2} Bytes, falling back to x86.", processInfo.Architecture, addressWidth, IntPtr.Size);
                }
                else
                {
                    // no fallback possible
                    processInfo.AddWarning("Expected the detected processing architecture of {0} to have an address width of {1} Bytes but was {2} Bytes.", processInfo.Architecture, addressWidth, IntPtr.Size);

                }
            }

            return processInfo;
        }
Ejemplo n.º 9
0
        private IntPtr LoadLibraryInternal(string dllName, string baseDirectory, ProcessArchitectureInfo processArchInfo)
        {
            IntPtr libraryHandle = IntPtr.Zero;
            var platformName = GetPlatformName(processArchInfo.Architecture);
            var expectedDllDirectory = Path.Combine(
                Path.Combine(baseDirectory, DllDirectory), platformName);
            var fileName = FixUpDllFileName(Path.Combine(expectedDllDirectory, dllName));

            if (File.Exists(fileName))
            {
                // Attempt to load dll
                try
                {
                    // Show where we're trying to load the file from
                    Trace.TraceInformation(String.Format(CultureInfo.CurrentCulture,
                          "Trying to load native library \"{0}\"...",
                          fileName));

                    libraryHandle = Win32LoadLibrary(fileName);
                    if (libraryHandle != IntPtr.Zero)
                    {
                        // library has been loaded
                        Trace.TraceInformation(String.Format(CultureInfo.CurrentCulture,
                          "Successfully loaded native library \"{0}\".",
                          fileName));
                        loadedAssemblies.Add(dllName);
                    }
                    else
                    {
                        Trace.TraceError(
                            "Failed to load native library \"{0}\".\r\nCheck windows event log.", 
                            fileName);
                    }
                }
                catch (Exception e)
                {
                    var lastError = Marshal.GetLastWin32Error();
                    Trace.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);
                }
            }
            else
            {
                Trace.TraceWarning(String.Format(CultureInfo.CurrentCulture,
                          "The native library \"{0}\" does not exist.",
                          fileName));
            }

            return libraryHandle;
        }