locked
How can I define startup parameters for a windows service while installing it? RRS feed

  • Question

  • Hi Everybody!
    I got a problem with installing a windows service. How can I define startup parameters for a windows service while installing it?

    Here is the detailed story which I think that it would be helpful for you help :-)
    I wrote and installed my custom windows service. I didn’t use the right mouse click to create a service installer instead I derived it from the “Installer” class and wrote it myself. Now I need to use my service sometimes as a windows application. In the main Main() method of my application I made a check for parameters. If there is a parameter called “-ui” it works as a Windows application. If there is not a parameter it checks if it’s already installed into the windows services or not. If not installed then it installs itself and runs. The service/windows application runs well. Now I just want it to run it without a parameter as a Windows application and with a parameter as a service (the total opposite as I am using it right now). So I changed my Main() method for the parameters. With a double click on the exe it runs as windows application. For starting the service now the application is expecting a parameter called “-service” from the command line. The service must also be notified that it can take parameters. By double clicking in the “services” window we can see “Path to executable" (which is the “ImagePath” key in the registry). I modified the value of the “ImagePath” key by hand and the service worked well. Now I want to skip the editing the registry by hand part and automate it. First I tried add the parameter into the assembly location.

    this.Context.Parameters["assemblypath"]= Assembly.GetEntryAssembly().Location + " -service";

    this modification truly changed the see “Path to executable" of my service in the services window and and “ImagePath” in the registry like this:

    “myPath\MyService.exe –service”

    but the funny thing was that my service stoped working after the modification. The reason that it was working when I modified the registry by hand was that the ”-service” parameter wasn’t in the double quotes. Instead it was like this.
    “myPath\MyService.exe’ –service
    I checked several web sites to figure out how to define startup parameters into a windows service and I always came to a code which modified the registry key “ImagePath”. like this

                using (RegistryKey system = Registry.LocalMachine.OpenSubKey("System"))
                {
                    if (system == null)
                        return;

                    using (RegistryKey currentControlSet = system.OpenSubKey("CurrentControlSet"))
                    {
                        if (currentControlSet == null)
                            return;

                        using (RegistryKey services = currentControlSet.OpenSubKey("Services"))
                        {
                            if (services == null)
                                return;

                            using (RegistryKey service = services.OpenSubKey("MyService"))
                            {
                                string imagePath = service.GetValue("ImagePath") as string;
                                service.SetValue("ImagePath", imagePath + " -service");
                                service.Close();
                            }
                            services.Close();
                        }
                        currentControlSet.Close();
                    }
                    system.Close();
                }

    But now I got permission issues to write to the registry. How can I solve this problem. Because I can normally write to the registry otherwise it would not write to it in the first place.
    In the end thank you in advance…
    Sincerely
    Mehmet Rasim Inceoglu

    • Moved by YiChun Chen Wednesday, March 4, 2009 8:11 AM Deployment issue (Moved from .NET Framework Setup to ClickOnce and Setup & Deployment Projects)
    Monday, March 2, 2009 6:10 PM

Answers

  • Service paths are quoted in Windows for a good reason related to security, and undoing that will open that security hole again, and some versions of Windows might be enforcing that service paths are in quotes.

    I'm not quite sure what your final question means. Are you saying that during the install you don't have permission to write to that part of the registry?
    Phil Wilson
    • Marked as answer by Kira Qian Monday, March 9, 2009 9:32 AM
    Wednesday, March 4, 2009 8:48 PM

All replies

  • Hi Arborinfelix,

    I am moving this thread from Base ".Net Framework Setup" forum to the "ClickOnce and Setup & Deployment Projects" forum, since the issue is related to deployment. There are more deployment experts in the "ClickOnce and Setup & Deployment Projects" forum.

    Thanks


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, March 4, 2009 8:10 AM
  • Service paths are quoted in Windows for a good reason related to security, and undoing that will open that security hole again, and some versions of Windows might be enforcing that service paths are in quotes.

    I'm not quite sure what your final question means. Are you saying that during the install you don't have permission to write to that part of the registry?
    Phil Wilson
    • Marked as answer by Kira Qian Monday, March 9, 2009 9:32 AM
    Wednesday, March 4, 2009 8:48 PM
  •  

    Hi Phil and thank you for your post.

    My problem was something like this: My service installer was able to modify the registry but when I tried I wasn't. But it was the same code with the same privileges accessing the registry. In the end it became clear that it was related to something I forgot while editing the registry. I solved my problem by adding the "true" flag into the line below to open the key of my registry for editing. in the end it was not the user right issue in the first place...

    using (RegistryKey service = services.OpenSubKey(KmsService.SERVICE_NAME,true))

    Before I saw your reply I was happy with my solution. But now you tell me that this solution might be not secure because of the path outside the double quotes. Is there something that I can pass my parameter into my service and meanwhile having no security issues? Or does the windows service installer have some methods to install my service with parameters so that I don't need to edit my registry with this block of code.

    Thank you for your time and reply...

    Sincerely
    Mehmet Rasim Inceoglu

    • Edited by arborinfelix Monday, March 9, 2009 6:25 PM misstyping
    Monday, March 9, 2009 3:55 PM
  • This is an old quote from the MSI team describing the issue:

    Carolyn_MSFT : A: There is a known issue relating to Windows Installer installation of services where
    the binary path of the service is not quoted if it contains a space. When installing services the
    binary path of the service should be quoted if it contains a space. If that doesn't happen, then the
    service that is started starts at (considering a service in c:\Program Files\Service\serice.exe)
    "c:\program.exe" followed then by "c:\program files\service\service.exe". Your customer probably has
    a file called program.exe at the root of the drive. This issue is fixed in the upcoming MSI 3.0 release.
    Until then you can use a custom action to alter the service binary path to include quotes or use the
    Registry table directly if the WriteRegistryValues standard action is sequenced after InstallServices.
    end quote/

    It is a vulnerability because the unquoted path is ambiguous, and as the above quote says, a service can be stolen by putting a name that fits the search (program.exe in the above example) onto the system.  The way that Windows parses unquoted paths trying to find the service executable causes the issue.

    Now that's when MSI installs services. I'd expect the ServiceInstaller classes used by Visual Studio to behave the same safe way by default and quote the full path to the service executable.
    Phil Wilson
    Monday, March 9, 2009 10:27 PM