Hi there. I'm trying to write a program that purges the print queue for a
selected printer. I was hoping to do this by simply calling the external
prnqctl.vbs script, but this program has to run on Windows 2000, which does
not support that script file. Instead, I'm trying to use the SetPrinter
API, but I'm not having any luck. Here is my test code:
public struct PrinterDefaults
{
public Int32 pDataType;
public Int32 pDevMode;
public Int32 permissions;
}
[DllImport(@"win spool.drv", EntryPoint="Ope nPrinter")]
public static extern Int32 OpenPrinter(Str ing pPrinterName, ref IntPtr
phPrinter,
PrinterDefaults pDefault);
[DllImport(@"win spool.drv", EntryPoint="Set Printer")]
public static extern Int32 SetPrinter(IntP tr hPrinter, long Level, Object
pPrinter,
long Command);
[DllImport(@"win spool.drv", EntryPoint="Clo sePrinter")]
public static extern Int32 ClosePrinter(In tPtr hPrinter);
public void ClearPrintQueue s2()
{
const Int32 PRINTER_ACCESS_ ADMINISTER = 4;
const long PRINTER_CONTROL _PAUSE = 1;
const long PRINTER_CONTROL _PURGE = 3;
const Int32 STANDARD_RIGHTS _REQUIRED = 0xF0000;
const Int32 PRINTER_ACCESS_ USE = 8;
const Int32 PRINTER_ALL_ACC ESS = STANDARD_RIGHTS _REQUIRED +
PRINTER_ACCESS_ USE + PRINTER_ACCESS_ ADMINISTER;
IntPtr hPrinter = new IntPtr(0);
PrinterDefaults PrinterDefs = new PrinterDefaults ();
Int32 intReturn;
PrinterDefs.per missions = PRINTER_ALL_ACC ESS;
// Open printer
intReturn = OpenPrinter("Le xmark Optra E312L", ref hPrinter,
PrinterDefs);
MessageBox.Show ("OpenPrinte r: " + intReturn.ToStr ing() + "\n\nhPrint er:
" + hPrinter.ToStri ng());
// Pause printer
try
{
intReturn = SetPrinter(hPri nter, 0, null, PRINTER_CONTROL _PAUSE);
if (intReturn == 0)
{
Marshal.GetLast Win32Error();
throw new Win32Exception( );
}
}
catch (Win32Exception e) { MessageBox.Show ("Return: " +
intReturn.ToStr ing() + "\n\n" + e.ErrorCode + ": " + e.Message); }
// Purge print queue
try
{
intReturn = SetPrinter(hPri nter, 0, null, PRINTER_CONTROL _PURGE);
if (intReturn == 0)
{
Marshal.GetLast Win32Error();
throw new Win32Exception( );
}
}
catch (Win32Exception e) { MessageBox.Show ("Return: " +
intReturn.ToStr ing() + "\n\n" + e.ErrorCode + ": " + e.Message); }
// Close printer
intReturn = ClosePrinter(hP rinter);
MessageBox.Show ("ClosePrint er: " + intReturn.ToStr ing());
}
The error I'm getting is on SetPrinter, both on the pause and on the purge.
I get this: "-2147467259: The specified procedure could not be found."
Any ideas on what could be wrong here? Thank you in advance for taking a
look.
selected printer. I was hoping to do this by simply calling the external
prnqctl.vbs script, but this program has to run on Windows 2000, which does
not support that script file. Instead, I'm trying to use the SetPrinter
API, but I'm not having any luck. Here is my test code:
public struct PrinterDefaults
{
public Int32 pDataType;
public Int32 pDevMode;
public Int32 permissions;
}
[DllImport(@"win spool.drv", EntryPoint="Ope nPrinter")]
public static extern Int32 OpenPrinter(Str ing pPrinterName, ref IntPtr
phPrinter,
PrinterDefaults pDefault);
[DllImport(@"win spool.drv", EntryPoint="Set Printer")]
public static extern Int32 SetPrinter(IntP tr hPrinter, long Level, Object
pPrinter,
long Command);
[DllImport(@"win spool.drv", EntryPoint="Clo sePrinter")]
public static extern Int32 ClosePrinter(In tPtr hPrinter);
public void ClearPrintQueue s2()
{
const Int32 PRINTER_ACCESS_ ADMINISTER = 4;
const long PRINTER_CONTROL _PAUSE = 1;
const long PRINTER_CONTROL _PURGE = 3;
const Int32 STANDARD_RIGHTS _REQUIRED = 0xF0000;
const Int32 PRINTER_ACCESS_ USE = 8;
const Int32 PRINTER_ALL_ACC ESS = STANDARD_RIGHTS _REQUIRED +
PRINTER_ACCESS_ USE + PRINTER_ACCESS_ ADMINISTER;
IntPtr hPrinter = new IntPtr(0);
PrinterDefaults PrinterDefs = new PrinterDefaults ();
Int32 intReturn;
PrinterDefs.per missions = PRINTER_ALL_ACC ESS;
// Open printer
intReturn = OpenPrinter("Le xmark Optra E312L", ref hPrinter,
PrinterDefs);
MessageBox.Show ("OpenPrinte r: " + intReturn.ToStr ing() + "\n\nhPrint er:
" + hPrinter.ToStri ng());
// Pause printer
try
{
intReturn = SetPrinter(hPri nter, 0, null, PRINTER_CONTROL _PAUSE);
if (intReturn == 0)
{
Marshal.GetLast Win32Error();
throw new Win32Exception( );
}
}
catch (Win32Exception e) { MessageBox.Show ("Return: " +
intReturn.ToStr ing() + "\n\n" + e.ErrorCode + ": " + e.Message); }
// Purge print queue
try
{
intReturn = SetPrinter(hPri nter, 0, null, PRINTER_CONTROL _PURGE);
if (intReturn == 0)
{
Marshal.GetLast Win32Error();
throw new Win32Exception( );
}
}
catch (Win32Exception e) { MessageBox.Show ("Return: " +
intReturn.ToStr ing() + "\n\n" + e.ErrorCode + ": " + e.Message); }
// Close printer
intReturn = ClosePrinter(hP rinter);
MessageBox.Show ("ClosePrint er: " + intReturn.ToStr ing());
}
The error I'm getting is on SetPrinter, both on the pause and on the purge.
I get this: "-2147467259: The specified procedure could not be found."
Any ideas on what could be wrong here? Thank you in advance for taking a
look.