I'm trying to use a LogonUser API call from a VB6 app running on win2k to do Local user authentication before calling another application. My LogonUser call always returns 0, and a call to GetLastError immediately following also always return 0. If this was a SE_TCB_NAME permission problem, I would think that GetLastError would return something. I copied this code from Hampster's reply to a question back in December. It apparently worked for the poster, but not for me for some reason. I'll bet I'm doing something stupid.
Here's the code:
***********************************
Public Declare Function GetUserName Lib "ADVAPI32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetLastError Lib "kernel32.dll" () As Long
Private Declare Function LogonUser _
Lib "Advapi32" 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
' Constants used by LogonUser
Private Const LOGON32_PROVIDER_DEFAULT As Long = 0&
Private Const LOGON32_PROVIDER_WINNT35 As Long = 1&
Private Const LOGON32_LOGON_INTERACTIVE As Long = 2&
Private Const LOGON32_LOGON_NETWORK As Long = 3&
Private Const LOGON32_LOGON_BATCH As Long = 4&
Private Const LOGON32_LOGON_SERVICE As Long = 5&
Public Function Login(ByVal xi_strUserID As String, _
ByVal xi_strPassword As String) As Boolean
On Error Resume Next ' Don't accept errors here
Dim p_lngToken As Long
Dim p_lngRtn As Long
If p_lngRtn = 0 Then
Login = False
MsgBox "Login Failure: " & GetLastError()
Else
Login = True
End If
On Error GoTo 0
End Function
************************************
And here's the call and the code leading to it.
************************************
Private Sub cmdSubmit_Click()
Dim rc As Boolean
rc = Login(txtUserName.Text & Chr(0), txtPassword.Text & Chr(0))
If rc = True Then
MsgBox "Authentication success.", vbOKOnly, "Auth results"
Else
MsgBox "Authentication failure.", vbOKOnly, "Auth results"
End If
End Sub
The first obvious error I see in your code is that you pass 0& to lpszDomain even though it is declared as a string. If you want to pass NULL here simply change the declaration to take a Long instead (still using ByVal so you pass NULL and not the address the null value is stored in).
First I tried changing the
Private Function Declare LogonUser ... ByVal lpszDomain As String
to
Private Function Declare LogonUser ... ByVal lpszDomain As Long
and leaving the call with lpszDomain as 0&
no change.
I then tried using the original Declare LogonUser and passing in "." instead of 0&.
Unfortunately -- I think the LogonUser API needs special permissions (local Admin) to run properly... I ran into problems with that -- I found this module instead...
Use the IsValidNTAccount function (returns boolean)
Thx - pd
{edit} It occurs to me that I may have gotten this code from this board (in all likelyhood) -- so my thanks to whomever was the original author!