Using Ruby to change printer preferences in Win32

A

Anders

Our Windows XP sys admins recently changed things so that every day,
our networked printer preferences are reset using a script. Rather
than changing the preferences by hand every day so they suit my work
needs, I'd like to write a Ruby program I can run each morning. Any
pointers on where to get started? For example, is this something I
should
do via Win32OLE? If so, where do I find the right OLE calls to change
printer printing prefs?

Thanks,
Anders Schneiderman
 
V

vasudevram

Anders said:
Our Windows XP sys admins recently changed things so that every day,
our networked printer preferences are reset using a script. Rather
than changing the preferences by hand every day so they suit my work
needs, I'd like to write a Ruby program I can run each morning. Any
pointers on where to get started? For example, is this something I
should
do via Win32OLE? If so, where do I find the right OLE calls to change
printer printing prefs?

Thanks,
Anders Schneiderman
printer printing prefs?

In the MSDN documentation, probably. Its one of the main sources of
docs for Windows development.

A few ideas:

- once you've found the docs (don't know if there are OLE calls, you
might have to use Win32 or .NET APIs), you can either:

a) write a C/C++ program that does the printer pref setting, then call
it from Ruby via system() and pass the needed arguments (if not too
many, and if it work well to pass them as command-line args) or else
via a file that the Ruby program writes to and the C/C++ program reads
from. Though system() is a little slow, as it means spawning an
external EXE, if its done only once a day it might not matter.

b) write a Ruby-C/C++ extension to wrap the needed Windows calls so you
can call the needed APIs directly from Ruby. This is probably more work
than a).

c) use some other option, like communicate between Ruby and C/C++
programs via sockets. Again, some more work.

~~~~~~~~~~~~~~~~~~~~~~
Vasudev Ram
Dancing Bison Enterprises
http://www.dancingbison.com
~~~~~~~~~~~~~~~~~~~~~~
 
J

Jan Svitok

Our Windows XP sys admins recently changed things so that every day,
our networked printer preferences are reset using a script. Rather
than changing the preferences by hand every day so they suit my work
needs, I'd like to write a Ruby program I can run each morning. Any
pointers on where to get started? For example, is this something I
should
do via Win32OLE? If so, where do I find the right OLE calls to change
printer printing prefs?

If OLE then WMI is the most probable way. Have a look at WMI (Windows
Management Interface or something like that), if there's something
interesting. Other possibilities are through the registry or direct
WINAPI calls (Win32API).

If I understood correctly, you want top reverse the changes made by
your admins. If that is right, I assume the best place to look for
information is the admins' script itself. Chances are that it is a
wsh or .vbs script, that is more or less directly translatable to
ruby, using Win32OLE. It's possible that it is an .exe file, and that
is a harder case (regmon from sysinternals might be helpful then).
 
R

rpardee

This works for me:

require 'win32ole'
wn = WIN32OLE.new('Wscript.Network')
wn.SetDefaultPrinter('\\\mpe-ls1\mpe01lp')
puts("finished")

I'm not sure how the backslash escaping is working there, but like I
say--that's working for me...

HTH,

-Roy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top