|
-
Apr 27th, 2001, 01:20 PM
#1
Thread Starter
Addicted Member
default printer
Does anybody have the code for getting the default printer and setting it in windows 2000?
-
Apr 27th, 2001, 01:48 PM
#2
Guru
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.
-
Apr 27th, 2001, 02:06 PM
#3
Thread Starter
Addicted Member
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?
-
Apr 27th, 2001, 02:20 PM
#4
Guru
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.
-
Apr 27th, 2001, 02:29 PM
#5
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|