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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OpenPrinter Crashes VB

Status
Not open for further replies.

mortgagedeveloper

Programmer
Sep 27, 2002
22
US
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
 

First off you declaration of OpenPrinter is not the standard declaration, the correct one is...

[tt]
Public Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As PRINTER_DEFAULTS) As Long
[/tt]

The type for PRINTER_DEFAULTS is...

[tt]
Public Type PRINTER_DEFAULTS
pDatatype As String
pDevMode As DEVMODE
DesiredAccess As Long
End Type
[/tt]

I have the openprinter API working with the standard declaration on a Win2KPro SP2 VS6SP5 machine.

I Hope this helps, Good Luck.

 
Thanks, works beautifully! That's what I get for getting the API declare from online...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top