我编写了一个脚本,为新用户安装了几台打印机。
我想更改其中一些设置,以便它们可以在页面的两面打印。
我相信这涉及使用printui修改属性,但它可能需要VB脚本或可能需要另一种.NET语言(我要么使用VB,C#或IronPython)。
我可以为给定的打印机添加注释,但如何选择首选项并对其进行修改?
伪代码看起来像这样:
printui.exe / n printername / ??? [如何改变所需的质量]
OR调用相关的Windows API。
答案 0 :(得分:1)
您可以使用printui执行此操作,将打印机设置为双面打印,然后使用printui / Ss将设置放入文件中。浏览各种选项,即2,7,d ......并查看哪个文件包含双工设置。
另一种方法是使用打印机api,并使用PRINTER_INFO_8(全局)或PRINTER_INFO_9(用户)结构 - MSDN link。然后使用DEVMODE结构将dmDuplex设置为双面。
首先打开打印机,然后在完成后将其关闭:
[DllImport("winspool.drv", EntryPoint = "OpenPrinter", SetLastError = true)]
internal static extern bool OpenPrinter(string pPrinterName, ref IntPtr phPrinter, PRINTER_DEFAULTS pDefault);
[DllImport("winspool.drv", EntryPoint = "ClosePrinter", SetLastError = true)]
internal static extern int ClosePrinter(IntPtr hPrinter);
使用GetPrinter检索当前的打印机设置,使用SetPrinter放回修改后的设置:
[DllImport("winspool.drv", CharSet = CharSet.Auto, EntryPoint = "GetPrinter", SetLastError = true)]
internal static extern bool GetPrinter(IntPtr hPrinter, uint Level, IntPtr pPrinter, uint cbBuf, out uint pcbNeeded);
[DllImport("winspool.drv", CharSet = CharSet.Auto, EntryPoint = "SetPrinter", SetLastError = true)]
internal static extern bool SetPrinter(IntPtr hPrinter, uint Level, IntPtr pPrinter, uint Command);
实际执行此操作的代码应如下所示:
var oldPrinter = printerFunctions.OpenPrinterHandle(String.Format(@"{0}\{1}", oldServerName, oldPrinterName));
var printerInfo8 = printerFunctions.GetPrinterInfo<PRINTER_INFO_8>(oldPrinter, 8);
// Change the dmDuplex value here.
printerFunctions.SetPrinter(oldPrinter, printerInfo8, 8);
printerFunctions.ClosePrinterHandle(oldPrinter);
public IntPtr OpenPrinterHandle(string printerName)
{
var def = new PRINTER_DEFAULTS { pDatatype = null, pDevMode = IntPtr.Zero, DesiredAccess = OpenPrinterAccessCodes.PRINTER_ALL_ACCESS };
var hPrinter = IntPtr.Zero;
if (!PrinterNativeMethods.OpenPrinter(printerName, ref hPrinter, def))
{
var lastWin32Error = new Win32Exception(Marshal.GetLastWin32Error());
Logger.Log("Failed open Printer: " + lastWin32Error.Message);
throw lastWin32Error;
}
return hPrinter;
}
public void ClosePrinterHandle(IntPtr hPrinter)
{
PrinterNativeMethods.ClosePrinter(hPrinter);
}
public void SetPrinter<T>(IntPtr hPrinter, T printerInfo, uint level)
{
var size = (uint)Marshal.SizeOf(printerInfo);
var printerInfoPtr = Marshal.AllocHGlobal((int)size);
Marshal.StructureToPtr(printerInfo, printerInfoPtr, true);
var result = PrinterNativeMethods.SetPrinter(hPrinter, level, printerInfoPtr, 0);
if (!result)
{
var win32Error = Marshal.GetLastWin32Error();
var lastWin32Error = new Win32Exception(win32Error);
Logger.Log("Failed set printer: " + lastWin32Error.Message);
throw lastWin32Error;
}
Marshal.FreeHGlobal(printerInfoPtr);
}
public T GetPrinterInfo<T>(IntPtr hPrinter, uint level)
{
uint pcbNeeded;
var bFlag = PrinterNativeMethods.GetPrinter(hPrinter, level, IntPtr.Zero, 0, out pcbNeeded);
var win32Error = Marshal.GetLastWin32Error();
if ((!bFlag) && (win32Error != PrinterNativeMethods.ERROR_INSUFFICIENT_BUFFER) || (pcbNeeded == 0))
{
var lastWin32Error = new Win32Exception(win32Error);
Logger.Log("Failed get printer: " + lastWin32Error.Message);
throw lastWin32Error;
}
var currentPrinterInfoPtr = Marshal.AllocHGlobal((int)pcbNeeded);
bFlag = PrinterNativeMethods.GetPrinter(hPrinter, level, currentPrinterInfoPtr, pcbNeeded, out pcbNeeded);
if (!bFlag)
{
var lastWin32Error = new Win32Exception();
Logger.Log("Failed get printer: " + lastWin32Error.Message);
throw lastWin32Error;
}
return (T)Marshal.PtrToStructure(currentPrinterInfoPtr, typeof(T));
}