Changing printer preferences in Windows programmat

2019-07-26 03:15发布

I've written a script that installs several printers for a new user.

I want to change the settings on some of these so that they can print on both sides of the page.

I BELIEVE this involves modifying an attribute with printui, however it might need VB script or possibly another .NET language (I'd either use VB, C# or IronPython).

I can add a comment to a given printer, but how do I select preferences and modify them?

Pseudocode would look like this:

printui.exe /n printername /??? [how to change quality desired]

OR calls to the relevant Windows API.

1条回答
可以哭但决不认输i
2楼-- · 2019-07-26 04:04

You could probably do this using printui, set a printer to duplex then use printui /Ss to drop the settings into a file. Go through the various options, ie 2, 7, d... and see which file holds the duplex setting.

The other way is to use the printer api, and use either PRINTER_INFO_8 (global) or PRINTER_INFO_9 (user) structure - MSDN link. Then use the DEVMODE structure to set the dmDuplex to double sided.

First open the printer, and then close it when you're finished:

[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);

Use GetPrinter to retrieve the current printer settings, and SetPrinter to put back the modified settings:

[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);

The code to actually do this should look something like:

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));
}
查看更多
登录 后发表回答