Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何检查是否有默认打印机(Windows)?_Java_.net_Windows_Delphi_Winapi - Fatal编程技术网

Java 如何检查是否有默认打印机(Windows)?

Java 如何检查是否有默认打印机(Windows)?,java,.net,windows,delphi,winapi,Java,.net,Windows,Delphi,Winapi,是否有API或注册表项可以从应用程序(本机、Java或.Net)中使用,以检查当前登录的用户是否配置了默认打印机 更新:非常感谢到目前为止的答案!根据KB文章,注册表项(读/写)仅在Windows 2000之前记录。在较新版本中是否有用于本机访问的Win API方法?在.NET中,此代码适用于我: public static string DefaultPrinterName() { string functionReturnValue = null; System.Drawing.Pr

是否有API或注册表项可以从应用程序(本机、Java或.Net)中使用,以检查当前登录的用户是否配置了默认打印机


更新:非常感谢到目前为止的答案!根据KB文章,注册表项(读/写)仅在Windows 2000之前记录。在较新版本中是否有用于本机访问的Win API方法?

在.NET中,此代码适用于我:

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;
}
发件人:
有一个Java API可以获取默认打印机:

PrintService defaultPrinter = PrintServiceLookup.lookupDefaultPrintService();
如果没有默认打印机或服务,则返回
null
。这可以作为一个测试


旧答案

这些信息可以在注册表中找到。您不能使用普通Java访问注册表,但是有一些JNDI解决方案可以解决这个问题。因此,基本上,您必须测试注册表中是否存在某个键。而且,作为奖励,如果您已经到目前为止,您甚至应该能够获得默认打印机的名称:)

进一步阅读:

  • (例如,有更多的解决方案)

    • 非托管中有一个函数。您可以调用函数返回默认打印机的名称

      这是非托管函数的p/Invoke签名:

      [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 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 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();
          }
      
      要在控制台应用程序中测试的完整代码:

      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();
              }
          }
      }
      

      确实需要
      oPS=null
      吗?不,我想不是-我从链接复制了代码,但没有更改它(只是为了编译并检查它是否工作),因为有非常好的API来确定这些信息,而不必求助于不受支持的注册表黑客(知识库文章中的信息声称仅支持Windows 2000)@Larry-Java中完美的API?请随便举个例子。我想学习一些新的东西。OP要求将.net或本机作为选项。我希望Java有打印API,不是吗?@Larry-谢谢你的评论-我已经研究了Java,看起来Java支持检测默认打印机。更改了我的答案。