Skip to content
⌘ NSIS Forum Archive

NSIS Simple Service Plugin

142 posts

Speed78#

NSIS Simple Service Plugin

Hi,

I´ve create a service plugin. This plugin contains basic service functions like start, stop the service or checking the service status. It also contains advanced service functions for example setting the service description, changed the logon account, granting or removing the service logon privilege.

Here the function list:

SimpleSC::Install [name_of_service] [display_name] [start_type] [service_commandline] [dependencies] [account] [password]
SimpleSC::Remove [name_of_service]
SimpleSC::StartService [name_of_service]
SimpleSC::StopService [name_of_service]
SimpleSC::PauseService [name_of_service]
SimpleSC::ContinueService [name_of_service]
SimpleSC::RestartService [name_of_service]
SimpleSC::ExistsService [name_of_service]
SimpleSC::GetServiceDisplayName [name_of_service]
SimpleSC::GetServiceName [display_name]
SimpleSC::GetServiceStatus [name_of_service]
SimpleSC::SetServiceDescription [name_of_service] [service_description]
SimpleSC::SetServiceStartType [name_of_service] [start_type]
SimpleSC::SetServiceLogon [name_of_service] [account] [password]
SimpleSC::GrantServiceLogonPrivilege [account]
SimpleSC::RemoveServiceLogonPrivilege [account]
SimpleSC::ServiceIsPaused [name_of_service]
SimpleSC::ServiceIsRunning [name_of_service]
SimpleSC::ServiceIsStopped [name_of_service]
Here is the link to the wiki: http://nsis.sourceforge.net/NSIS_Simple_Service_Plugin

Any comments, suggestions and questions are welcome...

Best regards

Rainer
Nicolas Cuny#
Hi,

I just tested your plugin and was surprised to see that it doesn't need admin privileges to check the status of a service, contrary to the other available plugins. This was tested on Vista and XP Pro SP2.

This is very useful to me and I would like to know if this behaviour is intended and maybe a little explanation too 🙂

These are the scripts I used to double-check :

Name "simpleservice-test"
Outfile "simpleservice-test.exe"
ShowInstDetails show
RequestExecutionLevel user

Section -Main SEC0000
SetAutoClose false
SimpleSC::GetServiceStatus "W32Time"
pop $0
pop $1
DetailPrint $0
DetailPrint $1
SectionEnd
Name "nsscm-test"
Outfile "nsscm-test.exe"
ShowInstDetails show
RequestExecutionLevel user

Section -Main SEC0000
SetAutoClose false
nsSCM::QueryStatus "W32Time"
Pop $0
Pop $1
DetailPrint $0
DetailPrint $1
SectionEnd
!include "servicelib.nsh"

Name "servicelib-test"
Outfile "servicelib-test.exe"
ShowInstDetails show
RequestExecutionLevel user

Section -Main SEC0000
SetAutoClose false
!insertmacro SERVICE "status" "W32Time" ""
pop $0
DetailPrint $0
SectionEnd
And these are the results :

* simpleservice-test:
0
4

* nsscm-test:
error
1

* servicelib-test:
false
Thanks for the great job !
Nicolas Cuny#
I found out so far that :

- GetServiceStatus works with admin and user privileges
- ExistsService only works with admin privileges
- ServiceIsRunning works with both privileges, but documentation on the wiki is wrong : '1' means the service is running

This has been tested on Vista and XP.
Speed78#
Hi,

Originally posted by Nicolas Cuny
I found out so far that :

- ExistsService only works with admin privileges
- ServiceIsRunning works with both privileges, but documentation on the wiki is wrong : '1' means the service is running

This has been tested on Vista and XP.
I´ve changed the access flags for each functions so every function will be executed with the lowest privilege which is necessary. So I think ExistsService should now work with user privileges. A new version (1.01) of this plugin is available now.

I´ve changed the wrong Result-Description for the functions ServiceIsRunning, ServiceIsPaused and ServiceIsStopped.

Any comments, suggestions and questions are welcome...

Best regards

Rainer
psmika#
Hello,

I'm am currently building an installer for a service application and I am using this plugin to create the service itself.

I am able to create the service but in the service control manager there is no description. Also when the service starts a message is logged in the even viewer and then it says Service1 has been started even tho my service is called something else.
Most likely im doing something wrong, it would be nice if someone could point me in the right direction.

I have used the following command to add the service:
SimpleSC::InstallService "MyService" "My Service" "2" "$INSTDIR\bin\MyService.exe" "" "" ""

Thanks in advance for any help with this!

