Share via

DnLib & Patching

Arsium ***** 331 Reputation points
2021-06-25T11:20:39.417+00:00

Hello ,

I was trying to patch a static member (which has a type of custom class I've created) from a static class in an exe file using dnlib. However it was not possible to do that do you have any idea ?

 try
 {

 foreach (TypeDef type in asmDef.Types)
 {
 asmDef.Assembly.Name = Path.GetFileNameWithoutExtension(AsmName);
 asmDef.Name = Path.GetFileName(AsmName);

 if (type.Name == "Starting") 
 {
 foreach (MethodDef method in type.Methods)
 {


 for (int i = 0; i < method.Body.Instructions.Count(); i++)
 {


 try
 {



 if (method.Body.Instructions[i].Operand.ToString().Contains("HostsList"))
 {
 MessageBox.Show(method.Body.Instructions[i].Operand.ToString());
 method.Body.Instructions[i].Operand = new List<Host>() { new Host("127.0.0.1", 5900) };
 }



 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.ToString());
 }

 }
 }
 }
 }
 }

I even tried to check the type with operator "is" like following :

method.Body.Instructions[i].Operand is List<Host>

and

((List<Host>)method.Body.Instructions[i].Operand).Add(new Host("127.0.0.1", 5900));

but it did not work too.

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.