简体   繁体   中英

C#: Fastest way to detect change of byte in memory of program?

I injected a DLL into a process with Easyhook and I can read a byte at a specific memory location with Marshal.ReadByte.

Now i want to be notified when that byte changes. What dou you think is the best way to do that when I want to detect a change as fast as possible?

Unfortunately, unless you are able to hook on to the function call that would modify the memory in question to tell you when the external process performed a change, your best option is to create a thread that just spins and checks the memory over and over for a new value.

I would recommend not making it "as fast as possible" as that is going to burn up a lot of CPU, you need to ask your self "what is the longest delay between a change and I get notified acceptable". If for example your answer to that was "0.1 seconds" your pooling loop would look like

private void PoolingLoop()
{
    var lastValue = Marshal.ReadByte(_location)
    while(_running)
    {
         Thread.Sleep(100);
         var newValue = Marshal.ReadByte(_location);

         if(lastValue != newValue)
             DoThisOnMemoryChanged();

         lastValue = newValue;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM