Mapping printers in VB.net

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
Hello, I am trying to write a program that will allow users to select a printer from a listbox and have it automatically add it to their machine. I think im on the right track with the code, but I keep running into a strange issue:

Code:
   Shell("rundll32 printui.dll,PrintUIEntry /in /n\\<PrintServerName>\MS - Library")

The problem that im having is that for some reason, windows is trying to connect to MS, instead of MS - Library. I read here:
http://www.vbforums.com/showthread....Network-Printer-to-All-Profiles-on-a-Computer

That if your networked printer name contains spaces, then to put double quotes around it, but I tried every single quote combination possible and it didnt help. I also created a string with the name of the printer and tried using that, but no matter what it cant figure out how to read past the MS part. Any help would be appreciated.
 

KLin

Lifer
Feb 29, 2000
29,917
362
126
Code:
Shell("rundll32 printui.dll,PrintUIEntry /in /n""\\<PrintServerName>\MS - Library""")

See if that works.
 

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
Code:
Shell("rundll32 printui.dll,PrintUIEntry /in /n""\\<PrintServerName>\MS - Library""")
See if that works.

That worked perfectly! Thank you very much for the help. Heck, you can even take the rest of the day off, youve earned it :)
 

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
OK then, so now I guess I have a new problem :p

I am trying to set the newly mapped printer as default, but I cant get it to do that either. I read on technet that the switch I want is /y, but even after reading all the examples I cant get it to set the printer as default. My current code looks like:
Code:
Shell("rundll32 printui.dll,PrintUIEntry /y /in /n""\\<print_server>\MS - Library""")
I have put the /y after the /in too, and that just results in the name being wrong again.
 

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
So I found a workaround, but its not really user friendly:
Code:
                Shell("rundll32 printui.dll,PrintUIEntry /in /n""\\<Print Server>\MS - Library""")
                MessageBox.Show("Please wait for installation to complete, then press enter")
                Shell("RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n""\\<print server>\MS - Library""")

Basically all this does is add the printer, popup a messagebox before the computer tries to set it as default, then sets it as default once you hit OK. Im still hoping there is a way to do this without any user intervention or wait timers.
 

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
Looks like shell has a wait option. Maybe that would help?

Great idea, thats exactly what I was looking for. My current code looks like this:
Code:
                Shell("rundll32 printui.dll,PrintUIEntry /in /n""\\<print server>\MS - Library""", ,True)
                Shell("RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n""\\<print server>\MS - Library""")
It adds the printer, waits for that operation to complete, then moves onto the next line of code which sets it as default.

Thank you!
 

sm625

Diamond Member
May 6, 2011
8,172
137
106
Thread should be titled "mapping printer from command line". It is not really relevant to VB.
 

Smoblikat

Diamond Member
Nov 19, 2011
5,184
107
106
Thread should be titled "mapping printer from command line". It is not really relevant to VB.

Well, im using vb.net as my pretty GUI, and just executing commands to CMD through it, so its semi-relevant.