We are trying to pause a printer from C# or VB code using the Win32 printer API's in the winspool.drv. We are having varying degrees of success. With the following VB code, the OpenPrinter call works, but we get an access denied error message upon calling SetPrinter. I have auditing turned on the printer and get no failure audits, so it is hard to imagine that this error message is correct
Dim strPrinterName As Strin
strPrinterName = "Translator Image Printer
Dim lngReturn As Lon
Dim hPrinter As Lon
Dim printDefaults As PRINTER_DEFAULT
printDefaults.DesiredAccess = PRINTER_ALL_ACCES
lngReturn = OpenPrinterA(strPrinterName, hPrinter, printDefaults
If lngReturn <> 1 Or Err.LastDllError <> 0 The
MsgBox "Could not open printer!
En
End I
lngReturn = SetPrinterA(hPrinter, 0, 0, PrinterControlCodes.PrinterControlPause
If lngReturn <> 1 Or Err.LastDllError <> 0 The
Dim LangOptions As Lon
LangOptions = &H
Dim buffer As Strin
buffer = Space(256
lngReturn = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0&, Err.LastDllError, LangOptions, buffer, Len(buffer), 0&
MsgBox "Could not set printer: " & buffe
End I
lngReturn = ClosePrinter(hPrinter
If lngReturn <> 1 The
MsgBox "Could not close printer!
End I
In the C# world, we cannot even get the OpenPrinter call to work. It always returns with 0 and the Marshal.GetLastWin32Error() returns 87. The following C# code is where we are having issues
string printerName = "Translator Image Printer"
PrinterDefaults defaults = new PrinterDefaults()
defaults.DesiredAccess = PrinterAccessRights.PRINTER_ALL_ACCESS
int printerHandle = 0
int returnValue = OpenPrinterA(printerName, printerHandle, defaults)
if (returnValue == 1
Assert.AreEqual(0, Marshal.GetLastWin32Error(), String.Format("Marshal error #{0}: Could not open printer {1}", Marshal.GetLastWin32Error(), printerName))
els
Assert.Fail(String.Format("Last error: {0}", Marshal.GetLastWin32Error()))
returnValue = SetPrinterA(printerHandle, 0, 0, PrinterControlCommands.PRINTER_CONTROL_PAUSE); //(both Level and Buffer are empty to send commands
if (returnValue == 1
Assert.AreEqual(0, Marshal.GetLastWin32Error(), String.Format("Marshal error #{0}: Could not set printer {1}", Marshal.GetLastWin32Error(), printerName))
returnValue = ClosePrinter(printerHandle)
if (returnValue == 1
Assert.AreEqual(0, Marshal.GetLastWin32Error(), String.Format("Marshal error #{0}: Could not close printer {1}", Marshal.GetLastWin32Error(), printerName))
Mainly, we are interested in getting the C# code to work, and used the VB code to troubleshoot the OpenPrinter/SetPrinter issues. Any ideas on what to checkout would be greatly appreciated..
Thanks