LogonUser and advapi32.dll
Hello, I wrote a VB application to validate an NT login screen. Nothing
fancy, just trying new things. This program (source code below) was working
perfect until I rebuilt my machine. For some reason now when it calls the
LOGONUSER function it always returns false! I'm lost. I'm on an NT
workstation with VB6.0 loaded with MDAC 2.1, NT OPtions pack 4.0 with SP4.
(if you need to know what else was loaded that might help please let me
know). I theory is that at some point prior, I must have loaded something
which might have given me the latest advapi32.dll (not sure what). Where
can I get the latest dll (from what product)?
Any advice or leads into the right direction would be greatly appreciated!!
Thanks,
Mark Lavoie
============================================================================
===
Option Explicit
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpbuffer As String, nSize As Long) As Long
Private Declare Function LogonUser Lib "advapi32.dll" Alias "LogonUserA"
(ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal
lpszPassword As String, ByVal dwLogonType As Long, ByVal dwLogonProvider As
Long, phToken As Long) As Long
Private Declare Function ImpersonateLoggedOnUser Lib "advapi32.dll" (ByVal
hToken As Long) As Long
Private Declare Function RevertToSelf Lib "advapi32.dll" () As Long
Private UserName As String
Private Password As String
Private Domain As String
Public Function ValLogon(ByVal strAdminUser As String, ByVal
strAdminPassword As String, ByVal strAdminDomain As String)
Dim Ok As Boolean
Dim lngTokenHandle As Long
Dim lngLogonType As Long
Dim lngLogonProvider As Long
On Error GoTo ErrorHandler
lngLogonType = 2 'LOGON32_LOGON_INTERACTIVE
lngLogonProvider = 0 'LOGON32_PROVIDER_DEFAULT
Ok = RevertToSelf()
Ok = LogonUser(strAdminUser, strAdminDomain, strAdminPassword,
lngLogonType, lngLogonProvider, lngTokenHandle)
Ok = ImpersonateLoggedOnUser(lngTokenHandle)
ValLogon = Ok
Exit Function
ErrorHandler:
MsgBox Err.Description
End Function
Private Sub cmdOK_Click()
UserName = txtUserName.Text
Domain = "tbdomn01"
Password = txtPassword.Text
If ValLogon(UserName, Password, Domain) Then
MsgBox "You're In!!", , "Login"
Else
MsgBox "Invalid Password, try again!", , "Login"
End If
End Sub
Public Sub Form_Load()
Dim sBuffer As String
Dim lSize As Long
sBuffer = Space$(255)
lSize = Len(sBuffer)
Call GetUserName(sBuffer, lSize)
If lSize > 0 Then
txtUserName.Text = Left$(sBuffer, lSize)
Else
UserName = vbNullString
End If
End Sub