How to check if there is a default printer (Windows)?

Is there an API or registry key that I can use from applications (native, Java or .Net) to check if the user who is currently logged in has set up a default printer?

Update: Thanks so much for the answers! According to KB article http://support.microsoft.com/kb/156212 registry entry (read / write) is only documented before Windows 2000. Is there a Win API method in new versions for native access?

+4
source share
3 answers

In .NET, this code works for me:

public static string DefaultPrinterName() { string functionReturnValue = null; System.Drawing.Printing.PrinterSettings oPS = new System.Drawing.Printing.PrinterSettings(); try { functionReturnValue = oPS.PrinterName; } catch (System.Exception ex) { functionReturnValue = ""; } finally { oPS = null; } return functionReturnValue; } 

From: http://in.answers.yahoo.com/question/index?qid=20070920032312AAsSaPx

+3
source

To get the default printer, there is a Java API:

 PrintService defaultPrinter = PrintServiceLookup.lookupDefaultPrintService(); 

It returns null if there is no default printer or service. This can be used as a test.


Old answer

Information can be found in the registry. You cannot access the registry with plain Java, but there are JNDI solutions for this problem. So basically you should test if a certain key exists in the registry. And as a bonus, if you go this far, you can even get the default printer name :)

Further reading:

+3
source

There is a function in the unmanaged print function of the Spooler API winspool.drv . You can call the GetDefaultPrinter function to return the default printer name.

This is the P / Invoke signature for an unmanaged function:

 [DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)] private static extern bool GetDefaultPrinter( StringBuilder buffer, ref int bufferSize); 

Use this function to determine if the default printer is installed:

  public static bool IsDefaultPrinterAssigned() { //initialise size at 0, used to determine size of the buffer int size = 0; //for first call provide a null StringBuilder and 0 size to determine buffer size //return value will be false, as the call actually fails internally setting the size to the size of the buffer GetDefaultPrinter(null, ref size); if (size != 0) { //default printer set return true; } return false; } 

Use this function to return the default printer name, returns an empty string if the default value is not set:

  public static string GetDefaultPrinterName() { //initialise size at 0, used to determine size of the buffer int size = 0; //for first call provide a null StringBuilder and 0 size to determine buffer size //return value will be false, as the call actually fails internally setting the size to the size of the buffer GetDefaultPrinter(null, ref size); if (size == 0) { //no default printer set return ""; } StringBuilder printerNameStringBuilder = new StringBuilder(size); bool success = GetDefaultPrinter(printerNameStringBuilder, ref size); if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return printerNameStringBuilder.ToString(); } 

Full code for testing in a console application:

 using System; using System.ComponentModel; using System.Runtime.InteropServices; using System.Text; namespace DefaultPrinter { class Program { static void Main(string[] args) { Console.WriteLine(IsDefaultPrinterAssigned()); Console.WriteLine(GetDefaultPrinterName()); Console.ReadLine(); } [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool GetDefaultPrinter( StringBuilder buffer, ref int bufferSize); public static bool IsDefaultPrinterAssigned() { //initialise size at 0, used to determine size of the buffer int size = 0; //for first call provide a null StringBuilder to and 0 size to determine buffer size //return value will be false, as the call actually fails internally setting the size to the size of the buffer GetDefaultPrinter(null, ref size); if (size != 0) { //default printer set return true; } return false; } public static string GetDefaultPrinterName() { //initialise size at 0, used to determine size of the buffer int size = 0; //for first call provide a null StringBuilder to and 0 size to determine buffer size //return value will be false, as the call actually fails internally setting the size to the size of the buffer GetDefaultPrinter(null, ref size); if (size == 0) { //no default printer set return ""; } StringBuilder printerNameStringBuilder = new StringBuilder(size); bool success = GetDefaultPrinter(printerNameStringBuilder, ref size); if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return printerNameStringBuilder.ToString(); } } } 
+3
source

All Articles