We're going to use those same OpenPrinter and ClosePrinter calls that we used in that last sample, except we're going to have to make one mod on the OpenPrinter call. The third argument there is an optional printer_defaults structure that contains an attribute indicating the level of access we need to the printer. When we just wanted to read the status we didn't need any special rights, but now that we want to set the status we need to tell Windows that we want to have administrative right to it.
We do that by declaring a slightly different version of the local external function call that takes a structure for the last argument rather than a long. The structure we're going to pass is defined as:
global type printer_defaults from structure
And the modified version of the OpenPrinter call we're going to use is:
Function long OpenPrinter ( string pPrinterName,
ref ulong phPrinter, printer_defaults pDefault )
Library "winspool.drv" Alias For "OpenPrinterW"
We're also going to declare a local external function for the SetPrinter Windows API call used to set the status:
Function long SetPrinter (ulong hPrinter, long Level,
long pPrinter, long command ) Library "winspool.drv"
Once we've got that in place, setting the printer status is as simple as:
constant long PRINTER_CONTROL_PAUSE = 1
constant long PRINTER_ACCESS_ADMINISTER = 4
ls_printer = PrintGetPrinter()
ll_pos=pos (ls_printer, "~t")
ls_printer=left(ls_printer, ll_pos -1)
pd.desiredaccess = PRINTER_ACCESS_ADMINISTER
rc = OpenPrinter(ls_printer, hPrinter, pd )
rc = SetPrinter ( hPrinter, 0, 0, PRINTER_CONTROL_PAUSE )
rc = ClosePrinter ( hPrinter )
Hi Bruce, Thanks for that. Just what I'm looking for...I think. I'm trying to get a handle to a printer to then use in some third party ole control. I've tried your code (exactly how it is above) but when calling OpenPrinter, hPrinter = 0. I'm using PB 10.5 on Windows 7. Any idea why?
Is it a network printer? If you're going to assume Admin rights on the printer, you'll need to actually have those rights. You may not be able to assert those rights on a network printer.
Post a Comment