Windows 相当于 Linux“屏幕”或其他替代方案?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5058072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:56:15  来源:igfitidea点击:

Windows equivalent for Linux "screen" or another alternative?

windowslinuxgnu-screen

提问by

I'm on the lookout for a method to control programs in a Windows environment.

我正在寻找一种在 Windows 环境中控制程序的方法。

I would prefer it to be a bit similar to the Linux software "screen".

我希望它有点类似于 Linux 软件“ screen”。

The reason of my search is that I need to start a program (on Windows) with an identifier, so I later would be able to close that specific program, without closing anything else - even though the actual program (.exe file) is running multiple times. So is there anyway I can "name" it?

我搜索的原因是我需要使用标识符启动一个程序(在 Windows 上),所以我以后可以关闭该特定程序,而无需关闭任何其他程序 - 即使实际程序(.exe 文件)正在运行多次。那么无论如何我可以“命名”它吗?

I'm using the following on Linux now:

我现在在 Linux 上使用以下内容:

$ screen <params> <id> ./softwareprogram

And then I know I can always find it with it's ID :)

然后我知道我总能找到它的 ID :)

It's an external program which is closing or starting a 3rd party program.

这是一个正在关闭或启动第 3 方程序的外部程序。

My program is written in C++, if that does any difference.

我的程序是用 C++ 编写的,如果这有什么不同的话。

Edit: Can't I by any means not choose the ID myself? Like start the program with a specific name? Also how do I kill the process by providing a name?

编辑:我不能以任何方式不自己选择 ID 吗?喜欢用特定名称启动程序吗?另外,如何通过提供名称来终止进程?

回答by Christian

When you start the program, you can get its process id, even on windows. You can later use that process id to kill the program again.

当您启动程序时,即使在 Windows 上,您也可以获得它的进程 ID。您可以稍后使用该进程 ID 再次终止该程序。

If you need to give it your own id, then you can use e.g. the registry to store a mapping of your id to process id. If you receive a start command, store the identifier in the registry as key and the pid of the started process as value. If you receive a stop command, retrieve the pid from the registry and kill that pid and delete the key from the registry.

如果您需要给它自己的 id,那么您可以使用例如注册表来存储您的 id 到进程 id 的映射。如果您收到启动命令,请将标识符作为键存储在注册表中,并将启动进程的 pid 作为值存储。如果您收到停止命令,请从注册表中检索 pid 并终止该 pid 并从注册表中删除该键。

Instead of the registry you could of course also use a database or a folder with files where the key (your identifier) is the filename and the pid is inside each file

除了注册表之外,您当然也可以使用包含文件的数据库或文件夹,其中密钥(您的标识符)是文件名,而 pid 位于每个文件中

回答by CodesInChaos

While I have no clue what the linux screencommand does, why don't you just use the process-handle you get from CreateProcess?

虽然我不知道 linuxscreen命令是做什么的,但为什么不直接使用从中获得的进程句柄CreateProcess呢?

You get the process-id too, but its only valid as long as there are open handles to that process.

您也可以获得进程 ID,但它仅在该进程有打开的句柄时才有效。

The lpProcessInformationparameter will contain both the process-id and the process-handle.

lpProcessInformation参数将包含进程 ID 和进程句柄。

回答by Charles Grunwald

At the moment, I don't believe there's a native equivalent for screen. Cygwin contains an implementation of screen, but it comes with the overhead of cygwin. The problem is that Windows and Posix-based operating systems implement consoles in a very different way. Windows does not support many of the tty functionality that is part of the POSIX environment. That being said, it may be possible to write a minimal implementation of the functionality in the screen utility using the API calls: AttachConsoleand FreeConsole. I'd have to do a bit more research on the subject, however.

目前,我认为屏幕上没有原生的等价物。Cygwin 包含 screen 的实现,但它带有 cygwin 的开销。问题在于 Windows 和基于 Posix 的操作系统以非常不同的方式实现控制台。Windows 不支持作为 POSIX 环境一部分的许多 tty 功能。也就是说,可以使用 API 调用在屏幕实用程序中编写功能的最小实现:AttachConsoleFreeConsole。但是,我必须对该主题进行更多研究。