如何查找启动应用程序的cmd.exe进程ID?

发布于 2024-08-30 01:17:53 字数 286 浏览 4 评论 0原文

我有简单的 win 应用程序(使用 .net C# 编写),需要从 cmd.exe 启动,然后写入该控制台,但问题是我需要来自该 cmd.exe 进程的进程 ID。我可以获取所有正在运行的进程,

Process[] procList = Process.GetProcessesByName("cmd");

但是如何找到我的? :)

也许是为了读取输入,并检查里面是否写有“ApplicationName.exe”,或者获取当前活动的 cmd.exe 窗口?但有什么功能呢?

I have simple win application(writen using .net C#) that needs to be started from cmd.exe, and then write to that console, but problem is that i need process id from that cmd.exe process. I can get all running process,

Process[] procList = Process.GetProcessesByName("cmd");

but how to find my? :)

Maybe to read input, and check is "ApplicationName.exe" writen inside, or to get curently active cmd.exe window? But with what function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素食主义者 2024-09-06 01:17:53

即使你找到了“你的”cmd,你要如何写入它呢?

有一种更好的方法可以实现您所描述的目标。只需将您的项目编译为“控制台应用程序”,但无论如何都要让它创建一个 WinForms 窗口。是的,这是可能的,没有什么可以阻止你。然后您就可以使用 System.Console 类写入控制台,无需任何魔法。

Even if you find "yours" cmd, how are you going to write into it?

There is a better way to achieve what you describe. Just compile your project as "Console Application", but have it create a WinForms window anyway. Yes, it is possible, nothing stops you from it. Then you would be able to write to the console using your System.Console class, no magic required.

雨的味道风的声音 2024-09-06 01:17:53

以下代码创建一个 WQL 事件查询来检测新进程,等待新进程启动,然后显示有关该进程的信息:

WqlEventQuery query = new WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 1), "TargetInstance isa \"Win32_Process\"");
ManagementEventWatcher watcher = new ManagementEventWatcher(query);
ManagementBaseObject e = watcher.WaitForNextEvent();
Console.WriteLine("Process {0} has been created, path is: {1}", 
((ManagementBaseObject)e["TargetInstance"])["Name"],
((ManagementBaseObject)e["TargetInstance"])["ExecutablePath"]);
watcher.Stop();

我认为您可以通过检查此 WMI 代码示例找到问题的解决方案。希望有帮助。

The following code creates a WQL event query to detect a new process, waits for a new process to start and then displays the information about the process:

WqlEventQuery query = new WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 1), "TargetInstance isa \"Win32_Process\"");
ManagementEventWatcher watcher = new ManagementEventWatcher(query);
ManagementBaseObject e = watcher.WaitForNextEvent();
Console.WriteLine("Process {0} has been created, path is: {1}", 
((ManagementBaseObject)e["TargetInstance"])["Name"],
((ManagementBaseObject)e["TargetInstance"])["ExecutablePath"]);
watcher.Stop();

I think you may find a solution to your problem by examining this WMI code sample. Hope it helps.

白色秋天 2024-09-06 01:17:53

将此函数添加到项目的类中的某个位置:
(需要在类顶部使用 using System.Runtime.InteropServices;

    [DllImport("kernel32.dll")]
    private static extern bool AttachConsole(int dwProcessId);
    [DllImport("kernel32.dll")]
    private static extern bool FreeConsole();

然后调用 AttachConsole(-1)
现在您可以像往常一样使用 System.Console 类写入控制台并设置颜色等。
如果您不再需要写入控制台,请调用 FreeConsole() 将您的进程与其分离。

请注意,这实际上并不会阻止控制台处理进一步的命令。用户仍然可以随时在控制台中输入任何内容。

推荐的方法如下:

  1. 创建一个控制台应用程序,
  2. 向其中写入任何您想要的内容。
  3. 一旦不再需要,请调用 FreeConsole()
  4. 使用 Application.Run() 打开表单。不使用 application.Run 并仅显示表单不会创建正确的消息循环,并且可能会发生奇怪的事情。

您可以切换 3 和 4,但是一旦调用 Application.Run 方法,代码将不会继续,直到表单关闭。因此,要么写入表单中的控制台并从那里释放它,要么在单独的线程中生成表单(这会产生其他意想不到的副作用)。

您可以向 AttachConsole 函数提供任何进程 ID。 -1 默认为父进程,这很可能是您想要的。

Add this functions somewhere in a class to your project:
(requires a using System.Runtime.InteropServices; on top of the class)

    [DllImport("kernel32.dll")]
    private static extern bool AttachConsole(int dwProcessId);
    [DllImport("kernel32.dll")]
    private static extern bool FreeConsole();

Then call AttachConsole(-1)
Now you can use the System.Console class as usual to write to the console and set colors and whatnot.
If you no longer need to write to the console, call FreeConsole() to detach your process from it.

Be aware that this does not actually blocks the console from processing further commands. The user is still allowed to enter anything into the console at anytime.

The recommended way is as follows:

  1. Create a console application
  2. Write to it whatever you want.
  3. Call FreeConsole() once you no longer need it.
  4. Open your form using Application.Run(). Not using application.Run and only showing the form does not creates a proper message loop and strange things can happen.

You can switch 3 and 4, but as soon as you call the Application.Run method, the code will not continue until the form is closed. So either write to the console in the form and free it from there or spawn the form in a separate thread (which has other unintended side consequences)

You can supply any process ID to the AttachConsole function. -1 default to the parent process which is most likely what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文