代码之家  ›  专栏  ›  技术社区  ›  gregmac

是否可以为Windows服务启动失败提供自定义错误消息?

  •  0
  • gregmac  · 技术社区  · 5 年前

    我有一个Windows服务,它有几个故障条件阻止它启动,每个都有唯一的退出代码。我想知道是否有一种方法可以向用户显示一些有用的东西。

    作为测试,我有一个服务:

    internal class MyServiceName : ServiceBase
    {
        protected override void OnStart(string[] startArgs)
        {
            // always fail, for demo purposes
            ExitCode = 164; 
            Stop();
        }
    }
    

    开始的时候,因为 164 standard System Error Code ,Windows显示相应的内置消息:

    Windows could not start the service: Error 164: No more threads can be created in the system

    如果我使用的代码不是Windows使用的,比如 0x8744 ( 34628 ),我得到一个显示整数和十六进制值的一般错误消息:

    Windows could not start the service: Error 34628: 0x8744

    net start myservicename 它显示了以下内容:

    The MyServiceName service is starting.
    The MyServiceName service could not be started.
    
    A system error has occurred.
    
    System error 34628 has occurred.
    
    The system cannot find message text for message number 0x8744 in the message file for BASE.
    

    • 有没有办法为这个服务提供一个“消息文件”,这样我就可以设置有用的消息?
    • 我可以覆盖“标准”代码(如164)的消息吗?
    0 回复  |  直到 5 年前