Click here to Skip to main content
15,917,875 members

Comments by Tom6012 (Top 3 by date)

Tom6012 21-Jul-14 23:17pm View    
Hi Duncan,

Yes, Win7 work well!
But Win 8 have system error 6 found in disable printer rountine.
As checking SetPrinter return False, and Marshal.GetLastWin32Error() return 6.

Portion of disable printer routine.
static unsafe void CSDisablePrinter(int hPrinter)
{
if (!SetPrinter(hPrinter, 0,
(byte*)PRINTER_STATUS_OFFLINE, PRINTER_CONTROL_SET_STATUS))
{
throw new ApplicationException("Cannot disable printer " + Marshal.GetLastWin32Error());
}

List all of code as below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; // DllImport

// rference page http://support.microsoft.com/kb/315720/zh-tw

namespace PrintAPI
{
public partial class Form1 : Form
{

unsafe struct PRINTER_DEFAULTS
{
public void* pDatatype; // LPTSTR
public void* pDevMode; // LPDEVMODE
public uint DesiredAccess; // ACCESS_MASK
};

[DllImport("kernel32", SetLastError = true)]
static extern int GetLastError();

[DllImport("WinSpool.drv", SetLastError = true)]
static extern unsafe bool OpenPrinter
(string pPrinterName, int* phPrinter, void* pDefault);

[DllImport("WinSpool.drv", SetLastError = true)]
static extern bool ClosePrinter(int hPrinter);

[DllImport("WinSpool.drv", SetLastError = true)]
static extern unsafe bool SetPrinter(int hPrinter, uint Level, void* pPrinter, uint Command);

const uint PRINTER_ACCESS_ADMINISTER = 0x00000004;
const uint PRINTER_STATUS_OFFLINE = 0x00000080;
const uint PRINTER_CONTROL_PAUSE = 1;
const uint PRINTER_CONTROL_RESUME = 2;
const uint PRINTER_CONTROL_PURGE = 3;
const uint PRINTER_CONTROL_SET_STATUS = 4;

// Open a printer for administration operations.
static unsafe int CSOpenPrinter(string printerName)
{
bool bResult;
int hPrinter;
PRINTER_DEFAULTS pd;

pd.pDatatype = null;
pd.pDevMode = null;
pd.DesiredAccess = PRINTER_ACCESS_ADMINISTER;

bResult = OpenPrinter(printerName, &hPrinter, &pd);
if (!bResult)
{
throw new ApplicationException("Cannot open printer '" + printerName + "' " + Marshal.GetLastWin32Error());
}
return hPrinter;
}

// Close the printer.
static void CSClosePrinter(int hPrinter)
{
if (!ClosePrinter(hPrinter))
{
throw new ApplicationException("Cannot close printer " + Marshal.GetLastWin32Error());
}
}

// Pause printer.
static unsafe void CSPausePrinter(int hPrinter)
{
if (!SetPrinter(hPrinter, 0, null, PRINTER_CONTROL_PAUSE))
{
throw new ApplicationException("Cannot pause printer " + Marshal.GetLastWin32Error());
}
}

// Resume printer.
static unsafe void CSResumePrinter(int hPrinter)
{
if (!SetPrinter(hPrinter, 0, null, PRINTER_CONTROL_RESUME))
{
throw new ApplicationException("Cannot resume printer " + Marshal.GetLastWin32Error());
}
}

// Disable printer (offline).
static unsafe void CSDisablePrinter(int hPrinter)
{
if (!SetPrinter(hPrinter, 0,
(byte*)PRINTER_STATUS_OFFLINE, PRINTER_CONTROL_SET_STATUS))
{
throw new ApplicationException("Cannot disable printer " + Marshal.GetLastWin32Error());
}
}

// Enable printer.
static unsafe void CSEnablePrinter(int hPrinter)
{
if (!SetPr
Tom6012 17-Jul-14 6:54am View    
It looks not support Win7/8.
Tom6012 16-Jul-14 3:06am View    
Any idea to set printer as offline by C#?