Hi ho :) I've noticed something weird. When I try to read an unexisting address location (using ReadProcessMemory of kernel32 libraries), it gives an exception. This is totally normal. Then I'm able to retrieve the error code with GetLastError(). This code can be formatted, which returns a nice string message in my native language. But now I've noticed something that's not quite normal. When I run the application normally, and the expected exception happens, the FormatMessage returns something like: "A Read/WriteProcessMemory-operation is only partly executed." Riiiiiight... :wtf: Now the weirdest thing is, if I put a debugging breakpoint just before I retrieve the error code with GetLastError(), and then do a step-by-step running (you know, the F11/F10 thing), it returns something totally different: "Tried to point to an unexisting token." Which is the message I want, because that's exactly what I did wrong. But still he doesn't return it if I don't put a breakpoint just before the GetLastError() function. It's as if the system needs a little time to store the error code, and my program is too fast requesting it. I tried a System.Threading.Thread.Sleep(100); or so, but that didn't work. Here's my code, if that helps: if (!ReadProcessMemory(handle, new IntPtr(address), buffer, length, out bytesReadPtr)) { StringBuilder str = new StringBuilder(); str.Capacity = 1000; FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, IntPtr.Zero, GetLastError(), 0, str, 1000, IntPtr.Zero); throw new Exception("Unable to read process memory.\n" + str.ToString()); }
Anyone knows what's going on? :) Thanks in advance. (Btw. Can't I use decent tabs in code on this forum? :confused:)
modified on Sunday, March 2, 2008 12:03 PM