Print system functions
Posted: Tue May 30, 2006 7:49 am
Code: Select all
*******************
FUNCTION WinGetPrnD(cNomDriver)
*******************
* Return the driver filename without his extension
* Recherche le driver d'une imprimante installée sous windows
* dans le registre de Windows (remplace le WIN.INI
* qui est obsolète sous Windows 2000 et suivants)
* Auteur Badara THIAM
LOCAL nHandle
LOCAL cValue
LOCAL n1
LOCAL n2
LOCAL nok
LOCAL nLen
LOCAL cSubkeys
LOCAL cSubkeys2
LOCAL X, X2
LOCAL TENV := {}
LOCAL TENVDRIVER := {}
LOCAL oReg
LOCAL cDriver := ""
cSubKeys := "System\CurrentControlSet\Control\Print\Environments"
IF RegOpenKey( HKEY_LOCAL_MACHINE, cSubKeys, @nHandle ) == 0
SysRefresh()
n1 := 0
DO WHILE .T.
cValue := ""
n2 := RegEnumKey( nHandle, n1, @cvalue )
IF n2 = 0
IF ASCAN(TENV, STRTRAN(cValue, "," , "\")) = 0
AADD(TENV, STRTRAN(cValue, "," , "\"))
ENDIF
ELSE
EXIT
ENDIF
n1 ++
SysRefresh()
ENDDO
RegCloseKey( nHandle )
SysRefresh()
FOR X := 1 TO LEN(TENV)
cSubKeys2 := cSubKeys + "\" + TENV[X] + "\Drivers"
oReg := TReg32():New(HKEY_LOCAL_MACHINE, cSubKeys2 + "\" + cNomDriver)
IF oReg:nError = 0
cDriver := oReg:Get("Driver", "")
oReg:Close()
oReg := NIL
IF !EMPTY(cDriver)
cDriver := IIF("." $ cDriver, LEFT(cDriver, AT(".", cDriver) - 1), cDriver)
EXIT
ENDIF
ELSE
oReg:Close()
oReg := NIL
IF RegOpenKey( HKEY_LOCAL_MACHINE, cSubKeys2, @nHandle ) == 0
n1 := 0
TENVDRIVER := {}
DO WHILE .T.
cValue := ""
n2 := RegEnumKey( nHandle, n1, @cvalue )
IF n2 = 0
IF ASCAN(TENVDRIVER, STRTRAN(cValue, "," , "\")) = 0
AADD(TENVDRIVER, STRTRAN(cValue, "," , "\"))
ENDIF
ELSE
EXIT
ENDIF
SysRefresh()
n1 ++
ENDDO
RegCloseKey( nHandle )
SysRefresh()
FOR X2 := 1 TO LEN(TENVDRIVER)
oReg := TReg32():New(HKEY_LOCAL_MACHINE, cSubKeys2 + "\" + TENVDRIVER[X2] + "\" + cNomDriver)
IF oReg:nError = 0
cDriver := oReg:Get("Driver", "")
oReg:Close()
oReg := NIL
IF !EMPTY(cDriver)
cDriver := IIF("." $ cDriver, LEFT(cDriver, AT(".", cDriver) - 1), cDriver)
EXIT
ENDIF
ELSE
oReg:Close()
oReg := NIL
ENDIF
SysRefresh()
NEXT X2
IF !EMPTY(cDriver)
EXIT
ENDIF
ENDIF
ENDIF
SysRefresh()
NEXT X
ENDIF
SysRefresh()
RETURN cDriver
*******************
FUNCTION WinGetPrnP(cNomImpr)
*******************
* Return the driver filename and the port of any printer
* Renvoie le (nom du fichier) Driver et le Port de l'imprimante
* dont le nom est le contenu de cNomImpr.
* Le Driver et le Port sont renvoyés dans une chaine,
* séparés par une virgule.
* Auteur Badara THIAM
LOCAL cDriver := ""
LOCAL cNomDriver := ""
LOCAL cPort := ""
LOCAL oReg
LOCAL X
LOCAL TIMP := WINGETPRN()
* Recherche l'imprimante Windows ayant le même nom,
* en convertissant en minuscule et en supprimant les espaces
FOR X := 1 TO LEN(TIMP)
IF MEMEIMPRIM(@cNomImpr, TIMP[X])
* Si c'est la même imprimante
EXIT
ENDIF
NEXT X
IF X <= LEN(TIMP)
oReg := TReg32():New(HKEY_LOCAL_MACHINE,;
"System\CurrentControlSet\Control\Print\Printers\" + STRTRAN(TIMP[X],"\",","))
IF oReg:nError = 0
cPort := oReg:Get("Port", "")
cNomDriver := oReg:Get("Printer Driver", "")
ENDIF
oReg:Close()
oReg := NIL
IF cNomDriver != ""
cDriver := WINGETPRND(@cNomDriver)
ENDIF
ENDIF
RETURN cDriver + "," + cPort