-3

I am writing a service program and I debugged most of the code fine using

#If DEBUG Then
#Else

but after installing the server and running it I get this error that I get with writing ex.tostring to a text file:

System.NullReferenceException: Object reference not set to an instance of an object. at HimayaService.Himaya.TimerBackup_Tick(Object sender, ElapsedEventArgs e)

so HimayaService is the name of my service, Himaya the name of my class and TimerBackupTick was declared right:

AddHandler TimerBackup.Elapsed, AddressOf TimerBackup_Tick
TimerBackup.Enabled = True
TimerBackup.Interval = 30000`

after trying to find where the error is at it was at the first line in TimerBackup_Tick which is

Dim databasekey3 As RegistryKey = Registry.CurrentUser.OpenSubKey(mainsubkey)

mainsubkey is an actual subkey that I am sure works fine since it works when debugging with Visual Studio, what might be the problem here as the other Timer's tick events work fine and don't give errors.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Zayd
  • 21
  • 7
  • 1
    It is unclear where the error is. Can you provide a stack trace? And then the line of code which is causing the error? – Michael Foster Mar 21 '23 at 15:13
  • 1
    Come on man. You are telling us about the error but not showing the code that generates the error. – video.baba Mar 21 '23 at 15:51
  • 2
    Have you set the service to run as the correct user? If not, then `Registry.CurrentUser` will not be what you want it to be. – Andrew Morton Mar 21 '23 at 18:14
  • Guys it's a service the only way i can know about the error is try and catch and get the error in a text file unless you can give me another method i'd be thankful. – Zayd Mar 21 '23 at 20:10
  • I set the service to run as local system. – Zayd Mar 21 '23 at 20:13
  • You can attach the debugger to the service process. Even so, you've not really given enough info of these methods for any really assistance. – Hursey Mar 21 '23 at 22:25
  • The following may be of interest: https://stackoverflow.com/a/73806343/10024425 - it shows how one can use `log4net` in a Windows service (for logging). – Tu deschizi eu inchid Mar 22 '23 at 03:26
  • 1
    You wrote "mainsubkey is an actual subkey that I am sure works fine since it works when debugging with Visual Studio," but that does not help because VS is not running under the LocalSystem account at that time. Please see [LocalSystem Account](https://learn.microsoft.com/en-us/windows/win32/services/localsystem-account) for more information about that account. – Andrew Morton Mar 22 '23 at 06:46
  • Attaching the debugger to the service process didn't work for me also i thought information about what is inside the method is pointless as the error happens on the first line of code, i'll try `log4net` thanks @user09938 and thanks @Andrew-Morton for the formatting correction. – Zayd Mar 22 '23 at 06:49
  • Hey @AndrewMorton you already helped me find the answer, it's down below. – Zayd Mar 22 '23 at 17:49
  • @Zayd You can accept your own answer if you want to. It's usual to ask someone who helped you in comments to make an answer for you, which you can then accept. One of the founding principles of Stack Overflow is to "gamify" the Q&A system: good answers and questions give points. (I'm not bothered about not getting points from this one.) The downvotes are probably because people thought that not enough research had been done before asking. – Andrew Morton Mar 23 '23 at 17:15

1 Answers1

0

I have found the problem, as @AndrewMorton suggested the current user of the service is not the same as the current user I get when I access the registry manually, in other words I thought the service is accessing HKEY_CURRENT_USER but instead it is accessing HKEY_USERS\.DEFAULT.

Zayd
  • 21
  • 7
  • The service is using whatever account you provided when the service was installed, which has no relationship at all to the currently logged in user. If you're seeing the .DEFAULT hive, it's probably running as the SYSTEM user. You can change this to a different user, but you can't make it depend on who is currently logged in, or even be sure there's only one user at a time (Windows allows there to be _several_ actively logged in users) – Joel Coehoorn Mar 22 '23 at 16:18