GetPrinters() and GetDefaultPrinter()
Posted: Sat Jun 14, 2008 12:57 pm
Hi all
Neither of these functions exist in the LInux version of xHarbour apparently. I have just written such functions. My Linux version of GetPrinters() supports the first parameter (lPortInfo) but not the second one (lLocalPrinters). I have used the lpstat command to retrieve the necessary information. The xHarbour code is as follows:
The above work fine on Ubuntu 8.04 (my system) but should work on most Linux systems I believe. Having written the above it would be easy to create IsPrinter(), PrinterExists() and PrinterPortToName() functions if anyone needs them.
Regards
xProgrammer
Neither of these functions exist in the LInux version of xHarbour apparently. I have just written such functions. My Linux version of GetPrinters() supports the first parameter (lPortInfo) but not the second one (lLocalPrinters). I have used the lpstat command to retrieve the necessary information. The xHarbour code is as follows:
Code: Select all
FUNCTION GetPrinters( xPortInfo )
LOCAL sOSCommand
LOCAL sPrinterInfo
LOCAL iFileHandle
LOCAL iEndOffset
LOCAL sPrinterLine
LOCAL sPrinterName
LOCAL aPrinters
LOCAL aPrinter
IF PCOUNT() < 1
lPortInfo := .F.
ELSE
lPortInfo := xPortInfo
ENDIF
aPrinters := ARRAY( 0 )
sOSCommand := "lpstat -s > lpstat.txt"
RUN ( sOSCommand )
iFileHandle := FOpen( "lpstat.txt" )
DO WHILE HB_FReadLine( iFileHandle, @sPrinterLine, CHR(10) ) = 0
sTest := SUBSTR( sPrinterLine, 1, 11 )
IF sTest = "device for "
sTest := SUBSTR( sPrinterLine, 12 )
iEndOffset := AT( ": ", sTest )
IF iEndOffset > 0
sPrinterName := SUBSTR( sTest, 1, iEndOffset - 1 )
IF lPortInfo
sPrinterInfo := SUBSTR( sTest, iEndOffset + 2 )
aPrinter := ARRAY( 2 )
aPrinter[1] := sPrinterName
aPrinter[2] := sPrinterInfo
AAdd( aPrinters, aPrinter )
ELSE
AAdd( aPrinters, sPrinterName )
ENDIF
ENDIF
ENDIF
ENDDO
FClose( iFileHandle )
RETURN aPrinters
FUNCTION GetDefaultPrinter()
LOCAL sOSCommand
LOCAL sPrinterInfo
LOCAL iFileHandle
LOCAL sPrinterLine
LOCAL sPrinterName
aPrinters := ARRAY( 0 )
sOSCommand := "lpstat -s > lpstat.txt"
RUN ( sOSCommand )
iFileHandle := FOpen( "lpstat.txt" )
DO WHILE HB_FReadLine( iFileHandle, @sPrinterLine, CHR(10) ) = 0
sTest := SUBSTR( sPrinterLine, 1, 27 )
IF sTest = "system default destination:"
FClose( iFileHandle )
RETURN ALLTRIM( SUBSTR( sPrinterLine, 28 ) )
ENDIF
ENDDO
FClose( iFileHandle )
RETURN ""
Regards
xProgrammer