gpt4 book ai didi

com - 我如何拒绝 ATL 7 中的 Windows "Service Stop"请求?

转载 作者:行者123 更新时间:2023-12-04 17:07:10 25 4
gpt4 key购买 nike

我有一个基于 ATL 7 的 CAtlServiceModuleT 类的 Windows 服务。此服务为系统上的各种应用程序使用的 COM 对象提供服务,如果在这些应用程序仍在运行时停止该服务,这些其他应用程序自然会开始出错。

我知道如果 CComModule 的 GetLockCount() 返回 0,ATL DLL 通过在 DllCanUnloadNow() 中返回 S_OK 来解决这个问题。也就是说,它会检查以确保没有人当前正在使用 DLL 提供的任何 COM 对象。我想要服务中的等效功能。

这是我在覆盖 CAtlServiceModuleT::OnStop() 时所做的:

void CMyServiceModule::OnStop()
{
if( GetLockCount() != 0 ) {
return;
}

BaseClass::OnStop();
}

现在,当用户尝试从“服务”面板停止服务时,他们会看到一条错误消息:

Windows could not stop the XYZ service on Local Computer.The service did not return an error. This could be an internal Windows error or an internal service error.If the problem persists, contact your system administrator.

停止请求确实被拒绝了,但它似乎使服务处于错误状态。第二个停止请求导致此错误消息:

Windows could not stop the XYZ service on Local Computer.

Error 1061: The service cannot accept control messages at this time.

有趣的是,这次服务确实停止了(尽管我不希望它停止,因为仍然有未完成的 COM 引用)。

我有两个问题:

  1. 服务在被询问时拒绝停止是否被视为不良做法?
  2. 是否有一种礼貌的方式来表示停止请求被拒绝?不会将服务置于不良状态的?

最佳答案

你不能这样做。一旦 SCM 向您的服务发送 SERVICE_CONTROL_STOP,您就必须停止。

如果您的其他应用也是服务,您可以使它们成为 SCM 中的依赖项。当然,如果使用该服务的应用只是普通应用,则不能使用。

关于com - 我如何拒绝 ATL 7 中的 Windows "Service Stop"请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2601756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com