LPRINT ATTACH statement   

Purpose

Connect to a line-printer device for use with LPRINT.

Syntax

LPRINT ATTACH device$

Remarks

LPRINT ATTACH attempts a direct connection to the specified [line] printer device.  A line printer is one that will accept standard ASCII text and any device-specific control codes, such as CR, LF, and FF.

A line printer is named by the port to which it is attached (LPT1, etc.) because the data is sent directly to the port, not through a device driver.  That is, LPRINT communicates directly with the attached line printer device, bypassing the spooler and printer driver.  Therefore, any settings such as "work offline" in the Printer Properties dialog will be ignored.

Once the printer is attached by LPRINT ATTACH, print data can be sent to it with the LPRINT statement.

LPRINT ATTACH allows you to change the printer device used by LPRINT operation.  When executed, the current connection (if any) is closed and the new connection is established.  No colon is used in the device name.  For example, to connect to LPT2:

LPRINT ATTACH "LPT2"

or to a printer on a network server:

LPRINT ATTACH "\\SERVER\HPLJ5"

device$ must be a valid device name and cannot exceed 32 characters in length.  In some circumstances, such as with the Novell network client, LPRINT ATTACH with a UNC name may be rejected, and the LPRINT ATTACH will be unsuccessful, and a subsequent LPRINT$ test will return an empty string.

If LPRINT ATTACH is not successful, an Error 68 ("Device unavailable") is generated and LPRINT$ returns a nul (empty) string.  If no LPRINT ATTACH is ever executed (successful or not), PowerBASIC will attempt to connect to the line printer at LPT1.  Once any LPRINT ATTACH is attempted, no default to LPT1 will be presumed.

Care must be used with line printers in Windows, since if there is no available printer attached to the port, program execution may be suspended, with no errors.  So, it is wise to use LPRINT ATTACH to explicitly connect the intended printer device, and test for the successful connection by the examination of LPRINT$ and ERR.  For example:

ERRCLEAR

LPRINT ATTACH "LPT3"

IF ERR OR LPRINT$ = "" THEN PRINT "Connection failed"

Once all the data has been sent to the printer, detach the printer so other applications can use it., with the LPRINT CLOSE statement

Note: The Win32 API call EnumPrinters can give you a list of all valid printers and print devices, or you can enumerate the list of printers with the PRINTERCOUNT and PRINTER$ functions.

Restrictions

If device$ is an empty string, the current connection (if any) is detached.  This is equivalent to the LPRINT CLOSE statement.

See also

LPRINT, LPRINT CLOSE, LPRINT FLUSH, LPRINT FORMFEED, LPRINT$, XPRINT, XPRINT ATTACH

Example

' Typical LPRINT printing strategy

ERRCLEAR

LPRINT ATTACH "LPT2" ' Use LPT2 device

IF (ERR<>0) OR (LEN(LPRINT$)) THEN

  LPRINT "This is your line-printer talking"

  LPRINT FORMFEED      ' Issue a formfeed

  LPRINT FLUSH         ' flush the buffer

  LPRINT CLOSE     ' detach the printer

END IF