.NET Windows Service call winspool.Drv access is denied

0

I have a Windows Service written in C#. It is exception When the OpenPrinter method of winspool.drv is called to open the printer in the LAN

The problem only appears on Windows 7 (x64). When I run the same service code as a desktop application on Windows 7 (x64), it works fine.

var deviceName = "\\ip\printername";
IntPtr hPrinter =new IntPtr();
var  DesiredAccess = PrinterAccessRights.PRINTER_ACCESS_USE | PrinterAccessRights.READ_CONTROL;
bool bRet = SpoolerApi.OpenPrinter(DeviceName, ref hPrinter, new PRINTER_DEFAULTS(DesiredAccess));

System.ComponentModel.Win32Exception (0x80004005): Access is denied
c#
windows-services
access-denied
asked on Stack Overflow Jun 26, 2019 by wingfay • edited Jun 27, 2019 by wingfay

1 Answer

0

It is probably a user problem. Try to change the user running the service ( from service control manager ), maybe just for a test you can make the service run as your user. If it works you should probably change the way the printer is configured in order to make available to the user running the service, or create a custom user to run the service with access rights to the printer.

answered on Stack Overflow Jun 27, 2019 by Felice Pollano

User contributions licensed under CC BY-SA 3.0