Dear,

We read the topic Determining the Default Printer, but
it didn't cover the whole story.

Question is: is there a way to determine which printer is currently set as the default.
Also we want to known: is mine printer locally connected.

For this task in W9x and NTx environment we have define two script versions.

kixtart 4.x edition
code:
FUNCTION Get_Default_Printer()
;
; Action: it returns the default printer in W9x and NTx environments.
; Syntax: Get_Default_printer()
; Parameters: none
; Returns: printer name
; or
; NONE (when no printer is default)
; Dependencies: none
; Examples: ? "default_printer "+Get_Default_Printer()
; result
; default_printer HP Laserjet 8000
;
DIM $key, $info
DIM $default_printer

; - printer mapping information - NTx -

$key="HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows"
IF (existkey($key) = 0) ; - NT -
$default_printer=ReadValue($key, "device")
IF (len($default_printer) <> 0) ; - HKCU.CurrentVersion.Windows[NT:default.printer] -
$info=$default_printer
ELSE
$info="NONE"
ENDIF
ELSE
IF (@inwin = 1)
$info="NONE"
ENDIF
ENDIF

; - printer mapping information - W9x -

;$key="HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\IDConfigDB:CurrentConfig" -> "0001"
$key="HKEY_LOCAL_MACHINE\Config\0001\System\CurrentControlSet\Control\Print\Printers"
IF (existkey($key) = 0) ; - 95 -
$default_printer=ReadValue($key, "default")
IF (len($default_printer) <> 0) ; - HKLM.print.printers[95:default.printer] -
$info=$default_printer
ELSE
$info="NONE"
ENDIF
ELSE
IF (@inwin <> 1)
$info="NONE"
ENDIF
ENDIF
;? "default_printer "+$info

$Get_Default_Printer=$info
ENDFUNCTION
FUNCTION Get_Default_Printer_Status()
;
; Action: it returns the connection status of default printer. connected by LPT or server.
; Syntax: Get_Default_printer_Status()
; Parameters: none
; Returns: LPTx: or NONE
; Dependencies: none
; Examples: ? "default_printer_status "+Get_Default_Printer()
; result
; default_printer_status LPT1:
;
DIM $key, $info
DIM $default_printer, $default_port

; - printer mapping information - NTx -

$key="HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows"
IF (existkey($key) = 0) ; - NT -
$default_printer=ReadValue($key, "device")
$default_port=""
IF (INSTR(LCASE($default_printer),",lpt") <> 0)
$default_port=UCASE(substr($default_printer,INSTR(LCASE($default_printer),",lpt")+1,5))
ENDIF
;
IF (len($default_port) <> 0)
$info=$info+$default_port ; - LPTx type -
ELSE
$info="NONE"
ENDIF
ELSE
IF (@inwin = 1)
$info="NONE"
ENDIF
ENDIF

; - printer mapping information - W9x -

;$key="HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\IDConfigDB:CurrentConfig" -> "0001"
$key="HKEY_LOCAL_MACHINE\Config\0001\System\CurrentControlSet\Control\Print\Printers"
IF (existkey($key) = 0) ; - 95 -
$default_printer=ReadValue($key, "default")
$default_port=""
$key="HKEY_LOCAL_MACHINE\System\CurrentControlSet\control\Print\Printers\"+$default_printer
IF (existkey($key) = 0)
$default_port=ReadValue($key, "Port")
ENDIF
;
IF (len($default_port) = 0)
$default_port="NONE"
ENDIF
;
$info="" ; - HKLM.print.printers[95:default.printer] -
IF (len($default_port) <> 0)
$info=$default_port
ELSE
$info=$info+"NONE"
ENDIF
ELSE
IF (@inwin <> 1)
$info="NONE"
ENDIF
ENDIF
;? "default_printer_status "+$info

$Get_Default_Printer_Status=$info
ENDFUNCTION

? "default_printer "+Get_Default_Printer()
? "default_printer_status "+Get_Default_Printer_Status()

The output can be something like this
code:
 default_printer            HP Laserjet 8000
default_printer_status LPT1:

the kixtart 3.6x and 4.x edition is
code:
    ; - printer mapping information - NTx -

$key="HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows"
IF (existkey($key) = 0) ; - NT -
$default_printer=ReadValue($key, "device")
$default_port=""
IF (INSTR(LCASE($default_printer),",lpt") <> 0)
$default_port=UCASE(substr($default_printer,INSTR(LCASE($default_printer),",lpt")+1,5))
ENDIF
;
IF (len($default_printer) = 0)
$default_printer="NONE"
ENDIF
IF (len($default_port) = 0)
$default_port="NONE"
ENDIF
;
$info="" ; - HKCU.CurrentVersion.Windows[NT:default.printer] -
IF (len($default_port) <> 0) AND ($default_port <> "NONE")
$info=$info+$default_port+" " ; - LPT type -
ENDIF
IF (len($default_printer) <> 0)
$info=$info+"'"+$default_printer+"'"
ENDIF
ELSE
IF (@inwin = 1)
$info="NONE"
ENDIF
ENDIF

; - printer mapping information - W9x -

;$key="HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\IDConfigDB:CurrentConfig" -> "0001"
$key="HKEY_LOCAL_MACHINE\Config\0001\System\CurrentControlSet\Control\Print\Printers"
IF (existkey($key) = 0) ; - 95 -
$default_printer=ReadValue($key, "default")
$default_port=""
$key="HKEY_LOCAL_MACHINE\System\CurrentControlSet\control\Print\Printers\"+$default_printer
IF (existkey($key) = 0)
$default_port=ReadValue($key, "Port")
ENDIF
;
IF (len($default_printer) = 0)
$default_printer="NONE"
ENDIF
IF (len($default_port) = 0)
$default_port="NONE"
ENDIF
;
$info="" ; - HKLM.print.printers[95:default.printer] -
IF (len($default_port) <> 0)
$info=$info+$default_port+" "
ENDIF
IF (len($default_printer) <> 0)
$info=$info+"'"+$default_printer+"'"
ENDIF
ELSE
IF (@inwin <> 1)
$info="NONE"
ENDIF
ENDIF

? "default_printer "+$info

The output can be something like this
code:
 default_printer     LPT1: 'HP Laserjet 8000'

greetings.
_________________________
email scripting@wanadoo.nl homepage scripting@wanadoo.nl | Links | Summary of Site Site KiXforms FAQ kixtart.org library collection mirror MCA | FAQ & UDF help file UDF kixtart.org library collection mirror MCA | mirror USA | mirror europe UDF scriptlogic library collection UDFs | mirror MCA