PrintingServicesWin32.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. internal PrintingServicesWin32 ()
  37. {
  38. }
  39. internal override void LoadPrinterSettings (string printer, PrinterSettings settings)
  40. {
  41. int ret;
  42. settings.maximum_copies = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_COPIES, IntPtr.Zero, IntPtr.Zero);
  43. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_DUPLEX, IntPtr.Zero, IntPtr.Zero);
  44. settings.can_duplex = (ret == 1) ? true : false;
  45. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_COLORDEVICE, IntPtr.Zero, IntPtr.Zero);
  46. settings.supports_color = (ret == 1) ? true : false;
  47. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_ORIENTATION, IntPtr.Zero, IntPtr.Zero);
  48. if (ret != -1)
  49. settings.landscape_angle = ret;
  50. }
  51. internal override void LoadPrinterResolutions (string printer, PrinterSettings settings)
  52. {
  53. int ret;
  54. IntPtr ptr, buff = IntPtr.Zero;
  55. settings.PrinterResolutions.Clear ();
  56. LoadDefaultResolutions (settings.PrinterResolutions);
  57. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_ENUMRESOLUTIONS, IntPtr.Zero, IntPtr.Zero);
  58. if (ret == -1)
  59. return;
  60. ptr = buff = Marshal.AllocHGlobal (ret * 2 * Marshal.SizeOf (buff));
  61. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_ENUMRESOLUTIONS, buff, IntPtr.Zero);
  62. int x, y;
  63. if (ret != -1) {
  64. for (int i = 0; i < ret; i++) {
  65. x = Marshal.ReadInt32 (ptr);
  66. ptr = new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (x));
  67. y = Marshal.ReadInt32 (ptr);
  68. ptr = new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (y));
  69. settings.PrinterResolutions.Add (new PrinterResolution
  70. (x,y, PrinterResolutionKind.Custom));
  71. }
  72. }
  73. Marshal.FreeHGlobal (buff);
  74. }
  75. internal override void LoadPrinterPaperSizes (string printer, PrinterSettings settings)
  76. {
  77. int items, ret;
  78. IntPtr ptr_names, buff_names = IntPtr.Zero;
  79. IntPtr ptr_sizes, buff_sizes = IntPtr.Zero;
  80. IntPtr ptr_sizes_enum, buff_sizes_enum = IntPtr.Zero;
  81. string name;
  82. settings.PaperSizes.Clear ();
  83. items = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_PAPERSIZE, IntPtr.Zero, IntPtr.Zero);
  84. if (items == -1)
  85. return;
  86. try {
  87. ptr_sizes = buff_sizes = Marshal.AllocHGlobal (items * 2 * 4);
  88. ptr_names = buff_names = Marshal.AllocHGlobal (items * 64 * 2);
  89. ptr_sizes_enum = buff_sizes_enum = Marshal.AllocHGlobal (items * 2);
  90. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_PAPERSIZE, buff_sizes, IntPtr.Zero);
  91. if (ret == -1) {
  92. // the finally clause will free the unmanaged memory before returning
  93. return;
  94. }
  95. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_PAPERS, buff_sizes_enum, IntPtr.Zero);
  96. ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_PAPERNAMES, buff_names, IntPtr.Zero);
  97. int x, y;
  98. PaperSize ps;
  99. PaperKind kind;
  100. for (int i = 0; i < ret; i++) {
  101. x = Marshal.ReadInt32 (ptr_sizes, i * 4);
  102. y = Marshal.ReadInt32 (ptr_sizes, (i + 1) * 4);
  103. x = PrinterUnitConvert.Convert (x, PrinterUnit.TenthsOfAMillimeter,
  104. PrinterUnit.Display);
  105. y = PrinterUnitConvert.Convert (y, PrinterUnit.TenthsOfAMillimeter,
  106. PrinterUnit.Display);
  107. name = Marshal.PtrToStringUni (ptr_names);
  108. ptr_names = new IntPtr (ptr_names.ToInt64 () + 64 * 2);
  109. kind = (PaperKind) Marshal.ReadInt16 (ptr_sizes_enum);
  110. ptr_sizes_enum = new IntPtr (ptr_sizes_enum.ToInt64 () + 2);
  111. ps = new PaperSize (name, x,y);
  112. ps.SetKind (kind);
  113. settings.PaperSizes.Add (ps);
  114. }
  115. }
  116. finally {
  117. if (buff_names != IntPtr.Zero)
  118. Marshal.FreeHGlobal (buff_names);
  119. if (buff_sizes != IntPtr.Zero)
  120. Marshal.FreeHGlobal (buff_sizes);
  121. if (buff_sizes_enum != IntPtr.Zero)
  122. Marshal.FreeHGlobal (buff_sizes_enum);
  123. }
  124. }
  125. internal override bool StartDoc (GraphicsPrinter gr, string doc_name, string output_file)
  126. {
  127. DOCINFO di = new DOCINFO ();
  128. int ret;
  129. di.cbSize = Marshal.SizeOf (di);
  130. di.lpszDocName = Marshal.StringToHGlobalUni (doc_name);
  131. di.lpszOutput = IntPtr.Zero;
  132. di.lpszDatatype = IntPtr.Zero;
  133. di.fwType = 0;
  134. ret = Win32StartDoc (gr.Hdc, ref di);
  135. Marshal.FreeHGlobal (di.lpszDocName);
  136. return (ret > 0) ? true : false;
  137. }
  138. internal override bool StartPage (GraphicsPrinter gr)
  139. {
  140. int ret = Win32StartPage (gr.Hdc);
  141. return (ret > 0) ? true : false;
  142. }
  143. internal override bool EndPage (GraphicsPrinter gr)
  144. {
  145. int ret = Win32EndPage (gr.Hdc);
  146. return (ret > 0) ? true : false;
  147. }
  148. internal override bool EndDoc (GraphicsPrinter gr)
  149. {
  150. int ret = Win32EndDoc (gr.Hdc);
  151. Win32DeleteDC (gr.Hdc);
  152. gr.Graphics.Dispose ();
  153. return (ret > 0) ? true : false;
  154. }
  155. internal override IntPtr CreateGraphicsContext (PrinterSettings settings)
  156. {
  157. IntPtr dc = IntPtr.Zero;
  158. dc = Win32CreateDC (null, settings.PrinterName, null, IntPtr.Zero /* DEVMODE */);
  159. return dc;
  160. }
  161. // Properties
  162. internal override string DefaultPrinter {
  163. get {
  164. StringBuilder name = new StringBuilder (1024);
  165. int length = name.Capacity;
  166. Win32GetDefaultPrinter (name, ref length);
  167. return name.ToString ();
  168. }
  169. }
  170. internal override PrinterSettings.StringCollection InstalledPrinters {
  171. get {
  172. PrinterSettings.StringCollection col = new PrinterSettings.StringCollection (new string[] {});
  173. PRINTER_INFO printer_info;
  174. uint cbNeeded = 0, printers = 0;
  175. IntPtr ptr, buff;
  176. string s;
  177. // Determine space need it
  178. Win32EnumPrinters (2 /* PRINTER_ENUM_LOCAL */,
  179. null, 2, IntPtr.Zero, 0, ref cbNeeded, ref printers);
  180. ptr = buff = Marshal.AllocHGlobal ((int) cbNeeded);
  181. try {
  182. // Give us the printer list
  183. Win32EnumPrinters (2 /* PRINTER_ENUM_LOCAL */,
  184. null, 2, buff, (uint)cbNeeded, ref cbNeeded, ref printers);
  185. for (int i = 0; i < printers; i++) {
  186. printer_info = (PRINTER_INFO) Marshal.PtrToStructure (ptr, typeof (PRINTER_INFO));
  187. s = Marshal.PtrToStringUni (printer_info.pPrinterName);
  188. col.Add (s);
  189. ptr = new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (printer_info));
  190. }
  191. }
  192. finally {
  193. Marshal.FreeHGlobal (buff);
  194. }
  195. return col;
  196. }
  197. }
  198. //
  199. // DllImports
  200. //
  201. [DllImport("winspool.drv", CharSet=CharSet.Unicode, EntryPoint="DeviceCapabilities", SetLastError=true)]
  202. static extern int Win32DeviceCapabilities (string device, string port, DCCapabilities cap, IntPtr outputBuffer, IntPtr deviceMode);
  203. [DllImport("winspool.drv", CharSet=CharSet.Unicode, EntryPoint="EnumPrinters", SetLastError=true)]
  204. static extern int Win32EnumPrinters (int Flags, string Name, uint Level, IntPtr pPrinterEnum, uint cbBuf,
  205. ref uint pcbNeeded, ref uint pcReturned);
  206. [DllImport("winspool.drv", EntryPoint="GetDefaultPrinter", CharSet=CharSet.Unicode, SetLastError=true)]
  207. private static extern int Win32GetDefaultPrinter (StringBuilder buffer, ref int bufferSize);
  208. [DllImport("gdi32.dll", EntryPoint="CreateDC")]
  209. static extern IntPtr Win32CreateDC (string lpszDriver, string lpszDevice,
  210. string lpszOutput, IntPtr lpInitData);
  211. [DllImport("gdi32.dll", CharSet=CharSet.Unicode, EntryPoint="StartDoc")]
  212. static extern int Win32StartDoc (IntPtr hdc, [In] ref DOCINFO lpdi);
  213. [DllImport("gdi32.dll", EntryPoint="StartPage")]
  214. static extern int Win32StartPage (IntPtr hDC);
  215. [DllImport("gdi32.dll", EntryPoint="EndPage")]
  216. static extern int Win32EndPage (IntPtr hdc);
  217. [DllImport("gdi32.dll", EntryPoint="EndDoc")]
  218. static extern int Win32EndDoc (IntPtr hdc);
  219. [DllImport("gdi32.dll", EntryPoint="DeleteDC")]
  220. public static extern IntPtr Win32DeleteDC (IntPtr hDc);
  221. //
  222. // Structs
  223. //
  224. [StructLayout (LayoutKind.Sequential)]
  225. internal struct PRINTER_INFO
  226. {
  227. public IntPtr pServerName;
  228. public IntPtr pPrinterName;
  229. public IntPtr pShareName;
  230. public IntPtr pPortName;
  231. public IntPtr pDriverName;
  232. public IntPtr pComment;
  233. public IntPtr pLocation;
  234. public IntPtr pDevMode;
  235. public IntPtr pSepFile;
  236. public IntPtr pPrintProcessor;
  237. public IntPtr pDatatype;
  238. public IntPtr pParameters;
  239. public IntPtr pSecurityDescriptor;
  240. public uint Attributes;
  241. public uint Priority;
  242. public uint DefaultPriority;
  243. public uint StartTime;
  244. public uint UntilTime;
  245. public uint Status;
  246. public uint cJobs;
  247. public uint AveragePPM;
  248. }
  249. [StructLayout (LayoutKind.Sequential)]
  250. internal struct DOCINFO
  251. {
  252. public int cbSize;
  253. public IntPtr lpszDocName;
  254. public IntPtr lpszOutput;
  255. public IntPtr lpszDatatype;
  256. public int fwType;
  257. }
  258. // Enums
  259. internal enum DCCapabilities : short
  260. {
  261. DC_FIELDS = 1,
  262. DC_PAPERS = 2,
  263. DC_PAPERSIZE = 3,
  264. DC_MINEXTENT = 4,
  265. DC_MAXEXTENT = 5,
  266. DC_BINS = 6,
  267. DC_DUPLEX = 7,
  268. DC_SIZE = 8,
  269. DC_EXTRA = 9,
  270. DC_VERSION = 10,
  271. DC_DRIVER = 11,
  272. DC_BINNAMES = 12,
  273. DC_ENUMRESOLUTIONS = 13,
  274. DC_FILEDEPENDENCIES = 14,
  275. DC_TRUETYPE = 15,
  276. DC_PAPERNAMES = 16,
  277. DC_ORIENTATION = 17,
  278. DC_COPIES = 18,
  279. DC_BINADJUST = 19,
  280. DC_EMF_COMPLIANT = 20,
  281. DC_DATATYPE_PRODUCED = 21,
  282. DC_COLLATE = 22,
  283. DC_MANUFACTURER = 23,
  284. DC_MODEL = 24,
  285. DC_PERSONALITY = 25,
  286. DC_PRINTRATE = 26,
  287. DC_PRINTRATEUNIT = 27,
  288. DC_PRINTERMEM = 28,
  289. DC_MEDIAREADY = 29,
  290. DC_STAPLE = 30,
  291. DC_PRINTRATEPPM = 31,
  292. DC_COLORDEVICE = 32,
  293. DC_NUP = 33
  294. }
  295. }
  296. }