• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

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.

  • 3
  • 2
1 Solution
Peter KipropCommented:
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 _

Private Sub cmdCancel_Click()
    LoginSucceeded = False
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, _
    ' a non-zero value means success
    If ret Then
        CheckWindowsUser = True
        CloseHandle hToken
        'Load main formor do action etc
      '  Unload Me 
        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

AndrewBanferAuthor Commented:
Thanks,  I'll give this a try shortly.
AndrewBanferAuthor Commented:
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?
Peter KipropCommented:
Hi AndrewBanfer,

This is not possible, unless done administratively. probably the use of biometric will help
AndrewBanferAuthor Commented:
Thanks very much and have a great day!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now