// Mika
Speed78#
Hi,

I am able to create the service but in the service control manager there is no description.
Thats correct. If you install the service the service description is not set. Please use the function "SetServiceDescription" to set a seperate description.

Also when the service starts a message is logged in the even viewer and then it says Service1 has been started even tho my service is called something else.
I think Service1 is your Display name, otherwise you can´t see it in eventlog. But I think your Key-Name (It is called [name_of_service] in plugin doc) is always the same. Don´t confuse Display-Name and Key-Name.

The Display-Name is a unique name you can see in the service controller at the column "Name".

The Key-Name is a unique name you can see if you show the properties of a service.

Best regards

Rainer
psmika#
Thank you so much for your response.

The SetServiceDescription function works like a charm!
I found out that the Service1 issue is a programming issue and it has nothing to do with this plugin.

Thank you for the fast response and for this plugin!
Andy1988#
I tried to use your dll.
I copied it into the Plugins folder in the folder where NSIS is installed, but the compiler always says that "SimpleSC::Install" is an illegal command.

Do I need to define somewhere in my NSIS Script that I want to use your dll? I was not able to find anything about this in the Wiki, this forum or with google.
Perhaps I'm blind 😉

Thank you for your help!
Speed78#
Hello Andy,

Originally posted by Andy1988
I tried to use your dll.
I copied it into the Plugins folder in the folder where NSIS is installed, but the compiler always says that "SimpleSC::Install" is an illegal command.

Do I need to define somewhere in my NSIS Script that I want to use your dll? I was not able to find anything about this in the Wiki, this forum or with google.
Perhaps I'm blind 😉
A good message to you: You are not blind 😉. That was my mistake. The documentation (in the readme.txt and on the wiki) was wrong about the name of the install and remove function. Only in the example it was correct. The function names are:
  • SimpleSC::InstallService
    SimpleSC::RemoveService


I´ve fixed it in the file and on the wiki.

Best regards

Rainer
Andy1988#
Originally posted by Speed78
Hello Andy,



A good message to you: You are not blind 😉. That was my mistake. The documentation (in the readme.txt and on the wiki) was wrong about the name of the install and remove function. Only in the example it was correct. The function names are:
  • SimpleSC::InstallService
    SimpleSC::RemoveService


I´ve fixed it in the file and on the wiki.

Best regards

Rainer
It's great to hear that there is nothing wrong with my eyes 😉
Now everything works like expected!

BTW: Great plugin! And thank you for your work and fast help!
mr.hanky#
Request

I think this is the best service plugin so far.

I do have one request. An option that will show the config info.
Like the qc option does for SC.exe in windows
qc--------------Queries the configuration information for a service.

The option I am looking for the most is to read BINARY_PATH_NAME

Thanks again for the great plugin
Chris
Speed78#
Hello Chris,

I will add a function like "GetServicePath" in the next release. I think I will release it next days.

Best regards

Rainer
lushdog#
When I do a SimpleSC::RemoveService yet the service is still in the Services list. If I try to restart the service I get a "The specified service has been marked for deletion."

The service is removed from the list upon the next reboot.

The only reason I ask is I'd like to let the user uninstall/reinstall without rebooting.

I believe the "sc delete" command removes the service without a reboot.

Does your plugin use "sc" or some other command?

Thanks in advance,

Matt
Speed78#
Hi lushdog,

Originally posted by lushdog
When I do a SimpleSC::RemoveService yet the service is still in the Services list. If I try to restart the service I get a "The specified service has been marked for deletion."

The service is removed from the list upon the next reboot.

The only reason I ask is I'd like to let the user uninstall/reinstall without rebooting.

I believe the "sc delete" command removes the service without a reboot.

Does your plugin use "sc" or some other command?

Thanks in advance,

Matt
The plugin uses the windows api functions. I think you have removed the service during your service list windows was opened!? If the service list is opened this is a default behaviour. Try to close the service list window and remove the service and then open the service list. You will see that the service is removed.

"sc" uses the same functions like my plugin so "sc" has the same behaviour.

Please remember: You should stop the service before you removing it.

Best Regards

Rainer
Speed78#
New Version 1.03

Hi,

I´ve released a new version of the NSIS Simple Service Plugin. The new version provides the function "SimpleSC::GetServiceBinaryPath" to get the binary path of a specified service.

