Visual Basic System Services
GetPrinterDriverDirectory: Obtain Windows' Printer Driver Directory
     
Posted:   Saturday August 06, 2005
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None.
Author:   VBnet - Randy Birch
     

Related:  

AddPrinter: Add/Delete Local/Remote Printers using Existing Drivers
EnumPrinters: Enumerating Local and Network Printers
SetDefaultPrinter: Changing Windows' Default Printer
WriteProfileString: Changing Windows' Default Printer
     
 Prerequisites
None.

In case you have to monkey around in the printer driver folder, here's a quick way to retrieve the folder name on either local or remote machines. This folder contains driver files and dependent files. (For Windows 2000 and later, a dependent file is a printer driver file that is included in a printer INF file Install section with a dirid (directory identifiers) of 66000, but not assigned to the DriverFile, DataFile, ConfigFile, or HelpFile entries. If you care.)
 BAS Module Code
None.

 Form Code
To a form, add a text box (Text1) and a command button (Command1) with the following code:

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 Private Declare Function GetPrinterDriverDirectory Lib "winspool.drv" _
   Alias "GetPrinterDriverDirectoryA" _
  (ByVal pName As String, _
   ByVal pEnvironment As String, _
   ByVal level As Long, _
   ByVal pDriverDirectory As String, _
   ByVal cbBuff As Long, _
   pcbNeeded As Long) As Long



Private Sub Form_Load()

   Command1.Caption = "GetPrinterDriverDirectory"
   
End Sub


Private Sub Command1_Click()

   Dim level As Long
   Dim cbBuff As Long
   Dim pcbNeeded As Long
   Dim pName As String
   Dim pEnvironment As String
   Dim pDriverDirectory As String
   
  'initialization to determine size of buffer required
   level = 1    'must be 1
   cbBuff = 0   'must be 0 initially
   pDriverDirectory = vbNullString  'must be null string initially
   
  'string that specifies the name of the
  'server on which the printer driver resides.
  'If this parameter is vbNullString the
  'local driver-directory path is retrieved.
   pName = vbNullString
   
  'string that specifies the environment
  '(for example, "Windows NT x86", "Windows NT R4000",
  '"Windows NT Alpha_AXP", or "Windows 4.0"). If
  'this parameter is NULL, the current environment
  'of the calling application and client machine
  '(not of the destination application and print
  'server) is used.
   pEnvironment = vbNullString
   
  'find out how large the buffer
  'needs to be (pcbNeeded). Call will return 0.
   If GetPrinterDriverDirectory(pName, _
                                pEnvironment, _
                                level, _
                                pDriverDirectory, _
                                cbBuff, _
                                pcbNeeded) = 0 Then
    
    
     'create a buffer large enough for the 
     'string and a trailing null
      pDriverDirectory = Space$(pcbNeeded)
      cbBuff = Len(pDriverDirectory)
         
     'call again. Success = 1
      If GetPrinterDriverDirectory(pName, _
                                   pEnvironment, _
                                   level, _
                                   pDriverDirectory, _
                                   cbBuff, _
                                   pcbNeeded) = 1 Then
                                        
           'result
            Text1.Text = Left$(pDriverDirectory, pcbNeeded)
            
      End If  'GetPrinterDriverDirectory/2
   End If  'GetPrinterDriverDirectory/1

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