Page 1 of 1 1
Topic Options
#143884 - 2005-07-19 11:32 PM PrinterList() suggestions...
Allen Administrator Offline
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 Offline
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

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#143886 - 2005-07-20 05:53 AM Re: PrinterList() suggestions...
Allen Administrator Offline
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
#143887 - 2005-07-20 06:07 AM Re: PrinterList() suggestions...
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Al, can you start IM. Just curious what's the difference between this and the

EnumPrinterConnections2() - Enumerates all connected printers UDF I did?




Edited by NTDOC (2005-07-20 06:11 AM)

Top
#143888 - 2005-07-20 06:10 AM Re: PrinterList() suggestions...
Allen Administrator Offline
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
#143889 - 2005-07-21 04:33 AM Re: PrinterList() suggestions...
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
If you really want to use bit-wise options then the followign is a much better approach:
Code:

bit 1 = port info
bit 2 = local printers
bit 3 = remote printers


This leaves room for expansion after bit three, thus
Code:

0 = nothing
1 = show port info, thus nothing
2 = show local printers
3 = show local printers, port info
4 = show remote printers
5 = show remote printers, port info
6 = show local and remote printers
7 = show local and remote printers, port info

_________________________
There are two types of vessels, submarines and targets.

Top
#143890 - 2005-07-21 05:12 AM Re: PrinterList() suggestions...
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4572
Loc: USA
Jens, just for my own learning... you described them as bits...is this correct?

&1 = bit 1
&2 = bit 2
&4 = bit 3
&8 = bit 4
...

The main reason I set it in the order I did, was so that anyone who happened to use an older version of the UDF would not have to change anything in their script if they chose to use the newer version.
_________________________
(... better days ahead)

Top
#143891 - 2005-07-21 05:20 AM Re: PrinterList() suggestions...
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
And before somebody chastizes me, the correct terminology would have been
Code:

[1|0] * 2^0 for the first bit
[1|0] * 2^1 for the second bit


and so on for 2^n and n in 0-7 for one byte.
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.137 seconds in which 0.104 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org