#143884 - 2005-07-19 11:32 PM
PrinterList() suggestions...
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4572
Loc: USA
|
From this thread, Rad suggested adding local, remote, or all printers...
I don't much like the way I'm doing this, any suggestions?
$displaymode Value Meaning 0 - show all printers, don't display port info 1 - show all printers, display port info 2 - show local printers, don't display port info 3 - show local printers, display port info 4 - show remote printers, don't display port info 5 - show remote printers, display port info
Code:
Function PrinterList(optional $remotepc, optional $displaymode) dim $service,$printer,$printers,$printerdesc[0],$counter if $remotepc="" $remotepc="." endif $Service = GetObject("winmgmts:\\" + $remotepc + "\root\cimv2") if @error exit @error endif $Printers=$service.execquery ('select * from Win32_Printer') for each $printer in $printers redim preserve $printerdesc[$counter] select case $displaymode=5 if left($printer.portname,2)="\\" $printerdesc[$counter]=$printer.name + "," + $printer.portname $counter=$counter+1 endif case $displaymode=4 if left($printer.portname,2)="\\" $printerdesc[$counter]=$printer.name $counter=$counter+1 endif case $displaymode=3 if left($printer.portname,2)<>"\\" $printerdesc[$counter]=$printer.name + "," + $printer.portname $counter=$counter+1 endif case $displaymode=2 if left($printer.portname,2)<>"\\" $printerdesc[$counter]=$printer.name $counter=$counter+1 endif case $displaymode=1 $printerdesc[$counter]=$printer.name + "," + $printer.portname $counter=$counter+1 case 1 $printerdesc[$counter]=$printer.name $counter=$counter+1 endselect next $PrinterList=$printerdesc endfunction
_________________________
(... better days ahead)
|
|
Top
|
|
|
|
#143885 - 2005-07-20 12:08 AM
Re: PrinterList() suggestions...
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
try this (untested)
result is array to get ports split each element on chr(13)
Code:
Function PrinterList(optional $remotepc, optional $ports, optional $types) dim $service,$printer,$printers,$printerdesc[0],$counter
if not $remotepc $remotepc="." endif if not $types $types = "b" endif
$Service = GetObject("winmgmts:\\" + $remotepc + "\root\cimv2") if @error exit @error endif
$Printers=$service.execquery ('select * from Win32_Printer') for each $printer in $printers $port = iif($ports, $printer.portname,'') if not left($types,1) = 'l' if left($printer.portname,2) = '\\' $installedprinters = $installedprinters + chr(13) + $printer.name + chr(10) + $port endif endif
if not left($types,1) = 'n' if not left($printer.portname,2) = '\\' $installedprinters = $installedprinters + chr(13) + $printer.name + chr(10) + $port endif endif next if not $port $installedprinters = join(split($installedprinters,chr(10),'') endif
$PrinterList = split(subst($installedprinters,2),chr(13)) endfunction
|
|
Top
|
|
|
|
#143886 - 2005-07-20 05:53 AM
Re: PrinterList() suggestions...
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4572
Loc: USA
|
Rad, you've definately got some bug-a-boos in your code... 
I think I've got it the way I want now, but I'm still looking for suggestions...
BTW, using bitwise logic is slick... first time I've really used it.
Code:
Function PrinterList(optional $remotepc, optional $displaymode) dim $service,$printer,$printers,$printerdesc[0],$counter,$portname if $remotepc="" $remotepc="." endif $Service = GetObject("winmgmts:\\" + $remotepc + "\root\cimv2") if @error exit @error endif $Printers=$service.execquery ('select * from Win32_Printer') for each $printer in $printers redim preserve $printerdesc[$counter] if $displaymode & 1 $portname = "," + $printer.portname endif select case $displaymode & 4 ;remote printers if left($printer.portname,2)="\\" $printerdesc[$counter]=$printer.name + $portname $counter=$counter+1 endif case $displaymode & 2 ;local printers if left($printer.portname,2)<>"\\" $printerdesc[$counter]=$printer.name + $portname $counter=$counter+1 endif case 1 ; all printers $printerdesc[$counter]=$printer.name + $portname $counter=$counter+1 endselect next $PrinterList=$printerdesc endfunction
_________________________
(... better days ahead)
|
|
Top
|
|
|
|
#143888 - 2005-07-20 06:10 AM
Re: PrinterList() suggestions...
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4572
Loc: USA
|
...this reduces some of the duplicate code
Code:
Function PrinterList(optional $remotepc, optional $displaymode) dim $service,$printer,$printers,$printerdesc[0],$counter,$portname,$printername if $remotepc="" $remotepc="." endif $Service = GetObject("winmgmts:\\" + $remotepc + "\root\cimv2") if @error exit @error endif $Printers=$service.execquery ('select * from Win32_Printer') for each $printer in $printers redim preserve $printerdesc[$counter] if $displaymode & 1 $portname = "," + $printer.portname endif select case $displaymode & 4 ;remote printers if left($printer.portname,2)="\\" $printername=$printer.name endif case $displaymode & 2 ;local printers if left($printer.portname,2)<>"\\" $printername=$printer.name endif case 1 ; all printers $printername=$printer.name endselect if $printername<>"" $printerdesc[$counter]=$printername + $portname $counter=$counter + 1 $printername="" endif next $PrinterList=$printerdesc endfunction
_________________________
(... better days ahead)
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 2220 anonymous users online.
|
|
|