2

我正在尝试从 dnlib 调用 MessageBox.Show。我有这个函数可以找到 Show 方法:

MethodDef GetSystemMethod(Type DeclaringType, string Methodname, Type[] MethodParams)
    {
        var filename = DeclaringType.Module.FullyQualifiedName;
        var mod = ModuleDefMD.Load(filename);
        TypeDef[] types = mod.GetTypes().ToArray();
        foreach(TypeDef t in types)
        {
            if(t.Name==DeclaringType.Name)
            {
                foreach(var m in t.Methods)
                {
                    bool ok = true;
                    int i = 0;
                    foreach(var p in m.Parameters)
                    {
                        if (p.Type.TypeName != MethodParams[i].Name) ok = false;
                    }
                    if(ok)
                    {
                        return m;
                    }
                }
            }
        }
        throw new Exception("System Method not found!");
    }

这就是我使用它的方式:

method.Body.Instructions.Clear();
                    method.Body.Instructions.Add(dnlib.DotNet.Emit.OpCodes.Nop.ToInstruction());
                    method.Body.Instructions.Add(dnlib.DotNet.Emit.OpCodes.Ldstr.ToInstruction("changed method here!"));
                    method.Body.Instructions.Add(dnlib.DotNet.Emit.OpCodes.Call.ToInstruction(GetSystemMethod(typeof(MessageBox), "Show", new Type[] { typeof(string)})));
                    method.Body.Instructions.Add(dnlib.DotNet.Emit.OpCodes.Pop.ToInstruction());
                    method.Body.Instructions.Add(dnlib.DotNet.Emit.OpCodes.Ret.ToInstruction());
                    module.Write(Path.GetDirectoryName(file) + "\\changed-" + Path.GetFileName(file));

当我运行它时,当它尝试将模块写入文件时出现此异常。任何人都可以看看并解释我做错了什么吗?

dnlib.DotNet.Writer.ModuleWriterException: 'Method System.Windows.Forms.HelpInfo System.Windows.Forms.MessageBox::get_HelpInfo() (06002BE3) is not defined in this module (TestProj.exe). A method was removed that is still referenced by this module.'
4

0 回答 0