1. Herfried K. Wagner’s VB.Any
  2. .NET
  3. Frequently Asked Questions

Getting and setting the system default printer

Getting and setting the system default printer
<URL:https://dotnet.currifex.org/dotnet/faqs/defaultprinter/en/>
----------------------------------------------------------------------------

Getting and setting the system default printer

The 'PrinterManager' class provides p/invoke wrapper methods which can be
used to get and set the system default printer.  'GetDefaultPrinter' and
'SetDefaultPrinter' are supported by Windows 2000 and later.  On older
versions of Windows the default printer can be changed using INI access
functions as described in the articles below:

How to retrieve and set the default printer in Windows
<URL:http://support.microsoft.com/?scid=kb;EN-US;246772>

How To Set Which Printer Is the System Default Printer
<URL:http://support.microsoft.com/?scid=kb;EN-US;266767>

Windows GDI -- 'SetDefaultPrinter'
<URL:http://msdn.microsoft.com/library/en-us/gdi/prntspol_027m.asp>

\\\
Public Class PrinterManager
    Private Declare Auto Function GetDefaultPrinter Lib "winspool.drv" ( _
        ByVal pszBuffer As String, _
        ByRef pcchBuffer As Int32 _
    ) As Boolean

    Private Const ERROR_FILE_NOT_FOUND As Int32 = 2
    Private Const ERROR_INSUFFICIENT_BUFFER As Int32 = 122

    Private Declare Auto Function SetDefaultPrinter_API _
        Lib "winspool.drv" _
        Alias "SetDefaultPrinter" _
    ( _
        ByVal pszPrinter As String _
    ) As Boolean

    Public Shared Sub SetDefaultPrinter(ByVal PrinterName As String)
        If Not SetDefaultPrinter_API(PrinterName) Then
            Throw New Win32Exception
        End If
    End Sub
    
    Public Shared Function GetDefaultPrinter() As String
        Dim s As String = Space(128)
        Dim n As Int32 = s.Length
        Dim Success As Boolean = GetDefaultPrinter(s, n)
        If Success Then
            Return Strings.Left(s, n - 1)
        Else
            Dim LastError As Integer = Marshal.GetLastWin32Error()
            If LastError = ERROR_FILE_NOT_FOUND Then
                Throw _
                    New Win32Exception( _
                        LastError, _
                        "There is no default printer." _
                    )
            ElseIf LastError = ERROR_INSUFFICIENT_BUFFER Then
                s = Space(n)
                Success = GetDefaultPrinter(s, n)
                If Success Then
                    Return Strings.Left(s, n - 1)
                Else
                    Throw New Win32Exception
                End If
            Else
                Throw New Win32Exception
            End If
        End If
    End Function
End Class
///

Alternative methods to set the system default printer are based on WMI or
the WSH:

Windows Management Instrumentation -- 'SetDefaultPrinter' Method of the 'Win32_Printer' Class
<URL:http://msdn.microsoft.com/library/en-us/wmisdk/wmi/setdefaultprinter_method_in_class_win32_printer.asp>

Windows Script Host -- 'SetDefaultPrinter' Method
<URL:http://msdn.microsoft.com/library/en-us/script56/html/wsMthSetDefaultPrinter.asp>