Two questions about the windows admin account
1. How can retrieve in code the administrator login details. I need this because people are going to use my program from within many user accounts and it will have to run using the administrator account using the LogonUser function.
2. How can I tell in code when I'm not running the windows administrator account? I saw this function for determining if I can write to the registry, but it does not always return false, and I want something more general.
VB Code:
Private Function CheckTokenPrivileges()
Dim dateStr As String
If winVersion = "WNT3" Or winVersion = "WNT4" Or winVersion = "W2000" Or winVersion = "WXP" Then
If EnablePrivilege(SE_BACKUP_NAME) = False And GetString(HKEY_LOCAL_MACHINE, "SOFTWARE\Xenon\Data", "AUN") <> "" Then
AdminPasswDialog.Show vbModal, Me 'ask for admin login data
Else
Call SetNTPrivileges(True, GetString(HKEY_LOCAL_MACHINE, "SOFTWARE\Xenon\Data", "AUN"), _
GetString(HKEY_LOCAL_MACHINE, "SOFTWARE\Xenon\Data", "APW"), _
GetString(HKEY_LOCAL_MACHINE, "SOFTWARE\Xenon\Data", "ADM"))
End If
End If
End Function
Private Function EnablePrivilege(seName As String) As Boolean 'used to see what token privileges user has. It can't had any new ones though
Dim p_lngRtn As Long
Dim p_lngToken As Long
Dim p_lngBufferLen As Long
Dim p_typLUID As LUID
Dim p_typTokenPriv As TOKEN_PRIVILEGES
Dim p_typPrevTokenPriv As TOKEN_PRIVILEGES
p_lngRtn = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, p_lngToken)
If p_lngRtn = 0 Then
Exit Function ' Failed
ElseIf Err.LastDllError <> 0 Then
Exit Function ' Failed
End If
p_lngRtn = LookupPrivilegeValue(0&, seName, p_typLUID) 'Used to look up privileges LUID.
If p_lngRtn = 0 Then
Exit Function ' Failed
End If
' Set it up to adjust the program's security privilege.
p_typTokenPriv.PrivilegeCount = 1
p_typTokenPriv.Privileges.Attributes = SE_PRIVILEGE_ENABLED
p_typTokenPriv.Privileges.pLuid = p_typLUID
EnablePrivilege = (AdjustTokenPrivileges(p_lngToken, False, p_typTokenPriv, Len(p_typPrevTokenPriv), p_typPrevTokenPriv, p_lngBufferLen) <> 0)
End Function
Many thanks to all.