Page 1 of 1 1
Topic Options
#53945 - 2001-02-16 03:10 AM Weirdness with enumkey function
Anonymous
Unregistered


I am working on the following script...

----- start script
;
; This script is called from the logon script. It will remove all the network
; printers that have been added, then it will add the printers needed for the
; workstation that the user is logging into.
;
; Pat Hennessy - 2/15/2001
;

;
; Delete all network printers.
;
$index_one = 0

:del_printer_loop

$pinfo = enumkey("HKEY_CURRENT_USER\Printers\Connections\", $index_one)
? "$index_one"
if (@error = 0)
$pconnection = ""
$index_two = 1
do
$letter = substr($pinfo, $index_two, 1)
if ($letter = ",")
$pconnection = $pconnection + "\"
else
$pconnection = $pconnection + $letter
endif
$index_two = $index_two + 1
until ($index_two = len($pinfo) + 1)
? "$pconnection"
if delprinterconnection($pconnection)
$return = messagebox("An error occurred while removing $pconnection.", "Error removing printer (@error)", 4096 + 16)
endif
; $index_one = $index_one + 1
goto del_printer_loop
endif

;
; Delete old HKCU registry stuff
;
if (existkey("HKEY_CURRENT_USER\Software\DTCC") = 0)
$return = deltree("HKEY_CURRENT_USER\Software\DTCC")
endif

;
; Next we see if this workstation uses a windows print server.
;
if (existkey("HKEY_LOCAL_MACHINE\SOFTWARE\DTCC\Printing") = 0)
gosub "ntprint"
exit 0
else
exit 1
endif

;
; Thats all folks (except for the subroutine below)
;
exit 0

;
; Start of ntprint subroutine.
;
:ntprint

;
; Now we need to find and add the local printers.
;
$index = 0

:add_printer_loop

$val_name = enumvalue("HKEY_LOCAL_MACHINE\SOFTWARE\DTCC\Printing\Local Printers", $index)
? "$index"
if (@error = 0)
$new_printer = readvalue("HKEY_LOCAL_MACHINE\SOFTWARE\DTCC\Printing\Local Printers", $val_name)
? "$new_printer"
if (addprinterconnection($new_printer))
$return = messagebox("An error occurred while adding $new_printer.", "Error adding printer (@error)", 4096 + 16)
else
if (substr($new_printer, 13, 2) = "pl")
use lpt3: /del
use lpt3: "$new_printer"
if (@error > 0)
$return = messagebox("An error occurred while setting lpt3: to $new_printer.", "Error adding printer (@error)", 4096 + 16)
endif
endif
endif
$index = $index + 1
goto add_printer_loop
endif

;
; Next we need to determine the default printer.
;
$default_printer = readvalue("HKEY_LOCAL_MACHINE\SOFTWARE\DTCC\Printing", "Default Printer")

if (@error = 0)
if (setdefaultprinter($default_printer))
$return = messagebox("An error occurred while setting the default printer to $default_printer.", "Error setting default printer (@error)", 4096 + 16)
endif
if (substr($default_printer, 1, 2) = "\\")
use lpt2: /del
use lpt2: "$default_printer"
if (@error > 0)
$return = messagebox("An error occurred while setting lpt2: to $default_printer.", "Error setting default printer (@error)", 4096 + 16)
endif
endif
endif

; End of ntprint subroutine.
return
----- end script

The problem is using the enumkey() function in the del_printer_loop. I had to comment out the line to increment the $index_one variable because if I did increment $index_one it would only get the name of every other key. If $index_one stays at 0 the whole time, then it will read all of the keys. I've once played with perl modules that enumerate registry keys and I think they work the same way. Is this the expected behavior? The manual says that you need to use an index. I'm afraid to start using this on the domain if the script may hang on some machines.

Here is the output from running the script with the line incrementing the index commented out...

----- start
D:\temp>kix32 newprint

0
\\metroform\sp_a227
0
\\metroform\sp_a228
0
\\metroform\sp_a229
0
\\metroform\sp_a230
0
\\metroform\sp_a231
0
0
\\metroform\sp_a227
1
\\metroform\sp_a230
2
\\metroform\sp_a231
3
\\metroform\sp_a228
4
\\metroform\sp_a229
5
----- end

Here is what happens if the line is not commented out...

----- start
D:\temp>kix32 newprint

0
\\metroform\sp_a227
1
\\metroform\sp_a229
2
\\metroform\sp_a231
3
0
\\metroform\sp_a227
1
\\metroform\sp_a230
2
\\metroform\sp_a231
3
\\metroform\sp_a228
4
\\metroform\sp_a229
5
----- end


Top
#53946 - 2001-02-16 11:56 AM Re: Weirdness with enumkey function
kncowans Offline
Getting the hang of it

Registered: 2000-11-11
Posts: 98
Loc: Doncaster, UK
Hello

I know this does not seem to make sense (I know it confused me) but if you add another line that subtracts one straight after the line that adds one then it will work, i.e.

$index_one = $index_one + 1
$index_one = $index_one - 1

I hope this helps.

Bye for now

Kevin

_________________________
Kevin Cowans Senior ICT Technician The Armthorpe School

Top
#53947 - 2001-02-16 04:58 PM Re: Weirdness with enumkey function
Anonymous
Unregistered


Yea, that would work because the index needs to stay 0.

I just figured out the problem, the delprinterconnection() function is actually removing the subkeys that I am trying to read (from within the loop). So, the index needs to stay at zero the whole time to always get the first subkey. I then will remove that printer connection and there will be one less subkey in the list. I may not be explaining this the best way but it works.

So, what I did to get it to work is to get rid of $index_one from the script all together. I now just put 0 in the enumkey() function.

Next thing I want to do is get rid of storing which printers the script installs in the HKLM registry. I would like to put an ini file on a public share that the script can read. This way if we change printers for a certain classroom, we will only have to change it in one place.

Back to work....

Thanks for the input!

[This message has been edited by path (edited 16 February 2001).]

Top
#53948 - 2001-02-16 05:04 PM Re: Weirdness with enumkey function
Anonymous
Unregistered


By the way, here is the code that works...

code:

;
; This script is called from the logon script. It will remove all the network
; printers that have been added, then it will add the printers needed for the
; workstation that the user is logging into.
;
; Pat Hennessy - 2/16/2001
;

;
; Delete all network printers.
;
:del_printer_loop

$pinfo = enumkey("HKEY_CURRENT_USER\Printers\Connections\", 0)
if (@error = 0)
$pconnection = ""
$index = 1
do
$letter = substr($pinfo, $index, 1)
if ($letter = ",")
$pconnection = $pconnection + "\"
else
$pconnection = $pconnection + $letter
endif
$index = $index + 1
until ($index = len($pinfo) + 1)
if delprinterconnection($pconnection)
$return = messagebox("An error occurred while removing $pconnection.", "Error removing printer (@error)", 4096 + 16)
endif
goto del_printer_loop
endif

;
; Delete old HKCU registry stuff
;
if (existkey("HKEY_CURRENT_USER\Software\DTCC") = 0)
$return = deltree("HKEY_CURRENT_USER\Software\DTCC")
endif

;
; Next we see if this workstation uses a windows print server.
;
if (existkey("HKEY_LOCAL_MACHINE\SOFTWARE\DTCC\Printing") = 0)
gosub "ntprint"
exit 0
else
exit 1
endif

;
; Thats all folks (except for the subroutine below)
;
exit 0

;
; Start of ntprint subroutine.
;
:ntprint

;
; Now we need to find and add the local printers.
;
$index = 0

:add_printer_loop

$val_name = enumvalue("HKEY_LOCAL_MACHINE\SOFTWARE\DTCC\Printing\Local Printers", $index)
if (@error = 0)
$new_printer = readvalue("HKEY_LOCAL_MACHINE\SOFTWARE\DTCC\Printing\Local Printers", $val_name)
if (addprinterconnection($new_printer))
$return = messagebox("An error occurred while adding $new_printer.", "Error adding printer (@error)", 4096 + 16)
else
if (substr($new_printer, 13, 2) = "pl")
use lpt3: /del
use lpt3: "$new_printer"
if (@error > 0)
$return = messagebox("An error occurred while setting lpt3: to $new_printer.", "Error adding printer (@error)", 4096 + 16)
endif
endif
endif
$index = $index + 1
goto add_printer_loop
endif

;
; Next we need to determine the default printer.
;
$default_printer = readvalue("HKEY_LOCAL_MACHINE\SOFTWARE\DTCC\Printing", "Default Printer")

if (@error = 0)
if (setdefaultprinter($default_printer))
$return = messagebox("An error occurred while setting the default printer to $default_printer.", "Error setting default printer (@error)", 4096 + 16)
endif
if (substr($default_printer, 1, 2) = "\\")
use lpt2: /del
use lpt2: "$default_printer"
if (@error > 0)
$return = messagebox("An error occurred while setting lpt2: to $default_printer.", "Error setting default printer (@error)", 4096 + 16)
endif
endif
endif

; End of ntprint subroutine.
return



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
1 registered (Allen) and 1607 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.098 seconds in which 0.073 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