GetDefaultDriverName – Retrieving the default printer Driver name

GetDefaultDriverName – Retrieving the default printer Driver name

Private Declare Function GetProfileString Lib "kernel32" Alias _    "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, _    ByVal lpDefault As String, ByVal lpReturnedString As String, _    ByVal nSize As Long) As LongPrivate Declare Function PrinterProperties Lib "winspool.drv" (ByVal hwnd As _    Long, ByVal hPrinter As Long) As LongPrivate Declare Function SHInvokePrinterCommand Lib "shell32.dll" Alias _    "SHInvokePrinterCommandA" (ByVal hwnd As Long, _    ByVal uAction As enPrinterActions, ByVal Buffer1 As String, _    ByVal Buffer2 As String, ByVal Modal As Long) As LongPrivate Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" _    (ByVal pPrinterName As String, phPrinter As Long, _    pDefault As PRINTER_DEFAULTS) As LongPrivate Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" _    (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, _    ByVal Command As Long) As LongPrivate 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 LongPrivate Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal _    lpString1 As String, ByVal lpString2 As Any) As LongPrivate Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As _    Long) As Long' *** Constants for DEVMODE structurePrivate Const CCHDEVICENAME = 32Private Const CCHFORMNAME = 32' *** constants for DesiredAccess member of PRINTER_DEFAULTSPrivate Const STANDARD_RIGHTS_REQUIRED = &HF0000Private Const PRINTER_ACCESS_ADMINISTER = &H4Private Const PRINTER_ACCESS_USE = &H8Private Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _    PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)Private Type DEVMODE   dmDeviceName         As String * CCHDEVICENAME   dmSpecVersion        As Long   dmDriverVersion      As Long   dmSize               As Long   dmDriverExtra        As Long   dmFields             As Long   dmOrientation        As Long   dmPageSize           As Long   dmPaperLength        As Long   dmPaperWidth         As Long   dmScale              As Long   dmCopies             As Long   dmDefaultSource      As Long   dmPrintQuality       As Long   dmColor              As Long   dmDuplex             As Long   dmYResolution        As Long   dmTToption           As Long   dmCollate            As Long   dmFormName           As String * CCHFORMNAME   dmLogPixels          As Long   dmBitsPerPel         As Long   dmPelsWidth          As Long   dmPelsHeight         As Long   dmDisplayFlags       As Long   dmDisplayFrequency   As Long   dmICMMethod          As Long        ' // Windows 95 only   dmICMIntent          As Long        ' // Windows 95 only   dmMediaType          As Long        ' // Windows 95 only   dmDitherType         As Long        ' // Windows 95 only   dmReserved1          As Long        ' // Windows 95 only   dmReserved2          As Long        ' // Windows 95 onlyEnd TypePrivate Type PRINTER_DEFAULTS   pDatatype            As Long   pDevMode             As DEVMODE   DesiredAccess        As LongEnd TypePrivate Enum enPrinterActions   PRINTACTION_OPEN = 0   PRINTACTION_PROPERTIES = 1   PRINTACTION_NETINSTALL = 2   PRINTACTION_NETINSTALLLINK = 3   PRINTACTION_TESTPAGE = 4   PRINTACTION_OPENNETPRN = 5   PRINTACTION_DOCUMENTDEFAULTS = 6   PRINTACTION_SERVERPROPERTIES = 7End Enum' Get the default printer Driver name' Example: Debug.Print GetDefaultDriverName()Private Function GetDefaultDriverName() As String   Dim sTmp As String   On Error Resume Next   sTmp = Space$(1000)   Call GetProfileString("windows", "Device", vbNullString, sTmp, 1000)   sTmp = Replace(sTmp, Chr$(0), vbNullString)   sTmp = Trim$(sTmp)   If sTmp = vbNullString Then      sTmp = Printer.DriverName   Else      sTmp = GetToken(sTmp, ",", 1)   End If   If sTmp = vbNullString Then      sTmp = Printer.DriverName   End If   GetDefaultDriverName = sTmpEnd Function' This function returns the nToken sToken in a string' Example:'   Debug.Print GetToken("This is a test.", " ", 2) ' => "is"Private Function GetToken(sSearchIn As String, sToken As String, _    nToken As Long) As String   Dim nI As Long   Dim nJ As Long   Dim nK As Long   If nToken < 1 Then      GetToken = vbNullString      Exit Function   End If   nK = 0   For nI = 1 To nToken      nJ = nK      nK = InStr(nJ + 1, sSearchIn, sToken)      If nK = 0 Then         If nI = nToken Then            GetToken = Mid$(sSearchIn, nJ + 1, Len(sSearchIn) - nJ)         Else            GetToken = vbNullString         End If         Exit Function      End If   Next   GetToken = Mid$(sSearchIn, nJ + 1, nK - nJ - 1)End Function'========================================' You can find more routines like this on,'  a site devoted to  VB developers'========================================

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as