我正在尝试编写一个程序,它将挂接到应用程序启动并捕获命令行。不知道从哪里开始,因为我对 Windows 编程还很陌生。 将不胜感激任何帮助 谢谢
最佳答案
您没有提到您的首选编程语言,所以我将使用 C# 作为示例代码段。
您可以启动一个进程并捕获/写入其标准 IO 流。
以下片段打开一个进程并捕获其 StdOut 流:
using (var process = Process.Start(new ProcessStartInfo(FileName = @"yourExecutablePath", UseShellExecute = false, RedirectStandardOutput = true)))
using (var stdout = process.StandardOutput)
Console.WriteLine(stdout.ReadToEnd());
编辑 1
看起来您想要 Hook Windows API,例如 CreateProcess。
一种方法是编写内核驱动程序并使用 Hook 技术,例如 SSTD 修补。但是编写内核驱动程序 IMO 很麻烦。
在某些情况下,您可以使用用户级 Hook 。有一些库可能会帮助您,包括:EasyHook、Deviare 和 MS Detour。
编辑 2
您还可以将 WMI 用作 @David Heffernan
建议,但它只会在进程启动后之后通知您(与 Hook 相反, Hook 允许您在 Hook 函数被调用和/或覆盖之前之前运行一些任意代码函数调用):
using System.Management;
// Run this in another thread and make sure the event watcher gets disposed before exit
var start = new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"));
start.EventArrived += new EventArrivedEventHandler(delegate (object sender, EventArrivedEventArgs e) {
console.WriteLine("Name: {0}, Command Line: {1}", e.NewEvent.Properties["ProcessName"].Value, e.NewEvent.Properties["Commandline"].Value);
});
start.Start()
关于windows - 如何挂接到 Windows 中的应用程序和进程启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8157548/