ClosePrinter Function

Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long

Platforms

Description & Usage

ClosePrinter closes a printer which had been previously opened by OpenPrinter. After this function closes the printer, the handle to the printer obviously can no longer be used, even if the function fails.

Return Value

If successful, the function returns a non-zero value. If an error occured, the function returns zero (use GetLastError to get the error code).

Visual Basic-Specific Issues

None.

Parameters

hPrinter
A handle to the open printer to close.

Example

Display the Properties dialog box for the system's default printer. The dialog box is opened when the user clicks the button cmdProperties. To use the example, first place a command button named cmdProperties on a form window.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Type PRINTER_INFO_1
	flags As Long
	pDescription As String
	pName As String
	pComment As String
End Type
Public Declare Function EnumPrinters Lib "winspool.drv" Alias "EnumPrintersA" (ByVal flags As Long, _
	ByVal name As String, ByVal Level As Long, pPrinterEnum As Long, ByVal cdBuf As Long, _
	pcbNeeded As Long, pcReturned As Long) As Long
Public Const PRINTER_ENUM_DEFAULT = &H1
Public Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As Any, _
	ByVal lpString2 As Any) As Long
Public Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Any) As Long
Public Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName _
	As String, phPrinter As Long, pDefault As Any) As Long
Public Declare Function PrinterProperties Lib "winspool.drv" (ByVal hWnd As Long, ByVal hPrinter _
	As Long) As Long
Public Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long

' *** Place the following code inside a form window. ***

Private Sub cmdProperties_Click()
	Dim pi1 As PRINTER_INFO_1  ' a little info about the printer
	Dim bytesNeeded As Long    ' size needed for buffer
	Dim numPrinters As Long    ' number of printers enumerated (should be 1)
	Dim buffer() As Long       ' buffer for printer information
	Dim slength As Long        ' length of string to copy
	Dim hPrinter As Long       ' handle to the printer
	Dim retval As Long         ' generic return value
	
	' Figure out how much space is needed to store the printer information.
	retval = EnumPrinters(PRINTER_ENUM_DEFAULT, vbNullString, 1, ByVal 0, 0, bytesNeeded, numPrinters)
	' Allocate that much space in the buffer array.
	ReDim buffer(0 To bytesNeeded / 4 - 1) As Long
	' Get information about the default printer.
	retval = EnumPrinters(PRINTER_ENUM_DEFAULT, vbNullString, 1, buffer(0), bytesNeeded, _
		bytesNeeded, numPrinters)
	' Make sure we were successful.
	If retval = 0 Or numPrinters <> 1 Then
		Debug.Print "No default printer or some other error."
		Exit Sub
	End If
	
	' Copy the data into the structure.
	With pi1
		' Copy numerical data directly.
		.flags = buffer(0)
		' Strings require more work, since the buffer holds pointers to them.
		.pDescription = Space(lstrlen(buffer(1)))
		retval = lstrcpy(.pDescription, buffer(1))
		.pName = Space(lstrlen(buffer(2)))
		retval = lstrcpy(.pName, buffer(2))
		.pComment = Space(lstrlen(buffer(3)))
		retval = lstrcpy(.pComment, buffer(3))
	End With
	
	' Open the printer.
	retval = OpenPrinter(pi1.pName, hPrinter, ByVal CLng(0))
	If retval <> 0 Then
		' Display the properties dialog.
		retval = PrinterProperties(Me.hWnd, hPrinter)
		' Close the printer.
		retval = ClosePrinter(hPrinter)
	Else
		Debug.Print "Unable to open printer!"
	End If
End Sub

See Also

OpenPrinter

Category

Printers

Go back to the Function listing.
Go back to the Reference section index.


Last Modified: January 21, 2001
This page is copyright © 2001 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/c/closeprinter.html