Deviare2 是 MS Windows 上的专业 API Hook 库。它易于使用且功能强大。但是当我想WriteFile
使用 C# 虚拟编写器进行挂钩时,我发现它被挂钩WriteFile
了两次。我尝试使用 API Monitor 来挂钩 dummy writer,我发现 API Monitor 只是为每个 write 调用挂钩一个。那很奇怪!
挂钩代码:
spyMgr = new NktSpyMgr();
NktProcess _process = GetProcess("DummyWriter.exe");
while (_process == null)
{
Console.WriteLine("wait for process start...");
System.Threading.Thread.Sleep(10);
_process = GetProcess("DummyWriter.exe");
}
hookDllGetClassObj = spyMgr.CreateHook("kernel32.dll!WriteFile", (int)(eNktHookFlags.flgOnlyPreCall));
hookDllGetClassObj.Attach(_process, true);
hookDllGetClassObj.Hook(true);
hookDllGetClassObj.OnFunctionCalled += OnDllGetClassObjectCalled;
虚拟写:
string key = "";
Task.Factory.StartNew(() => {
int index=1;
while (key == "")
{
using (StreamWriter sw = new StreamWriter("d:\\dummy.txt",true))
{
string str = string.Format("{0}:oh gotcha!", index);
Console.WriteLine(str);
sw.WriteLine(str);
index++;
}
Thread.Sleep(500);
}
});
key = Console.ReadLine();