unit main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls,Winapi.WinSpool,Generics.collections,
Vcl.Menus;
type
// Ñòàíäàðòíàÿ ñòðóêòóðà äëÿ ïîëó÷åíèÿ ÒÑÐ ñòàòèñòèêè
PTMibTCPRow = ^TMibTCPRow;
TMibTCPRow = packed record
dwState: DWORD;
dwLocalAddr: DWORD;
dwLocalPort: DWORD;
dwRemoteAddr: DWORD;
dwRemotePort: DWORD;
end;
// Â äàííóþ ñòðóêòóðó áóäåò ïåðåäàâàòüñÿ ðåçóëüòàò GetTcpTable
PTMibTCPTable = ^TMibTCPTable;
TMibTCPTable = packed record
dwNumEntries: DWORD;
Table: array[0..0] of TMibTCPRow;
end;
TProcessEntry32 = packed record
dwSize: DWORD;
cntUsage: DWORD;
th32ProcessID: DWORD;
th32DefaultHeapID: DWORD;
th32ModuleID: DWORD;
cntThreads: DWORD;
th32ParentProcessID: DWORD;
pcPriClassBase: Longint;
dwFlags: DWORD;
szExeFile: array [0..MAX_PATH - 1] of WideChar;
end;
JOB_INFO_1_ARRAY = array [0..0] of JOB_INFO_1;
TForm1 = class(TForm)
ComboBox1: TComboBox;
Button1: TButton;
ListView1: TListView;
PopupMenu1: TPopupMenu;
ComboBox2: TComboBox;
procedure FormCreate(Sender: TObject);
procedure ComboBox1Select(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function GetTcpTable(pTCPTable: PTMibTCPTable; var pDWSize: DWORD;
bOrder: BOOL): DWORD; stdcall; external 'IPHLPAPI.DLL';
var
Form1: TForm1;
JobList: TList<JOB_INFO_1>;
implementation
uses Printers;
{$R *.dfm}
function GetStatus(Job: JOB_INFO_1): AnsiString;
begin
case Job.Status of
JOB_STATUS_PAUSED: result := 'JOB_STATUS_PAUSED';
JOB_STATUS_ERROR: result := 'JOB_STATUS_ERROR';
JOB_STATUS_DELETING: result := 'JOB_STATUS_DELETING';
JOB_STATUS_SPOOLING: result := 'JOB_STATUS_SPOOLING';
JOB_STATUS_PRINTING: result := 'JOB_STATUS_PRINTING';
JOB_STATUS_OFFLINE: result := 'JOB_STATUS_OFFLINE';
JOB_STATUS_PAPEROUT: result := 'JOB_STATUS_PAPEROUT';
JOB_STATUS_PRINTED: result := 'JOB_STATUS_PRINTED';
JOB_STATUS_DELETED: result := 'JOB_STATUS_DELETED';
JOB_STATUS_BLOCKED_DEVQ: result :=
'JOB_STATUS_BLOCKED_DEVQ';
JOB_STATUS_USER_INTERVENTION: result :=
'JOB_STATUS_USER_INTERVENTION';
JOB_STATUS_RESTART: result := 'JOB_STATUS_RESTART';
JOB_POSITION_UNSPECIFIED: result :=
'JOB_POSITION_UNSPECIFIED';
else
result := 'Unknown status...';
end;
end;
procedure GetPortStats();
var
Size,i: DWORD;
TCPTable: PTMibTCPTable;
begin
GetMem(TCPTable, SizeOf(TMibTCPTable));
try
// Ïîêàçûâàåì ÷òî ïàìÿòè ó íàñ íå âûäåëåíî
Size := 0;
// Âûïîëíÿåì ôóíêöèþ è ïîñëå ýòîãî ïåðåìåííàÿ Size
// áóäåò ñîäåðæàòü êîë-âî íåîáõîäèìîé ïàìÿòè
if GetTcpTable(TCPTable, Size, True) <> ERROR_INSUFFICIENT_BUFFER then Exit;
finally
// îñâîáîæäàåì ïàìÿòü çàíÿòóþ ïîä îäèí ýëåìåíò
FreeMem(TCPTable);
end;
// Òåïåðü âûäåëÿåì óæå òðåáóåìîå êîë-âî ïàìÿòè
GetMem(TCPTable, Size);
try
// Âûïîëíÿåì ôóíêöèþ
if GetTcpTable(TCPTable, Size, True) = NO_ERROR then
begin
// è íàñèíàåì âûâîäèòü äàííûå ïî ÒÑÐ
for I := 0 to TCPTable^.dwNumEntries - 1 do
// Memo1.Lines.Add(Format('%15s: | %5d %s', [inet_ntoa(in_addr(TCPTable^.Table[i].dwLocalAddr)),
// htons(TCPTable^.Table[i].dwLocalPort), PortStateToStr(TCPTable^.Table[i].dwState)]));
end;
finally
// Íå çàáûâàåì îñâîáîäèòü ïàìÿòü
FreeMem(TCPTable);
end;
end;
procedure JobToListView(Job: JOB_INFO_1);
var
lv: TListItem;
begin
Form1.ListView1.items.BeginUpdate;
lv:=Form1.ListView1.Items.Add;
lv.Caption:=IntToStr(Job.JobId);
lv.SubItems.Add(Job.pPrinterName);
lv.SubItems.Add(Job.pMachineName);
lv.SubItems.Add(Job.pUserName);
lv.SubItems.Add(Job.pDocument);
lv.SubItems.Add(Job.pDatatype);
lv.SubItems.Add(GetStatus(Job));
lv.SubItems.Add(IntToStr(Job.TotalPages));
lv.SubItems.Add(IntToStr(Job.PagesPrinted));
lv.SubItems.Add(DateTimeToStr(SystemTimeToDateTime(Job.Submitted)));
Form1.ListView1.Items.EndUpdate;
end;
function ListJobOfPrinter(PrinterName: AnsiString):boolean;
var
hPrinter: THandle;
dwNeeded, dwReturned: dword;
i: integer;
PJobInfo: ^JOB_INFO_1_ARRAY;
begin
if not OpenPrinterA(PAnsiChar(PrinterName), hPrinter,nil) then
begin
result:=false;
exit;
end;
if not EnumJobs( hPrinter, 0, $FFFFFFFF, 1, nil, 0, dwNeeded, dwReturned ) then
begin
if( GetLastError() <> ERROR_INSUFFICIENT_BUFFER ) then
begin
ClosePrinter(hPrinter);
result:=false;
exit;
end;
end;
try
GetMem(PJobInfo,sizeof(JOB_INFO_1)*dwNeeded);
if not EnumJobs( hPrinter, 0, $FFFFFFFF, 1, LPBYTE(pJobInfo),
dwNeeded, dwNeeded, dwReturned ) then
begin
ClosePrinter(hPrinter);
result:=false;
end;
for I := 0 to dwReturned -1 do
JobList.Add(PJobInfo[i]);
finally
ClosePrinter(hPrinter);
freemem(PJobInfo);
end;
end;
procedure TForm1.ComboBox1Select(Sender: TObject);
var
i: integer;
begin
if ListJobOfPrinter(ComboBox1.Items[ComboBox1.ItemIndex]) then
for i := 0 to JobList.Count - 1 do
JobToListView(JobList[i]);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Items.Assign(Printer.Printers);
Combobox1.ItemIndex:=0;
JobList:= TList<JOB_INFO_1>.Create;
end;
end.