Ok, this is a weird one. The junk data isn't random either, it appears to be substrings of the executable itself.
private void Form1_Load(object sender, EventArgs e)
{
string s = GetValue();
// at this point, s == "400". Why isn't really relevant (dumbed down a test)
if (s != "18446744073709551615")
throw new Exception();
// When the exception is thrown though, the string is set to random
// data from inside the executable.
}
This seems dependant on certain seemingly insignificant implementation details in GetValue() such as calls to string.Format() being in different places.
Has anyone ever run into something similar or have any ideas what might cause this?