# Using Ruby to change printer preferences in Win32

**URL:** <https://www.ruby-forum.com/t/using-ruby-to-change-printer-preferences-in-win32/78782>\
**Category:** Ruby\
**Created:** [December 8, 2006, 3:36pm UTC](https://www.ruby-forum.com/t/using-ruby-to-change-printer-preferences-in-win32/78782 "2006-12-08T15:36:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@Anders](https://www.ruby-forum.com/u/Anders)\
**Post date:** [December 8, 2006, 3:36pm UTC](https://www.ruby-forum.com/t/using-ruby-to-change-printer-preferences-in-win32/78782/1 "2006-12-08T15:36:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Anders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@Anders](https://www.ruby-forum.com/u/Anders)\
**Post date:** [December 8, 2006, 3:50pm UTC](https://www.ruby-forum.com/t/using-ruby-to-change-printer-preferences-in-win32/78782/2 "2006-12-08T15:50:50Z")

</div>

On 12/8/06, Anders [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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).

---

<div class="post-metadata">

**Author:** ![Anders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@Anders](https://www.ruby-forum.com/u/Anders)\
**Post date:** [December 8, 2006, 3:52pm UTC](https://www.ruby-forum.com/t/using-ruby-to-change-printer-preferences-in-win32/78782/3 "2006-12-08T15:52:50Z")

</div>

Anders wrote:

> Anders Schneiderman
> 
> > do via Win32OLE? If so, where do I find the right OLE calls to change  
> > 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.

```auto
Vasudev Ram
Dancing Bison Enterprises
http://www.dancingbison.com

```

---

<div class="post-metadata">

**Author:** ![Anders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@Anders](https://www.ruby-forum.com/u/Anders)\
**Post date:** [December 8, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/using-ruby-to-change-printer-preferences-in-win32/78782/4 "2006-12-08T20:15:50Z")

</div>

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
