WinSpool OpenPrinter Access denied

This 335th time this question was asked, I did not find the answer. I am trying to send raw data directly to the printer via the WinSpool api from an ASP.net C # application.

My code is just a copy here .

Error here

if( OpenPrinter( szPrinterName.Normalize(), out hPrinter, IntPtr.Zero ) )

It works fine for a local printer, but for a shared network printer, the result OpenPrinter( GetLastErroractually the result ) is always 5 - Access Denied.

I tried

  • different values ​​for PRINTER_DEFAULTSwith different combinationsDesiredAccess
  • grant the user administrator rights.
  • set up a printer like this

I should note that I just want to print, not change the printer configuration or something that requires administrator rights.

" " . . API?


: , , Windows . -?


2: , - ( : DMZ), ( )


3: . (, \\host_pc\C$), , . " " , "" , .

+4
2

- ASP.Net, IIS, IIS_APPPOOL\mysite. -, , IIS ( ) , ( IIS).

(, ) IIS APPPool NetworkService. MyDomain\MyHostName $, , ( - ).

, IIS, " " "".

: http://www.iis.net/learn/manage/configuring-security/application-pool-identities

+2

, , , , , :

  • AnyCPU x64.
  • x86.

, , GC PRINTER_DEFAULTS, OpenPrinter . - , .

public class PrinterSettings
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    internal class PRINTERDEFAULTSClass
    {
        public IntPtr pDatatype;
        public IntPtr pDevMode;
        public int DesiredAccess;
    } 

    [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    private static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, PRINTERDEFAULTSClass pdc);

    public DEVMODE GetPrinterSettings(string PrinterName)
    {
        DEVMODE dm;

        var pdc = new PRINTERDEFAULTSClass
        {
            pDatatype = new IntPtr(0),
            pDevMode = new IntPtr(0),
            DesiredAccess = PRINTER_ALL_ACCESS
        };

        var nRet = Convert.ToInt32(OpenPrinter(PrinterName,
                out hPrinter, pdc));
    }
}
0

All Articles