none
Why the service will not work? RRS feed

  • Question

  • I have created a service, set serviceProcessInstaller1 account to LocalSystem, set serviceInstaller1 StartType to auto, and installed it using InstallUtil.exe successfully, start the servcie, but why the code will not run? The test vaule has not been created. The code is below,

       Protected Overrides Sub OnStart(ByVal args() As String)
            Dim rk As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows", True)
            rk.SetValue("test", 1)
        End Sub

    Saturday, January 13, 2018 11:04 AM

Answers

  • Yes, i have known the reason, untick the first for 32bit in compiler in 64bit OS, the service created the value in Registry successfully for Local Machine, but not for Current User.

    • Marked as answer by gaxjyxq Saturday, January 13, 2018 1:48 PM
    Saturday, January 13, 2018 1:48 PM

All replies

  • Best guess here is that the service does not have permissions to write to the registry but should have no problem reading keys from the registry. This is because the service is not loaded with the current user's account/profile. With that said, try writing to HKLM (Local machine) and see if that works for you. Of course if you need different setting for different users sharing the same machine that would not work yet in today's world usually it's one machine, one user

    Okay, to confirm the above, I took a Windows Service I wrote in C# (language does not matter), created a class with a single method to write the exact same value you wanted to write. wrote the value, zero issues but it did not write the value.

    VZB.NET from C#

    Imports Microsoft.Win32
    Imports System
    
    Namespace KarenPayneService.Classes
    	Public Class RegistryOperations
    		Public Function WriteValue() As String
    			Try
    				Dim rk As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows", True)
    				rk.SetValue("test", 1)
    				Return "Success writing to the registry"
    
    			Catch ex As Exception
    				Return $"Failed writing to the registry: {ex.Message}"
    			End Try
    		End Function
    	End Class
    End Namespace

    Back to C# but that does not matter, here is how I called it

    So I started the service via a utility I created for any service e.g.

    Keeping Visual Studio open to the solution with the service I fire up the util above from Windows Explorer, press Install service, when Debugger.Launch is hit Visual Studio ask to debug which I do, traverse to the call of interest and note zero issues (of course for production you would remove Debugger.Launch or use a condition if statement)

    So the real issue is SetValue has no return type so if there is an issue it's unknown to use and really the try-catch is of no use.

    So you would then need to have the account for the service setup as per what DA924x mentioned and see if that works.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Saturday, January 13, 2018 11:17 AM
    Moderator
  • It most likely needs to run using an administrator account, since you are doing something with the  registry. You want to see that it worked, then write a message to the event-log or to a text file. 
    Saturday, January 13, 2018 11:18 AM
  • Thank you, i did not used the debug, i only to check if the value "test" created successfully in Registry, but not luck, the values "test" in both "HKEY_Current_User\SOFTWARE\Microsoft\Windows" and "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows" have not been created!

    Saturday, January 13, 2018 1:35 PM
  • Thank you, the values in "Current" and "Machine" have not been created. How to write the value to Registry in a service?

    • Edited by gaxjyxq Saturday, January 13, 2018 1:37 PM
    Saturday, January 13, 2018 1:36 PM
  • Thank you, the values in "Current" and "Machine" have not been created. How to write the value to Registry in a service?

    If the registry setting is only for the service, why not store settings in app.config? For example I use the following 

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
        </startup>
      <appSettings>
        <!-- Daily and Interval -->
        <add key="Mode" value="Interval" />
        <!-- <add key ="Mode" value ="Interval"/>-->
        <!--
              It is used when Mode is set to Interval. It consist of the Interval 
              value in Minutes after which the Windows Service will perform a task. 
              In other words it is the delay value. Currently set to one minute for easily testing things out 
        -->
        <add key="IntervalMinutes" value="1" />
        <!-- 
              This setting is used when the Mode is set to Daily. It is used to notify the 
              Windows Service the time it should perform a task. The value specified is 
              in 24 hour time format.-->
        <add key="ScheduledTime" value="23:58" />
        <add key="ClientSettingsProvider.ServiceUri" value="" />
      </appSettings>
    
    </configuration>

    Which you can then read and write to as you see fit. 

    Bottom line is a windows service should not need to alter the registry and as you have learned it's not working most likely because of permissions to the registry for the account the service is running under.

    if you want to configure a service to use a specific user account you have to

    • install the service
    • open up service manager (control panel - administrative tools - services)
    • right click on the service, choose properties
    • in the tab Logon, choose this account and supply the necessary info


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, January 13, 2018 1:44 PM
    Moderator
  • Yes, i have known the reason, untick the first for 32bit in compiler in 64bit OS, the service created the value in Registry successfully for Local Machine, but not for Current User.

    • Marked as answer by gaxjyxq Saturday, January 13, 2018 1:48 PM
    Saturday, January 13, 2018 1:48 PM
  • I have created a service, set serviceProcessInstaller1 account to LocalSystem, set serviceInstaller1 StartType to auto, and installed it using InstallUtil.exe successfully, start the servcie, but why the code will not run? The test vaule has not been created. The code is below,


    Yes Karen wrote so. Not nice to mark your own parroting as answer. 

    Success Cor

    Saturday, January 13, 2018 1:59 PM