我已经搜索了几个小时并尝试了我找到的所有内容,但它似乎不起作用。
我的代码是:
private static long _i;
private static readonly System.Timers.Timer Timer = new System.Timers.Timer(1000);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll")]
private static extern int GetWindowModuleFileName(IntPtr hWnd, StringBuilder text, int count);
static void Main()
{
Timer.Elapsed += Timer_Elapsed;
Timer.AutoReset = true;
Timer.Start();
while (Timer.Enabled)
Console.ReadLine();
}
static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("-|- - " + _i + " - -|-");
Console.WriteLine("WindowHandle: " + GetForegroundWindow());
var buff = new StringBuilder(2048);
GetWindowModuleFileName(GetForegroundWindow(), buff, 1024);
Console.WriteLine("ModuleName: " + Path.GetFileName(buff.ToString()));
Console.WriteLine("-|- - | - -|-");
_i++;
}
这段代码的输出总是这样的:
-|- - 1 - -|-
WindowHandle: 8128962
ModuleName: ConsoleApplication.vshost.exe
-|- - | - -|-
但即使我针对 Chrome 或 Fiddler 等其他应用程序,它总是会打印出我的可执行文件名或根本没有文件名。有没有我可能遗漏的问题?