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

时间:2009-10-05 14:14:35

标签: windows registry

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

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

2 个答案:

答案 0 :(得分:8)

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

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

如果有多个用户登录,则他们的个人资料将通过他们的SID加载到HKEY_USERS下。只需更新该位置即可。

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

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

这需要Russell Libby的'Privilege'组件,which is available here

//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;

我刚刚写了一个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

答案 1 :(得分:1)

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

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

并且,如果您希望为任何新用户提供设置,您还应该将设置应用于HKEY_USERS/.DEFAULT键。

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

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

对于每个存在的SID和:

HKEY_USERS\.DEFAULT\Software\...