开发者

Something like INotifyPropertyChanged when reading memory from unmanaged process

I am reading memory from other running process which source code I don't have by using

[DllImport("kernel32.dll")]
public static extern int ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, uint size, out IntPtr lpNumberOfBytesRead);

and everything works fine. I 开发者_如何学Cget the value I need, but now I want to implement something like INotifyPropertyChanged in my program, so when value in that unmanaged process changes, that my code registers it and updates my variable X.

So far I have

private string x;
public string X { get { return x; } }
....
Threading.Timer timerX = new Timer(x => UpdateX(), null, 0, 500)
....
private void UpdateX()
{
    //Read value from unmanaged process
    OnPropertyChanged("X");
}

which updates X by reading that unmanaged process memory every 500ms, but that feels like hacking. Also, memory in unmanaged process can change few times in 500ms or once in few minutes, so putting a less interval seems like overkill. Is there more elegant solution. Thanks.


No, there is no way to get a notification when memory in another process changes. If you were a debugger, you could use a CPU data breakpoint to hear about changes in a small set of places. But this would have a significant perf impact, and is only the right approach if you are actually debugging.

If the other process is something you own, then the best approach would be to disassemble and understand the code for other process, and add code of your own to send a notification. Working without source and making a small change is probably less hard than you think it is.

Martyn

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