SetPrinter API problems

M

Mike Ripplinger

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(@"winspool.drv", EntryPoint="OpenPrinter")]
public static extern Int32 OpenPrinter(String pPrinterName, ref IntPtr
phPrinter,
PrinterDefaults pDefault);

[DllImport(@"winspool.drv", EntryPoint="SetPrinter")]
public static extern Int32 SetPrinter(IntPtr hPrinter, long Level, Object
pPrinter,
long Command);

[DllImport(@"winspool.drv", EntryPoint="ClosePrinter")]
public static extern Int32 ClosePrinter(IntPtr hPrinter);

public void ClearPrintQueues2()
{
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_ACCESS = STANDARD_RIGHTS_REQUIRED +
PRINTER_ACCESS_USE + PRINTER_ACCESS_ADMINISTER;

IntPtr hPrinter = new IntPtr(0);
PrinterDefaults PrinterDefs = new PrinterDefaults();
Int32 intReturn;
PrinterDefs.permissions = PRINTER_ALL_ACCESS;

// Open printer
intReturn = OpenPrinter("Lexmark Optra E312L", ref hPrinter,
PrinterDefs);
MessageBox.Show("OpenPrinter: " + intReturn.ToString() + "\n\nhPrinter:
" + hPrinter.ToString());

// Pause printer
try
{
intReturn = SetPrinter(hPrinter, 0, null, PRINTER_CONTROL_PAUSE);
if (intReturn == 0)
{
Marshal.GetLastWin32Error();
throw new Win32Exception();
}
}
catch (Win32Exception e) { MessageBox.Show("Return: " +
intReturn.ToString() + "\n\n" + e.ErrorCode + ": " + e.Message); }

// Purge print queue
try
{
intReturn = SetPrinter(hPrinter, 0, null, PRINTER_CONTROL_PURGE);
if (intReturn == 0)
{
Marshal.GetLastWin32Error();
throw new Win32Exception();
}
}
catch (Win32Exception e) { MessageBox.Show("Return: " +
intReturn.ToString() + "\n\n" + e.ErrorCode + ": " + e.Message); }

// Close printer
intReturn = ClosePrinter(hPrinter);
MessageBox.Show("ClosePrinter: " + intReturn.ToString());
}

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top