c# - EnumPrinters function fails for Remote printers -
i trying network printer details (i have network printers on lan). working printer_enum_local
. when try printer_enum_remote
or printer_enum_network
enumprinters()
function fails (cbneeded
become 0)
what possibly goes wrong.
i reffer http://www.pinvoke.net/default.aspx/winspool.enumprinters , https://msdn.microsoft.com/en-us/library/windows/desktop/dd162692(v=vs.85).aspx
public class prnspool { [dllimport("winspool.drv", charset = charset.auto, setlasterror = true)] private static extern bool enumprinters(printerenumflags flags, string name, uint level, intptr pprinterenum, uint cbbuf, ref uint pcbneeded, ref uint pcreturned); private const int error_insufficient_buffer = 122; public static printer_info_1[] enumprinters1(printerenumflags flags) { uint cbneeded = 0; uint creturned = 0; if (enumprinters(flags, null, 1, intptr.zero, 0, ref cbneeded, ref creturned)) { return null; } int lastwin32error = marshal.getlastwin32error(); if (lastwin32error == error_insufficient_buffer) { intptr paddr = marshal.allochglobal((int)cbneeded); if (enumprinters(flags, null, 1, paddr, cbneeded, ref cbneeded, ref creturned)) { printer_info_1[] printerinfo1 = new printer_info_1[creturned]; long offset = paddr.toint64(); type type = typeof(printer_info_1); int increment = marshal.sizeof(type); (int = 0; < creturned; i++) { printerinfo1[i] = (printer_info_1)marshal.ptrtostructure(new intptr(offset), type); offset += increment; } marshal.freehglobal(paddr); return printerinfo1; } lastwin32error = marshal.getlastwin32error(); } throw new win32exception(lastwin32error); } } public struct printer_info_1 { uint flags; string pdescription; string pname; string pcomment; } [flagsattribute] public enum printerenumflags { printer_enum_default = 0x00000001, printer_enum_local = 0x00000002, printer_enum_connections = 0x00000004, printer_enum_favorite = 0x00000004, printer_enum_name = 0x00000008, printer_enum_remote = 0x00000010, printer_enum_shared = 0x00000020, printer_enum_network = 0x00000040, }
}
Comments
Post a Comment