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
}