Registriert seit: 23. Jul 2009
23 Beiträge
|
Re: How to set use printer offline
22. Nov 2009, 16:58
Delphi-Quellcode:
function TestPrinterOffLine:Boolean;
var
HPrinter : THandle;
InfoSize,
BytesNeeded: Cardinal;
PI2: PPrinterInfo2;
PrinterDefaults: TPrinterDefaults;
SelectedPrinter: string;
begin
SelectedPrinter:=Printer.Printers[Printer.PrinterIndex];
with PrinterDefaults do begin
DesiredAccess := PRINTER_ACCESS_USE;
pDatatype := nil;
pDevMode := nil;
end;
if OpenPrinter(PChar(SelectedPrinter), HPrinter, @PrinterDefaults) then
try
if not GetPrinter(HPrinter, 2, nil, 0, @BytesNeeded) then begin
PI2 := AllocMem(BytesNeeded);
try
InfoSize := SizeOf(TPrinterInfo2);
if GetPrinter(HPrinter, 2, PI2, BytesNeeded, @BytesNeeded) then
Result:=PI2^.Attributes and PRINTER_ATTRIBUTE_WORK_OFFLINE = PRINTER_ATTRIBUTE_WORK_OFFLINE
else
Result:=False;
finally
FreeMem(PI2, BytesNeeded);
end;
end;
finally
ClosePrinter(HPrinter);
end;
end;
AND
Delphi-Quellcode:
procedure WorkPrinterOnLine(Path:string);
var
F :TextFile;
ScriptName: string;
begin
ScriptName:=Path+'temp.vbs';
AssignFile(F,ScriptName);
Rewrite(F);
WriteLn(F,'PrinterName = "'+ Printer.Printers[Printer.PrinterIndex]+'"');
WriteLn(F,'VerbName = "&Use Printer Online"');
WriteLn(F,'Dim sa');
WriteLn(F,'Set sa = CreateObject("Shell.Application")');
WriteLn(F,'Set printers = sa.NameSpace(4).Items()');
WriteLn(F,'''WScript.Echo TypeName(printers)');
WriteLn(F,'For each printer in printers');
WriteLn(F,'''WScript.Echo printer.Name');
WriteLn(F,'if printer.Name = PrinterName Then');
WriteLn(F,'Dim verbs: Set verbs = printer.Verbs');
WriteLn(F,'For Each verb in verbs');
WriteLn(F,'''WScript.Echo verb.Name');
WriteLn(F,'if verb.Name = VerbName Then');
// WriteLn(F,'WScript.Echo PrinterName & " was offline"');
WriteLn(F,'verb.Doit()');
WriteLn(F,'End If');
WriteLn(F,'Next');
WriteLn(F,'End If');
WriteLn(F,'Next');
CloseFile(F);
ShellExecute(0, PChar('Open'),PChar(ScriptName),nil, nil, SW_HIDE);
if FileExists(ScriptName) then
DeleteFile(ScriptName);
end;
Usage:
Delphi-Quellcode:
if TestPrinterOffLine then
WorkPrinterOnLine(AppPath);
|
|
Zitat
|