Search code examples
perlwinapiperl-module

Win32::Daemon::State() does not return the state


Consider this skeleton code from Dave Roth's "Win32 Perl Scripting: The Administrator's Handbook":

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();

The function State() can be used to set the status of the service (as is done in the above code snippet), and also to return the service's state at the current instant. However, when I try to print out $State, numerical values - I've seen 0, 1 and 4 so far - are what I get.

I have been unable to find any material that suggests that these states are constants defined as 0, 1, etc. anywhere. Could anyone point me in the right direction?


Solution

  • I get SERVICE_CONTROL_USER_DEFINED => 4144,. Zero seems to have a few possible meanings

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

    So its probably not SERVICE_BOOT_START 0x00000000 A device driver started by the system loader. This value is valid only for driver services.

    So it probably means its SERVICE_NOT_READY or NO_ERROR :) See https://api.metacpan.org/source/JDB/Win32-Daemon-20131206/test/DumpConstants.pl

    And also

    #!/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,
    }