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] How do I change const string?

Featured Replies

Posted
public const string Test = "{ConstText}";

And Found Const

using (var module = ModuleDefMD.Load(Resources.Stub))
{
  foreach (var type in module.GetTypes().Where(t => t.HasMethods))
  {
    IList<FieldDef> fieldstr = type.Fields;
    for (int i = 0; i < fieldstr.Count; i++)
    {
       // fieldstr[i].Name - This found const string
    }
}

How to change ?

 

See code here

It's a FieldDef, not a MethodDef

  • Author
7 hours ago, XenocodeRCE said:

It's a FieldDef, not a MethodDef

So I'm looking in FieldDef and not MethodDef. How do I replace a string?

 

foreach (var type in module.GetTypes())
{
    IList<FieldDef> fieldstr = type.Fields;
    for (int i = 0; i < fieldstr.Count; i++)
    {
       // fieldstr[i].Name - This found const string
    }
}

 

Edited by r3xq1

Very simple example, just to show the idea..

        static void Main(string[] args)
        {
            using (var module = ModuleDefMD.Load(args[0]))
            {
                foreach (var type in module.GetTypes())
                {
                    foreach (FieldDef field in type.Fields)
                    {
                        // this will change all string constant values to "kao". Make sure to fix the `if`!!!
                        if (field.HasConstant && field.ElementType == ElementType.String)
                        {
                            field.Constant.Value = "kao";
                        }
                    }
                }
                module.Write(args[1]);
            }
        }

 

  • 2 weeks later...
  • Author
On 4/2/2021 at 7:50 PM, kao said:

Very simple example, just to show the idea..

It worked great!
And how can you find a constant not by a field but by an object of type string?

public const string Test = "{ConstText}";

I can find the constant I need by the field name

field.Name.String.Contains("Test")

But how to find by name: {ConstText}

 

I already figured it out myself))

You just need to check the value:

if (field.HasConstant && field.Constant.Value.Equals("{ConstText}"))

----------

Why if you try to output to the console

Console.WriteLine(Test);


Changed, then it shows the default text that was set in it Out: {ConstText}  ???

Edited by r3xq1

  • 2 weeks later...
  • Author

So how do you fix the problem? who knows?

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.