tags:

views:

56

answers:

2

I have a project which references another project in order to utilise some portions of code. One of the projects is a console application (that is the project type) but is made up of a significant code-base. The other is a GUI application that references this console application . The reason this is done is so the application can be run either as a console application or a GUI application.

When the GUI application runs it accesses the registry absolutely fine, but when I run the console version, which accesses the exact same code as the GUI application , it fails security-wise to get access to the registry keys. I'm assuming both the GUI and console application would run via the same user.

Here's what's failing (changes made to hide specific details):

String user = Environment.UserDomainName + "\\" + Environment.UserName;
RegistrySecurity rs = new RegistrySecurity();
rs.AddAccessRule(new RegistryAccessRule(user, RegistryRights.ReadKey | RegistryRights.Delete | RegistryRights.WriteKey | RegistryRights.FullControl, InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow));
mapsynckey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\AppPath\\AppName\\");

UserName = mapsynckey.GetValue("user").ToString();
Password = mapsynckey.GetValue("pass").ToString();
SyncFolderPath = mapsynckey.GetValue("syncfolderpath").ToString();

Ignore the access rights, I've just been trying every single one hence the random mixture. The error I receive is as follows:

Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\AppPath\AppName' is denied

Here's the other thing. I can't find the entries anywhere in the registry, not even under the 64-bit section. However, I am loading the registry key details into the application which are shown fine, but if I browse the registry, they're not there.

I'm completely stumped, I've spent the last 3 hours working on this with no clue what's going on.


   at ApplicationSettings..ctor() in C:\Projects\MapSync\MapSyncConsole\ApplicationSettings.cs:line 34
   at ApplicationSettings.get_Instance() in C:\Projects\MapSync\MapSyncConsole\ApplicationSettings.cs:line 47
   at MainApplication..ctor(Boolean forceDownload) in C:\Projects\MapSync\MapSyncConsole\MainApplication.cs:line 27
   at MapSyncConsole.Program.Main(String[] args) in C:\Projects\MapSync\MapSyncConsole\Program.cs:line 21
+1  A: 

You can't call CreateSubKey in HKLM without administrator privileges.

You should consider switching to HKCU.

SLaks
Indeed, this is what I assumed also, but why would the GUI app allow this and the console app not? They run from the same user.
Kezzer
Oh and also, I'm a local administrator, so that baffles me even more.
Kezzer
Is UAC enabled?
SLaks
+1  A: 

First and foremost: Did you check the (UAC) virtualized registry locations? (See here). Registry uses a similar mechanism as file system for transparent redirection.

Additionally:

  • which token is your process running under, elevated or not? Assuming you have UAC enabled: Do you start the console app from a elevated prompt (seems not)? I guess you start the UI version by double-clicking?
  • less probable: does one of the executables have an embedded manifest which manipulates the elevation behavior?
  • less probable: do the processes (console vs. UI) target different processor architectures?