Results 1 to 3 of 3

Thread: VB - Login to another computer on the network

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    VB - Login to another computer on the network

    VB Code:
    1. 'in a .bas module
    2.  
    3. Option Explicit
    4. 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
    5. Private Declare Function ImpersonateLoggedOnUser Lib "advapi32.dll" (ByVal hToken As Long) As Long
    6. Private Declare Function RevertToSelf Lib "advapi32.dll" () As Long
    7. Private Const LOGON32_PROVIDER_DEFAULT    As Long = 0&
    8. Private Const LOGON32_PROVIDER_WINNT35    As Long = 1&
    9. Private Const LOGON32_LOGON_INTERACTIVE   As Long = 2&
    10. Private Const LOGON32_LOGON_NETWORK       As Long = 3&
    11. Private Const LOGON32_LOGON_BATCH         As Long = 4&
    12. Private Const LOGON32_LOGON_SERVICE       As Long = 5&
    13.  
    14.  
    15. Public Function doLogon(ByVal strAdminUser As String, ByVal strAdminPassword As String, ByVal strAdminDomain As String) As Boolean
    16. On Error GoTo DamnErr
    17.     Dim lngTokenHandle As Long
    18.     Dim lngLogonType As Long
    19.     Dim lngLogonProvider As Long
    20.     Dim blnResult As Boolean
    21.     lngLogonType = LOGON32_LOGON_INTERACTIVE
    22.     lngLogonProvider = LOGON32_PROVIDER_DEFAULT
    23.     blnResult = RevertToSelf()
    24.     blnResult = LogonUser(strAdminUser, strAdminDomain, strAdminPassword, _
    25.                                          lngLogonType, lngLogonProvider, _
    26.                                          lngTokenHandle)
    27.     blnResult = ImpersonateLoggedOnUser(lngTokenHandle)
    28.     doLogon = blnResult
    29.     Exit Function
    30. DamnErr:
    31.    Dim sERRORtxt As String
    32.    sERRORtxt = "Error Number: " & Err.Number & vbCrLf & _
    33.                "Description: " & Err.Description & vbCrLf & _
    34.                "Source: " & Err.Source & vbCrLf & _
    35.                "Function: doLogon" & vbCrLf & _
    36.                "Date: " & Now() & vbCrLf & _
    37.                "Input:" & vbCrLf & _
    38.                "  strAdminUser=" & strAdminUser & vbCrLf & _
    39.                "  strAdminPassword=" & strAdminPassword & vbCrLf & _
    40.                "  strAdminDomain=" & strAdminDomain
    41.    App.LogEvent sERRORtxt, vbLogEventTypeError
    42.    Err.Clear
    43. End Function
    44.  
    45. Public Function doLogoff() As Boolean
    46. On Error GoTo DamnErr
    47.     doLogoff = RevertToSelf()
    48.     Exit Function
    49. DamnErr:
    50. Dim sERRORtxt As String
    51.    sERRORtxt = "Error Number: " & Err.Number & vbCrLf & _
    52.                "Description: " & Err.Description & vbCrLf & _
    53.                "Source: " & Err.Source & vbCrLf & _
    54.                "Function: doLogoff" & vbCrLf & _
    55.                "Date: " & Now()
    56.    App.LogEvent sERRORtxt, vbLogEventTypeError
    57.    Err.Clear
    58. End Function



    When you call it:

    VB Code:
    1. doLogon "username", "password", "domain-if-needed"
    2. 'code here
    3. 'after tasks completed, log off from the pc
    4. doLogoff

  2. #2
    New Member
    Join Date
    May 2004
    Posts
    3

    Asking

    Hi,
    I try to do with your code given ,but when I login other computer in net work the function always return false

    (When i login by window it's ok )
    Example :
    I login computer with ip 192.1.1.20 with username : Greatful and pass : 1223456
    the function returns false

    Please give me your advices !

    Thanks,

  3. #3
    New Member
    Join Date
    Jun 2004
    Posts
    5

    the code didnot work

    Dear ....,

    Many thanks to your effort, but your code didnot work

    I send u the project ,


    waiting your reply
    Attached Files Attached Files
    My Best Wishes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width