WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Sample Code from Users

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Add key to HKEY_CURRENT_USER for all profiles


Question:

I'm fairly new to Winbatch. I'm trying to locate all the profiles on a Win2000 or XP machine, and load a REG file into the NTUSER.DAT. This REG file modifies HKEY_CURRENT_USER. RegLoadHive doesn't help because it only works with @REGMACHINE and @REGUSERS, not @REGCURRENT. In searching through these postings I've found a way to find the NTUSER.DAT of each profile. But I can't find a way to modify each one without logging in as each user.

Answer:

Example 1

key = RegOpenkey(@REGMACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList")
who = RegQueryKeys(key)
RegClosekey(key)
length = StrLen(who)
start = 1
count = 0
while @TRUE
	finish = StrScan(who, @tab, start, @FWDSCAN)
	If finish == 0
		finish = StrScan(who, @tab, start, @BACKSCAN)
		count = count + 1
		param%count% = StrSub(who, start, length)
		SID = param%count%
		ProfileDir = StrUpper(RegQueryStr(@REGMACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%SID%[ProfileImagePath]"))
		CurProfile = StrUpper(RegQueryValue(@REGMACHINE,"SOFTWARE\Microsoft\Windows NT\CurrentVersion\winlogon[DefaultUserName]"))
		if StrIndex(ProfileDir, CurProfile, 1, @Fwdscan) == 0
			length=StrIndex(ProfileDir, "\", 1, @Backscan)
			username=StrSub(ProfileDir, 1, length)
			if username <> ""
				ErrorMode(@OFF)
				RegLoadHive(@REGUSERS, "TmpUser", "%ProfileDir%\NTUSER.DAT")
				ErrorMode(@CANCEL)
				if LastError( ) == 1526
					RegUnloadHive(@REGUSERS, "TmpUser")
					RegLoadHive(@REGUSERS, "TmpUser", "%ProfileDir%\NTUSER.DAT")
				endif
				if RegExistKey(@REGUSERS, "TmpUser\SOFTWARE\Key") == @FALSE
					RegCreateKey(@REGUSERS, "TmpUser\SOFTWARE\Key")
					RegCloseKey(@REGUSERS, "TmpUser\SOFTWARE\Key")
				endif
				RegSetValue(@REGUSERS, "TmpUser\SOFTWARE\Key[User Name]", username)
				RegUnloadHive(@REGUSERS, "TmpUser")
			endif
		endif
		break
	else
		count = count + 1
		param%count% = StrSub(who, start, finish - start)
		SID = param%count%
		ProfileDir = StrUpper(RegQueryStr(@REGMACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%SID%[ProfileImagePath]"))
		CurProfile = StrUpper(RegQueryValue(@REGMACHINE,"SOFTWARE\Microsoft\Windows NT\CurrentVersion\winlogon[DefaultUserName]"))
		userlen=StrIndex(ProfileDir, "\", StrLen(ProfileDir), @BACKSCAN)
		username=StrSub(ProfileDir, userlen+1, StrLen(ProfileDir))
		if username <> ""
			if StrIndex(ProfileDir, CurProfile, 1, @Fwdscan ) == 0
				ErrorMode(@OFF)
				RegLoadHive(@REGUSERS, "TmpUser", "%ProfileDir%\NTUSER.DAT")
				ErrorMode(@CANCEL)
				if LastError( ) == 1526
					RegUnloadHive(@REGUSERS, "TmpUser")
					RegLoadHive(@REGUSERS, "TmpUser", "%ProfileDir%\NTUSER.DAT")
				endif
				if RegExistKey(@REGUSERS, "TmpUser\SOFTWARE\Key") == @FALSE
					RegCreateKey(@REGUSERS, "TmpUser\SOFTWARE\Key")
					RegCloseKey(@REGUSERS, "TmpUser\SOFTWARE\Key")
				endif
				RegSetValue(@REGUSERS, "TmpUser\SOFTWARE\Key[Value]", username)
				RegUnloadHive(@REGUSERS, "TmpUser")
			else
				if RegExistKey(@REGUSERS, "%SID%\SOFTWARE\Key") == @FALSE
					RegCreateKey(@REGUSERS, "%SID%\SOFTWARE\Key")
					RegCloseKey(@REGUSERS, "TmpUser\SOFTWARE\Key")
				endif
				RegSetValue(@REGUSERS, "%SID%\SOFTWARE\Key[Value]", CurProfile)
			endif
		endif
		start = finish + 1
		if finish == length then break
	endif
endwhile


Well, I have a slightly differnt approach. Here it is if anyone wants to take a look or compare.

Example 2

;
===========================================================================
; Start of Code
;
===========================================================================
CStartMenuDir = RegQueryValue (@RegMachine,
"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders[Common
Start Menu]")
ProfileDir = StrSub (CStartMenuDir, 1, StrIndex (CStartMenuDir, "\All
Users", 0, @FwdScan) -1)

IntControl (5, 1, 0, 0, 0) ; System & Hidden files or directories are seen
and used
ProfileList = DirItemize ("%ProfileDir%\*.*")
For Count = 1 to ItemCount (ProfileList, @TAB)
Profile = ItemExtract (Count, ProfileList, @Tab)
If StrUpper(Profile) <> "ALL USERS"
Select FileExist ("%ProfileDir%\%Profile%\NTUser.DAT")
Case 1 ; File Exists and is NOT in use.
RegLoadHive (@RegUsers, "TempUser",
"%ProfileDir%\%Profile%\NTUser.DAT")
;RegSetValue (@RegUsers,
"TempUser\...[...]", "...")
RegUnloadHive (@RegUsers, "TempUser")
Break
Case 2 ; File Exists & IN USE. [
;RegSetValue (@RegCurrent, "...[...]",
"...")
Break
EndSelect
EndIf
Next


Article ID:   W16174
File Created: 2004:03:30:15:43:08
Last Updated: 2004:03:30:15:43:08