Reading Registry Values in Windows NT Service

A

Ahmad Jalil Qarshi

Hi!

I am developing an application in C# as a windows NT Service.

This application needs to check for eventlog using
EventLog.Exists("System")

But unfortunately it generates exception "Requested registry access is not
allowed."

After that I tried to verify it using registry
RegistryKey eventLogKey
=Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\Eventlog\\System");

But it also generate exception.

I check on net but found no solution.

Furthermore if I try to read any registry value under HKEY_CURRENT_USER.

using the above mentioned method it return null.

Any solution to solve this problem will be highly appriciated.

Regards,



Ahmad Jalil Qarshi
 
W

Willy Denoyette [MVP]

| Hi!
|
| I am developing an application in C# as a windows NT Service.
|
| This application needs to check for eventlog using
| EventLog.Exists("System")
|
| But unfortunately it generates exception "Requested registry access is not
| allowed."
|
| After that I tried to verify it using registry
| RegistryKey eventLogKey
|
=Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\Eventlog\\System");
|
| But it also generate exception.
|
| I check on net but found no solution.
|
| Furthermore if I try to read any registry value under HKEY_CURRENT_USER.
|
| using the above mentioned method it return null.
|
| Any solution to solve this problem will be highly appriciated.
|
| Regards,
|
|
|
| Ahmad Jalil Qarshi
|
|

1. Why do you need to check for the existence of the System log? Your System
is badly broken if this one doesn't exist.
2. HKCU for a service is refering to the User hive of the user account
running the service (if any), not the hive of an interactive user (which
might even not be present).

Willy.
 
A

Ahmad Jalil Qarshi

Thanks Willy.

Actually I am making a EventLog Monitor as a service for that I have to
check it out.
Furthermore I have to store some settings in the HKCU, which is a
requirement from the user that's why I need that.

Any Help,

Regards,

Ahmad jalil Qarshi
 
W

Willy Denoyette [MVP]

The "System" eventlog must exist all the time, else your system would not
function. Anyway, if you really need to check this, you need to make sure
that your "service account" (the identity used to run your service) has
appropriate rights to open and read this registry key.
As for your second problem, you need to be aware that a service wich runs
under a specific service account has it's HKCU mapped from:
- HKU\.Default or S-1-5-18 for Local System (SYSTEM) and all other
accounts running the service
- HKU \S-1-5-19 for Local Service
- HKU \ S-1-5-20 for Network Service
So, when a service that runs under "Network User", reads from HKCU, it
effectively reads from HKU\S-11-5-20, not from what you may think - the HKCU
of the interactive logon user! If your service runs under a 'normal' user
account, it's HKCU is mapped from .DEFAULT (or S-1-5-18 which is a mirror
of Default).
That means that the service account needs to have access privileges to the
HKU hive and it's subkeys, this is not a problem for the service accounts,
they are granted full control to their respectively hives, but a 'normal'
account (except an administrator) only has read privileges to the .DEFAULT
hive.

Willy.


| Thanks Willy.
|
| Actually I am making a EventLog Monitor as a service for that I have to
| check it out.
| Furthermore I have to store some settings in the HKCU, which is a
| requirement from the user that's why I need that.
|
| Any Help,
|
| Regards,
|
| Ahmad jalil Qarshi
|
| | >
| >
| > | > | Hi!
| > |
| > | I am developing an application in C# as a windows NT Service.
| > |
| > | This application needs to check for eventlog using
| > | EventLog.Exists("System")
| > |
| > | But unfortunately it generates exception "Requested registry access is
| > not
| > | allowed."
| > |
| > | After that I tried to verify it using registry
| > | RegistryKey eventLogKey
| > |
| >
=Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\Eventlog\\System");
| > |
| > | But it also generate exception.
| > |
| > | I check on net but found no solution.
| > |
| > | Furthermore if I try to read any registry value under
HKEY_CURRENT_USER.
| > |
| > | using the above mentioned method it return null.
| > |
| > | Any solution to solve this problem will be highly appriciated.
| > |
| > | Regards,
| > |
| > |
| > |
| > | Ahmad Jalil Qarshi
| > |
| > |
| >
| > 1. Why do you need to check for the existence of the System log? Your
| > System
| > is badly broken if this one doesn't exist.
| > 2. HKCU for a service is refering to the User hive of the user account
| > running the service (if any), not the hive of an interactive user (which
| > might even not be present).
| >
| > Willy.
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top