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   DMPAPER_USER = 256;
42 
43 type
ndnull44   TFcntHook = function(Wnd: HWND; uiMsg: UINT; wParam: WPARAM; lParam: LPARAM): UINT_PTR; stdcall;
45 
46 // todo: remove when fpc will be released with them
47 {$ifndef win64}
48   {$packrecords 1}
49 {$endif}
50 
51   tagPSD = record
52     lStructSize: DWORD;
53     hWndOwner: HWND;
54     hDevMode: HGLOBAL;
55     hDevNames: HGLOBAL;
56     Flags: DWORD;
57     ptPaperSize: TPOINT;
58     rtMinMargin: TRECT;
59     rtMargin: TRECT;
60     hInstance: HINST;
61     lCustData: LPARAM;
62     lpfnPageSetupHook: TFcntHook;
63     lpfnPagePaintHook: TFcntHook;
64     lpPageSetupTemplateName: PChar;
65     hPageSetupTemplate: HGLOBAL;
66   end;
67   PtagPSD = ^tagPSD;
68 
69   tagPD = record
70     lStructSize: DWORD;
71     hWndOwner: HWND;
72     hDevMode: HGLOBAL;
73     hDevNames: HGLOBAL;
74     hDC: HDC;
75     Flags: DWORD;
76     nFromPage: word;
77     nToPage: word;
78     nMinPage: word;
79     nMaxPage: word;
80     nCopies: word;
81     hInstance: HINST;
82     lCustData: LPARAM;
83     lpfnPrintHook: TFcntHook;
84     lpfnSetupHook: TFcntHook;
85     lpPrintTemplateName: PChar;
86     lpSetupTemplateName: PChar;
87     hPrintTemplate: HGLOBAL;
88     hSetupTemplate: HGLOBAL;
89   end;
90   PtagPD = ^tagPD;
91 
92 
93   {
94   _PRINTER_DEFAULTSA = record
95        pDatatype : LPSTR;
96        pDevMode : LPDEVMODE;
97        DesiredAccess : ACCESS_MASK;
98     end;
99   PRINTER_DEFAULTSA = _PRINTER_DEFAULTSA;
100   PPRINTER_DEFAULTSA = ^_PRINTER_DEFAULTSA;
101   LPPRINTER_DEFAULTSA = ^_PRINTER_DEFAULTSA;
102   }
103 
104   _PRINTER_DEFAULTSW = record
105     pDatatype: pwidechar;
106     pDevMode: LPDEVMODEW;
107     DesiredAccess: ACCESS_MASK;
108   end;
109   LPPRINTER_DEFAULTSW = ^_PRINTER_DEFAULTSW;
110 
111 
112   _PRINTER_INFO_2A = record
113        pServerName : LPTSTR;
114        pPrinterName : LPTSTR;
115        pShareName : LPTSTR;
116        pPortName : LPTSTR;
117        pDriverName : LPTSTR;
118        pComment : LPTSTR;
119        pLocation : LPTSTR;
120        pDevMode : LPDEVMODE;
121        pSepFile : LPTSTR;
122        pPrintProcessor : LPTSTR;
123        pDatatype : LPTSTR;
124        pParameters : LPTSTR;
125        pSecurityDescriptor : PSECURITY_DESCRIPTOR;
126        Attributes : DWORD;
127        Priority : DWORD;
128        DefaultPriority : DWORD;
129        StartTime : DWORD;
130        UntilTime : DWORD;
131        Status : DWORD;
132        cJobs : DWORD;
133        AveragePPM : DWORD;
134     end;
135   PRINTER_INFO_2A = _PRINTER_INFO_2A;
136   PPRINTER_INFO_2A = ^_PRINTER_INFO_2A;
137   LPPRINTER_INFO_2A = ^_PRINTER_INFO_2A;
138 
139   PRINTER_INFO_2 = PRINTER_INFO_2A;
140   PPRINTER_INFO_2 = ^PRINTER_INFO_2;
141   LPPRINTER_INFO_2 = ^PRINTER_INFO_2;
142 
143 
144 
145   _PRINTER_INFO_2W = record
146        pServerName : LPWSTR;
147        pPrinterName : LPWSTR;
148        pShareName : LPWSTR;
149        pPortName : LPWSTR;
150        pDriverName : LPWSTR;
151        pComment : LPWSTR;
152        pLocation : LPWSTR;
153        pDevMode : LPDEVMODEW;
154        pSepFile : LPWSTR;
155        pPrintProcessor : LPWSTR;
156        pDatatype : LPWSTR;
157        pParameters : LPWSTR;
158        pSecurityDescriptor : PSECURITY_DESCRIPTOR;
159        Attributes : DWORD;
160        Priority : DWORD;
161        DefaultPriority : DWORD;
162        StartTime : DWORD;
163        UntilTime : DWORD;
164        Status : DWORD;
165        cJobs : DWORD;
166        AveragePPM : DWORD;
167     end;
168 
169   PRINTER_INFO_2W = _PRINTER_INFO_2W;
170   PPRINTER_INFO_2W = ^_PRINTER_INFO_2W;
171   LPPRINTER_INFO_2W = ^_PRINTER_INFO_2W;
172 
173   //PRINTER_INFO_2 = PRINTER_INFO_2W;
174   //PPRINTER_INFO_2 = ^PRINTER_INFO_2;
175   //LPPRINTER_INFO_2 = ^PRINTER_INFO_2;
176 
177 
178 
179 
180   _PRINTER_INFO_4A = record
181        pPrinterName : LPSTR;
182        pServerName : LPSTR;
183        Attributes : DWORD;
184     end;
185   PRINTER_INFO_4A = _PRINTER_INFO_4A;
186   PPRINTER_INFO_4A = ^_PRINTER_INFO_4A;
187   LPPRINTER_INFO_4A = ^_PRINTER_INFO_4A;
188 
189   PRINTER_INFO_4 = PRINTER_INFO_4A;
190   PPRINTER_INFO_4 = ^PRINTER_INFO_4;
191   LPPRINTER_INFO_4 = ^PRINTER_INFO_4;
192 
193 
194   _PRINTER_INFO_4W = record
195        pPrinterName : LPWSTR;
196        pServerName : LPWSTR;
197        Attributes : DWORD;
198     end;
199   PRINTER_INFO_4W = _PRINTER_INFO_4W;
200   PPRINTER_INFO_4W = ^_PRINTER_INFO_4W;
201   LPPRINTER_INFO_4W = ^_PRINTER_INFO_4W;
202 
203   //PRINTER_INFO_4 = PRINTER_INFO_4W;
204   //PPRINTER_INFO_4 = ^PRINTER_INFO_4;
205   //LPPRINTER_INFO_4 = ^PRINTER_INFO_4;
206 
207 type
208   { TPrinterDevice }
209 
210   TPrinterDevice = class
211   public
212     Name: string;
213     Driver: string;
214     Device: string;
215     Port: string;
216     DefaultPaperName: string;
217     DefaultPaper: Short;
218     DefaultBin: short;
219     DevModeW: PDeviceModeW;
220     DevModeSize: integer;
221     destructor Destroy; override;
222   end;
223 
DeviceCapabilitiesnull224 function DeviceCapabilities(pDevice, pPort: PChar; fwCapability: word;
225   pOutput: PChar; DevMode: PDeviceMode): integer; stdcall; external LibWinSpool Name 'DeviceCapabilitiesA';
DeviceCapabilitiesWnull226 function DeviceCapabilitiesW(pDevice, pPort: PWideChar; fwCapability: word;
227   pOutput: PWideChar; DevMode: PDeviceModeW): integer; stdcall; external LibWinSpool Name 'DeviceCapabilitiesW';
228 
GetProfileStringnull229 function GetProfileString(lpAppName: PChar; lpKeyName: PChar; lpDefault: PChar;
230   lpReturnedString: PChar; nSize: DWORD): DWORD; stdcall; external 'kernel32' Name 'GetProfileStringA';
231 
PrintDlgnull232 //function PrintDlg(lppd: PtagPD): BOOL; stdcall; external 'comdlg32.dll' Name 'PrintDlgA';
233 function PrintDlgW(lppd: PTagPD): BOOL; stdcall; external 'comdlg32.dll' name 'PrintDlgW';
PageSetupDlgnull234 //function PageSetupDlg(lppd: PtagPSD): BOOL; stdcall; external 'comdlg32.dll' Name 'PageSetupDlgA';
235 function PageSetupDlgW(lppd: PtagPSD): BOOL; stdcall; external 'comdlg32.dll' Name 'PageSetupDlgW';
CommDlgExtendedErrornull236 function CommDlgExtendedError: DWORD; stdcall; external 'comdlg32.dll' Name 'CommDlgExtendedError';
237 
CreateICnull238 //function CreateIC(lpszDriver, lpszDevice, lpszOutput: PChar; lpdvmInit: PDeviceMode): HDC; stdcall; external 'gdi32.dll' Name 'CreateICA';
239 function CreateICW(lpszDriver, lpszDevice, lpszOutput: pwidechar; lpdvmInit: PDeviceModeW): HDC; stdcall; external 'gdi32.dll' Name 'CreateICW';
CreateDCnull240 //function CreateDC(lpszDriver, lpszDevice, lpszOutput: PChar; lpdvmInit: PDeviceMode): HDC; stdcall; external 'gdi32.dll' Name 'CreateDCA';
241 function CreateDCW(lpszDriver, lpszDevice, lpszOutput: pwidechar; lpdvmInit: PDeviceModeW): HDC; stdcall; external 'gdi32.dll' Name 'CreateDCW';
242 
DeleteDCnull243 function DeleteDC(DC: HDC): BOOL; stdcall; external 'gdi32.dll' Name 'DeleteDC';
StartDocnull244 function StartDoc(DC: HDC; Inf: PDocInfo): integer; stdcall; external 'gdi32.dll' Name 'StartDocA';
EndDocnull245 function EndDoc(DC: HDC): integer; stdcall; external 'gdi32.dll' Name 'EndDoc';
StartPagenull246 function StartPage(DC: HDC): integer; stdcall; external 'gdi32.dll' Name 'StartPage';
EndPagenull247 function EndPage(DC: HDC): integer; stdcall; external 'gdi32.dll' Name 'EndPage';
AbortDocnull248 function AbortDoc(DC: HDC): integer; stdcall; external 'gdi32.dll' Name 'AbortDoc';
GlobalFreenull249 function GlobalFree(HMem: HGlobal): HGlobal; stdcall; external 'kernel32.dll' Name 'GlobalFree';
250 
251 // todo: remove when WinSpool.pp will be released with fpc
OpenPrinternull252 //function OpenPrinter(_para1:LPSTR; _para2:PHANDLE; _para3:LPPRINTER_DEFAULTSA):BOOL;stdcall; external LibWinSpool name 'OpenPrinterA';
253 function OpenPrinterW(_para1:pwidechar; _para2:PHANDLE; _para3:LPPRINTER_DEFAULTSW):BOOL;stdcall; external LibWinSpool name 'OpenPrinterW';
ClosePrinternull254 function ClosePrinter(_para1:HANDLE):BOOL;stdcall; external LibWinSpool Name 'ClosePrinter';
DocumentPropertiesnull255 //function DocumentProperties(_para1:HWND; _para2:HANDLE; _para3:LPSTR; _para4:PDEVMODE; _para5:PDEVMODE; _para6:DWORD):LONG;stdcall; external LibWinSpool name 'DocumentPropertiesA';
256 function DocumentPropertiesW(_para1:HWND; _para2:HANDLE; _para3:pwidechar; _para4:PDEVMODEW; _para5:PDEVMODEW; _para6:DWORD):LONG;stdcall; external LibWinSpool name 'DocumentPropertiesW';
EnumPrintersnull257 //function EnumPrinters(_para1:DWORD; _para2:LPSTR; _para3:DWORD; _para4:PBYTE; _para5:DWORD; _para6:PDWORD; _para7:PDWORD):BOOL;stdcall; external LibWinSpool name 'EnumPrintersA';
258 function EnumPrintersW(_para1:DWORD; _para2:Pwidechar; _para3:DWORD; _para4:PBYTE; _para5:DWORD; _para6:PDWORD; _para7:PDWORD):BOOL;stdcall; external LibWinSpool name 'EnumPrintersW';
GetPrinternull259 function GetPrinter(_para1:HANDLE; _para2:DWORD; _para3:PBYTE; _para4:DWORD; _para5:PDWORD):BOOL;stdcall; external LibWinSpool name 'GetPrinterA';
StartDocPrinternull260 function StartDocPrinter(hPrinter: THANDLE; Level: DWORD; DocInfo: PByte): DWORD; stdcall; external LibWinSpool Name 'StartDocPrinterA';
StartPagePrinternull261 function StartPagePrinter(_para1:HANDLE):BOOL;stdcall; external LibWinSpool name 'StartPagePrinter';
EndDocPrinternull262 function EndDocPrinter(hprinter: THANDLE): BOOL; stdcall; external LibWinSpool Name 'EndDocPrinter';
EndPagePrinternull263 function EndPagePrinter(hprinter: THANDLE): BOOL; stdcall; external LibWinSpool Name 'EndPagePrinter';
AbortPrinternull264 function AbortPrinter(hPrinter: THANDLE): BOOL; stdcall; external LibWinSpool Name 'AbortPrinter';
WritePrinternull265 function WritePrinter(hPrinter: THANDLE; Buffer: Pointer; Count: DWord; Written: PDWORD): BOOL; stdcall; external LibWinSpool Name 'WritePrinter';
266 
ResetDCWnull267 function ResetDCW(DC: HDC; const p2: TDeviceModeW):HDC; stdcall; external 'gdi32.dll' name 'ResetDCW';
268 
269 implementation
270 
271 { TPrinterDevice }
272 
273 destructor TPrinterDevice.Destroy;
274 begin
275   ReallocMem(DevModeW, 0);
276   inherited Destroy;
277 end;
278 
279 end.
280 
281