Registriert seit: 2. Mär 2004
5.508 Beiträge
Delphi 5 Professional
|
Infos über Drucker abfragen
29. Sep 2005, 14:32
mit folgenden Prozeduren kann man relativ umfangreiche Informationen über einen Drucker abfragen.
Die Info's werden aus dem PrinterInfo2 Record in einen eigenen Record kopiert, damit man bequem die Daten auslesen kann.
(Der PrinterInfo2 Record enthält Zeiger, die auf Müll zeigen, sobald das Printer-Handle geschlossen wird. Deshalb das Umkopieren)
Delphi-Quellcode:
interface
Uses Windows;
type
TPrinterInfo2Easy = record
ServerName: string;
PrinterName: string;
ShareName: string;
PortName: string;
DriverName: string;
Comment: string;
Location: string;
SepFile: string;
PrintProcessor: string;
Datatype: string;
Parameters: string;
Attributes: DWORD;
Priority: DWORD;
DefaultPriority: DWORD;
StartTime: DWORD;
UntilTime: DWORD;
Status: DWORD;
cJobs: DWORD;
AveragePPM: DWORD;
DevMode : TDeviceModeA;
// SecurityDescriptor : TSecurityDescriptor;
end;
procedure GetPrinterInfo2(hPrinter: HDC; var Info: TPrinterInfo2Easy); overload;
procedure GetPrinterInfo2(const pname: string; var Info: TPrinterInfo2Easy); overload;
implementation
Uses SysUtils;
// local helper procdure
procedure PrinterErrCheck(v: Boolean);
begin
if not v then
raise EPrinter.Create(SysErrorMessage(GetLastError));
end;
procedure GetPrinterInfo2(hPrinter: HDC; var Info: TPrinterInfo2Easy); overload;
var
hGlobal : THandle;
PPI2 : PPrinterInfo2;
dwNeeded : DWORD;
begin
GetPrinter(hPrinter, 2, Nil, 0, @dwNeeded);
//ShowMessage('Needed:'+IntToStr(dwNeeded)+' '+IntToStr(SizeOf(TPrinterInfo2)));
hGlobal := GlobalAlloc(GHND, dwNeeded);
try
PPI2 := PPrinterInfo2(GlobalLock(hGlobal));
PrinterErrCheck( GetPrinter(hPrinter, 2, PPI2, dwNeeded, @dwNeeded));
Info.ServerName := PPI2^.pServerName;
Info.PrinterName := PPI2^.pPrinterName;
Info.ShareName := PPI2^.pShareName;
Info.PortName := PPI2^.pPortName;
Info.DriverName := PPI2^.pDriverName;
Info.Comment := PPI2^.pComment;
Info.Location := PPI2^.pLocation;
Info.SepFile := PPI2^.pSepFile;
Info.PrintProcessor := PPI2^.pPrintProcessor;
Info.Datatype := PPI2^.pDatatype;
Info.Parameters := PPI2^.pParameters;
Info.Attributes := PPI2^.Attributes;
Info.Priority := PPI2^.Priority;
Info.DefaultPriority := PPI2^.DefaultPriority;
Info.StartTime := PPI2^.StartTime;
Info.UntilTime := PPI2^.UntilTime;
Info.Status := PPI2^.Status;
Info.cJobs := PPI2^.cJobs;
Info.AveragePPM := PPI2^.AveragePPM;
// folgender Move führt auf manchen Rechnern zu einer Zugriffverletzung
// Move(PPI2^.pDevMode^, Info.DevMode, sizeof(Info.DevMode));
// Info.SecurityDescriptor
finally
GlobalUnlock(hGlobal);
GlobalFree(hGlobal);
end;
end;
procedure GetPrinterInfo2(const pname: string; var Info: TPrinterInfo2Easy); overload;
var
hPrinter : THandle;
PD : TPrinterDefaults;
begin
FillChar(PD, SizeOf(PD), 0);
PD.DesiredAccess := PRINTER_READ; //
PrinterErrCheck( OpenPrinter(PChar(pname), hPrinter, @PD));
try
GetPrinterInfo2(hPrinter, Info);
finally
ClosePrinter(hPrinter);
end;
end;
[edit=Chakotay1308]Styleguide. Mfg, Chakotay1308[/edit]
Andreas
|
|
Zitat
|