Home All Groups Group Topic Archive Search About

Windows Service reg key access fails

Author
29 Mar 2006 10:27 PM
John A Grandy
I've built a .net 2.0 Windows Service that accesses a Registry key shortly
after startup.

On one XP Pro SP2 I build the service and referenced libs, deploys the
servie, starts it up, and it runs fine.

I transfer the service and libs to another XP Pro SP2 box , deploys it ,
starts it up , and it throws the access error shown below.

On both machines, the Registry key Permissions list contains :

{local machine}\Administrators
{localmachine}\ASPNET
{localmachine}\CREATOR OWNER
{localmachine}\Power Users
{localmachine}\SYSTEM
{localmachine}\Users
domain account used to log-on
System.Security.SecurityException: Requested registry access is not allowed.

   at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)

   at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)

   at System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData
sourceData)

   at System.Diagnostics.EventLog.VerifyAndCreateSource(String sourceName,
String currentMachineName)

   at System.Diagnostics.EventLog.WriteEntry(String message,
EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)

   at System.Diagnostics.EventLog.WriteEntry(String message,
EventLogEntryType type)

   at Microsoft.MSNMarketplace.clsEventMgr.EventMgrAuxConstructor(Exception
objException, EventLogEntryType objExType, String strService, String
strMessage, String strCountryCode, String strServer) in
C:\SilverLine\v2.0\WinMP\Src\Common\esutil\esutil.cs:line 1990

   at Microsoft.MSNMarketplace.clsEventMgr..ctor(EventLogEntryType
objExType, String strService, String strMessage, String strCountryCode,
String strServer) in
C:\SilverLine\v2.0\WinMP\Src\Common\esutil\esutil.cs:line 1954

   at Microsoft.MSNMarketplace.IndexerService.CIndexerSvc..ctor(String
strMrktPlace, String strLocale) in
C:\SilverLine\v2.0\WinMP\Src\Backend\Indexer\IndexerService\CIndexerService.cs:line
384

The Zone of the assembly that failed was:

MyComputer

Author
31 Mar 2006 1:20 PM
oldbear
Hi

This is a very common issue. Look at the event log in the Computer
Management applet. Open any event. You'll see that there's a Source parameter
at the top right.

Event sources can only be created by an admin account.

On your machine, you're an administrator. The first time you run the code
when you're developing as admin, your account has the necessary Registry
permissions to create the sources for your events. You see, when you choose
to write to the event log with a specific event source, if that source
doesn't already exist, then Windows tries to create it. Thus, during
development, all of your event sources mentioned in code have been created.

Later, you test the code on your machine with a lower privilege user
account, and you see it works fine. This is because the event sources were
already created during development.

Now you transfer to another machine and, and the service account doesn't
have the necessary permissions to create event sources. Security Exception!

Part of the install for your service should include the creation of event
sources. Don't try to dynamically create event sources.

Hope this helps

Chris Seary

http://blog.searyblog.com/