Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Access is Denied when trying to SetPrinter

940 views
Skip to first unread message

Stuart Macaulay

unread,
Jul 8, 2003, 12:13:08 AM7/8/03
to

Hi,

I'm sure someone has experienced this problem before. When I call GetPrinter all is fine. Then I change the setting how I would like them also seems to work OK. When I try to SetPrinter I get the following error "System Error. Code: 5. Access is denied" This error appears to happen locally as I get error warnings in my PC's Security Event Log.

Anyone have any clues on this. It could drive a man to drink.

Cheers
Stuart

gc

unread,
Jul 8, 2003, 7:52:54 AM7/8/03
to
One thing that may be way off .. how are you logged in to your PC? Are you
as administrator, power user etc. It could be that you don't have the
access rights from a user level, not a code level.

G

"Stuart Macaulay" <stuart....@sovereign.co.nz> wrote in message
news:3f0a44d4$1...@newsgroups.borland.com...

Peter Below (TeamB)

unread,
Jul 8, 2003, 2:07:44 PM7/8/03
to
In article <3f0a44d4$1...@newsgroups.borland.com>, Stuart Macaulay wrote:
>
> I'm sure someone has experienced this problem before. When I call
> GetPrinter all is fine. Then I change the setting how I would like
> them also seems to work OK. When I try to SetPrinter I get the
> following error "System Error. Code: 5. Access is denied"

The problem is likely to be the way you call OpenPrinter to get the printer
handle. You need to specify an access mode of PRINTER_ACCESS_ADMINISTER to be
able to use SetPrinter on the handle.

{-- GetCurrentPrinterHandle -------------------------------------------}
{: Retrieves the handle of the current printer
@Returns an API printer handle for the current printer
@Desc Uses WinSpool.OpenPrinter to get a printer handle. The caller
takes ownership of the handle and <b>must</b> call ClosePrinter on it
once the handle is no longer needed. Failing to do that creates a
serious resource leak! <P>
Requires Printers and WinSpool in the Uses clause.
@Raises EWin32Error if the OpenPrinter call fails.
}{ Created 30.9.2000 by P. Below
-----------------------------------------------------------------------}
Function GetCurrentPrinterHandle: THandle;
Const
Defaults: TPrinterDefaults = (
pDatatype : nil;
pDevMode : nil;
DesiredAccess : PRINTER_ACCESS_USE or PRINTER_ACCESS_ADMINISTER );
Var
Device, Driver, Port : array[0..255] of char;
hDeviceMode: THandle;
Begin { GetCurrentPrinterHandle }
Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
If not OpenPrinter(@Device, Result, @Defaults) Then
RaiseLastWin32Error;
End; { GetCurrentPrinterHandle }


--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be


0 new messages