2

I have a windows service that is deployed using a VS2010 deployment project. I require that a username/password be entered in the installer and then these details commited to the registry for the service to use.

The installer works fine, and the custom actions are setup correctly. If i try to commt to HKLM i get no error but no output either, the same command to HKCU works fine. This is the same as both a standard and administrative user (including RunAs).

public override void Install(IDictionary stateSaver)
{
    base.Install(stateSaver);

    var username = Context.Parameters["username"];
    var password = Context.Parameters["password"];

    // HKLM\Software\MySoftware
    RegistryKey hklm = Registry.LocalMachine.CreateSubKey("SOFTWARE\\MySoftware");
    hklm.SetValue("username", username, RegistryValueKind.String);
    hklm.SetValue("password", password, RegistryValueKind.String);
    hklm.Close();

    // HKCU\Software\MySoftware
    RegistryKey hkcu = Registry.CurrentUser.CreateSubKey("SOFTWARE\\MySoftware");
    hkcu.SetValue("username", username, RegistryValueKind.String);
    hkcu.SetValue("password", password, RegistryValueKind.String);
    hkcu.Close();
}

I have tried using .OpenSubkey(x, true) instead of CreateSubkey(x). The results are the same.

Any help would be greatly appriciated.

Regards

Chris

Chris
  • 23
  • 3
  • Possible duplicate: http://stackoverflow.com/questions/1782492/installer-custom-action-problem-cant-write-to-register-key – Blazes Jun 27 '11 at 10:48
  • I cannot find a good reference on the CustomAction, so I cannot verify, but maybe you do not have elevated permissions in the 'Install' part of the CustomAction? have you tried moving that code to 'Commit'. – Blazes Jun 27 '11 at 11:06
  • Just given the commit action a try and the same result! Have moved the code to a console app now, being explicitly run with administrative credentials and no luck either. I dont see any reason why my security settings for HKLM would be anything other than stock either! – Chris Jun 27 '11 at 11:19
  • What kind of username and password are you saving? – Oskar Kjellin Jun 27 '11 at 11:19
  • Hi Oskar, they are just clean strings. "123" and "123". – Chris Jun 27 '11 at 11:21
  • @Chris I mean, perhaps you can set your Service to sign in as that user instead? So that you don't have to save the password in the registry – Oskar Kjellin Jun 27 '11 at 11:22
  • Ah i see what you mean! The service itself that is being installed communicates with an internally hosted webservice that is using strings for authentication. Previously i had been holding the credentials in a file, serialized from an object. The reason for moving credentials to the registry was to avoid a fixed disk location for the credentials. – Chris Jun 27 '11 at 11:24
  • @Chris, okay, then I am with you :) – Oskar Kjellin Jun 27 '11 at 11:35
  • Aha! I have been checking with powershell and regedit to see if the data has been commited to the registry correctly. I'm not sure why i didnt try this earlier but pulling the keys back with code works absolutely fine! This together with the fact that there are no error messages at all whilst it is executing leads me to believe that it is my permissions viewing the registry that are the issue and not the data being commited itself! – Chris Jun 27 '11 at 11:50
  • Haha, you might consider posting that as an answer and accept it. Firstly to increase your accept ratio and to help others with the same problem – Oskar Kjellin Jun 27 '11 at 13:27
  • Dont think i can submit answers to my own questions? Cant see any buttons for it! *EDIT* Unless the big button beneath labeled "Answer Question" is the one...having one of those days.. :) – Chris Jun 27 '11 at 15:37

1 Answers1

4

On a 64-bit operating system, you'll find these keys back in HKLM\Software\Wow6432Node\MySoftware. These registry keys are virtualized for 32-bit programs.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536