DrWhy has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Disciples,

Win32::Service lets you start, stop and otherwise control an existing Windows service, but it doesn't let you reconfigure it (e.g. change the account that the service runs under). Win32::Daemon::Simple let's your Perl script be a windows service which can reconfigure itself.

I have a C++ program that is installed as a service and I need to reconfigure it -- specifically, I need to change the account the service runs under; it would be handy to be able to control whether the service is 'automatic' or 'manual', but that is a secondary concern. Does anyone know of a way to do that from inside a Perl script?

Update: I just found the 'sc' command line tool (for XP, don't know about other Windows OS's) that lets you do this. So this can be done in a perl script using system calls to 'sc'. I still wonder if there's a more Perlish way to accomplish this.

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

  • Comment on Reconfigure an existing windows service

Replies are listed 'Best First'.
Re: Reconfigure an existing windows service
by BrowserUk (Patriarch) on Sep 07, 2005 at 21:07 UTC

    To change a service configuration you need ChangeServiceConfig() or ChangeServiceConfig2().

    Look at the lpServiceStartName parameter to set the user account and dwStartType for auto/manual etc.

    There shouldn't be too much problem with accessing this via Win32::API.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
Re: Reconfigure an existing windows service
by PodMaster (Abbot) on Sep 07, 2005 at 21:13 UTC
    Reading the source reveals what was learned from MSDN:
    http://search.cpan.org/src/JENDA/Win32-Daemon-Simple-0.2.6/Simple.pm
    # parameters use Win32::Registry; if ($Win32::Registry::VERSION < 0.08 and !defined($Win32::Registry2::V +ERSION)) { die "Please update your Win32::Registry to 0.08 or newer or instal +l the patch from http://jenda.krynicky.cz/#Win32::Registry2\n\n"; } my $ServiceKey; { my $paramkey; sub ReadParam { my ( $param, $default) = @_; $ServiceKey = $HKLM->Open('SYSTEM\CurrentControlSet\Services\\ +'.$svcid) unless $ServiceKey; return $default unless $ServiceKey; $paramkey = $ServiceKey->Create('Parameters') unless $paramkey; my $value = $paramkey->GetValue($param); $value = $default unless defined $value; return $value; } sub SaveParam { my ( $param, $value) = @_; $ServiceKey = $HKLM->Open('SYSTEM\CurrentControlSet\Services\\ +'.$svcid) unless $ServiceKey; return unless $ServiceKey; # do not save anything if the servi +ce is not installed $paramkey = $ServiceKey->Create('Parameters') unless $paramkey; die "Can't open/create the Parameters subkey in the service se +ttings!\n" unless $paramkey; # do not save anything if the service is + not installed if (!defined $value) { $paramkey->DeleteValue($param); } elsif ($value =~ /^\d+$/) { $paramkey->SetValues($param, REG_DWORD, $value); } else{ $value =~ s/\r?\n/\r\n/g; $paramkey->SetValues($param, REG_SZ, $value); } } }
    It would make sense for those two functions to end up in Win32::Service (with appropriate changes of course).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.