G
Gordon Truslove
I've been trying to get the printer status using GetPrinter and
Printer_Info_2
I'm getting closer, but it still fails.
Error 122 - The data area passed to a system call is too small.
---------------------------------------------------------------
[StructLayout( LayoutKind.Sequential,CharSet=CharSet.Auto)]
public struct PRINTERINFO2
{
[MarshalAs(UnmanagedType.LPTStr)]public string pServerName;
[MarshalAs(UnmanagedType.LPTStr)]public string pPrinterName;
[MarshalAs(UnmanagedType.LPTStr)]public string pShareName;
[MarshalAs(UnmanagedType.LPTStr)]public string pPortName;
[MarshalAs(UnmanagedType.LPTStr)]public string pDriverName;
[MarshalAs(UnmanagedType.LPTStr)]public string pComment;
[MarshalAs(UnmanagedType.LPTStr)]public string pLocation;
public IntPtr pDevMode;
[MarshalAs(UnmanagedType.LPTStr)]public string pSepFile;
[MarshalAs(UnmanagedType.LPTStr)]public string pPrintProcessor;
[MarshalAs(UnmanagedType.LPTStr)]public string pDatatype;
[MarshalAs(UnmanagedType.LPTStr)]public string pParameters;
public IntPtr pSecurityDescriptor;
public int Attributes;
public int Priority;
public int DefaultPriority;
public int StartTime;
public int UntilTime;
public int Status;
public int cJobs;
public int AveragePPM;
}
public class Printer
{
[ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,
CallingConvention=CallingConvention.StdCall )]
public static extern long OpenPrinter(string pPrinterName,ref IntPtr
phPrinter, int pDefault);
[ DllImport(
"winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall )]
public static extern long ClosePrinter(IntPtr hPrinter);
[ DllImport(
"winspool.drv" ,CharSet=CharSet.Auto)]
public static extern bool GetPrinter(IntPtr hPrinter,int Level,ref
PRINTERINFO2 pPrinter,int cbBuf,ref int pcbNeeded);
}
public class App
{
public static void Main ()
{
System.IntPtr lhPrinter=new System.IntPtr();
Printer.OpenPrinter("Epson EPL-5700",ref lhPrinter,0);
PRINTERINFO2 PI=new PRINTERINFO2();
int Need=0;
Console.WriteLine(Printer.GetPrinter(lhPrinter,2,ref
PI,Marshal.SizeOf(PI),ref Need));
Console.WriteLine("Error "+Marshal.GetLastWin32Error());
Printer.ClosePrinter(lhPrinter);
Console.ReadLine();
}
}
Printer_Info_2
I'm getting closer, but it still fails.
Error 122 - The data area passed to a system call is too small.
---------------------------------------------------------------
[StructLayout( LayoutKind.Sequential,CharSet=CharSet.Auto)]
public struct PRINTERINFO2
{
[MarshalAs(UnmanagedType.LPTStr)]public string pServerName;
[MarshalAs(UnmanagedType.LPTStr)]public string pPrinterName;
[MarshalAs(UnmanagedType.LPTStr)]public string pShareName;
[MarshalAs(UnmanagedType.LPTStr)]public string pPortName;
[MarshalAs(UnmanagedType.LPTStr)]public string pDriverName;
[MarshalAs(UnmanagedType.LPTStr)]public string pComment;
[MarshalAs(UnmanagedType.LPTStr)]public string pLocation;
public IntPtr pDevMode;
[MarshalAs(UnmanagedType.LPTStr)]public string pSepFile;
[MarshalAs(UnmanagedType.LPTStr)]public string pPrintProcessor;
[MarshalAs(UnmanagedType.LPTStr)]public string pDatatype;
[MarshalAs(UnmanagedType.LPTStr)]public string pParameters;
public IntPtr pSecurityDescriptor;
public int Attributes;
public int Priority;
public int DefaultPriority;
public int StartTime;
public int UntilTime;
public int Status;
public int cJobs;
public int AveragePPM;
}
public class Printer
{
[ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,
CallingConvention=CallingConvention.StdCall )]
public static extern long OpenPrinter(string pPrinterName,ref IntPtr
phPrinter, int pDefault);
[ DllImport(
"winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall )]
public static extern long ClosePrinter(IntPtr hPrinter);
[ DllImport(
"winspool.drv" ,CharSet=CharSet.Auto)]
public static extern bool GetPrinter(IntPtr hPrinter,int Level,ref
PRINTERINFO2 pPrinter,int cbBuf,ref int pcbNeeded);
}
public class App
{
public static void Main ()
{
System.IntPtr lhPrinter=new System.IntPtr();
Printer.OpenPrinter("Epson EPL-5700",ref lhPrinter,0);
PRINTERINFO2 PI=new PRINTERINFO2();
int Need=0;
Console.WriteLine(Printer.GetPrinter(lhPrinter,2,ref
PI,Marshal.SizeOf(PI),ref Need));
Console.WriteLine("Error "+Marshal.GetLastWin32Error());
Printer.ClosePrinter(lhPrinter);
Console.ReadLine();
}
}