I just finished running a script as a service. But it does not do anything when I run it as a service (but does the job if called directly with perl). Does anyone know why?
I used the following for adding it:
use strict;
use Win32::OLE;
use Config;
use File::Basename;
my $szPerlScript = shift @ARGV;
my $szServiceName = shift @ARGV;
my $szSrvAnyPath = shift @ARGV;
unless (-e $szPerlScript) {
die "Please give me a script\n";
}
unless (-e $szSrvAnyPath) {
die "srvany.exe not found\n";
}
unless ($szServiceName) {
$szServiceName = basename($szPerlScript);
}
my $szComputer = '.';
my $szPerlPath = $Config{perlpath};
$szPerlScript =~ s/\//\\/g;
$szSrvAnyPath =~ s/\//\\/g;
$szPerlPath =~ s/\//\\/g;
use constant HKLM => 0x80000002;
# Service Type
use constant KERNEL_DRIVER => 1;
use constant FS_DRIVER => 2;
use constant ADAPTER => 4;
use constant RECOGNIZER_DRIVER => 8;
use constant OWN_PROCESS => 16;
use constant SHARE_PROCESS => 32;
use constant INTERACTIVE_PROCESS => 256;
my $INTERACT_WITH_DESKTOP = 0;
my $_NOTIFED;
# Error Control
use constant NOT_NOTIFIED => 0;
use constant USER_NOTIFIED => 1;
use constant SYSTEM_RESTARTED => 2;
use constant SYSTEM_STARTS => 3;
# Create Service
my $hWMI = Win32::OLE->GetObject('winmgmts:\\\\'.$szComputer.'\\root\\
+cimv2');
my $hService = $hWMI->Get('Win32_Service');
my $wResult = $hService->Create($szServiceName, $szServiceName, $szSrv
+AnyPath, OWN_PROCESS, !$_NOTIFED, 'Automatic', $INTERACT_WITH_DESKTOP
+, 'NT AUTHORITY\\LocalService', '');
if ($wResult > 0) {
print "Error creating service $szServiceName: $wResult\n";
exit 0;
}
else {
print "Successfully created service $szServiceName: $wResult\n";
}
# Create Registry Entry
my $szKeyPath = 'SYSTEM\\CurrentControlSet\\Services\\'.$szServiceName
+.'\\Parameters';
my $hReg = Win32::OLE->GetObject('winmgmts:\\\\'.$szComputer.'\\root\\
+default:StdRegProv');
$hReg->CreateKey(HKLM, $szKeyPath);
$hReg->SetStringValue(HKLM, $szKeyPath, 'Application', $szPerlPath);
$hReg->SetStringValue(HKLM, $szKeyPath, 'AppParameters', $szPerlScript
+);
print "Created registry values\n";
# Start Service
$hService = $hWMI->Get('Win32_Service.Name=\''.$szServiceName.'\'');
$wResult = $hService->StartService();
if ($wResult > 0) {
print "Error starting service $szServiceName: $wResult\n";
}
else {
print "Successfully started service $szServiceName\n";
}
The scripts I tested are:
my $szPath = 'C:\winzip.log';
while (1) {
unlink $szPath;
select(undef, undef, undef, 60);
}
and
use Wx qw(:everything);
use Wx::Locale qw(:default);
use Wx::Event qw(:everything);
my $hApp = Handler->new();
$hApp->MainLoop();
package Handler;
use strict;
use Wx qw(:everything);
use Wx::Event qw(EVT_TIMER);
use base qw(Wx::App);
sub new {
my ($class) = @_;
my $this = $class->SUPER::new();
return $this;
}
sub OnInit {
my ($this) = @_;
$this->{hWnd} = Wx::MessageDialog->new(undef, 'Timer Message');
$this->{wId} = Wx::NewId();
$this->{hTimer} = Wx::Timer->new($this, $this->{wId});
$this->{hTimer}->Start(1000);
EVT_TIMER($this, $this->{wId}, \&OnTimer);
return 1;
}
sub OnTimer {
my ($this, $hEvent) = @_;
# Stop Timer while processing
$this->{hTimer}->Stop();
if (-e 'c:/winzip.log') {
unlink 'c:/winzip.log';
}
$this->{hWnd}->ShowModal();
# Restart Timer for more
$this->{hTimer}->Start(1000);
}
Any suggestions what I have to do to make the scripts do what I want them to do (as Service), as they work when executed directly? Does anyone know some good references on Windows Services and Perl Scripts (just ordered "Windows XP Cookbook", but will be delivered next week)?
Used: Perl 5.8.8 on Windows XP SP2.