AW: Windows-Passwort vergleichen
24.01.2023 13:03:54
Nepumuk
Hallo Michael,
hier die Übersetzung in das "moderne" Excel:
Option Explicit
Private Declare PtrSafe Function CloseHandle Lib "kernel32" ( _
ByVal hObject As LongPtr) As Long
Private Declare PtrSafe Function GetVersion Lib "kernel32" () As Long
Private Declare PtrSafe Function LogonUserA Lib "Advapi32" ( _
ByVal lpszUserName As String, _
ByVal lpszDomain As String, _
ByVal lpszPassword As String, _
ByVal dwLogonType As Long, _
ByVal dwLogonProvider As Long, _
ByRef phToken As LongPtr) As Long
Private Declare PtrSafe Function WNetVerifyPasswordA Lib "mpr.dll" ( _
ByVal lpszPassword As String, _
ByRef pfMatch As Long) As Long
Private Declare PtrSafe Function GetComputerNameA Lib "kernel32" ( _
ByVal lpBuffer As String, _
ByRef nSize As Long) As Long
Private Declare PtrSafe Function GetUserNameA Lib "advapi32.dll" ( _
ByVal lpBuffer As String, _
ByRef nSize As Long) As Long
Private Function WhereAmI() As String
Dim s As String * 255
GetComputerNameA s, Len(s)
WhereAmI = Left$(s, InStr(s, vbNullChar) - 1)
End Function
Private Function WhoAmI() As String
Dim s As String * 255
GetUserNameA s, Len(s)
WhoAmI = Left$(s, InStr(s, vbNullChar) - 1)
End Function
Private Function IsNT() As Boolean
IsNT = (GetVersion And &H80000000) = 0
End Function
Public Function CheckPassword( _
ByVal Password As String, _
Optional ByVal UserName As String, _
Optional ByVal Domain As String) As Boolean
Const LOGON32_PROVIDER_DEFAULT = 0&
Const LOGON32_LOGON_NETWORK = 3&
Dim hToken As LongPtr
Dim fMatch As Long
If IsNT Then
If UserName = "" Then UserName = WhoAmI
CheckPassword = LogonUserA(UserName, Domain, Password, _
LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, hToken)
CloseHandle hToken
Else
If CBool(Len(Domain)) Then Exit Function
If CBool(Len(UserName)) Then If UserName > WhoAmI Then Exit Function
If WNetVerifyPasswordA(Password, fMatch) = 0 Then CheckPassword = CBool(fMatch)
End If
End Function
Gruß
Nepumuk