You can find more informations and download file on the wiki page (http://nsis.sourceforge.net/NSIS_Simple_Service_Plugin).

If you have any questions please let me now.

Best regards

Rainer
Speed78#
New Version 1.04

Hi,

I´ve released a new version of the NSIS Simple Service Plugin. This is an bugfix release that concerns to the functions StartService, StopService, ContinueService and PauseService. It is recommend to use this version of the plugin because it is possible that using this functions ends in an endless loop. This happens for example if the service doesn´t start, stop aso. or e.g. the service crashes during startup (Function: StartService).

You can find more informations and download file on the wiki page (http://nsis.sourceforge.net/NSIS_Simple_Service_Plugin).

If you have any questions please let me now.

Best regards

Rainer
jpderuiter#
Hello Rainer,

this is a great plugin.

I have a request:
Could you add an option to SimpleSC::StopService to stop dependant services first?
(Like the DOS command "net stop {ServiceName} /y".)
Have a look at http://support.microsoft.com/kb/245230 for an idea.

Thanks in advance,
JP
Speed78#
New Version 1.05

Hi,

I´ve released a new version of the NSIS Simple Service Plugin. The functions SimpleSC::StopService and SimpleSC::RestartService are now improved. Now all dependent services are stopping recursively too. You can find more informations and download file on the wiki page (http://nsis.sourceforge.net/NSIS_Simple_Service_Plugin).

If you have any questions please let me now.

Best regards

Rainer
Afrow UK#
It would be very handy if you could add support for enabling desktop interaction when installing a service. This is ideal for Microsoft's srvany.exe executable which you can use to run any Windows executable as a service (silly enough though, the app that comes with it, instsrv.exe does not allow you to enable desktop interaction!)

Stu
Speed78#
Hello Stu,

I will add the feature in the next release. This feature will only run on systems older than Windows Vista. Vista doesn´t support desktop interaction anymore (See: http://msdn2.microsoft.com/en-us/library/ms683502(VS.85).aspx)

Best regards

Rainer
bradkohl#edited
Could you add the ability to start a service with a parameter? I tried adding my param to the end of the binary path and that didn't work.

So far I like the plugin a lot, my only complaint is that it's a little bigger than other options like nsSCM and the ExistsService function seems to have odd return values compared to the other functions, the return value(1) seems more like a success case with that one, as opposed to the others which return (0) on success.
Speed78#
Originally posted by bradkohl
Could you add the ability to start a service with a parameter? I tried adding my param to the end of the binary path and that didn't work.
What exactly did you mean? If you mean the "InstallService" function then you can use your parameter in the "[service_commandline]" parameter.

For example:

SimpleSC::InstallService "MyService" "My Service Display Name" "2" "C:\MyPath\MyService.exe /process=server" "" "" ""
Pop $0 ; return error(1)/success(0)

If you mean a start parameter in the "StartService" function then you are right. At the moment it is not implemented yet. If you need this please let me now and I will add this functionality.

[i]
So far I like the plugin a lot, my only complaint is that it's a little bigger than other options like nsSCM
[/B]
I think this is not really a problem. This plugin provides more functions and the harddisk are very cheap.

[i]
The ExistsService function seems to have odd return values compared to the other functions, the return value(1) seems more like a success case with that one, as opposed to the others which return (0) on success.
[/B]
Yes, you are right. The reason for this is that the function returns "True" or "False" - "True" is 1 and "False" is 0. I think its ok because it´s documented in the wiki.

Best regards

Rainer
bradkohl#
thanks for the reply, you're right the added functionality and flexibility does make up for the size, and yes I was talking about service 'start' parameters. It would be great if you could add them but in the mean time, I've hardcoded the param I needed in the source.
Speed78#
New Version 1.10

Hi,

I´ve released a new version of the NSIS Simple Service Plugin. The changes are:

- SimpleSC::InstallService supports now more than one dependencies (delimitter is the forward slash).
- SimpleSC::InstallService supports now more service types e.g. to create an interactive service.
- SimpleSC::StartService supports now arguments.

Note: The signature of the functions SimpleSC::InstallService and SimpleSC::StartService has been changed. If you update from a previous version you must check your parameters.

You can find more informations and download file on the wiki page (http://nsis.sourceforge.net/NSIS_Simple_Service_Plugin).

If you have any questions please let me now.

Best regards

Rainer
pcrowley#
One request

I tried using this and got a consistent failure on creating a service. I need to create a kernel driver service which is a little different - the type is 1 rather than 16.

Unfortunately, there doesn't seem to be a good way to get at the error returned. I might have been able to do a call with System to do a GetLastError, or maybe not. It would be very helpful to be able to get the reason for a failure. Even a new function like SimpleSC::GetError would be helpful.