0

Is there any way I can see the contents of the string table in a running .NET application?

I want to compare a console application with vanilla string concatinations and one using the string builder.

BanksySan
  • 27,362
  • 33
  • 117
  • 216
  • @stuartd I *think* that question is about string literals in an assembly, I'm wanting the strings cached at run time by the framework. – BanksySan Nov 27 '15 at 11:53
  • "The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system." -- https://msdn.microsoft.com/en-us/library/system.string.intern.aspx – stuartd Nov 27 '15 at 11:57

1 Answers1

2

You can use ClrMD to attach to a process and retrieve information from it. Something along the lines of the following should work:

var proc = Process.GetProcessesByName("myapp.exe").FirstOrDefault();
using (var target = DataTarget.AttachToProcess(proc.Id, 1000))
{
    var runtime = target.ClrVersions[0].CreateRuntime();
    var heap = runtime.GetHeap();
    foreach (var obj in heap.EnumerateObjectAddresses())
    {
        var type = heap.GetObjectType(obj);
        if (type.Name == "System.String")
        {
            var value = (string)type.GetValue(obj);
            // Write value to disk or something.
        }
    }
}
Jonathan Dickinson
  • 9,050
  • 1
  • 37
  • 60