Visual Basic Network Services

ConnectToPrinterDlg: Install Network Printers
     
Posted:   Wednesday January 04, 2006
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   Windows NT, 2000, XP or later
Author:   VBnet - Randy Birch
     

Related:  

WNetAddConnection2: Transparently Connect to Network Shares
NetShareCheck: Determine Remote Folder or Device Share Status
WNetEnumResource: Enumerating Local Network Resources
NetConnectionEnum: Enumerating Share Connection Information
NetShareEnum: Enumerating Shared Resources on Other Machines  
NetShareAdd: Create a Local or Remote Share
WNetGetUser: User, Share and Share User for Network Resources
WNetGetConnection: Get UNC Path for Mapped Drive
     
 Prerequisites
Windows NT, 2000, XP or later to use ConnectToPrinterDlg; none to use the alternate Rundll32 method detailed in the Command1_Click code comments.

The ConnectToPrinterDlg function displays a dialog box that lets users browse and connect to printers on a network. If the user selects a printer, the function attempts to create a connection to it; if a suitable driver is not installed on the server, the user is given the option of creating a printer locally.

To view the printers folder use the Open Explorer button.

 BAS Module Code
None.

 Form Code
To a form add one command buttons (Command1) and set the Index property to 0 to create a control array. The code will take care of creating the remaining buttons. Add the following code to the form:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const SW_SHOWNORMAL As Long = 1

Private Declare Function ConnectToPrinterDlg Lib "winspool.drv" _
   (ByVal hwnd As Long, ByVal flags As Long) As Long

Private Declare Function ShellExecute Lib "shell32.dll" _
    Alias "ShellExecuteA" _
   (ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long
    


Private Sub Form_Load()

   Dim cnt As Long
   
   For cnt = 0 To 2
   
      If cnt > 0 Then Load Command1(cnt)
      
      With Command1(cnt)
      
         .Move 200, 200 + (370 * cnt), 2000, 345
         .Visible = True
         
         Select Case cnt
            Case 0: .Caption = "Connect Net Printer"
            Case 1: .Caption = "Open Explorer"
            Case 2: .Caption = "Exit"
         End Select
         
      End With
      
   Next  'cnt
         
End Sub


Private Sub Command1_Click(Index As Integer)

   Select Case Index
   
      Case 0: 'Map network printer
              
              'If ConnectToPrinterDlg succeeds and the
              'user selects a printer, the return value
              'is a handle to the selected printer. If
              'the call fails, or the user cancels the
              'dialog box without selecting a printer,
              'the return value is 0.
 
              'Note that ConnectToPrinterDlg is not
              'available on Windows versions prior to NT 3.51.
              'Windows 95-ME users must use the corresponding
              'rundll32 command to invoke the Add Printer dialog:
              'Call Shell("rundll32.exe shell32.dll,SHHelpShortcuts_RunDLL AddPrinter", vbNormalFocus)
              
              'print result to debug window
               Debug.Print ConnectToPrinterDlg(Me.hwnd, 0&)

      Case 1: 'show printers
               Call ShellExecute(0&, "Open", _
                                     "explorer.exe", _
                                     "/e,::{2227A280-3AEA-1069-A2DE-08002B30309D}", _
                                     0&, SW_SHOWNORMAL)
      Case 2: 'quit
               Unload Me
      
   End Select
   
End Sub
 Comments
 

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter