Win32 :: Daemon :: State()不返回状态

时间:2015-10-06 08:10:23

标签: perl winapi perl-module

从Dave Roth的“Win32 Perl脚本:管理员手册”中考虑这个骨架代码:

use Win32::Daemon;
Win32::Daemon::StartService();
while( SERVICE_STOPPED != ( $State = Win32::Daemon::State() ) )
{
   if( SERVICE_START_PENDING == $State )
   {

     # Initialization code
     Win32::Daemon::State( SERVICE_RUNNING );
   }
   elsif( SERVICE_PAUSE_PENDING == $State )
   {

     Win32::Daemon::State( SERVICE_PAUSED );
     next;
   }
   elsif( SERVICE_CONTINUE_PENDING == $State )
   {

     Win32::Daemon::State( SERVICE_RUNNING );
     next;
   }
   elsif( SERVICE_STOP_PENDING == $State )
   {

     Win32::Daemon::State( SERVICE_STOPPED );
     next;
   }
   elsif( SERVICE_RUNNING == $State)
   {
     #Add code here to perform whatever work the service must accomplish
   }
   sleep(5);
}
Win32::Daemon::StopService();

函数State()可用于设置服务的状态(如上面的代码片段所示),也可用于返回服务在当前时刻的状态。但是,当我尝试打印出$State数值时 - 我到目前为止看到的是0,1和4 - 就是我得到的。

我无法找到任何暗示这些状态是常量定义为0,1等的材料。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

我得到SERVICE_CONTROL_USER_DEFINED => 4144,。零似乎有一些可能的含义

NO_ERROR                              => 0,
SERVICE_BOOT_START                    => 0,
SERVICE_NOT_READY                     => 0,

所以它可能不是SERVICE_BOOT_START 0x00000000 A device driver started by the system loader. This value is valid only for driver services.

所以它可能意味着它的SERVICE_NOT_READY或NO_ERROR :) 见https://api.metacpan.org/source/JDB/Win32-Daemon-20131206/test/DumpConstants.pl

#!/usr/bin/perl --
use Win32::Daemon;
use Data::Dump qw/ dd /;
dd( {map{ my $v = eval { $_->() }; $_ => defined $v ? $v : qq{EEP($v): $@}; } @Win32::Daemon::EXPORT});
__END__
{
  NO_ERROR                              => 0,
  SC_GROUP_IDENTIFIER                   => "+",
  SERVICE_ACCEPT_DEVICEEVENT            => "EEP(): Goto undefined subroutine &AutoLoader::AUTOLOAD at .../site/lib/Win32/Daemon.pm line 123.\n",
  SERVICE_ACCEPT_HARDWAREPROFILECHANGE  => 32,
  SERVICE_ACCEPT_NETBINDCHANGE          => 16,
  SERVICE_ACCEPT_PARAMCHANGE            => 8,
  SERVICE_ACCEPT_PAUSE_CONTINUE         => 2,
  SERVICE_ACCEPT_POWEREVENT             => 64,
  SERVICE_ACCEPT_SESSIONCHANGE          => 128,
  SERVICE_ACCEPT_SHUTDOWN               => 4,
  SERVICE_ACCEPT_STOP                   => 1,
  SERVICE_AUTO_START                    => 2,
  SERVICE_BOOT_START                    => 0,
  SERVICE_CONTINUE_PENDING              => 5,
  SERVICE_CONTROL_CONTINUE              => 3,
  SERVICE_CONTROL_DEVICEEVENT           => 11,
  SERVICE_CONTROL_HARDWAREPROFILECHANGE => 12,
  SERVICE_CONTROL_INTERROGATE           => 4,
  SERVICE_CONTROL_NETBINDADD            => 7,
  SERVICE_CONTROL_NETBINDDISABLE        => 10,
  SERVICE_CONTROL_NETBINDENABLE         => 9,
  SERVICE_CONTROL_NETBINDREMOVE         => 8,
  SERVICE_CONTROL_NONE                  => -1,
  SERVICE_CONTROL_PARAMCHANGE           => 6,
  SERVICE_CONTROL_PAUSE                 => 2,
  SERVICE_CONTROL_POWEREVENT            => 13,
  SERVICE_CONTROL_PRESHUTDOWN           => "EEP(): Goto undefined subroutine &AutoLoader::AUTOLOAD at .../site/lib/Win32/Daemon.pm line 123.\n",
  SERVICE_CONTROL_RUNNING               => 4160,
  SERVICE_CONTROL_SESSIONCHANGE         => 14,
  SERVICE_CONTROL_SHUTDOWN              => 5,
  SERVICE_CONTROL_START                 => 4112,
  SERVICE_CONTROL_STOP                  => 1,
  SERVICE_CONTROL_TIMER                 => 4128,
  SERVICE_CONTROL_USER_DEFINED          => 4144,
  SERVICE_DEMAND_START                  => 3,
  SERVICE_DISABLED                      => 4,
  SERVICE_ERROR_CRITICAL                => 3,
  SERVICE_ERROR_NORMAL                  => 1,
  SERVICE_ERROR_SEVERE                  => 2,
  SERVICE_FILE_SYSTEM_DRIVER            => 2,
  SERVICE_INTERACTIVE_PROCESS           => 256,
  SERVICE_KERNEL_DRIVER                 => 1,
  SERVICE_NOT_READY                     => 0,
  SERVICE_PAUSE_PENDING                 => 6,
  SERVICE_PAUSED                        => 7,
  SERVICE_RUNNING                       => 4,
  SERVICE_START_PENDING                 => 2,
  SERVICE_STOP_PENDING                  => 3,
  SERVICE_STOPPED                       => 1,
  SERVICE_SYSTEM_START                  => 1,
  SERVICE_WIN32_OWN_PROCESS             => 16,
  SERVICE_WIN32_SHARE_PROCESS           => 32,
  USER_SERVICE_BITS_1                   => 16384,
  USER_SERVICE_BITS_10                  => 536870912,
  USER_SERVICE_BITS_2                   => 32768,
  USER_SERVICE_BITS_3                   => 4194304,
  USER_SERVICE_BITS_4                   => 8388608,
  USER_SERVICE_BITS_5                   => 16777216,
  USER_SERVICE_BITS_6                   => 33554432,
  USER_SERVICE_BITS_7                   => 67108864,
  USER_SERVICE_BITS_8                   => 134217728,
  USER_SERVICE_BITS_9                   => 268435456,
}