Exemplo n.º 1
0
 private static void InitializeLibraries()
 {
     // Environment.SetEnvironmentVariable("PATH", $"{Environment.GetEnvironmentVariable("PATH")};{_dllsPath}");
     // LoadLibrary(Path.Combine(_dllsPath, "CSteamworks.dll"));
     // LoadLibrary(Path.Combine(_dllsPath, "steam_api.dll"));
     Kernel32.AddDllDirectory(_dllsPath);
     AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
     AppDomain.CurrentDomain.ProcessExit     += OnExit;
 }
Exemplo n.º 2
0
        public static void Initialize()
        {
            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            Type     typeLoader        = executingAssembly.GetType("Costura.AssemblyLoader");

            if (typeLoader == null)
            {
                return;
            }

            Dictionary <string, string> assemblyNames = ReflectionHelper.GetInstanceField <Dictionary <string, string> > (typeLoader, null, "assemblyNames");
            Dictionary <string, string> symbolNames   = ReflectionHelper.GetInstanceField <Dictionary <string, string> > (typeLoader, null, "symbolNames");
            Uri    uriOuter = new Uri(executingAssembly.Location == null ? executingAssembly.CodeBase : executingAssembly.Location);
            string path     = Path.GetDirectoryName(uriOuter.LocalPath);
            string appPath  = Path.Combine(path, executingAssembly.GetName().Name);

            if (!Directory.Exists(path))
            {
                return;
            }
            Directory.CreateDirectory(appPath);

#pragma warning disable CS0618 // 类型或成员已过时
            AppDomain.CurrentDomain.AppendPrivatePath(appPath);
#pragma warning restore CS0618 // 类型或成员已过时

            Kernel32.AddDllDirectory(appPath);

            foreach (var assemblyName in assemblyNames)
            {
                byte[] rawAssembly;
                using (Stream stream = AssemblyHelper.LoadStream(assemblyName.Value, executingAssembly))
                {
                    if (stream != null)
                    {
                        rawAssembly = AssemblyHelper.ReadStream(stream);
                        File.WriteAllBytes(Path.Combine(appPath, assemblyName.Key + ".dll"), rawAssembly);
                    }
                }
            }
            ;

            foreach (var pdbName in symbolNames)
            {
                byte[] rawAssembly;
                using (Stream stream = AssemblyHelper.LoadStream(pdbName.Value, executingAssembly))
                {
                    if (stream != null)
                    {
                        rawAssembly = AssemblyHelper.ReadStream(stream);
                        File.WriteAllBytes(Path.Combine(appPath, pdbName.Key + ".pdb"), rawAssembly);
                    }
                }
            }
            ;
        }
Exemplo n.º 3
0
        private static void InternalPreloadUnmanagedLibraries(Assembly executingAssembly, string tempBasePath, IList <string> libs, Dictionary <string, string> checksums)
        {
            string name;

            foreach (var lib in libs)
            {
                name = ResourceNameToPath(lib);

                var assemblyTempFilePath = Path.Combine(tempBasePath, name);

                if (File.Exists(assemblyTempFilePath))
                {
                    var checksum = CalculateChecksum(assemblyTempFilePath);
                    if (checksum != checksums[lib])
                    {
                        File.Delete(assemblyTempFilePath);
                    }
                }

                if (!File.Exists(assemblyTempFilePath))
                {
                    using (var copyStream = LoadStream(lib, executingAssembly))
                    {
                        using (var assemblyTempFile = File.OpenWrite(assemblyTempFilePath))
                        {
                            CopyTo(copyStream, assemblyTempFile);
                        }
                    }
                }
            }

            //SetDllDirectory(tempBasePath);
            Kernel32.AddDllDirectory(tempBasePath);

            uint errorModes        = 32771;
            var  originalErrorMode = Kernel32.SetErrorMode(errorModes);

            foreach (var lib in libs)
            {
                name = ResourceNameToPath(lib);

                if (name.EndsWith(".dll"))
                {
                    var assemblyTempFilePath = Path.Combine(tempBasePath, name);

                    //LoadLibrary(assemblyTempFilePath);
                    Kernel32.LoadLibraryEx(assemblyTempFilePath, IntPtr.Zero, LoadLibraryFlags.LOAD_LIBRARY_SEARCH_USER_DIRS);
                }
            }

            // restore to previous state
            Kernel32.SetErrorMode(originalErrorMode);
        }
Exemplo n.º 4
0
        public void TestSearchPath()
        {
            IntPtr dllLibraryCookie = IntPtr.Zero;

            try {
                AppDomain currentDomain = AppDomain.CurrentDomain;
                dllLibraryCookie = Kernel32.AddDllDirectory(currentDomain.BaseDirectory);
                IntPtr nativeDbgHelp = Kernel32.LoadLibraryEx("DBGHELP.DLL", IntPtr.Zero,
                                                              Kernel32.LoadFlags.SeachUserDirectories);
                if (IntPtr.Zero == nativeDbgHelp)
                {
                    throw new ApplicationException();
                }
                IntPtr nativeSymSrv = Kernel32.LoadLibraryEx("SYMSRV.DLL", IntPtr.Zero,
                                                             Kernel32.LoadFlags.SeachUserDirectories);
                if (IntPtr.Zero == nativeSymSrv)
                {
                    throw new ApplicationException();
                }
                string algExeSearchPath;
                string algPdbSearchPath;
                using (SymbolHandler handler = new SymbolHandler()) {
                    handler.SearchPath =
                        @"srv*c:\symbols*https://msdl.microsoft.com/download/symbols";
                    algExeSearchPath = handler.FindExeFile("alg.exe", 0x5632D7B5, 0x1c000);
                    Guid   pdbGuid;
                    uint   age;
                    string pdbFileName = SymbolHandler.GetPdbFileInfoFromExe(
                        new FileInfo(algExeSearchPath), out pdbGuid, out age);
                    if (null == pdbFileName)
                    {
                        throw new ApplicationException();
                    }
                    algPdbSearchPath = handler.FindPdbFile(pdbFileName, pdbGuid, age);
                }
                return;
            }
            finally {
                if (null != dllLibraryCookie)
                {
                    Kernel32.RemoveDllDirectory(dllLibraryCookie);
                }
            }
        }
Exemplo n.º 5
0
        private static void SetupDllPaths()
        {
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                return;
            }

            var libsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "libs");

            try
            {
                Kernel32.SetDefaultDllDirectories(Kernel32.LoadLibrarySearchDefaultDirs);
                Kernel32.AddDllDirectory(Path.Combine(libsDirectory, Environment.Is64BitProcess ? "x64" : "x86"));
            }
            catch
            {
                // Pre-Windows 7, KB2533623
                Kernel32.SetDllDirectory(Path.Combine(libsDirectory, Environment.Is64BitProcess ? "x64" : "x86"));
            }
        }
Exemplo n.º 6
0
        public void Initialize([NotNull] string directory)
        {
            AcToolsLogging.Write(directory);

            if (string.IsNullOrEmpty(directory))
            {
                throw new ArgumentNullException(nameof(directory));
            }

            Directory = directory;
            AppDomain.CurrentDomain.AssemblyResolve += Resolve;

            if (RegisterDllDirectory)
            {
                Kernel32.AddDllDirectory(directory);
            }

            foreach (var name in Imports)
            {
                Kernel32.LoadLibrary(Path.Combine(directory, name + DllExtension));
            }
        }
Exemplo n.º 7
0
 private static void InitializeLibraries()
 {
     Kernel32.AddDllDirectory(_dllsPath);
     AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
     AppDomain.CurrentDomain.ProcessExit     += OnExit;
 }