我试图编写一个程序,将挂钩到应用程序启动并捕获命令行。 不知道从哪里开始,因为我在Windows编程中非常绿色。 将不胜感激任何帮助谢谢
既然你没有指定一种语言,我使用C#示例代码:
你可以打开一个应用程序并挂接到它的StdOut
, StdIn
和StdErr
。 例如,要挂钩StdOut
,请在创建过程时设置RedirectStandardOutput = true
:
using (var process = Process.Start(new ProcessStartInfo(FileName = @"yourExecutablePath", UseShellExecute = false, RedirectStandardOutput = true))) using (var stdout = process.StandardOutput) Console.WriteLine(stdout.ReadToEnd());
不知道这是你想要的。 希望能帮助到你。
编辑1
我注意到你的问题的标题,似乎你想挂钩像CreateProcess()
某种类型的API。 根据你想要达到的目标,有很多种方法可以做到这一点。
你会编写一个内核驱动程序,并使用SSTD patching
等SSTD patching
方法,但编写内核驱动程序可能不是一件容易的事,而且这种方法经常用于安全应用程序,如反恶意软件。
在某些情况下,您可以使用用户级别的钩子,有许多库可以帮助您: EasyHook
, Deviare
, MS Detour
等
编辑2
您可以使用WMI
作为@David Heffernan
建议如果它适合您的需求:
using System.Management; // Run this in another thread and make sure you dispose the event watcher 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()