简体   繁体   English

写入64位注册表的32位Windows服务。 (AutoAdminLogon密钥)

[英]32-bit Windows service writing to the 64-bit registry. (AutoAdminLogon Keys)

// Edit: oh wow. //编辑:哦哇。 It's odd that I've been working on this for a day now and just realized I needed to do: 奇怪的是,我已经在这一天工作了一天,并且意识到我需要这样做:

key = key.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true);

And then everything worked. 然后一切正常。 I did not know that you had to do that. 我不知道你必须那样做。 Thanks for everyone that responded. 感谢所有回复的人。 I was just messing around and searched for my key and noticed it was being placed in the wrong spot. 我只是乱搞并搜索我的钥匙,发现它被放置在错误的位置。

// Original question: //原始问题:

I haven't seen a working solution for this and I'm not sure if it's a bug. 我没有看到这方面的工作解决方案,我不确定它是否是一个错误。

I have a C# 32-bit Windows service running on Windows 7 64-bit. 我有一个在Windows 7 64位上运行的C#32位Windows服务。 My goal is to write to the 64-bit registry and not the Wow6432Node subkey since for AutoAdminLogon a 64-bit system doesn't seem to check the 32-bit view of the keys. 我的目标是写入64位注册表而不是Wow6432Node子项,因为对于AutoAdminLogon,64位系统似乎不会检查密钥的32位视图。

So my code is as follows: 所以我的代码如下:

static public void LoginAsGuest(EventLog eventLogger)
{
    RegistrySecurity userSecurity = new RegistrySecurity();
    RegistryAccessRule userRule = new RegistryAccessRule("Everyone", RegistryRights.FullControl, AccessControlType.Allow);
    userSecurity.AddAccessRule(userRule);

    var key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
    key.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", RegistryKeyPermissionCheck.ReadWriteSubTree);

    if (key == null)
    {
        eventLogger.WriteEntry("Error accessing the registry key");
    }
    else
    {
        try
        {
            key.SetValue("AutoAdminLogon", "1", RegistryValueKind.String);
            key.SetValue("DefaultUserName", "guest", RegistryValueKind.String);
            key.SetValue("DefaultPassword", "password", RegistryValueKind.String);
        }
        catch (Exception exception)
        {
            eventLogger.WriteEntry("Problem setting up keys: " + exception);
        }
    }
    key.Close();

    Reboot();
}

No exception or error is thrown. 不会抛出任何异常或错误。 Nothing is written to the registry in the 32-bit or 64-bit view. 在32位或64位视图中没有任何内容写入注册表。 I've tried using: 我尝试过使用:

key.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true);

But it has the same outcome. 但它有相同的结果。 Now if I just write without any view then my program successfully writes to the subkey: 现在,如果我只是在没有任何视图的情况下编写,那么我的程序会成功写入子项:

SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\Winlogon

Basically all I want is to write to the subkey: 基本上我想要的只是写入子项:

SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon

Anyone know why the above code isn't writing to the requested key? 任何人都知道为什么上面的代码没有写入请求的密钥? (I will point out that AutoAdminLogon and the other two keys are used by the windows default credential provider such that when windows starts it checks those keys and if AutoAdminLogon is set to 1 then it logs in automatically with the given username and password. I'm doing this to allow a computer to be logged in as guest by setting the keys then rebooting the computer). (我将指出Windows默认凭据提供程序使用AutoAdminLogon和其他两个密钥,以便在Windows启动时检查这些密钥,如果AutoAdminLogon设置为1,则它会使用给定的用户名和密码自动登录。我这样做是为了允许计算机通过设置密钥然后重新启动计算机作为访客登录。

It's odd that I've been working on this for a day now and just realized I needed to do: 奇怪的是,我已经在这一天工作了一天,并且意识到我需要这样做:

key = key.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true);

And then everything worked. 然后一切正常。 I did not know that you had to do that. 我不知道你必须那样做。 Thanks for everyone that responded. 感谢所有回复的人。 I was just messing around and searched for my key and noticed it was being placed in the wrong spot. 我只是乱搞并搜索我的钥匙,发现它被放置在错误的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM