How to RegWrite a "current user" registry setting for unelevated user

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

How to RegWrite a "current user" registry setting for unelevated user

11 Dec 2018, 16:47

Hey all...
In learning the ins and outs of needing administrator privileges and using uac, I've run into a problem that I assume has an easy solution but I have only been able to kludge a workaround:
How to write a registry setting for a standard user, not the elevated/admin user, when admin privileges are needed to write to the registry?

RegWrite, REG_DWORD, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Policies\System, DisableLockWorkstation, 0

If I try to RegWrite the above as a standard user, I don't have the permission. If I elevate to administrator account and try to RegWrite the above, it writes but to user "administrator" not as the currently logged in un-elevated user, so the above key doesn't get written. So how do I write to this key for the standard user?

Advice?
Thanks,
- gwarble





My kludge of a workaround I figured out today is, from the elevated/admin account, RegWrite:
SID := GetStandardUserSID()
RegWrite, REG_DWORD, HKEY_USERS, %SID%\Software\Microsoft\Windows\CurrentVersion\Policies\System, DisableLockWorkstation, 0
But getting the SID of the un-elevated user just seems wrong.

Code: Select all

GetStandardUserSID() {
 wtsapi32 := DllCall("LoadLibrary", Str, "wtsapi32.dll", Ptr)
 DllCall("wtsapi32\WTSEnumerateSessionsEx", Ptr, 0, "UInt*", 1, UInt, 0, "Ptr*", pSessionInfo, "UInt*", wtsSessionCount)
 Loop % wtsSessionCount {
  _ := ((A_PtrSize == 8 ? 56 : 32) * (A_Index - 1)) + 8 + (A_PtrSize * 3)
  If(UserName := StrGet(NumGet(pSessionInfo+0, _, "Ptr"),, A_IsUnicode ? "UTF-16" : "CP0"))
   Break
 }
 DllCall("wtsapi32\WTSFreeMemoryEx", UInt, 2, Ptr, pSessionInfo, UInt, wtsSessionCount)
 DllCall("FreeLibrary", Ptr, wtsapi32)
 DllCall("advapi32\LookupAccountName", Str, System, Str, UserName, UPtr, 0, UPtrP, nSizeSID, UPtr, 0, UPtrP, nSizeRDN, UPtrP, eUser)
 VarSetCapacity(SID, nSizeSID, 0)
 VarSetCapacity(RDN, nSizeRDN, 0)
 DllCall("advapi32\LookupAccountName", Str, S, Str, UserName, Str, SID, UPtrP, nSizeSID, Str, RDN, UPtrP, nSizeRDN, UPtrP, eUser)
 DllCall("advapi32\ConvertSidToStringSid", Str, SID, UPtrP, pString)
 VarSetCapacity(sSid, DllCall("lstrlenW", UPtr, pString)*2)
 DllCall("lstrcpyW", Str, sSid, UPtr, pString)
 DllCall("LocalFree", UPtr, pString)
Return sSid
}
Last edited by gwarble on 12 Dec 2018, 18:41, edited 5 times in total.
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: How to RegWrite a "current user" registry setting for unelevated user

11 Dec 2018, 23:23

gwarble wrote:
11 Dec 2018, 16:47
....getting the SID of the un-elevated user just seems wrong.
but does it work ?? ;)
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: How to RegWrite a "current user" registry setting for unelevated user

12 Dec 2018, 08:36

True, it does seem to work, but i want to find the proper way and i've not seen it done like this elsewhere

Edit: actually it fails when another user is logged in, don't know how to differentiate the user sessions
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: How to RegWrite a "current user" registry setting for unelevated user

12 Dec 2018, 22:14

Seems like a needed function. Consider publishing it in the scripts section, or I can move this topic for you if like.
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: How to RegWrite a "current user" registry setting for unelevated user

13 Dec 2018, 00:27

I will once i'm sure I can get the actual logged in current standard user

the getting of Sid seems pretty solid, not sure what else its useful for

If this is really the only way it might be useful to wrap it into RegWriteStandardUser()
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: How to RegWrite a "current user" registry setting for unelevated user

13 Dec 2018, 19:25

