如何在Windows上向正在运行的服务发送信号并使用C++覆盖默认处理?

huangapple go评论77阅读模式
英文:

How to send a signal to a running service on Windows and override default handling with c++?

问题

我知道在Windows上信号不存在,但这个词被用来表示没有更好的词汇。

我看过很多类似的帖子,但没有一个提出不涉及第三方软件的解决方案。

我想知道是否有办法使用原生的Windows命令行工具向正在运行的Windows服务发送某种“信号”,并且可以在服务的C++源代码中处理(该源代码是多线程的)。

我已经查看过 SetConsoleCtrlHandlerGenerateConsoleCtrlEvent,但CTRL+Break、CTRL+C等仅在终端中与正在运行的程序进行交互模式时有效。我正在寻找一种方法,可以在一个空白的终端中通过命令发送信号给正在运行的服务。

由于C++的标准信号库不支持多线程,所以无法使用。

英文:

I know signals do not exist on Windows, but the word was used for lack of a better one.

I have seen a lot of similar posts, but none of them propose a solution that does not involve a third party software.

I am curious if there is a way to send some sort of "signal" to a running Windows service with vanilla Windows command line tools, which can be handled in the service's c++ source code (which is multithreaded).

I have looked at SetConsoleCtrlHandler and GenerateConsoleCtrlEvent, but the CTRL+Break, CTRL+C, etc. only work in interactive mode with the running program in the terminal. I am looking for something that I can open a blank slate terminal, and send a signal via a command to a running service.

C++'s standard signal library won't work due to it lacking support for multithreading.

答案1

得分: 2

The builtin command-line sc control 可以向服务发送简单的用户定义控制代码。服务的 Handler/Ex 回调可以查找这些代码。

否则,可以让服务创建一个IPC(进程间通信)服务器来监听,比如一个套接字或命名管道。然后,可以让编译的控制台EXE或PowerShell脚本等连接到该服务器,并根据需要向其发送数据。

英文:

The builtin command-line sc control can send simple user-defined control codes to a service. The service's Handler/Ex callback can look for those codes.

Otherwise, have the service create an IPC (interprocess communication) server to listen to, such as a socket or named pipe. You can then have a compiled console EXE, or a Powershell script, etc connect to that server and send data to it as needed.

huangapple
  • 本文由 发表于 2023年3月9日 22:47:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定