1 {
2   Author: Olivier Guilbaud
3 
4  *****************************************************************************
5   This file is part of the Printer4Lazarus package
6 
7   See the file COPYING.modifiedLGPL.txt, included in this distribution,
8   for details about the license.
9  *****************************************************************************
10 
11   Abstract:
12     This unit provide an access at Printers spool and other functions for manage
13     the printers on Win32
14 
15   Documentations
16     - Wine project
17     - Microsoft MSDN Web
18 }
19 
20 unit WinUtilPrn;
21 
22 {$mode objfpc}{$H+}
23 
24 interface
25 
26 {$IFNDEF MSWindows}
27 {$FATAL This unit is reserved to Win32/Win64}
28 {$ENDIF}
29 
30 uses
31   Windows, Types, Classes, SysUtils, LCLType, Printers;
32 
33 const
34   {$i winutilprnconst.inc}
35 
36   LibWinSpool = 'winspool.drv';
37 
38 const
39   Win32Orientations: array [TPrinterOrientation] of SHORT = (
40     DMORIENT_PORTRAIT, DMORIENT_LANDSCAPE, DMORIENT_PORTRAIT, DMORIENT_LANDSCAPE);
41 
42 type
ndnull43   TFcntHook = function(Wnd: HWND; uiMsg: UINT; wParam: WPARAM; lParam: LPARAM): UINT_PTR; stdcall;
44 
45 // todo: remove when fpc will be released with them
46 {$ifndef win64}
47   {$packrecords 1}
48 {$endif}
49 
50   tagPSD = record
51     lStructSize: DWORD;
52     hWndOwner: HWND;
53     hDevMode: HGLOBAL;
54     hDevNames: HGLOBAL;
55     Flags: DWORD;
56     ptPaperSize: TPOINT;
57     rtMinMargin: TRECT;
58     rtMargin: TRECT;
59     hInstance: HINST;
60     lCustData: LPARAM;
61     lpfnPageSetupHook: TFcntHook;
62     lpfnPagePaintHook: TFcntHook;
63     lpPageSetupTemplateName: PChar;
64     hPageSetupTemplate: HGLOBAL;
65   end;
66   PtagPSD = ^tagPSD;
67 
68   tagPD = record
69     lStructSize: DWORD;
70     hWndOwner: HWND;
71     hDevMode: HGLOBAL;
72     hDevNames: HGLOBAL;
73     hDC: HDC;
74     Flags: DWORD;
75     nFromPage: word;
76     nToPage: word;
77     nMinPage: word;
78     nMaxPage: word;
79     nCopies: word;
80     hInstance: HINST;
81     lCustData: LPARAM;
82     lpfnPrintHook: TFcntHook;
83     lpfnSetupHook: TFcntHook;
84     lpPrintTemplateName: PChar;
85     lpSetupTemplateName: PChar;
86     hPrintTemplate: HGLOBAL;
87     hSetupTemplate: HGLOBAL;
88   end;
89   PtagPD = ^tagPD;
90 
91 
92   {
93   _PRINTER_DEFAULTSA = record
94        pDatatype : LPSTR;
95        pDevMode : LPDEVMODE;
96        DesiredAccess : ACCESS_MASK;
97     end;
98   PRINTER_DEFAULTSA = _PRINTER_DEFAULTSA;
99   PPRINTER_DEFAULTSA = ^_PRINTER_DEFAULTSA;
100   LPPRINTER_DEFAULTSA = ^_PRINTER_DEFAULTSA;
101   }
102 
103   _PRINTER_DEFAULTSW = record
104     pDatatype: pwidechar;
105     pDevMode: LPDEVMODEW;
106     DesiredAccess: ACCESS_MASK;
107   end;
108   LPPRINTER_DEFAULTSW = ^_PRINTER_DEFAULTSW;
109 
110 
111   _PRINTER_INFO_2A = record
112        pServerName : LPTSTR;
113        pPrinterName : LPTSTR;
114        pShareName : LPTSTR;
115        pPortName : LPTSTR;
116        pDriverName : LPTSTR;
117        pComment : LPTSTR;
118        pLocation : LPTSTR;
119        pDevMode : LPDEVMODE;
120        pSepFile : LPTSTR;
121        pPrintProcessor : LPTSTR;
122        pDatatype : LPTSTR;
123        pParameters : LPTSTR;
124        pSecurityDescriptor : PSECURITY_DESCRIPTOR;
125        Attributes : DWORD;
126        Priority : DWORD;
127        DefaultPriority : DWORD;
128        StartTime : DWORD;
129        UntilTime : DWORD;
130        Status : DWORD;
131        cJobs : DWORD;
132        AveragePPM : DWORD;
133     end;
134   PRINTER_INFO_2A = _PRINTER_INFO_2A;
135   PPRINTER_INFO_2A = ^_PRINTER_INFO_2A;
136   LPPRINTER_INFO_2A = ^_PRINTER_INFO_2A;
137 
138   PRINTER_INFO_2 = PRINTER_INFO_2A;
139   PPRINTER_INFO_2 = ^PRINTER_INFO_2;
140   LPPRINTER_INFO_2 = ^PRINTER_INFO_2;
141 
142 
143 
144   _PRINTER_INFO_2W = record
145        pServerName : LPWSTR;
146        pPrinterName : LPWSTR;
147        pShareName : LPWSTR;
148        pPortName : LPWSTR;
149        pDriverName : LPWSTR;
150        pComment : LPWSTR;
151        pLocation : LPWSTR;
152        pDevMode : LPDEVMODEW;
153        pSepFile : LPWSTR;
154        pPrintProcessor : LPWSTR;
155        pDatatype : LPWSTR;
156        pParameters : LPWSTR;
157        pSecurityDescriptor : PSECURITY_DESCRIPTOR;
158        Attributes : DWORD;
159        Priority : DWORD;
160        DefaultPriority : DWORD;
161        StartTime : DWORD;
162        UntilTime : DWORD;
163        Status : DWORD;
164        cJobs : DWORD;
165        AveragePPM : DWORD;
166     end;
167 
168   PRINTER_INFO_2W = _PRINTER_INFO_2W;
169   PPRINTER_INFO_2W = ^_PRINTER_INFO_2W;
170   LPPRINTER_INFO_2W = ^_PRINTER_INFO_2W;
171 
172   //PRINTER_INFO_2 = PRINTER_INFO_2W;
173   //PPRINTER_INFO_2 = ^PRINTER_INFO_2;
174   //LPPRINTER_INFO_2 = ^PRINTER_INFO_2;
175 
176 
177 
178 
179   _PRINTER_INFO_4A = record
180        pPrinterName : LPSTR;
181        pServerName : LPSTR;
182        Attributes : DWORD;
183     end;
184   PRINTER_INFO_4A = _PRINTER_INFO_4A;
185   PPRINTER_INFO_4A = ^_PRINTER_INFO_4A;
186   LPPRINTER_INFO_4A = ^_PRINTER_INFO_4A;
187 
188   PRINTER_INFO_4 = PRINTER_INFO_4A;
189   PPRINTER_INFO_4 = ^PRINTER_INFO_4;
190   LPPRINTER_INFO_4 = ^PRINTER_INFO_4;
191 
192 
193   _PRINTER_INFO_4W = record
194        pPrinterName : LPWSTR;
195        pServerName : LPWSTR;
196        Attributes : DWORD;
197     end;
198   PRINTER_INFO_4W = _PRINTER_INFO_4W;
199   PPRINTER_INFO_4W = ^_PRINTER_INFO_4W;
200   LPPRINTER_INFO_4W = ^_PRINTER_INFO_4W;
201 
202   //PRINTER_INFO_4 = PRINTER_INFO_4W;
203   //PPRINTER_INFO_4 = ^PRINTER_INFO_4;
204   //LPPRINTER_INFO_4 = ^PRINTER_INFO_4;
205 
206 type
207   { TPrinterDevice }
208 
209   TPrinterDevice = class
210   public
211     Name: string;
212     Driver: string;
213     Device: string;
214     Port: string;
215     DefaultPaperName: string;
216     DefaultPaper: Short;
217     DefaultBin: short;
218     DevModeW: PDeviceModeW;
219     DevModeSize: integer;
220     destructor Destroy; override;
221   end;
222 
DeviceCapabilitiesnull223 function DeviceCapabilities(pDevice, pPort: PChar; fwCapability: word;
224   pOutput: PChar; DevMode: PDeviceMode): integer; stdcall; external LibWinSpool Name 'DeviceCapabilitiesA';
DeviceCapabilitiesWnull225 function DeviceCapabilitiesW(pDevice, pPort: PWideChar; fwCapability: word;
226   pOutput: PWideChar; DevMode: PDeviceModeW): integer; stdcall; external LibWinSpool Name 'DeviceCapabilitiesW';
227 
GetProfileStringnull228 function GetProfileString(lpAppName: PChar; lpKeyName: PChar; lpDefault: PChar;
229   lpReturnedString: PChar; nSize: DWORD): DWORD; stdcall; external 'kernel32' Name 'GetProfileStringA';
230 
PrintDlgnull231 //function PrintDlg(lppd: PtagPD): BOOL; stdcall; external 'comdlg32.dll' Name 'PrintDlgA';
232 function PrintDlgW(lppd: PTagPD): BOOL; stdcall; external 'comdlg32.dll' name 'PrintDlgW';
PageSetupDlgnull233 //function PageSetupDlg(lppd: PtagPSD): BOOL; stdcall; external 'comdlg32.dll' Name 'PageSetupDlgA';
234 function PageSetupDlgW(lppd: PtagPSD): BOOL; stdcall; external 'comdlg32.dll' Name 'PageSetupDlgW';
CommDlgExtendedErrornull235 function CommDlgExtendedError: DWORD; stdcall; external 'comdlg32.dll' Name 'CommDlgExtendedError';
236 
CreateICnull237 //function CreateIC(lpszDriver, lpszDevice, lpszOutput: PChar; lpdvmInit: PDeviceMode): HDC; stdcall; external 'gdi32.dll' Name 'CreateICA';
238 function CreateICW(lpszDriver, lpszDevice, lpszOutput: pwidechar; lpdvmInit: PDeviceModeW): HDC; stdcall; external 'gdi32.dll' Name 'CreateICW';
CreateDCnull239 //function CreateDC(lpszDriver, lpszDevice, lpszOutput: PChar; lpdvmInit: PDeviceMode): HDC; stdcall; external 'gdi32.dll' Name 'CreateDCA';
240 function CreateDCW(lpszDriver, lpszDevice, lpszOutput: pwidechar; lpdvmInit: PDeviceModeW): HDC; stdcall; external 'gdi32.dll' Name 'CreateDCW';
241 
DeleteDCnull242 function DeleteDC(DC: HDC): BOOL; stdcall; external 'gdi32.dll' Name 'DeleteDC';
StartDocnull243 function StartDoc(DC: HDC; Inf: PDocInfo): integer; stdcall; external 'gdi32.dll' Name 'StartDocA';
EndDocnull244 function EndDoc(DC: HDC): integer; stdcall; external 'gdi32.dll' Name 'EndDoc';
StartPagenull245 function StartPage(DC: HDC): integer; stdcall; external 'gdi32.dll' Name 'StartPage';
EndPagenull246 function EndPage(DC: HDC): integer; stdcall; external 'gdi32.dll' Name 'EndPage';
AbortDocnull247 function AbortDoc(DC: HDC): integer; stdcall; external 'gdi32.dll' Name 'AbortDoc';
GlobalFreenull248 function GlobalFree(HMem: HGlobal): HGlobal; stdcall; external 'kernel32.dll' Name 'GlobalFree';
249 
250 // todo: remove when WinSpool.pp will be released with fpc
OpenPrinternull251 //function OpenPrinter(_para1:LPSTR; _para2:PHANDLE; _para3:LPPRINTER_DEFAULTSA):BOOL;stdcall; external LibWinSpool name 'OpenPrinterA';
252 function OpenPrinterW(_para1:pwidechar; _para2:PHANDLE; _para3:LPPRINTER_DEFAULTSW):BOOL;stdcall; external LibWinSpool name 'OpenPrinterW';
ClosePrinternull253 function ClosePrinter(_para1:HANDLE):BOOL;stdcall; external LibWinSpool Name 'ClosePrinter';
DocumentPropertiesnull254 //function DocumentProperties(_para1:HWND; _para2:HANDLE; _para3:LPSTR; _para4:PDEVMODE; _para5:PDEVMODE; _para6:DWORD):LONG;stdcall; external LibWinSpool name 'DocumentPropertiesA';
255 function DocumentPropertiesW(_para1:HWND; _para2:HANDLE; _para3:pwidechar; _para4:PDEVMODEW; _para5:PDEVMODEW; _para6:DWORD):LONG;stdcall; external LibWinSpool name 'DocumentPropertiesW';
EnumPrintersnull256 //function EnumPrinters(_para1:DWORD; _para2:LPSTR; _para3:DWORD; _para4:PBYTE; _para5:DWORD; _para6:PDWORD; _para7:PDWORD):BOOL;stdcall; external LibWinSpool name 'EnumPrintersA';
257 function EnumPrintersW(_para1:DWORD; _para2:Pwidechar; _para3:DWORD; _para4:PBYTE; _para5:DWORD; _para6:PDWORD; _para7:PDWORD):BOOL;stdcall; external LibWinSpool name 'EnumPrintersW';
GetPrinternull258 function GetPrinter(_para1:HANDLE; _para2:DWORD; _para3:PBYTE; _para4:DWORD; _para5:PDWORD):BOOL;stdcall; external LibWinSpool name 'GetPrinterA';
StartDocPrinternull259 function StartDocPrinter(hPrinter: THANDLE; Level: DWORD; DocInfo: PByte): DWORD; stdcall; external LibWinSpool Name 'StartDocPrinterA';
StartPagePrinternull260 function StartPagePrinter(_para1:HANDLE):BOOL;stdcall; external LibWinSpool name 'StartPagePrinter';
EndDocPrinternull261 function EndDocPrinter(hprinter: THANDLE): BOOL; stdcall; external LibWinSpool Name 'EndDocPrinter';
EndPagePrinternull262 function EndPagePrinter(hprinter: THANDLE): BOOL; stdcall; external LibWinSpool Name 'EndPagePrinter';
AbortPrinternull263 function AbortPrinter(hPrinter: THANDLE): BOOL; stdcall; external LibWinSpool Name 'AbortPrinter';
WritePrinternull264 function WritePrinter(hPrinter: THANDLE; Buffer: Pointer; Count: DWord; Written: PDWORD): BOOL; stdcall; external LibWinSpool Name 'WritePrinter';
265 
ResetDCWnull266 function ResetDCW(DC: HDC; const p2: TDeviceModeW):HDC; stdcall; external 'gdi32.dll' name 'ResetDCW';
267 
268 implementation
269 
270 { TPrinterDevice }
271 
272 destructor TPrinterDevice.Destroy;
273 begin
274   ReallocMem(DevModeW, 0);
275   inherited Destroy;
276 end;
277 
278 end.
279 
280