So I added a break to the loop after first Username is found, tested on two systems they both seem to return the "standard user" i want from the elevated process first, before a secondary logon name, so maybe this works. I can't find any documentation or posts about the order in which sessions are enumerated, so more testing is needed

Currently putting together 3 concise functions for the S&F section:

Code: Select all

RegWriteUser(user,key...) ;Write directly to HKUsers to override elevated user redirection for HKCurrentUser, or maybe for other users?
GetUserSid(user) ; returns SID
GetStandardUser() ; get username of current user before elevation
;and
RegReadUser(user, key...) ;read from HKUsers to override elevated user redirection for HKCurrentUser
Last edited by gwarble on 15 Dec 2018, 12:49, edited 2 times in total.
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: How to RegWrite a "current user" registry setting for unelevated user

15 Dec 2018, 12:37

Something like this?
works on windows 7, fails on XP (getting SID misses the last "-1004" for example)

Code: Select all

If not A_IsAdmin
{
 Run *RunAs "%A_ScriptFullPath%"
 ExitApp
}
dlw1 := RegReadUser(GetStandardUser(),              "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableLockWorkstation")
       RegWriteUser(GetStandardUser(), "REG_DWORD", "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableLockWorkstation", !dlw1)
dlw2 := RegReadUser(GetStandardUser(),              "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableLockWorkstation")
       RegWrite, REG_DWORD, % "HKEY_USERS\" GetUserSID(GetStandardUser()) "\Software\Microsoft\Windows\CurrentVersion\Policies\System", DisableLockWorkstation, % !dlw2
dlw3 := RegReadUser(GetStandardUser(),              "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableLockWorkstation")

MsgBox, % "DisableLockWorkstation`nOriginally:          " dlw1 "`nRegWriteUser:   " dlw2 "`nRegWrite:           " dlw3


RegWriteUser(User, ValueType, KeyName , ValueName="", Value="") {
 StringReplace, KeyName, KeyName, HKEY_CURRENT_USER, % "HKEY_USERS\" GetUserSID(User)
 RegWrite, %ValueType%, %KeyName%, %ValueName%, %Value%
Return
}


RegReadUser(User, KeyName , ValueName="") {
 StringReplace, KeyName, KeyName, HKEY_CURRENT_USER, % "HKEY_USERS\" GetUserSID(User)
 RegRead, OutputVar, %KeyName%, %ValueName%
Return OutputVar
}

GetStandardUser() {
 wtsapi32 := DllCall("LoadLibrary", Str, "wtsapi32.dll", Ptr)
 DllCall("wtsapi32\WTSEnumerateSessionsEx", Ptr, 0, "UPtr*", 1, UPtr, 0, "Ptr*", pSessionInfo, "UPtr*", wtsSessionCount)
 Loop % wtsSessionCount {
  _ := ((A_PtrSize == 8 ? 56 : 32) * (A_Index - 1)) + 8 + (A_PtrSize * 3)
  If(UserName := StrGet(NumGet(pSessionInfo+0, _, "Ptr"),, A_IsUnicode ? "UTF-16" : "CP0"))
   Break
 }
Return Username
}

GetUserSID(UserName) {
 DllCall("wtsapi32\WTSFreeMemoryEx", UPtr, 2, Ptr, pSessionInfo, UPtr, wtsSessionCount)
 DllCall("FreeLibrary", Ptr, wtsapi32)
 DllCall("advapi32\LookupAccountName", Str, System, Str, UserName, UPtr, 0, UPtrP, nSizeSID, UPtr, 0, UPtrP, nSizeRDN, UPtrP, eUser)
 VarSetCapacity(SID, nSizeSID, 0)
 VarSetCapacity(RDN, nSizeRDN, 0)
 DllCall("advapi32\LookupAccountName", Str, S, Str, UserName, Str, SID, UPtrP, nSizeSID, Str, RDN, UPtrP, nSizeRDN, UPtrP, eUser)
 DllCall("advapi32\ConvertSidToStringSid", Str, SID, UPtrP, pString)
 VarSetCapacity(sSid, DllCall("lstrlenW", UPtr, pString)*2)
 DllCall("lstrcpyW", Str, sSid, UPtr, pString)
 DllCall("LocalFree", UPtr, pString)
Return sSid
}
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Lamron750, nacken012, septrinus and 226 guests