Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LogonUser API call returns false

Status
Not open for further replies.

btt423

Programmer
Jan 28, 2002
31
US
I'm trying to impersonate a user so that I can get a listing of printers that are available on a web server. My code is:

Code:
Const LOGON32_LOGON_INTERACTIVE = 2
Const LOGON32_PROVIDER_DEFAULT = 0

Public Sub Logon(ByVal strAdminUser As String, ByVal strAdminPassword As String, ByVal strAdminDomain As String)
    Dim lngTokenHandle, lngLogonType, lngLogonProvider As Long
    Dim blnResult As Boolean

    lngLogonType = LOGON32_LOGON_INTERACTIVE
    lngLogonProvider = LOGON32_PROVIDER_DEFAULT

    WriteToLog ("Logging on...")
    blnResult = RevertToSelf()
    blnResult = LogonUser(strAdminUser, strAdminDomain, strAdminPassword, lngLogonType, lngLogonProvider, lngTokenHandle)
    WriteToLog ("LogonUser executed... result = " & blnResult)
    blnResult = ImpersonateLoggedOnUser(lngTokenHandle)
    WriteToLog ("Impersonate executed... result = " & blnResult)
End Sub

Public Sub Logoff()
    Dim blnResult As Boolean

    blnResult = RevertToSelf()
    WriteToLog ("Logoff executed... result = " & blnResult)
End Sub

Public Function PrintTest() As Boolean
    Dim blnFoundPrinter As Boolean
    
    blnFoundPrinter = False
    WriteToLog ("Finding printers...")
    strChosenPrinter = "HP LaserJet 4050 Series PCL"
    'strChosenPrinter = "Win2PDF"
    i = 0
    For Each prnAvailPrinter In Printers
        WriteToLog "Printer Device: " & prnAvailPrinter.DeviceName
        If prnAvailPrinter.DeviceName = strChosenPrinter Then
            Set Printer = Printers(i)
            WriteToLog "Printer found: " & Printer.DeviceName
            blnFoundPrinter = True
            Exit For
        End If
        i = i + 1
    Next
    
    PrintTest = blnFoundPrinter
End Function

Public Sub WriteToLog(strText As String)
    
    Open App.Path & "\peLog.txt" For Append As #1
    Write #1, "[" & Now & "] " + strText
    Close #1
    
End Sub

The LogonUser function returns false every time. An asp page will call this (actually this is just a test, I have a print engine written that will incorporate this functionality), so it can then in turn fire off a procedure that will print to a pre-specified printer that will be available on the server through a network. Any help would be greatly appreciated!! I'm pulling my hair out!

Thanks, Brinson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top