windows 打开另一个用户注册表设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1520361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:14:13  来源:igfitidea点击:

Open another user registry settings

windowsregistry

提问by Remus Rigo

I want to write a application that writes to all users on a local machine a specified key (eg: i want set the location for IE Favorites for all users to the same folder)

我想编写一个应用程序,向本地机器上的所有用户写入一个指定的密钥(例如:我想将所有用户的 IE 收藏夹的位置设置为同一文件夹)

PS anybody used these functions? LoadUserProfile RegOpenCurrentUser CreateProcessAsUser

PS有人用过这些功能吗?LoadUserProfile RegOpenCurrentUser CreateProcessAsUser

回答by Mick

I've done this many times. The idea is to update the currently logged on user's HKCU (that's easy enough). Then you must enumerate every profile on the system and find their ntuser.dat file (that's easy enough too).

我已经做过很多次了。这个想法是更新当前登录用户的 HKCU(这很容易)。然后您必须枚举系统上的每个配置文件并找到它们的 ntuser.dat 文件(这也很容易)。

With the ntuser.dat file found, you load it into a temporary key in the HKLM hive (I usually use 'HKLM\TempHive'. Then edit away.

找到 ntuser.dat 文件后,您将其加载到 HKLM 配置单元中的临时密钥中(我通常使用“HKLM\TempHive”。然后进行编辑。

If there is more than 1 user logged on, their profile will be loaded under HKEY_USERS, by their SID. Simply update that location.

如果有 1 个以上的用户登录,他们的配置文件将通过他们的 SID 加载到 HKEY_USERS 下。只需更新该位置即可。

To modify the setting for any new users, simply modify the appropriate key under HKEY_USERS.DEFAULT, OR use the Delphi code below which will do this by loading the Default Users's HKCU registry hive (stored in ntuser.dat).

要修改任何新用户的设置,只需修改 HKEY_USERS.DEFAULT 下的相应键,或使用下面的 Delphi 代码,通过加载默认用户的 HKCU 注册表配置单元(存储在 ntuser.dat 中)来完成此操作。

UPDATE: I found my Delphi code that demonstrates how to update the HKCU hives of users that are not logged onto the system.

更新:我发现我的 Delphi 代码演示了如何更新未登录系统的用户的 HKCU 配置单元。

This requires Russell Libby's 'Privilege' component, which is available here.

这需要 Russell Libby 的“特权”组件,可在此处获得

//NOTE:   sPathToUserHive is the full path to the users "ntuser.dat" file.
//
procedure LoadUserHive(sPathToUserHive: string);
var
  MyReg: TRegistry;
  UserPriv: TUserPrivileges;
begin
  UserPriv := TUserPrivileges.Create;    
  try
    with UserPriv do
    begin
      if HoldsPrivilege(SE_BACKUP_NAME) and HoldsPrivilege(SE_RESTORE_NAME) then
      begin
        PrivilegeByName(SE_BACKUP_NAME).Enabled := True;
        PrivilegeByName(SE_RESTORE_NAME).Enabled := True;

        MyReg := TRegistry.Create;    
        try
          MyReg.RootKey := HKEY_LOCAL_MACHINE;
          MyReg.UnLoadKey('TEMP_HIVE'); //unload hive to ensure one is not already loaded

          if MyReg.LoadKey('TEMP_HIVE', sPathToUserHive) then
          begin
            //ShowMessage( 'Loaded' );
            MyReg.OpenKey('TEMP_HIVE', False);

            if MyReg.OpenKey('TEMP_HIVE\Environment', True) then
            begin
              // --- Make changes *here* ---
              //
              MyReg.WriteString('KEY_TO_WRITE', 'VALUE_TO_WRITE');
              //
              //
            end;

            //Alright, close it up
            MyReg.CloseKey;
            MyReg.UnLoadKey('TEMP_HIVE');
            //let's unload the hive since we are done with it
          end
          else
          begin
            WriteLn('Error Loading: ' + sPathToUserHive);
          end;
        finally
          FreeAndNil(MyReg);
        end;

      end;
      WriteLn('Required privilege not held');
    end;
  finally
    FreeAndNil(UserPriv);
  end;
end;

I also wrote a VBScript a while ago that accomplishes this task. I used it for modifying some Internet Explorer settings, but you can customize it to your needs. It also demonstrates the general process:

不久前我还写了一个 VBScript 来完成这个任务。我用它来修改某些 Internet Explorer 设置,但您可以根据需要对其进行自定义。它还演示了一般过程:

Option Explicit

Dim fso
Dim WshShell
Dim objShell
Dim RegRoot
Dim strRegPathParent01
Dim strRegPathParent02

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.shell")


'==============================================
' Change variables here
'==============================================
'
'This is where our HKCU is temporarily loaded, and where we need to write to it
RegRoot = "HKLM\TEMPHIVE"
'
strRegPathParent01 = "Software\Microsoft\Windows\CurrentVersion\Internet Settings" 
strRegPathParent02 = "Software\Microsoft\Internet Explorer\Main"
'
'======================================================================



Call ChangeRegKeys()  'Sets registry keys per user

Sub ChangeRegKeys
 'Option Explicit
 On Error Resume Next

 Const USERPROFILE = 40
 Const APPDATA = 26

 Dim iResult
 Dim iResult1
 Dim iResult2
 Dim objShell
 Dim strUserProfile
 Dim objUserProfile
 Dim strAppDataFolder
 Dim strAppData
 Dim objDocsAndSettings
 Dim objUser
 Set objShell = CreateObject("Shell.Application")
 Dim sCurrentUser

 sCurrentUser = WshShell.ExpandEnvironmentStrings("%USERNAME%")

 strUserProfile = objShell.Namespace(USERPROFILE).self.path
 Set objUserProfile = fso.GetFolder(strUserProfile)
 Set objDocsAndSettings = fso.GetFolder(objUserProfile.ParentFolder)

 'Update settings for the user running the script
 '(0 = default, 1 = disable password cache)
 WshShell.RegWrite "HKCU\" & strRegPathParent01 & "\DisablePasswordCaching", "00000001", "REG_DWORD"
 WshShell.RegWrite "HKCU\" & strRegPathParent02 & "\FormSuggest PW Ask", "no", "REG_SZ"


 strAppDataFolder = objShell.Namespace(APPDATA).self.path
 strAppData = fso.GetFolder(strAppDataFolder).Name

 ' Enumerate subfolders of documents and settings folder
 For Each objUser In objDocsAndSettings.SubFolders 
  ' Check if application data folder exists in user subfolder
  If fso.FolderExists(objUser.Path & "\" & strAppData) Then 
   'WScript.Echo "AppData found for user " & objUser.Name
   If ((objUser.Name <> "All Users") and _
    (objUser.Name <> sCurrentUser) and _
    (objUser.Name <> "LocalService") and _ 
    (objUser.Name <> "NetworkService")) then
    'Load user's HKCU into temp area under HKLM
    iResult1 = WshShell.Run("reg.exe load " & RegRoot & " " & chr(34) & objDocsAndSettings & "\" & objUser.Name & "\NTUSER.DAT" & chr(34), 0, True)
    If iResult1 <> 0 Then
     WScript.Echo("***   An error occurred while loading HKCU: " & objUser.Name)
    Else
     WScript.Echo("HKCU loaded: " & objUser.Name)
    End If

    WshShell.RegWrite RegRoot & "\" & strRegPathParent01 & "\DisablePasswordCaching", "00000001", "REG_DWORD"
    WshShell.RegWrite RegRoot & "\" & strRegPathParent02 & "\FormSuggest PW Ask", "no", "REG_SZ"

    iResult2 = WshShell.Run("reg.exe unload " & RegRoot,0, True) 'Unload HKCU from HKLM
    If iResult2 <> 0 Then
     WScript.Echo("*** An error occurred while unloading HKCU: " & objUser.Name & vbcrlf)
    Else
     WScript.Echo("   unloaded: " & objUser.Name & vbcrlf)
    End If
   End If

  Else
   'WScript.Echo "No AppData found for user " & objUser.Name
  End If
 Next

End Sub

回答by John Gietzen

We had the exact same issue the other day.

前几天我们遇到了完全相同的问题。

We discovered that you can just open the HKEY_USERShive and write the changes to each user's SID.

我们发现您只需打开HKEY_USERS配置单元并将更改写入每个用户的 SID。

And, If you want the settings to be present for any new users, you should also apply the settings to the HKEY_USERS/.DEFAULTkey.

而且,如果您希望为任何新用户提供这些设置,您还应该将这些设置应用于HKEY_USERS/.DEFAULT密钥。

That is to say, just write your settings to...

也就是说,只需将您的设置写入...

HKEY_USERS\S-1-5-XX-XXXXXXXX-XXXXXXXXX-XXXXXXXX-XXXX\Software\...

For each of the SIDs present and:

对于存在的每个 SID 和:

HKEY_USERS\.DEFAULT\Software\...