Results 1 to 5 of 5

Thread: default printer

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Location
    Pittsburgh, PA
    Posts
    149

    Question default printer

    Does anybody have the code for getting the default printer and setting it in windows 2000?

  2. #2
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    First, I would enumerate through all the local printers by using EnumPrinters with the PRINTER_ENUM_LOCAL value (printer enumeration level 5).

    To get the default printer: Check each printer's PRINTER_INFO_5.Attributes - if it contains PRINTER_ATTRIBUTE_DEFAULT, then there you have it.

    To set the default printer: Open the desired printer with OpenPrinter, and use SetPrinter to set the printer's info through a level-5 structure (PRINTER_INFO_5) - of course, before you do this, add PRINTER_ATTRIBUTE_DEFAULT to the Attributes member. And don't forget to ClosePrinter.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Location
    Pittsburgh, PA
    Posts
    149
    Do you have code for that. Dont have a win2k machine to build the api and as usual msdn doesn't have any useful code that I have found.
    Also what about the getdefaultprinter and setdefaultprinter functions?

  4. #4
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Actually you don't need much for getting the name of the default printer... Just Printer.DeviceName will do all the work.
    For setting the default printer, try this (untested):
    Code:
    Option Explicit
    
    Public Const PRINTER_ATTRIBUTE_DEFAULT = &H4
    
    Type PRINTER_INFO_5
        pPrinterName As String
        pPortName As String
        Attributes As Long
        DeviceNotSelectedTimeout As Long
        TransmissionRetryTimeout As Long
    End Type
    
    Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As Any) As Long
    Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As Long, pcbNeeded As Long) As Long
    Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal Command As Long) As Long
    Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
    
    Function SetDefaultPrinter(ByVal PrinterDeviceName As String) As Boolean
        Dim hPrinter As Long, cbNeeded As Long
        Dim tPrinterInfo As PRINTER_INFO_5
        
        If OpenPrinter(PrinterDeviceName, hPrinter, ByVal 0) = 0 Then Exit Function
        If GetPrinter(hPrinter, 5, tPrinterInfo, Len(tPrinterInfo), cbNeeded) = 0 Then
            Call ClosePrinter(hPrinter)
            Exit Function
        End If
        
        tPrinterInfo.Attributes = tPrinterInfo.Attributes Or PRINTER_ATTRIBUTE_DEFAULT
        SetDefaultPrinter = (SetPrinter(hPrinter, 5, tPrinterInfo, 0) <> 0)
        Call ClosePrinter(hPrinter)
    End Function
    VBBrowser v2.2.1
    Should return True on success, False on failure.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Location
    Pittsburgh, PA
    Posts
    149
    Cool, thanks a bunch

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width