Exemplo n.º 1
0
        public InstanceDll(string dllPath)
        {
            // copy the dll to a temp directory
            var path = TempFileManager.GetTempFilename(string.Format("{0}", Path.GetFileNameWithoutExtension(dllPath)), ".dll", false);

            using (var stream = new FileStream(path, FileMode.Create, System.Security.AccessControl.FileSystemRights.FullControl, FileShare.ReadWrite | FileShare.Delete, 4 * 1024, FileOptions.None))
                using (var sdll = File.OpenRead(dllPath))
                    sdll.CopyTo(stream);

            // try to locate dlls in the current directory (for libretro cores)
            // this isnt foolproof but its a little better than nothing
            // setting PWD temporarily doesnt work. that'd be ideal since it supposedly gets searched early on,
            // but i guess not with SetDllDirectory in effect
            var envpath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);

            try
            {
                string envpath_new = Path.GetDirectoryName(path) + ";" + envpath;
                Environment.SetEnvironmentVariable("PATH", envpath_new, EnvironmentVariableTarget.Process);
                _hModule = LoadLibrary(path);                 //consider using LoadLibraryEx instead of shenanigans?
                if (_hModule == IntPtr.Zero)
                {
                    var lastError = GetLastError();
                    throw new InvalidOperationException($"Failed to load plugin {path}, error code: 0x{lastError:X}");
                }
                var newfname = TempFileManager.RenameTempFilenameForDelete(path);
                File.Move(path, newfname);
            }
            finally
            {
                Environment.SetEnvironmentVariable("PATH", envpath, EnvironmentVariableTarget.Process);
            }
        }
        public InstanceDll(string dllPath)
        {
            // copy the dll to a temp directory
            var path = TempFileManager.GetTempFilename(Path.GetFileNameWithoutExtension(dllPath), ".dll", false);

            File.Copy(dllPath, path, true);
            // try to locate dlls in the current directory (for libretro cores)
            // this isn't foolproof but it's a little better than nothing
            // setting PWD temporarily doesn't work. that'd be ideal since it supposedly gets searched early on,
            // but i guess not with SetDllDirectory in effect
            var envpath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);

            try
            {
                var envpath_new = $"{Path.GetDirectoryName(path)};{envpath}";
                Environment.SetEnvironmentVariable("PATH", envpath_new, EnvironmentVariableTarget.Process);
                HModule = OSTailoredCode.LinkedLibManager.LoadOrThrow(path);                 // consider using LoadLibraryEx instead of shenanigans?
                var newfname = TempFileManager.RenameTempFilenameForDelete(path);
                File.Move(path, newfname);
            }
            catch
            {
                // ignored
            }
            Environment.SetEnvironmentVariable("PATH", envpath, EnvironmentVariableTarget.Process);
        }