PrintingServicesWin32.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. //
  2. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Author:
  24. //
  25. // Jordi Mas i Hernandez, [email protected]
  26. //
  27. using System.Runtime.InteropServices;
  28. using System.Collections;
  29. using System.Drawing.Printing;
  30. using System.ComponentModel;
  31. using System.Text;
  32. namespace System.Drawing.Printing
  33. {
  34. internal class PrintingServicesWin32 : PrintingServices
  35. {
  36. private string printer_name;
  37. private bool is_printer_valid;
  38. internal PrintingServicesWin32 ()
  39. {
  40. }
  41. internal override bool IsPrinterValid(string printer)
  42. {
  43. if (printer == null | printer == String.Empty)
  44. return false;
  45. int ret = Win32DocumentProperties (IntPtr.Zero, IntPtr.Zero, printer, IntPtr.Zero, IntPtr.Zero, 0);
  46. is_printer_valid = (ret > 0);
  47. this.printer_name = printer;
  48. return is_printer_valid;
  49. }
  50. internal override void LoadPrinterSettings (string printer, PrinterSettings settings)
  51. {
  52. int ret;
  53. DEVMODE devmode;
  54. IntPtr hPrn = IntPtr.Zero;
  55. IntPtr ptr_dev = IntPtr.Zero;
  56. settings.maximum_copies = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_COPIES, IntPtr.Zero, IntPtr.Zero);
  57. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_DUPLEX, IntPtr.Zero, IntPtr.Zero);
  58. settings.can_duplex = (ret == 1) ? true : false;
  59. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_COLORDEVICE, IntPtr.Zero, IntPtr.Zero);
  60. settings.supports_color = (ret == 1) ? true : false;
  61. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_ORIENTATION, IntPtr.Zero, IntPtr.Zero);
  62. if (ret != -1)
  63. settings.landscape_angle = ret;
  64. IntPtr dc = IntPtr.Zero;
  65. dc = Win32CreateIC (null, printer, null, IntPtr.Zero /* DEVMODE */);
  66. ret = Win32GetDeviceCaps (dc, (int)DevCapabilities.TECHNOLOGY);
  67. settings.is_plotter = ret == (int)PrinterType.DT_PLOTTER;
  68. Win32DeleteDC (dc);
  69. try {
  70. Win32OpenPrinter (printer, out hPrn, IntPtr.Zero);
  71. ret = Win32DocumentProperties (IntPtr.Zero, hPrn, null, IntPtr.Zero, IntPtr.Zero, 2);
  72. if (ret < 0)
  73. return;
  74. ptr_dev = Marshal.AllocHGlobal (ret);
  75. ret = Win32DocumentProperties (IntPtr.Zero, hPrn, null, ptr_dev, IntPtr.Zero, 2);
  76. devmode = (DEVMODE) Marshal.PtrToStructure (ptr_dev, typeof(DEVMODE));
  77. LoadPrinterPaperSizes (printer, settings);
  78. foreach (PaperSize paper_size in settings.PaperSizes) {
  79. if ((int) paper_size.Kind == devmode.dmPaperSize) {
  80. settings.DefaultPageSettings.PaperSize = paper_size;
  81. break;
  82. }
  83. }
  84. LoadPrinterPaperSources (printer, settings);
  85. foreach (PaperSource paper_source in settings.PaperSources) {
  86. if ((int) paper_source.Kind == devmode.dmDefaultSource) {
  87. settings.DefaultPageSettings.PaperSource = paper_source;
  88. break;
  89. }
  90. }
  91. }
  92. finally {
  93. Win32ClosePrinter (hPrn);
  94. if (ptr_dev != IntPtr.Zero)
  95. Marshal.FreeHGlobal (ptr_dev);
  96. }
  97. }
  98. internal override void LoadPrinterResolutions (string printer, PrinterSettings settings)
  99. {
  100. int ret;
  101. IntPtr ptr, buff = IntPtr.Zero;
  102. settings.PrinterResolutions.Clear ();
  103. LoadDefaultResolutions (settings.PrinterResolutions);
  104. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_ENUMRESOLUTIONS, IntPtr.Zero, IntPtr.Zero);
  105. if (ret == -1)
  106. return;
  107. ptr = buff = Marshal.AllocHGlobal (ret * 2 * Marshal.SizeOf (buff));
  108. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_ENUMRESOLUTIONS, buff, IntPtr.Zero);
  109. int x, y;
  110. if (ret != -1) {
  111. for (int i = 0; i < ret; i++) {
  112. x = Marshal.ReadInt32 (ptr);
  113. ptr = new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (x));
  114. y = Marshal.ReadInt32 (ptr);
  115. ptr = new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (y));
  116. settings.PrinterResolutions.Add (new PrinterResolution
  117. (x,y, PrinterResolutionKind.Custom));
  118. }
  119. }
  120. Marshal.FreeHGlobal (buff);
  121. }
  122. void LoadPrinterPaperSizes (string printer, PrinterSettings settings)
  123. {
  124. int items, ret;
  125. IntPtr ptr_names, buff_names = IntPtr.Zero;
  126. IntPtr ptr_sizes, buff_sizes = IntPtr.Zero;
  127. IntPtr ptr_sizes_enum, buff_sizes_enum = IntPtr.Zero;
  128. string name;
  129. if (settings.PaperSizes == null)
  130. settings.paper_sizes = new PrinterSettings.PaperSizeCollection (new PaperSize [0]);
  131. else
  132. settings.PaperSizes.Clear ();
  133. items = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_PAPERSIZE, IntPtr.Zero, IntPtr.Zero);
  134. if (items == -1)
  135. return;
  136. try {
  137. ptr_sizes = buff_sizes = Marshal.AllocHGlobal (items * 2 * 4);
  138. ptr_names = buff_names = Marshal.AllocHGlobal (items * 64 * 2);
  139. ptr_sizes_enum = buff_sizes_enum = Marshal.AllocHGlobal (items * 2);
  140. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_PAPERSIZE, buff_sizes, IntPtr.Zero);
  141. if (ret == -1) {
  142. // the finally clause will free the unmanaged memory before returning
  143. return;
  144. }
  145. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_PAPERS, buff_sizes_enum, IntPtr.Zero);
  146. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_PAPERNAMES, buff_names, IntPtr.Zero);
  147. int x, y;
  148. PaperSize ps;
  149. PaperKind kind;
  150. for (int i = 0; i < ret; i++) {
  151. x = Marshal.ReadInt32 (ptr_sizes, i * 8);
  152. y = Marshal.ReadInt32 (ptr_sizes, (i * 8) + 4);
  153. x = PrinterUnitConvert.Convert (x, PrinterUnit.TenthsOfAMillimeter,
  154. PrinterUnit.Display);
  155. y = PrinterUnitConvert.Convert (y, PrinterUnit.TenthsOfAMillimeter,
  156. PrinterUnit.Display);
  157. name = Marshal.PtrToStringUni (ptr_names);
  158. ptr_names = new IntPtr (ptr_names.ToInt64 () + 64 * 2);
  159. kind = (PaperKind) Marshal.ReadInt16 (ptr_sizes_enum);
  160. ptr_sizes_enum = new IntPtr (ptr_sizes_enum.ToInt64 () + 2);
  161. ps = new PaperSize (name, x,y);
  162. ps.SetKind (kind);
  163. settings.PaperSizes.Add (ps);
  164. }
  165. }
  166. finally {
  167. if (buff_names != IntPtr.Zero)
  168. Marshal.FreeHGlobal (buff_names);
  169. if (buff_sizes != IntPtr.Zero)
  170. Marshal.FreeHGlobal (buff_sizes);
  171. if (buff_sizes_enum != IntPtr.Zero)
  172. Marshal.FreeHGlobal (buff_sizes_enum);
  173. }
  174. }
  175. internal static bool StartDoc (GraphicsPrinter gr, string doc_name, string output_file)
  176. {
  177. DOCINFO di = new DOCINFO ();
  178. int ret;
  179. di.cbSize = Marshal.SizeOf (di);
  180. di.lpszDocName = Marshal.StringToHGlobalUni (doc_name);
  181. di.lpszOutput = IntPtr.Zero;
  182. di.lpszDatatype = IntPtr.Zero;
  183. di.fwType = 0;
  184. ret = Win32StartDoc (gr.Hdc, ref di);
  185. Marshal.FreeHGlobal (di.lpszDocName);
  186. return (ret > 0) ? true : false;
  187. }
  188. void LoadPrinterPaperSources (string printer, PrinterSettings settings)
  189. {
  190. int items, ret;
  191. IntPtr ptr_names, buff_names = IntPtr.Zero;
  192. IntPtr ptr_bins, buff_bins = IntPtr.Zero;
  193. PaperSourceKind kind;
  194. string name;
  195. if (settings.PaperSources == null)
  196. settings.paper_sources = new PrinterSettings.PaperSourceCollection (new PaperSource [0]);
  197. else
  198. settings.PaperSources.Clear ();
  199. items = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_BINNAMES, IntPtr.Zero, IntPtr.Zero);
  200. if (items == -1)
  201. return;
  202. try {
  203. ptr_names = buff_names = Marshal.AllocHGlobal (items * 2 * 24);
  204. ptr_bins = buff_bins = Marshal.AllocHGlobal (items * 2);
  205. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_BINNAMES, buff_names, IntPtr.Zero);
  206. if (ret == -1) {
  207. // the finally clause will free the unmanaged memory before returning
  208. return;
  209. }
  210. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_BINS, buff_bins, IntPtr.Zero);
  211. for (int i = 0; i < ret; i++) {
  212. name = Marshal.PtrToStringUni (ptr_names);
  213. kind = (PaperSourceKind) Marshal.ReadInt16 (ptr_bins);
  214. settings.PaperSources.Add (new PaperSource (name, kind));
  215. ptr_names = new IntPtr (ptr_names.ToInt64 () + 24 * 2);
  216. ptr_bins = new IntPtr (ptr_bins.ToInt64 () + 2);
  217. }
  218. }
  219. finally {
  220. if (buff_names != IntPtr.Zero)
  221. Marshal.FreeHGlobal (buff_names);
  222. if (buff_bins != IntPtr.Zero)
  223. Marshal.FreeHGlobal (buff_bins);
  224. }
  225. }
  226. internal static bool StartPage (GraphicsPrinter gr)
  227. {
  228. int ret = Win32StartPage (gr.Hdc);
  229. return (ret > 0) ? true : false;
  230. }
  231. internal static bool EndPage (GraphicsPrinter gr)
  232. {
  233. int ret = Win32EndPage (gr.Hdc);
  234. return (ret > 0) ? true : false;
  235. }
  236. internal static bool EndDoc (GraphicsPrinter gr)
  237. {
  238. int ret = Win32EndDoc (gr.Hdc);
  239. Win32DeleteDC (gr.Hdc);
  240. gr.Graphics.Dispose ();
  241. return (ret > 0) ? true : false;
  242. }
  243. internal static IntPtr CreateGraphicsContext (PrinterSettings settings, PageSettings default_page_settings)
  244. {
  245. IntPtr dc = IntPtr.Zero;
  246. dc = Win32CreateDC (null, settings.PrinterName, null, IntPtr.Zero /* DEVMODE */);
  247. return dc;
  248. }
  249. // Properties
  250. internal override string DefaultPrinter {
  251. get {
  252. StringBuilder name = new StringBuilder (1024);
  253. int length = name.Capacity;
  254. if (Win32GetDefaultPrinter (name, ref length) > 0)
  255. if (IsPrinterValid(name.ToString()))
  256. return name.ToString ();
  257. return String.Empty;
  258. }
  259. }
  260. internal static PrinterSettings.StringCollection InstalledPrinters {
  261. get {
  262. PrinterSettings.StringCollection col = new PrinterSettings.StringCollection (new string[] {});
  263. PRINTER_INFO printer_info;
  264. uint cbNeeded = 0, printers = 0;
  265. IntPtr ptr, buff;
  266. string s;
  267. // Determine space need it
  268. Win32EnumPrinters (2 /* PRINTER_ENUM_LOCAL */,
  269. null, 2, IntPtr.Zero, 0, ref cbNeeded, ref printers);
  270. if (cbNeeded <= 0)
  271. return col;
  272. ptr = buff = Marshal.AllocHGlobal ((int) cbNeeded);
  273. try {
  274. // Give us the printer list
  275. Win32EnumPrinters (2 /* PRINTER_ENUM_LOCAL */,
  276. null, 2, buff, (uint)cbNeeded, ref cbNeeded, ref printers);
  277. for (int i = 0; i < printers; i++) {
  278. printer_info = (PRINTER_INFO) Marshal.PtrToStructure (ptr, typeof (PRINTER_INFO));
  279. s = Marshal.PtrToStringUni (printer_info.pPrinterName);
  280. col.Add (s);
  281. ptr = new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (printer_info));
  282. }
  283. }
  284. finally {
  285. Marshal.FreeHGlobal (buff);
  286. }
  287. return col;
  288. }
  289. }
  290. internal override void GetPrintDialogInfo (string printer, ref string port, ref string type, ref string status, ref string comment)
  291. {
  292. IntPtr hPrn;
  293. PRINTER_INFO printer_info = new PRINTER_INFO ();
  294. int needed = 0;
  295. IntPtr ptr;
  296. Win32OpenPrinter (printer, out hPrn, IntPtr.Zero);
  297. if (hPrn == IntPtr.Zero)
  298. return;
  299. Win32GetPrinter (hPrn, 2, IntPtr.Zero, 0, ref needed);
  300. ptr = Marshal.AllocHGlobal (needed);
  301. Win32GetPrinter (hPrn, 2, ptr, needed, ref needed);
  302. printer_info = (PRINTER_INFO) Marshal.PtrToStructure (ptr, typeof (PRINTER_INFO));
  303. Marshal.FreeHGlobal (ptr);
  304. port = Marshal.PtrToStringUni (printer_info.pPortName);
  305. comment = Marshal.PtrToStringUni (printer_info.pComment);
  306. type = Marshal.PtrToStringUni (printer_info.pDriverName);
  307. status = GetPrinterStatusMsg (printer_info.Status);
  308. Win32ClosePrinter (hPrn);
  309. }
  310. private string GetPrinterStatusMsg (uint status)
  311. {
  312. string rslt = string.Empty;
  313. if (status == 0)
  314. return "Ready";
  315. if ((status & (uint) PrinterStatus.PS_PAUSED) != 0) rslt += "Paused; ";
  316. if ((status & (uint) PrinterStatus.PS_ERROR) != 0) rslt += "Error; ";
  317. if ((status & (uint) PrinterStatus.PS_PENDING_DELETION) != 0) rslt += "Pending deletion; ";
  318. if ((status & (uint) PrinterStatus.PS_PAPER_JAM) != 0) rslt += "Paper jam; ";
  319. if ((status & (uint) PrinterStatus.PS_PAPER_OUT) != 0) rslt += "Paper out; ";
  320. if ((status & (uint) PrinterStatus.PS_MANUAL_FEED) != 0) rslt += "Manual feed; ";
  321. if ((status & (uint) PrinterStatus.PS_PAPER_PROBLEM) != 0) rslt += "Paper problem; ";
  322. if ((status & (uint) PrinterStatus.PS_OFFLINE) != 0) rslt += "Offline; ";
  323. if ((status & (uint) PrinterStatus.PS_IO_ACTIVE) != 0) rslt += "I/O active; ";
  324. if ((status & (uint) PrinterStatus.PS_BUSY) != 0) rslt += "Busy; ";
  325. if ((status & (uint) PrinterStatus.PS_PRINTING) != 0) rslt += "Printing; ";
  326. if ((status & (uint) PrinterStatus.PS_OUTPUT_BIN_FULL) != 0) rslt += "Output bin full; ";
  327. if ((status & (uint) PrinterStatus.PS_NOT_AVAILABLE) != 0) rslt += "Not available; ";
  328. if ((status & (uint) PrinterStatus.PS_WAITING) != 0) rslt += "Waiting; ";
  329. if ((status & (uint) PrinterStatus.PS_PROCESSING) != 0) rslt += "Processing; ";
  330. if ((status & (uint) PrinterStatus.PS_INITIALIZING) != 0) rslt += "Initializing; ";
  331. if ((status & (uint) PrinterStatus.PS_WARMING_UP) != 0) rslt += "Warming up; ";
  332. if ((status & (uint) PrinterStatus.PS_TONER_LOW) != 0) rslt += "Toner low; ";
  333. if ((status & (uint) PrinterStatus.PS_NO_TONER) != 0) rslt += "No toner; ";
  334. if ((status & (uint) PrinterStatus.PS_PAGE_PUNT) != 0) rslt += "Page punt; ";
  335. if ((status & (uint) PrinterStatus.PS_USER_INTERVENTION) != 0) rslt += "User intervention; ";
  336. if ((status & (uint) PrinterStatus.PS_OUT_OF_MEMORY) != 0) rslt += "Out of memory; ";
  337. if ((status & (uint) PrinterStatus.PS_DOOR_OPEN) != 0) rslt += "Door open; ";
  338. if ((status & (uint) PrinterStatus.PS_SERVER_UNKNOWN) != 0) rslt += "Server unkown; ";
  339. if ((status & (uint) PrinterStatus.PS_POWER_SAVE) != 0) rslt += "Power save; ";
  340. return rslt;
  341. }
  342. //
  343. // DllImports
  344. //
  345. [DllImport("winspool.drv", CharSet=CharSet.Unicode, EntryPoint="OpenPrinter", SetLastError=true)]
  346. static extern int Win32OpenPrinter (string pPrinterName, out IntPtr phPrinter, IntPtr pDefault);
  347. [DllImport("winspool.drv", CharSet=CharSet.Unicode, EntryPoint="GetPrinter", SetLastError=true)]
  348. static extern int Win32GetPrinter (IntPtr hPrinter, int level, IntPtr dwBuf, int size, ref int dwNeeded);
  349. [DllImport("winspool.drv", CharSet=CharSet.Unicode, EntryPoint="ClosePrinter", SetLastError=true)]
  350. static extern int Win32ClosePrinter (IntPtr hPrinter);
  351. [DllImport("winspool.drv", CharSet=CharSet.Unicode, EntryPoint="DeviceCapabilities", SetLastError=true)]
  352. static extern int Win32DeviceCapabilities (string device, string port, DCCapabilities cap, IntPtr outputBuffer, IntPtr deviceMode);
  353. [DllImport("winspool.drv", CharSet=CharSet.Unicode, EntryPoint="EnumPrinters", SetLastError=true)]
  354. static extern int Win32EnumPrinters (int Flags, string Name, uint Level, IntPtr pPrinterEnum, uint cbBuf,
  355. ref uint pcbNeeded, ref uint pcReturned);
  356. [DllImport("winspool.drv", EntryPoint="GetDefaultPrinter", CharSet=CharSet.Unicode, SetLastError=true)]
  357. private static extern int Win32GetDefaultPrinter (StringBuilder buffer, ref int bufferSize);
  358. [DllImport("winspool.drv", EntryPoint="DocumentProperties", CharSet=CharSet.Unicode, SetLastError=true)]
  359. private static extern int Win32DocumentProperties (IntPtr hwnd, IntPtr hPrinter, string pDeviceName,
  360. IntPtr pDevModeOutput, IntPtr pDevModeInput, int fMode);
  361. [DllImport("gdi32.dll", EntryPoint="CreateDC")]
  362. static extern IntPtr Win32CreateDC (string lpszDriver, string lpszDevice,
  363. string lpszOutput, IntPtr lpInitData);
  364. [DllImport("gdi32.dll", EntryPoint="CreateIC")]
  365. static extern IntPtr Win32CreateIC (string lpszDriver, string lpszDevice,
  366. string lpszOutput, IntPtr lpInitData);
  367. [DllImport("gdi32.dll", CharSet=CharSet.Unicode, EntryPoint="StartDoc")]
  368. static extern int Win32StartDoc (IntPtr hdc, [In] ref DOCINFO lpdi);
  369. [DllImport("gdi32.dll", EntryPoint="StartPage")]
  370. static extern int Win32StartPage (IntPtr hDC);
  371. [DllImport("gdi32.dll", EntryPoint="EndPage")]
  372. static extern int Win32EndPage (IntPtr hdc);
  373. [DllImport("gdi32.dll", EntryPoint="EndDoc")]
  374. static extern int Win32EndDoc (IntPtr hdc);
  375. [DllImport("gdi32.dll", EntryPoint="DeleteDC")]
  376. public static extern IntPtr Win32DeleteDC (IntPtr hDc);
  377. [DllImport("gdi32.dll", EntryPoint="GetDeviceCaps")]
  378. public static extern int Win32GetDeviceCaps (IntPtr hDc, int index);
  379. //
  380. // Structs
  381. //
  382. [StructLayout (LayoutKind.Sequential)]
  383. internal struct PRINTER_INFO
  384. {
  385. public IntPtr pServerName;
  386. public IntPtr pPrinterName;
  387. public IntPtr pShareName;
  388. public IntPtr pPortName;
  389. public IntPtr pDriverName;
  390. public IntPtr pComment;
  391. public IntPtr pLocation;
  392. public IntPtr pDevMode;
  393. public IntPtr pSepFile;
  394. public IntPtr pPrintProcessor;
  395. public IntPtr pDatatype;
  396. public IntPtr pParameters;
  397. public IntPtr pSecurityDescriptor;
  398. public uint Attributes;
  399. public uint Priority;
  400. public uint DefaultPriority;
  401. public uint StartTime;
  402. public uint UntilTime;
  403. public uint Status;
  404. public uint cJobs;
  405. public uint AveragePPM;
  406. }
  407. [StructLayout (LayoutKind.Sequential)]
  408. internal struct DOCINFO
  409. {
  410. public int cbSize;
  411. public IntPtr lpszDocName;
  412. public IntPtr lpszOutput;
  413. public IntPtr lpszDatatype;
  414. public int fwType;
  415. }
  416. [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
  417. internal struct DEVMODE
  418. {
  419. [MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)]
  420. public string dmDeviceName;
  421. public short dmSpecVersion;
  422. public short dmDriverVersion;
  423. public short dmSize;
  424. public short dmDriverExtra;
  425. public int dmFields;
  426. public short dmOrientation;
  427. public short dmPaperSize;
  428. public short dmPaperLength;
  429. public short dmPaperWidth;
  430. public short dmScale;
  431. public short dmCopies;
  432. public short dmDefaultSource;
  433. public short dmPrintQuality;
  434. public short dmColor;
  435. public short dmDuplex;
  436. public short dmYResolution;
  437. public short dmTTOption;
  438. public short dmCollate;
  439. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  440. public string dmFormName;
  441. public short dmLogPixels;
  442. public short dmBitsPerPel;
  443. public int dmPelsWidth;
  444. public int dmPelsHeight;
  445. public int dmDisplayFlags;
  446. public int dmDisplayFrequency;
  447. public int dmICMMethod;
  448. public int dmICMIntent;
  449. public int dmMediaType;
  450. public int dmDitherType;
  451. public int dmReserved1;
  452. public int dmReserved2;
  453. public int dmPanningWidth;
  454. public int dmPanningHeight;
  455. }
  456. // Enums
  457. internal enum DCCapabilities : short
  458. {
  459. DC_FIELDS = 1,
  460. DC_PAPERS = 2,
  461. DC_PAPERSIZE = 3,
  462. DC_MINEXTENT = 4,
  463. DC_MAXEXTENT = 5,
  464. DC_BINS = 6,
  465. DC_DUPLEX = 7,
  466. DC_SIZE = 8,
  467. DC_EXTRA = 9,
  468. DC_VERSION = 10,
  469. DC_DRIVER = 11,
  470. DC_BINNAMES = 12,
  471. DC_ENUMRESOLUTIONS = 13,
  472. DC_FILEDEPENDENCIES = 14,
  473. DC_TRUETYPE = 15,
  474. DC_PAPERNAMES = 16,
  475. DC_ORIENTATION = 17,
  476. DC_COPIES = 18,
  477. DC_BINADJUST = 19,
  478. DC_EMF_COMPLIANT = 20,
  479. DC_DATATYPE_PRODUCED = 21,
  480. DC_COLLATE = 22,
  481. DC_MANUFACTURER = 23,
  482. DC_MODEL = 24,
  483. DC_PERSONALITY = 25,
  484. DC_PRINTRATE = 26,
  485. DC_PRINTRATEUNIT = 27,
  486. DC_PRINTERMEM = 28,
  487. DC_MEDIAREADY = 29,
  488. DC_STAPLE = 30,
  489. DC_PRINTRATEPPM = 31,
  490. DC_COLORDEVICE = 32,
  491. DC_NUP = 33
  492. }
  493. [Flags]
  494. internal enum PrinterStatus : uint
  495. {
  496. PS_PAUSED = 0x00000001,
  497. PS_ERROR = 0x00000002,
  498. PS_PENDING_DELETION = 0x00000004,
  499. PS_PAPER_JAM = 0x00000008,
  500. PS_PAPER_OUT = 0x00000010,
  501. PS_MANUAL_FEED = 0x00000020,
  502. PS_PAPER_PROBLEM = 0x00000040,
  503. PS_OFFLINE = 0x00000080,
  504. PS_IO_ACTIVE = 0x00000100,
  505. PS_BUSY = 0x00000200,
  506. PS_PRINTING = 0x00000400,
  507. PS_OUTPUT_BIN_FULL = 0x00000800,
  508. PS_NOT_AVAILABLE = 0x00001000,
  509. PS_WAITING = 0x00002000,
  510. PS_PROCESSING = 0x00004000,
  511. PS_INITIALIZING = 0x00008000,
  512. PS_WARMING_UP = 0x00010000,
  513. PS_TONER_LOW = 0x00020000,
  514. PS_NO_TONER = 0x00040000,
  515. PS_PAGE_PUNT = 0x00080000,
  516. PS_USER_INTERVENTION = 0x00100000,
  517. PS_OUT_OF_MEMORY = 0x00200000,
  518. PS_DOOR_OPEN = 0x00400000,
  519. PS_SERVER_UNKNOWN = 0x00800000,
  520. PS_POWER_SAVE = 0x01000000
  521. }
  522. // for use in GetDeviceCaps
  523. internal enum DevCapabilities
  524. {
  525. TECHNOLOGY = 2,
  526. }
  527. internal enum PrinterType
  528. {
  529. DT_PLOTTER = 0, // Vector Plotter
  530. DT_RASDIPLAY = 1, // Raster Display
  531. DT_RASPRINTER = 2, // Raster printer
  532. DT_RASCAMERA = 3, // Raster camera
  533. DT_CHARSTREAM = 4, // Character-stream, PLP
  534. DT_METAFILE = 5, // Metafile, VDM
  535. DT_DISPFILE = 6, // Display-file
  536. }
  537. }
  538. class GlobalPrintingServicesWin32 : GlobalPrintingServices
  539. {
  540. internal override PrinterSettings.StringCollection InstalledPrinters {
  541. get {
  542. return PrintingServicesWin32.InstalledPrinters;
  543. }
  544. }
  545. internal override IntPtr CreateGraphicsContext (PrinterSettings settings, PageSettings default_page_settings)
  546. {
  547. return PrintingServicesWin32.CreateGraphicsContext (settings, default_page_settings);
  548. }
  549. internal override bool StartDoc (GraphicsPrinter gr, string doc_name, string output_file)
  550. {
  551. return PrintingServicesWin32.StartDoc (gr, doc_name, output_file);
  552. }
  553. internal override bool EndDoc (GraphicsPrinter gr)
  554. {
  555. return PrintingServicesWin32.EndDoc (gr);
  556. }
  557. internal override bool StartPage (GraphicsPrinter gr)
  558. {
  559. return PrintingServicesWin32.StartPage (gr);
  560. }
  561. internal override bool EndPage (GraphicsPrinter gr)
  562. {
  563. return PrintingServicesWin32.EndPage (gr);
  564. }
  565. }
  566. }