using System.Runtime.InteropServices; namespace CloudPrint.Client.Helpers; /// /// Windows Print Spooler API P/Invoke 声明,用于 RAW 打印、队列枚举和任务取消。 /// public static class WindowsPrintApi { public const int JobControlCancel = 3; public const int JobControlDelete = 5; [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public sealed class DocInfo { [MarshalAs(UnmanagedType.LPWStr)] public string DocumentName = string.Empty; [MarshalAs(UnmanagedType.LPWStr)] public string? OutputFile; [MarshalAs(UnmanagedType.LPWStr)] public string DataType = "RAW"; } [StructLayout(LayoutKind.Sequential)] public struct SystemTime { public short Year; public short Month; public short DayOfWeek; public short Day; public short Hour; public short Minute; public short Second; public short Milliseconds; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct JobInfo1 { public int JobId; [MarshalAs(UnmanagedType.LPWStr)] public string? PrinterName; [MarshalAs(UnmanagedType.LPWStr)] public string? MachineName; [MarshalAs(UnmanagedType.LPWStr)] public string? UserName; [MarshalAs(UnmanagedType.LPWStr)] public string? DocumentName; [MarshalAs(UnmanagedType.LPWStr)] public string? DataType; [MarshalAs(UnmanagedType.LPWStr)] public string? StatusText; public int Status; public int Priority; public int Position; public int TotalPages; public int PagesPrinted; public SystemTime Submitted; } [DllImport("winspool.drv", EntryPoint = "OpenPrinterW", SetLastError = true, CharSet = CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool OpenPrinter(string printerName, out IntPtr printerHandle, IntPtr printerDefaults); [DllImport("winspool.drv", EntryPoint = "ClosePrinter", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ClosePrinter(IntPtr printerHandle); [DllImport("winspool.drv", EntryPoint = "StartDocPrinterW", SetLastError = true, CharSet = CharSet.Unicode)] public static extern int StartDocPrinter(IntPtr printerHandle, int level, [In] DocInfo documentInfo); [DllImport("winspool.drv", EntryPoint = "EndDocPrinter", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool EndDocPrinter(IntPtr printerHandle); [DllImport("winspool.drv", EntryPoint = "StartPagePrinter", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool StartPagePrinter(IntPtr printerHandle); [DllImport("winspool.drv", EntryPoint = "EndPagePrinter", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool EndPagePrinter(IntPtr printerHandle); [DllImport("winspool.drv", EntryPoint = "WritePrinter", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool WritePrinter(IntPtr printerHandle, byte[] data, int count, out int written); [DllImport("winspool.drv", EntryPoint = "EnumJobsW", SetLastError = true, CharSet = CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool EnumJobs( IntPtr printerHandle, int firstJob, int numberOfJobs, int level, IntPtr jobBuffer, int bufferSize, out int bytesNeeded, out int jobsReturned); [DllImport("winspool.drv", EntryPoint = "SetJobW", SetLastError = true, CharSet = CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetJob( IntPtr printerHandle, int jobId, int level, IntPtr jobInfo, int command); }