How to get Printer information like Status, Share Name, etc.
Posted on January 5, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | 'System & API - How to get Printer information like Status, Share Name, etc. Option Explicit Public Enum Printer_Control_Commands PRINTER_CONTROL_PAUSE = 1 PRINTER_CONTROL_RESUME = 2 PRINTER_CONTROL_PURGE = 3 PRINTER_CONTROL_SET_STATUS = 4 End Enum Public Enum Printer_Status PRINTER_STATUS_READY = &H0 PRINTER_STATUS_PAUSED = &H1 PRINTER_STATUS_ERROR = &H2 PRINTER_STATUS_PENDING_DELETION = &H4 PRINTER_STATUS_PAPER_JAM = &H8 PRINTER_STATUS_PAPER_OUT = &H10 PRINTER_STATUS_MANUAL_FEED = &H20 PRINTER_STATUS_PAPER_PROBLEM = &H40 PRINTER_STATUS_OFFLINE = &H80 PRINTER_STATUS_IO_ACTIVE = &H100 PRINTER_STATUS_BUSY = &H200 PRINTER_STATUS_PRINTING = &H400 PRINTER_STATUS_OUTPUT_BIN_FULL = &H800 PRINTER_STATUS_NOT_AVAILABLE = &H1000 PRINTER_STATUS_WAITING = &H2000 PRINTER_STATUS_PROCESSING = &H4000 PRINTER_STATUS_INITIALIZING = &H8000 PRINTER_STATUS_WARMING_UP = &H10000 PRINTER_STATUS_TONER_LOW = &H20000 PRINTER_STATUS_NO_TONER = &H40000 PRINTER_STATUS_PAGE_PUNT = &H80000 PRINTER_STATUS_USER_INTERVENTION = &H100000 PRINTER_STATUS_OUT_OF_MEMORY = &H200000 PRINTER_STATUS_DOOR_OPEN = &H400000 PRINTER_STATUS_SERVER_UNKNOWN = &H800000 PRINTER_STATUS_POWER_SAVE = &H1000000 End Enum Public Enum Printer_Attributes PRINTER_ATTRIBUTE_QUEUED = &H1 PRINTER_ATTRIBUTE_DIRECT = &H2 PRINTER_ATTRIBUTE_DEFAULT = &H4 PRINTER_ATTRIBUTE_SHARED = &H8 PRINTER_ATTRIBUTE_NETWORK = &H10 PRINTER_ATTRIBUTE_HIDDEN = &H20 PRINTER_ATTRIBUTE_LOCAL = &H40 PRINTER_ATTRIBUTE_ENABLE_DEVQ = &H80 PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS = &H100 PRINTER_ATTRIBUTE_DO_COMPLETE_FIRST = &H200 PRINTER_ATTRIBUTE_WORK_OFFLINE = &H400 PRINTER_ATTRIBUTE_ENABLE_BIDI = &H800 PRINTER_ATTRIBUTE_RAW_ONLY = &H1000 PRINTER_ATTRIBUTE_PUBLISHED = &H2000 End Enum Private Type PRINTER_INFO_1 flags As Long pDescription As String pName As String pComment As String End Type Private Type PRINTER_INFO_2 pServerName As String pPrinterName As String pShareName As String pPortName As String pDriverName As String pComment As String pLocation As String pDevMode As Long pSepFile As String pPrintProcessor As String pDatatype As String pParameters As String pSecurityDescriptor As Long Attributes As Long Priority As Long DefaultPriority As Long StartTime As Long UntilTime As Long Status As Long JobsCount As Long AveragePPM As Long End Type Private Type PRINTER_INFO_3 pSecurityDescriptor As Long End Type Private Type PRINTER_INFO_4 pPrinterName As String pServerName As String Attributes As Long End Type Private Type PRINTER_INFO_5 pPrinterName As String pPortName As String Attributes As Long DeviceNotSelectedTimeout As Long TransmissionRetryTimeout As Long End Type Private Type PRINTER_INFO_6 dwStatus As Long End Type Private Type PRINTER_INFO_7 pszObjectGUID As String dwAction As Long End Type Private Type PRINTER_INFO_8 pDevMode As Long End Type Private Declare Function SetPrinterApi Lib "winspool.drv" Alias "SetPrinterA" _ (ByVal hPrinter As Long, ByVal Level As Long, buffer As Any, ByVal command As Long) As Long Private Declare Function GetPrinterApi Lib "winspool.drv" Alias "GetPrinterA" _ (ByVal hPrinter As Long, ByVal Level As Long, buffer As Long, ByVal pbSize As Long, pbSizeNeeded As Long) As Long Private Type DEVMODE dmDeviceName As String * 32 dmSpecVersion As Integer dmDriverVersion As Integer dmSize As Integer dmDriverExtra As Integer dmFields As Long dmOrientation As Integer dmPaperSize As Integer dmPaperLength As Integer dmPaperWidth As Integer dmScale As Integer dmCopies As Integer dmDefaultSource As Integer dmPrintQuality As Integer dmColor As Integer dmDuplex As Integer dmYResolution As Integer dmTTOption As Integer dmCollate As Integer dmFormName As String * 32 dmUnusedPadding As Integer dmBitsPerPixel As Integer dmPelsWidth As Long dmPelsHeight As Long dmDisplayFlags As Long dmDisplayFrequency As Long dmICMMethod As Long dmICMIntent As Long dmMediaType As Long dmDitherType As Long dmReserved1 As Long dmReserved2 As Long 'following only appears in Windows 2000 dmPanningWidth As Long dmPanningHeight As Long End Type Private Type PRINTER_DEFAULTS pDatatype As String pDevMode As DEVMODE DesiredAccess As Long End Type Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" _ (ByVal pPrinterName As String, phPrinter As Long, pDefault As PRINTER_DEFAULTS) As Long Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long Private mhPrinter As Long Private mPRINTER_INFO_1 As PRINTER_INFO_1 Private mPRINTER_INFO_2 As PRINTER_INFO_2 Private mPRINTER_INFO_3 As PRINTER_INFO_3 Private mPRINTER_INFO_4 As PRINTER_INFO_4 Private mPRINTER_INFO_5 As PRINTER_INFO_5 Private mPRINTER_INFO_6 As PRINTER_INFO_6 Private mPRINTER_INFO_7 As PRINTER_INFO_7 Private mPRINTER_INFO_8 As PRINTER_INFO_8 Private mPRINTER_INFO_9 As PRINTER_INFO_8 Private Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, _ ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, pJob As Long, _ ByVal cdBuff As Long, pcbNeeded As Long, pcBytesReturned As Long) As Long Private Property Get Attributes() As Long Call RefreshPrinterInfo(2) Attributes = mPRINTER_INFO_2.Attributes End Property Public Property Get AveragePagesPerMonth() As Long Call RefreshPrinterInfo(2) AveragePagesPerMonth = mPRINTER_INFO_2.AveragePPM End Property Public Property Get Comment() As String Call RefreshPrinterInfo(1) Comment = mPRINTER_INFO_1.pComment End Property Public Property Get DataType() As String Call RefreshPrinterInfo(2) DataType = mPRINTER_INFO_2.pDatatype End Property Public Property Get DefaultPriority() As Long Call RefreshPrinterInfo(2) DefaultPriority = mPRINTER_INFO_2.DefaultPriority End Property Public Property Get Description() As String Call RefreshPrinterInfo(1) Description = mPRINTER_INFO_1.pDescription End Property Public Property Get DeviceName() As String Call RefreshPrinterInfo(1) DeviceName = mPRINTER_INFO_1.pName End Property Public Property Let DeviceName(ByVal newName As String) Dim lRet As Long Dim pDef As PRINTER_DEFAULTS If mhPrinter = 0 Then lRet = OpenPrinter(newName, mhPrinter, pDef) End If End Property Public Property Get Direct() As Boolean Direct = Attributes And PRINTER_ATTRIBUTE_DIRECT End Property Public Property Get DriverName() As String Call RefreshPrinterInfo(2) DriverName = mPRINTER_INFO_2.pDriverName End Property Public Property Get Location() As String Call RefreshPrinterInfo(2) Location = mPRINTER_INFO_2.pLocation End Property Public Property Get Parameters() As String Call RefreshPrinterInfo(2) Parameters = mPRINTER_INFO_2.pParameters End Property Public Property Get PendingJobsCount() As Long Call RefreshPrinterInfo(2) PendingJobsCount = mPRINTER_INFO_2.JobsCount End Property Public Property Get PortName() As String Call RefreshPrinterInfo(2) PortName = mPRINTER_INFO_2.pPortName End Property Public Property Let PrinterHandle(ByVal newHandle As Long) If newHandle <> mhPrinter Then mhPrinter = newHandle End If End Property Public Property Get PrintJobs() As Collection Dim colThis As Collection Dim oPrintJob As ApiPrintJob Dim lRet As Long Dim pcbSizeRequired As Long, pcbBytesReturned As Long Dim pJobId As Long Dim buffer() As Long Set colThis = New Collection ReDim Preserve buffer(0) As Long lRet = EnumJobs(mhPrinter, 0, 255, 1, buffer(0), UBound(buffer), pcbSizeRequired, pcbBytesReturned) If pcbSizeRequired > 0 Then ReDim Preserve buffer(0 To (pcbSizeRequired / 4) + 3) As Long lRet = EnumJobs(mhPrinter, 0, 255, 1, buffer(0), UBound(buffer) * 4, _ pcbSizeRequired, pcbBytesReturned) If Err.LastDllError <> 0 Then Call ReportError(Err.LastDllError, "ApiPrinter:PrintJobs", LastSystemError) End If For pJobId = 0 To (pcbBytesReturned - 1) 'each record is 16 bytes long Set oPrintJob = New ApiPrintJob With oPrintJob .PrinterHandle = mhPrinter .JobId = buffer(pJobId * 16) End With colThis.Add oPrintJob Next pJobId End If Set PrintJobs = colThis End Property Public Property Get PrintProcessor() As String Call RefreshPrinterInfo(2) PrintProcessor = mPRINTER_INFO_2.pPrintProcessor End Property Public Property Get Priority() As Long Call RefreshPrinterInfo(2) Priority = mPRINTER_INFO_2.Priority End Property Public Property Get Queued() As Boolean Queued = Attributes And PRINTER_ATTRIBUTE_QUEUED End Property Private Sub RefreshPrinterInfo(ByVal Index As Integer) Dim lRet As Long Dim SizeNeeded As Long Dim buffer() As Long ReDim Preserve buffer(0 To 1) As Long lRet = GetPrinterApi(mhPrinter, Index, buffer(0), UBound(buffer), SizeNeeded) ReDim Preserve buffer(0 To (SizeNeeded / 4) + 3) As Long lRet = GetPrinterApi(mhPrinter, Index, buffer(0), UBound(buffer) * 4, SizeNeeded) If Index < 1 Or Index > 9 Then Debug.Print "Error in call to ApiPrinter.RefreshPrinterInfo - invalid index" Else Select Case Index Case 1 With mPRINTER_INFO_1 .flags = buffer(0) .pDescription = StringFromPointer(buffer(1), 1024) .pName = StringFromPointer(buffer(2), 1024) .pComment = StringFromPointer(buffer(3), 1024) End With Case 2 With mPRINTER_INFO_2 .pServerName = StringFromPointer(buffer(0), 1024) .pPrinterName = StringFromPointer(buffer(1), 1024) .pShareName = StringFromPointer(buffer(2), 1024) .pPortName = StringFromPointer(buffer(3), 1024) .pDriverName = StringFromPointer(buffer(4), 1024) .pComment = StringFromPointer(buffer(5), 1024) .pLocation = StringFromPointer(buffer(6), 1024) .pDevMode = buffer(7) .pSepFile = StringFromPointer(buffer(8), 1024) .pPrintProcessor = StringFromPointer(buffer(9), 1024) .pDatatype = StringFromPointer(buffer(10), 1024) .pParameters = StringFromPointer(buffer(11), 1024) .pSecurityDescriptor = buffer(12) .Attributes = buffer(13) .Priority = buffer(14) .DefaultPriority = buffer(15) .StartTime = buffer(16) .UntilTime = buffer(17) .Status = buffer(18) .JobsCount = buffer(19) .AveragePPM = buffer(20) End With Case 3 With mPRINTER_INFO_3 .pSecurityDescriptor = buffer(0) End With Case 4 With mPRINTER_INFO_4 .pPrinterName = StringFromPointer(buffer(0), 1024) .pServerName = StringFromPointer(buffer(1), 1024) .Attributes = buffer(2) End With Case 5 With mPRINTER_INFO_5 .pPrinterName = StringFromPointer(buffer(0), 1024) .pPortName = StringFromPointer(buffer(1), 1024) .Attributes = buffer(2) .DeviceNotSelectedTimeout = buffer(3) .TransmissionRetryTimeout = buffer(4) End With Case 6 With mPRINTER_INFO_6 .dwStatus = buffer(0) End With Case 7 With mPRINTER_INFO_7 .pszObjectGUID = StringFromPointer(buffer(0), 1024) .dwAction = buffer(1) End With Case 8 With mPRINTER_INFO_8 .pDevMode = buffer(0) End With Case 9 With mPRINTER_INFO_9 .pDevMode = buffer(0) End With End Select If Err.LastDllError <> 0 Then ReportError Err.LastDllError, "ApiPrinter:RefreshPrinterInfo", LastSystemError End If End If End Sub Public Property Get SeparatorFile() As String Call RefreshPrinterInfo(2) SeparatorFile = mPRINTER_INFO_2.pSepFile End Property Public Property Get ServerName() As String Call RefreshPrinterInfo(4) ServerName = mPRINTER_INFO_4.pServerName End Property Public Property Get ShareName() As String Call RefreshPrinterInfo(2) ShareName = mPRINTER_INFO_2.pShareName End Property 'Returns the status of the selected printer when it is called. Public Property Get Status() As Printer_Status Call RefreshPrinterInfo(2) Status = mPRINTER_INFO_2.Status End Property |