2

我使用easyhookandSharpDX从 DirectX 游戏中获取 fps 数据。有时它会起作用。但是,当我下次启动它时(可能只是几分钟后),它会抛出异常System.IO.FileNotFoundException: Could not load file or assembly SharpDX

当我重新启动它几次时,它可以工作。为什么?有没有人和我一样的问题?

SharpDX 版本:2.4.2

4

1 回答 1

0

我不使用 EasyHook,但下面的代码也应该适合你。不要使用有一些限制的 ILMerge,而是执行以下操作:

1) 将 SharpDx.dll 和所有其他需要的 SharpDx 程序集的签名副本链接到您的项目。将“本地副本”属性设置为“假”。

2) 将这些库添加到您的项目中(就像使用 .cs 文件一样)并将文件的属性设置为“嵌入式资源”和“不要复制到输出文件夹”。确保这些文件与您在步骤 1 中链接的文件完全相同。

3)注入后,首先在您的入口点调用以下函数,如果找到,它将从您的资源中加载任意程序集(托管或非托管)。

private static void LoadAssemblyFromResources() {
    AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
         try {
             Assembly asm = Assembly.GetExecutingAssembly();
             string name = args.Name.Substring(0, args.Name.IndexOf(',')) + ".dll";
             string rsc = asm.GetManifestResourceNames().FirstOrDefault(s => s.EndsWith(name));
             if (rsc == null) return null;  //assembly not found in resources
             byte[] module;
             using (Stream stream = asm.GetManifestResourceStream(rsc)) {
                 if (stream == null) return null;
                 module = new byte[stream.Length];
                 stream.Read(module, 0, module.Length);
             }
             try {
                 return Assembly.Load(module); //Load managed assembly as byte array
             } catch (FileLoadException) { 
                 string file = Path.Combine(Path.GetTempPath(), name);
                 if (!File.Exists(file) || !module.SequenceEqual(File.ReadAllBytes(file)))
                     File.WriteAllBytes(file, module);
                 return Assembly.LoadFile(file); //Load unmanaged assembly as file
             }
         } catch {
             return null;
         }
     };
 }
于 2014-02-28T13:42:12.687 回答