使用Delphi的ShellExecute()与继承原始控制台的进程?

发布于 2024-08-27 16:41:00 字数 398 浏览 3 评论 0原文

在 C 中,我之前在控制台应用程序中使用过 system() 函数,如果我使用 system() 启动另一个进程,它会继承调用该函数的进程的控制台窗口它。

在 Delphi 中,system() 不存在,因此我使用 ShellExecute() 创建一个新进程,但新进程出现在新的控制台窗口中。有什么方法可以让它继承调用它的窗口的句柄吗?

我曾经

function GetConsoleWindow(): HWND; stdcall; external 'kernel32.dll';

获取控制台窗口并将其传递到 ShellExecute()HWND 部分,但这不起作用。

In C I've used the system() function before in a console application and if I start another process using system() it inherits the console window of the process that called it.

In Delphi system() doesn't exist so I'm using ShellExecute() to create a new process, but the new process comes up in a new console window. Is there some way that I can make it inherit the handle of the window that's calling it?

I've used

function GetConsoleWindow(): HWND; stdcall; external 'kernel32.dll';

to get the console window and passed it in the HWND part of ShellExecute(), but that didn't work.

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

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

发布评论

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

评论(1

西瓜 2024-09-03 16:41:00

使用 ShellExecute() 您将无法使生成的应用程序使用相同的控制台。 ShellExecute() 调用中的 HWND 元素已记录:

指定父窗口。该窗口接收应用程序生成的任何消息框。例如,应用程序可以通过生成消息框来报告错误。

所以它不会对控制台应用程序产生任何影响。

如果您使用 CreateProcess() 相反,您对生成的进程有更多的控制权。通过使用 dwCreationFlags 参数,您可以强制新进程获取自己的控制台(使用 CREATE_NEW_CONSOLE 标志),但默认情况下它也会继承父进程的控制台。

Using ShellExecute() you won't be able to make the spawned application use the same console. The HWND element in the ShellExecute() call is documented:

Specifies a parent window. This window receives any message boxes that an application produces. For example, an application may report an error by producing a message box.

so it won't have any effect for console applications.

If you use CreateProcess() instead you have much more control over the spawned process. By using the dwCreationFlags parameter you can force the new process to get its own console (use the CREATE_NEW_CONSOLE flag), but per default it will inherit the console of the parent process as well.

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