Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[C#, Dnlib] - Constants Data is not being replaced correctly [Help]

Featured Replies

Posted

From an old topic: https://forum.tuts4you.com/topic/42934-c-dnlib-how-do-i-change-const-string/?tab=comments#comment-208456

Source Code Stub file:

namespace DlibStub
{
    using System;

    public static class ConstTester
    {
        public const string MYCONST = "1111";
        public const string DNTEST = "2222";

        public static void GetResult()
        { 
            Console.WriteLine($"Result: {MYCONST}");
            Console.WriteLine($"Result: {DNTEST}");
        }
    }
}

IL Code:

field: System.String DlibStub.ConstTester::DNTEST
field: System.String DlibStub.ConstTester::MYCONST
Instruction: IL_0000: ldstr "Dnlib Tester"
Instruction: IL_0000: ldstr "Result: 1111"
Instruction: IL_0005: call System.Void System.Console::set_Title(System.String)
Instruction: IL_0005: call System.Void System.Console::WriteLine(System.String)
Instruction: IL_000A: call System.Void DlibStub.ConstTester::GetResult()
Instruction: IL_000A: ldstr "Result: 2222"
Instruction: IL_000F: call System.String System.Console::ReadLine()
Instruction: IL_000F: call System.Void System.Console::WriteLine(System.String)
Instruction: IL_0014: pop
Instruction: IL_0014: ret
Instruction: IL_0015: ret

Code to replace text:

public static void Inizialize(string fileModule)
{
  using var module = ModuleDefMD.Load(fileModule);
  using var text = File.AppendText("LogDnlib.txt"); // For write to txt
  foreach (TypeDef type in module.GetTypes())
  {
     foreach (FieldDef field in type.Fields)
     {
       text.WriteLine($"Field: {field}");
       text.WriteLine($"FieldName: {field.Name}");
       text.WriteLine($"Field: {field}");

       if (field.HasConstant && field.ElementType.Equals(ElementType.String))
       {
          if (field.Name.String.Contains("MYCONST")) // Find a text named MYCONST
          {
              field.Constant.Value = "test1";
          }
          if (field.Name.String.Contains("DNTEST")) // Find a text named DNTEST
          {
               field.Constant.Value = "test2";
          }
       }            
      }
    }
   module.Write("Patched.exe");
 }

Result (ScreenShots): 

How fix?

 

Screenshot_2.png

Screenshot_1.png

Edited by r3xq1

Hey, When you compile the program the Compiler it self replace those "Constants" to their value on method body because its Constant values :) so you can't really do that on dnlib.

Edited by CursedLand

  • Author

[Update]

I tried replacing both Fields and Ldstr and it worked.

 

foreach (MethodDef method in type.Methods)
{
   if (method.HasBody == false) continue;
   if (method.Body.HasInstructions)
   {
      for (int i = 0; i < method.Body.Instructions.Count; i++)
      {
         if (method.Body.Instructions[i].OpCode == OpCodes.Ldstr && method.Body.Instructions[i].Operand.ToString().Contains("1111"))
         {
             method.Body.Instructions[i].Operand = "test1"; 
         }
         if (method.Body.Instructions[i].OpCode == OpCodes.Ldstr && method.Body.Instructions[i].Operand.ToString().Contains("2222"))
         {
             method.Body.Instructions[i].Operand = "test2";
         } 
         method.Body.SimplifyBranches();
         method.Body.OptimizeMacros();
      }
   }
}

 

Screenshot_1.png

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.