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:
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
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