终止由CreateProcess启动的控制台应用程序. [英] Terminating a Console App started by CreateProcess.

查看:84
本文介绍了终止由CreateProcess启动的控制台应用程序.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于控制台的应用程序,它既可以作为Windows服务运行,也可以作为控制台应用程序运行,但是也可以由其他应用程序使用带有隐藏控制台窗口的CreateProcess生成. 如果我想干净地终止应用程序,则可以停止服务(作为服务运行时),可以在控制台窗口中键入CTRL + C并捕获SIGINT信号(作为控制台时),还可以安装ControlHandler和将控制台关闭(作为控制台时),但是在使用CreateProcess生成控制台时,我找不到干净地终止它的可靠方法.
从我阅读的文档中,:: TerminateProcess API发送了一个WM_CLOSE,显然控制台应用程序会忽略它,并且有人建议在控制台应用程序中安装一个事件泵来处理WM_CLOSE.
另外,iv听说使用FindWindow,然后发布WM_CLOSE.

请建议...

I have a console-based application, it may be run as a windows service or as a console application, but it also may be spawned by another application using CreateProcess with hidden console Window.
If I want to terminate cleanly the application, I can stop the service (when running as service), I can type CTRL+C in the console window and trap the SIGINT signal (when as a console), I can also install a ControlHandler and trap the console close (when as a console), but when spawning it with CreateProcess, I cannot find a reliable way to terminate it cleanly.
From the documents I read, the ::TerminateProcess API sends a WM_CLOSE , that is obviously ignored by a console application, and there are people out there suggesting to install an event pump inside the console application to handle WM_CLOSE.
Also, iv heard of using FindWindow & then post a WM_CLOSE.

Please suggest...

推荐答案



有很多方法可以做到这一点.您可以使用 GenerateConsoleCtrlEvent函数 [ ^ ]将信号发送到控制台应用程序.如果实现控制处理程序,则可以捕获信号并正确终止.

所有其他方法(例如线程消息)都是仅仅是黑客 [ ^ ].

最好的祝福,
-丑陋的黑客
Hi,

There are many ways to do this. You could use the GenerateConsoleCtrlEvent function[^] to send a signal to the console application. If you implement a control handler.. you could catch the signal and properly terminate.

All of the other methods such as thread messages are nothing more than a hack[^].

Best Wishes,
-Ugly Hack


来自( 但是,只需终止由CreateProces()API创建的某个进程,您可以使用其最后一个参数LPPROCESS_INFORMATION lpProcessInformation( TerminateProcess函数 [ ^ ])
From (Terminating a Process[^]) "The exit code for a process is either the value specified in the call to ExitProcess or TerminateProcess...". Of course, the preferred way is to use ExitProcess() function.
But to simply terminate a certain process created by CreateProces() API you can use its last parameter LPPROCESS_INFORMATION lpProcessInformation (CreateProcess function[^]) to get a handle to the created process. Then, call TerminateProcess()(TerminateProcess function[^])


调用TerminateProcess(handleFromCreateProcess,&exitcode)
确保等待,因为TerminateProcess在启动终止后立即返回.您需要调用WaitForSingleObject函数以确保进程已终止.
call TerminateProcess(handleFromCreateProcess,&exitcode)
make sure to wait because TerminateProcess returns immediately after initiating termination. you need to call WaitForSingleObject function to make sure process is terminated.


这篇关于终止由CreateProcess启动的控制台应用程序.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