Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I Can't Read registry values from a service

Within my service i have the following function in order to take some values from my registry:

 Public Function GetKeyValue(ByVal nKey As String, ByVal sPath As String) As String
        Dim RegKey As RegistryKey
        Dim kValue As String = Nothing
        Dim Pos As String
        If CheckRegistry(sPath) Then
            Try
                RegKey = Registry.CurrentUser.OpenSubKey(sPath)
                kValue = CStr(RegKey.GetValue(nKey))

            Catch ex As Exception
                StartLogFile(" GetKeyValue " & vbNewLine & "Stack Trace= " & ex.StackTrace, EventLogEntryType.Warning)
            End Try
        End If
        Return kValue
    End Function

the same function works fine within a Windows form, but if i call from a service then she can't read the value. Is there anybody how knows what is going on?

like image 939
Lefteris Gkinis Avatar asked Mar 20 '11 17:03

Lefteris Gkinis


People also ask

How do I view registry entries?

There are two ways to open Registry Editor in Windows 10: In the search box on the taskbar, type regedit, then select Registry Editor (Desktop app) from the results. Right-click Start , then select Run. Type regedit in the Open: box, and then select OK.

Where are services stored in registry?

The HKLM\SYSTEM\CurrentControlSet\Services registry tree stores information about each service on the system. Each driver has a key of the form HKLM\SYSTEM\CurrentControlSet\Services\DriverName. The PnP manager passes this path of a driver in the RegistryPath parameter when it calls the driver's DriverEntry routine.

How do I enable Winreg?

Click Start, click Run, type regedit, and then click OK. Right-click winreg, and then click Permissions. Click Add.

What are the three types of values in the registry?

There are three types of values; String, Binary, and DWORD - the use of these depends upon the context. There are six main branches, each containing a specific portion of the information stored in the Registry.


2 Answers

You should not store your data in HKEY_CURRENT_USER but under HKEY_LOCAL_MACHINE that makes more sense for a Windows-Service.

Be also aware that you can also set Permissions on Registry Keys. Check also that when try reading.

like image 114
gsharp Avatar answered Sep 28 '22 14:09

gsharp


You are almost surely reading registry settings of a different user. The service likely runs as one of the built-in service user accounts: SYSTEM, LOCALSERVICE or NETWORKSERVICE. These are not interactive users.

Your design is fundamentally flawed and I suspect you will need to move these settings into a file which is not part of a user profile.

like image 25
David Heffernan Avatar answered Sep 28 '22 14:09

David Heffernan