Link to home
Start Free TrialLog in
Avatar of AndrewBanfer
AndrewBanfer

asked on

I need to sync a VB 6 program's User ID and Password with the Windows User ID and Password

I need to sync a VB 6 program's User ID and Password with the Windows User ID and Password.

How can I capture the Windows User's Password in Visual Basic 6?  

The VB 6 program uses the Windows User ID and Password to commit a scientific action that a user performs.  Just in case someone walks away from the PC and another user goes to walks up and commits the scientific action.

Thanks!
Avatar of Peter Kiprop
Peter Kiprop
Flag of Kenya image

Hi AndrewBanfer,

There is no way you can capture windows users password. You can always get if the right password has been entered or not.

The below code is a sample of what i have used to authenticate users using windows paswords. You can always use thesame to update users password to a table if it returns a success etc.
]

Option Explicit

Public LoginSucceeded As Boolean
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
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As _
    Long
Const LOGON32_PROVIDER_DEFAULT = 0&
Const LOGON32_LOGON_NETWORK = 3&

Private Sub cmdCancel_Click()
    LoginSucceeded = False
    Me.Hide
End Sub

' Check whether a username/password pair is correct
'
' if DOMAIN is omitted, it uses the local account database
' and then asks trusted domains to search their account databases
' until it finds the account or the search is exhausted
' use DOMAIN="." to search only the local account database
'
'  IMPORTANT: works only under Windows NT,2000 and 2003

Private Function CheckWindowsUser(ByVal UserName As String, _
    ByVal Password As String, Optional ByVal Domain As String) As Boolean
    Dim hToken As Long, ret As Long

    ' provide a default for the Domain name
    If Len(Domain) = 0 Then Domain = vbNullString
    ' check the username/password pair
    ' using LOGON32_LOGON_NETWORK delivers the best performance
    ret = LogonUser(UserName, Domain, Password, LOGON32_LOGON_NETWORK, _
        LOGON32_PROVIDER_DEFAULT, hToken)
    
    ' a non-zero value means success
    If ret Then
        CheckWindowsUser = True
        CloseHandle hToken
        'Load main formor do action etc
      '  Unload Me 
    Else
        MsgBox "Credentials supplied are invalid. Please try again.", vbExclamation
    End If

End Function
Private Sub cmdOK_Click()
    CheckWindowsUser txtUserName, txtPassword, TxtDomain
End Sub

Open in new window

Avatar of AndrewBanfer
AndrewBanfer

ASKER

Thanks,  I'll give this a try shortly.
Hi Pthepebble,

I got midway through implementing your solution and talked to my client again.  

Is there a way to confirm a Windows User is valid to use the PC without being the Windows User who is actually on the PC?

For example,
1) we are acquiring data from an analytical instrument connected to a Windows PC.  

2)  We can't use the Windows Inactivity Logout because the instrument will stop acquiring data.  

3)  There are 3 shifts of personnel running the analytical instrument.  The instrument can acquire data for 20 hours every day.  So, one shift will leave while logged into Windows to keep the analytical instrument acquiring data.  If we make a program to require a login after a period of inactivity.  Can we check that the new user is a valid user of the PC without checking that the user is valid to the initial person logged in?
ASKER CERTIFIED SOLUTION
Avatar of Peter Kiprop
Peter Kiprop
Flag of Kenya image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks very much and have a great day!