Question Adding LPR ports and printers programatically??

trey5498

Member
Joined
Oct 13, 2008
Messages
6
Programming Experience
Beginner
VB.NET:
My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\LPR Port\Ports\at-cetus.ad.ufl.edu:CSE-211-CLQ", "EnableBannerPage", 0, Microsoft.Win32.RegistryValueKind.DWord) 
        My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\LPR Port\Ports\at-cetus.ad.ufl.edu:CSE-211-CLQ", "HpUxCompatibility", 0, Microsoft.Win32.RegistryValueKind.DWord) 
        My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\LPR Port\Ports\at-cetus.ad.ufl.edu:CSE-211-CLQ", "OldSunCompatibility", 0, Microsoft.Win32.RegistryValueKind.DWord) 
        My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\LPR Port\Ports\at-cetus.ad.ufl.edu:CSE-211-CLQ", "Printer Name", "CSE-211-CLQ") 
        My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\LPR Port\Ports\at-cetus.ad.ufl.edu:CSE-211-CLQ", "Server Name", "at-cetus.ad.ufl.edu") 
 
Shell("net stop spooler") 
Shell("net start spooler") 
 
Shell("rundll32 printui.dll, PrintUIEntry /if /b "test_1" /f "C:\7760.inf" /q /r "at-cetus.ad.ufl.edu:CSE-211-CLQ" /m "Xerox Phaser 7760GX PS" /z /u")



Above is the code I am using to add the correct LPR Printers. The registry edits work just fine, however the port still doesn't show up in the printer server properties. and the last shell call has obvious problems and I can't remember the escape char, think it is "\", but it still doesn't add the printer when I hand code it in. What am I doing wrong and how can I properly add this printer with code??
 
VB.NET:
Dim WshNetwork = CreateObject("WScript.Network")

Public Sub CSE211CLQ()
WshNetwork.AddWindowsPrinterConnection("at-cetus.ad.ufl.edu:CSE-211-CLQ", "CSE-211-CLQ")

Shell("net stop spooler")
Shell("net start spooler")

End Sub

I have the WshNetwork variable set up in the main class and then the subs are called. The issue is that I have no ida if I have done the syntax right as the printer never gets installed. The server queue is at-cetus.ad.ufl.edu:CSE-211-CLQ and the printer is CSE-211-CLQ. and it is a LPR port.
 
VB.NET:
WshNetwork.AddPrinterConnection(strLocalName, strRemoteName [,bUpdateProfile] [,strUser] [,strPassword])

If you wanted to install a printer on LPT1 and have it save the mapping to the current user's profile you'd do something like:

VB.NET:
WshNetwork.AddPrinterConnection("LPT1", "\\server\printer", True)
 
I could really use some help with this. I have tested it and refined it a bit and it doesn't work and I need some help. It is in a BG worker. Here is the complete code.

VB.NET:
Public Class prnProcessing 
    Dim WshNetwork = CreateObject("WScript.Network") 
 
    Public arrInstalled As New ArrayList() 
    Dim WithEvents ToDoWork As New System.ComponentModel.BackgroundWorker 
 
 
    Public Sub prnProcessing_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
 
        If prnSelection.arrToInstall.Count > 0 Then 
            'added 10-2-08 
            prnProgress.Style = ProgressBarStyle.Continuous 
            'prnProgress.Dock = DockStyle.Bottom 
            prnProgress.Visible = False 
            prnProgress.Minimum = 1 
            prnProgress.Maximum = prnSelection.arrToInstall.Count 
            'end add 10-2-08 
 
            ToDoWork.WorkerReportsProgress = True 
            ToDoWork.WorkerSupportsCancellation = True 
            ToDoWork.RunWorkerAsync(prnSelection.arrToInstall) 
            Me.CenterToScreen() 
        Else 
            MsgBox("There are no printers to install.  Please choose the printers you wish to install.") 
            prnSelection.Show() 
            Me.Close() 
        End If 
 
    End Sub 
 
    Public Sub ToDoWork_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles ToDoWork.DoWork 
 
        Dim worker As System.ComponentModel.BackgroundWorker = CType(sender, System.ComponentModel.BackgroundWorker) 
        If e.Argument IsNot Nothing AndAlso TypeOf (e.Argument) Is ArrayList Then 
            Dim arrToInstall As ArrayList = e.Argument 
            If arrToInstall.Count > 0 Then 
                Dim i As Integer = 1 
                For Each printer As String In arrToInstall 
                    CallByName(Me, printer, CallType.Method) 
                    worker.ReportProgress(i) 
                    i += 1 
                    System.Threading.Thread.Sleep(200) 
                Next 
            End If 
        End If 
    End Sub 
 
    Public Sub ToDoWork_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles ToDoWork.ProgressChanged 
        prnProgress.Visible = True 
        prnProgress.Value = e.ProgressPercentage 
    End Sub 
 
    Public Sub PrinterProgress(ByVal i As Integer, ByVal worker As System.ComponentModel.BackgroundWorker, ByVal e As System.ComponentModel.DoWorkEventArgs) 
        worker.ReportProgress(i) 
    End Sub 
    Public Sub ToDoWork_EndProgress() Handles ToDoWork.RunWorkerCompleted 
        ToDoWork.Dispose() 
        prnWrapup.Show() 
        Me.Close() 
    End Sub 
 
    Public Sub CSE211CLQ() 
        MsgBox("Does it start??", vbOK) 
        'arrInstalled.Add("CSE-211-CLQ") 
  
        WshNetwork.AddWindowsPrinterConnection("LPR", "\\at-cetus.ad.ufl.edu\CSE-211-CLQ", "True") 
        MsgBox("Did it install??", vbOK) 
 
        Shell("net stop spooler") 
        Shell("net start spooler") 
 
        Shell("rundll32 printui.dll, PrintUIEntry /if /b ""test_1/"" /f ""C:\7760.inf\"" /q /r ""at-cetus.ad.ufl.edu:CSE-211-CLQ/"" /m ""Xerox Phaser 7760GX PS"" /z /u") 
 
        MsgBox("Did it install??", vbOK) 
 
 
    End Sub 
End Class


I have added two msg boxes before and after and when it gets to the WshNetwork line it just conks out and quits the bg workers and goes to the next form without showing the next msgbox or doing anyother thing. How do I get this to work.
 
Back
Top