I have a bunch of win services written in .NET that use same exact executable with different configs. All services write to the same log file. However since I use the same .exe the service doesn't know its own service name to put in the log file.
Is there a way my service can programatically retrieve its own name?
I use this function in VB
Insight can be gained by looking at how Microsoft does this for the SQL Server service. In the Services control panel, we see:
Service name: MSSQLServer
Path to executable: "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn\sqlservr.exe" -sMSSQLSERVER
Notice that the name of the service is included as a command line argument. This is how it is made available to the service at run time. With some work, we can accomplish the same thing in .NET.
Basic steps:
Install/uninstall commands
To install the service (can omit /Name to use DEFAULT_SERVICE_NAME):
To uninstall the service (/Name is never required since it is stored in the stateSaver):
Installer code sample:
Main code sample: