mortgagedeveloper
Programmer
I am trying to get a handle for GetPrinterDriver with the OpenPrinter function but it keeps crashing VB. I need to be able to open a printer on a network print server so I can access the print driver and programatically install printers on the fly. Here is some of what I am working with...
========================================================
Private Declare Function OpenPrinter Lib "winspool.drv" _
Alias "OpenPrinterA" ( _
ByVal pPrinterName As String, _
phPrinter As Long, _
pDefault As Any) As Long
Private Declare Function GetPrinterDriver Lib "winspool.drv" _
Alias "GetPrinterDriverA" ( _
ByVal hPrinter As Long, _
ByVal pEnvironment As String, _
ByVal Level As Long, _
pDriverInfo As Byte, _
ByVal cdBuf As Long, _
pcbNeeded As Long) As Long
Private Function GetPrintDriverFromPrintServer( _
ByVal sPrintServer As String, _
ByVal sPrinterName As String, _
Optional ByVal sEnviron As String) As DRIVER_INFO_3
Dim cbRequired As Long
Dim cbBuffer As Long
Dim pPi2() As PRINTER_INFO_2
Dim Pi2 As PRINTER_INFO_2
Dim nEntries As Long
Dim sPrintDriverDir As String
Dim i As Integer
Dim hPrinter As Long
Dim pDi3() As DRIVER_INFO_3
Dim Di3 As DRIVER_INFO_3
'get the required size of the pointer
Call EnumPrinters(PRINTER_ENUM_NAME, sPrintServer, 2, _
0, 0, cbRequired, nEntries)
'resize the pointer
ReDim pPi2((cbRequired \ SIZEOFPRINTER_INFO_2))
cbBuffer = cbRequired
'now get the actual enumeration of PRINTER_INFO_2s
If EnumPrinters(PRINTER_ENUM_NAME, sPrintServer, 2, _
pPi2(0), cbBuffer, cbRequired, nEntries) Then
For i = 0 To nEntries - 1
'if found get a copy of the PRINTER_INFO_2 type
If InStr(GetStrFromPtrA(pPi2(i).pPrinterName), sPrinterName) > 0 Then
Debug.Print sPrinterName & " found!"
Pi2 = pPi2(i)
Exit For
End If
Next
If OpenPrinter(GetStrFromPtrA(Pi2.pPrinterName), hPrinter, Null) Then
GetPrinterDriver hPrinter, Null, 3, 0, 0, cbRequired
ClosePrinter hPrinter
Else
Debug.Print "Could not get a handle to " & sPrinterName
End If
End If
End Function
Sub Main
GetPrintDriverFromPrintServer "\\DEVTEST", "HP5"
End Sub
========================================================
Everything up to the openprinter call works fine but I get a memory reference error trying to open it. I tried putting in a local printer name also and it still crashes.
Using VB6 SP5 on W2K SP2
Any help would be appreciated.
Thanks
========================================================
Private Declare Function OpenPrinter Lib "winspool.drv" _
Alias "OpenPrinterA" ( _
ByVal pPrinterName As String, _
phPrinter As Long, _
pDefault As Any) As Long
Private Declare Function GetPrinterDriver Lib "winspool.drv" _
Alias "GetPrinterDriverA" ( _
ByVal hPrinter As Long, _
ByVal pEnvironment As String, _
ByVal Level As Long, _
pDriverInfo As Byte, _
ByVal cdBuf As Long, _
pcbNeeded As Long) As Long
Private Function GetPrintDriverFromPrintServer( _
ByVal sPrintServer As String, _
ByVal sPrinterName As String, _
Optional ByVal sEnviron As String) As DRIVER_INFO_3
Dim cbRequired As Long
Dim cbBuffer As Long
Dim pPi2() As PRINTER_INFO_2
Dim Pi2 As PRINTER_INFO_2
Dim nEntries As Long
Dim sPrintDriverDir As String
Dim i As Integer
Dim hPrinter As Long
Dim pDi3() As DRIVER_INFO_3
Dim Di3 As DRIVER_INFO_3
'get the required size of the pointer
Call EnumPrinters(PRINTER_ENUM_NAME, sPrintServer, 2, _
0, 0, cbRequired, nEntries)
'resize the pointer
ReDim pPi2((cbRequired \ SIZEOFPRINTER_INFO_2))
cbBuffer = cbRequired
'now get the actual enumeration of PRINTER_INFO_2s
If EnumPrinters(PRINTER_ENUM_NAME, sPrintServer, 2, _
pPi2(0), cbBuffer, cbRequired, nEntries) Then
For i = 0 To nEntries - 1
'if found get a copy of the PRINTER_INFO_2 type
If InStr(GetStrFromPtrA(pPi2(i).pPrinterName), sPrinterName) > 0 Then
Debug.Print sPrinterName & " found!"
Pi2 = pPi2(i)
Exit For
End If
Next
If OpenPrinter(GetStrFromPtrA(Pi2.pPrinterName), hPrinter, Null) Then
GetPrinterDriver hPrinter, Null, 3, 0, 0, cbRequired
ClosePrinter hPrinter
Else
Debug.Print "Could not get a handle to " & sPrinterName
End If
End If
End Function
Sub Main
GetPrintDriverFromPrintServer "\\DEVTEST", "HP5"
End Sub
========================================================
Everything up to the openprinter call works fine but I get a memory reference error trying to open it. I tried putting in a local printer name also and it still crashes.
Using VB6 SP5 on W2K SP2
Any help would be appreciated.
Thanks