I am using the dnlib and I want to insert Console.ReadKey() at the end of the Console.WriteLines in another executable.
The code below partly works. Though the issue is. It produces the MSIL:
IL_0028: call void [mscorlib]System.Console::ReadKey()
When I want it to produce
IL_015a: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
Code
ModuleDefMD mod = ModuleDefMD.Load(args[0]);
ModuleDef mode = new ModuleDefUser(args[0]);
Importer importer = new Importer(mod);
AssemblyDef asmr = mod.Assembly;
foreach (TypeDef type in mod.GetTypes())
{
foreach (MethodDef Method in type.Methods)
{
if (Method.Name == "Main")
{
TypeRef consoleRef = new TypeRefUser(mod, "System", "Console", mod.CorLibTypes.AssemblyRef);
MemberRef ReadAll = new MemberRefUser(mod, "ReadKey",
MethodSig.CreateStatic(mod.CorLibTypes.Void),
consoleRef);
var method = Method;
var instructions = method.Body.Instructions;
instructions.Insert(8, new Instruction(OpCodes.Call, ReadAll));
}
}
}