Програмне забезпеченння для адміністрування комп'ютерного клубу, страница 14

end;

function DG_InitWSA;

begin

if WSAStartup($101,DG_WSData)=0 then Result:=true

else Result:=false

end;

procedure DG_FreeWSA;

begin

WSACleanup

end;

function DG_CreateSocket;

begin

result:=true;

Socket:=WinSock.socket(PF_INET,SOCK_DGRAM,IPPROTO_IP);

if Socket=INVALID_SOCKET then result:=false;

end;

procedure DG_CloseSocket;

begin

closesocket(Socket)

end;

function DG_BindSocket;

var SockAddrIn:TSockAddrIn;

begin

result:=true;

SockAddrIn:=PRI_FillAddrIn('','',Port,false);

if bind(Socket,SockAddrIn,SizeOf(SockAddrIn))=SOCKET_ERROR then

result:=false

end;

function DG_SendString;

var addrto:TSockAddrIn;

begin

addrto:=PRI_FillAddrIn(compname,'',Port,False);

if sendto(Socket,Mess,length(Mess)+1,0,addrto,SizeOf(addrto))=SOCKET_ERROR then result:=false

else result:=true

end;

function DG_ReceiveString;

var fromaddr:TSockAddrIn;

fromlen:integer;

len:integer;

HostEnt:PHostEnt;

begin

ioctlsocket(Socket,FIONREAD,len);

if len=0 then begin

result:=false;

exit

end;

result:=true;

SetLength(Mess,len);

fromlen:=SizeOf(fromaddr);

if (recvfrom(Socket,Mess,SizeOf(Mess),0,fromaddr,fromlen)=SOCKET_ERROR)and(WSAGetLastError<>WSAEMSGSIZE) then result:=false

else begin

Mess:=Mess;

HostEnt:=gethostbyaddr(@fromaddr.sin_addr.s_addr,4,PF_INET);

if HostEnt<>nil then From:=HostEnt.h_name

else From:=''

end

end;

function DG_GetFreePortFrom;

var Sock:TSocket;

NextPort:integer;

found:boolean;

begin

result:=DG_CreateSocket(Sock);

DG_CloseSocket(Sock);

if result=false then exit;

found:=false;


 




NextPort:=Port;

repeat

DG_CreateSocket(Sock);

if DG_BindSocket(Sock,NextPort) then found:=true;

DG_CloseSocket(Sock);

inc(NextPort)

until (NextPort=0)or(found);

dec(NextPort);

result:=found;

if found then Port:=NextPort

end;

end.

unit SnapshotUnit;

interface

uses Windows, TlHelp32, Classes, SysUtils;

const

MAXSAVEDPROCESSES=1200;

var

//Saved processes

aprocesses:array [1..MAXSAVEDPROCESSES] of DWORD;

numaprocesses:word;

//Main utilities

function CreateSnapshot:THandle;

procedure CloseSnapshot(snp:THandle);

procedure CompleteSavedProcesses;

function ItsSavedProcess(ProcessId:DWORD):boolean;

//Misc utilities

function GetFirstProcess(snp:THandle;var processinfo:processentry32):boolean;

function GetNextProcess(snp:THandle;var processinfo:processentry32):boolean;

function GetFirstThread(snp:THandle;var threadinfo:threadentry32):boolean;

function GetNextThread(snp:THandle;var threadinfo:threadentry32):boolean;

procedure GetThreadListForProcess(snp:THandle;ProcessId:DWORD;stList:tstringlist);

function GetProcessInfoById(snp:THandle;ProcessId:DWORD;var processinfo:processentry32):boolean;

function GetProcessInfoByNumber(snp:THandle;num:cardinal;var processinfo:processentry32):boolean;

function GetExeFilename(procentry:processentry32):string;

function FineTime:boolean;

implementation

uses Common;

//Gets exe filename if it's specified

function GetExeFilename;

begin

if procentry.dwSize<sizeof(processentry32) then result:=''

else result:=procentry.szExeFile

end;

//Creates a process and thread snapshot

function CreateSnapshot;

begin

result:=createtoolhelp32snapshot(TH32CS_SNAPPROCESS or TH32CS_SNAPTHREAD,0)

end;

//Closes a handle to a snapshot

procedure CloseSnapshot;

begin

closehandle(snp)

end;

//Returns first process info in snapshot

function GetFirstProcess;

begin

processinfo.dwSize:=sizeof(processinfo);

result:=process32first(snp,processinfo)

end;

//Returns next process info in snapshot

function GetNextProcess;

begin

 


processinfo.dwSize:=sizeof(processinfo);

result:=process32next(snp,processinfo)

end;

//Returns first thread info in snapshot

function GetFirstThread;

begin

threadinfo.dwSize:=sizeof(threadinfo);

result:=thread32first(snp,threadinfo)

end;

//Returns next thread info in snapshot

function GetNextThread;

begin