GetDefaultPrinter (winspool)
Last changed: -62.227.250.230

.
Summary

The GetDefaultPrinter function retrieves the printer name of the default printer for the current user on the local computer

C# Signature:

[DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool GetDefaultPrinter(StringBuilder pszBuffer, ref int size);

VB Signature:

Declare Function GetDefaultPrinter Lib "winspool.dll" (TODO) As TODO

User-Defined Types:

None.

Notes:

Works even if print spooler service is stopped (W2K)

Tips & Tricks:

Sample Code:

StringBuilder dp = new StringBuilder(256);
int size = dp.Capacity;
if (GetDefaultPrinter(dp, ref size)) {
    Console.WriteLine(String.Format("Printer: {0}, name length {1}", dp.ToString().Trim(), size));
} else {
    int rc = GetLastError();
    Console.WriteLine(String.Format("Failed. Size: {0}, error: {1:X}", size, rc));
}

Alternative Managed API:

Do you know one? Please contribute it!

Documentation