给你一段我开发的软件中的代码。能看懂最好,看不懂我就没有办法了!
if not LoadAPI then FatalError('Unable to load API.');
if not IpHlpSupport then
begin
InstallAndStartDriver;
OpenDriver;
GetHandleTableAndSocketType;
end;
EnableDebugPrivilege;
ZeroMemory(@ResultPorts,SizeOf(ResultPorts));
ProcessInfoTable:=nil;
ProcessInfoTableSize:=$10000;
while ProcessInfoTable=nil do
begin
ProcessInfoTable:=Pointer(LocalAlloc(LMEM_FIXED,ProcessInfoTableSize));
Status:=NtQuerySystemInformation(SystemProcessesAndThreadsInformation,ProcessInfoTable,ProcessInfoTableSize,nil);
if Status=STATUS_INFO_LENGTH_MISMATCH then
begin
LocalFree(Cardinal(ProcessInfoTable));
ProcessInfoTable:=nil;
ProcessInfoTableSize:=2*ProcessInfoTableSize;
end else if Status<>0 then FatalError('Unable to get system process information table.',True);
end;
if IpHlpSupport then
begin
if AllocateAndGetTcpExTableFromStack(@TCPPortsTable,True,GetProcessHeap,0,2)=0 then
begin
TCPRow:=@TCPPortsTable^.Table;
for I:=0 to TCPPortsTable^.NumEntries-1 do
begin
ZeroMemory(@HandleInfo,SizeOf(HandleInfo));
HandleInfo.ProcessId:=TCPRow^.ProcessId;
AddPort(HandleInfo,ntohs(TCPRow^.LocalPort),0);
Inc(TCPRow);
end;
end;
if AllocateAndGetUdpExTableFromStack(@UDPPortsTable,False,GetProcessHeap,0,2)=0 then
begin
UDPRow:=@UDPPortsTable^.Table;
for I:=0 to UDPPortsTable^.NumEntries-1 do
begin
ZeroMemory(@HandleInfo,SizeOf(HandleInfo));
HandleInfo.ProcessId:=UDPRow^.ProcessId;
AddPort(HandleInfo,ntohs(UDPRow^.LocalPort),1);
Inc(UDPRow);
end;
end;
end else
begin
LocPID:=GetCurrentProcessId;
ZeroMemory(@LastObj,SizeOf(LastObj));
LastObj.ProcessId:=$FFFFFFFF;
ProcessHandle:=INVALID_HANDLE_VALUE;
PHandleInfo:=@HandleTable^.Handles;
for I:=0 to HandleTable^.NumberOfEntries-1 do
begin
if PHandleInfo^.ObjectTypeNumber=SockHandleType then
begin
if LastObj.ProcessId<>PHandleInfo^.ProcessId then
begin
if ProcessHandle<>INVALID_HANDLE_VALUE then CloseHandle(ProcessHandle);
ProcessHandle:=OpenProcess(PROCESS_DUP_HANDLE,False,PHandleInfo^.ProcessId);
LastObj:=PHandleInfo^;
if ProcessHandle=0 then ProcessHandle:=INVALID_HANDLE_VALUE;
end;
if (ProcessHandle<>INVALID_HANDLE_VALUE) and (PHandleInfo^.ProcessId<>LocPID) then
begin
if DuplicateHandle(ProcessHandle,PHandleInfo^.Handle,GetCurrentProcess,@DupHandle,0,False,DUPLICATE_SAME_ACCESS) then
begin
ObjInfo:=GetObjInfo(PHandleInfo^,Str1);
Proto:=0;
if (ObjInfo.Flags=1) or (ObjInfo.Flags=2) then
if ObjInfo.Ptr1=SockObjInfoTCP.Ptr1 then Proto:=1
else if ObjInfo.Ptr1=SockObjInfoUDP.Ptr1 then Proto:=2;
if Proto>0 then
begin
ZeroMemory(@TdiConnIn,SizeOf(TdiConnIn));
ZeroMemory(@TdiConnOut,SizeOf(TdiConnOut));
if ObjInfo.Flags=2 then
begin
TdiConnIn.RemoteAddressLength:=4;
if DeviceIoControl(DupHandle,$00210012,@TdiConnIn,SizeOf(TdiConnIn),@TdiConnOut,SizeOf(TdiConnOut)-$16,BytesRet,nil) then
begin
TdiConnIn.RemoteAddressLength:=3;
if DeviceIoControl(DupHandle,$00210012,@TdiConnIn,SizeOf(TdiConnIn),@TdiConnOut,SizeOf(TdiConnOut),BytesRet,nil) then
begin
Port:=ntohs(TdiConnOut.ReceivedTsdus);
AddPort(LastObj,Port,Proto-1);
end;
end;
end else
begin
TdiConnIn.RemoteAddressLength:=3;
if DeviceIoControl(DupHandle,$00210012,@TdiConnIn,SizeOf(TdiConnIn),@TdiConnOut,SizeOf(TdiConnOut),BytesRet,nil) then
begin
Port:=ntohs(TdiConnOut.ReceivedTsdus);
AddPort(LastObj,Port,Proto-1);
end;
end;
end;
CloseHandle(DupHandle);
end;
end;
end;
Inc(PHandleInfo);
end;
CloseHandle(ProcessHandle);
LocalFree(Cardinal(HandleTable));
CloseHandle(DrvHandle);
UninstallDriver;
DeleteFile(DriverBin);
end;
for I:=0 to 65535 do
with ResultPorts[0,I] do
if Active then
begin
PObj:=Objects;
for J:=0 to Count-1 do
begin
new(TempNetRecord);
TempNetRecord.Porto:='TCP';
TempNetRecord.PID:=PObj^.ProcessId;
TempNetRecord.Port:=I;
TempNetRecord.ProcessPath:= GetProcessPathByPID(PObj^.ProcessId);
TempNetRecord.ProcessName:=GetProcessNameByPID(PObj^.ProcessId);
if Length(TempNetRecord.ProcessName)=0 then
begin
TempNetRecord.ProcessName:='System';
PObj^.ProcessId:=Name2PID(TempNetRecord.ProcessName);
end;
List.Add(TempNetRecord);
Inc(PObj);
end;
if Objects<>nil then LocalFree(Cardinal(Objects));
end;
for I:=0 to 65535 do
with ResultPorts[1,I] do
if Active then
begin
PObj:=Objects;
for J:=0 to Count-1 do
begin
new(TempNetRecord);
TempNetRecord.Porto:='UDP';
TempNetRecord.PID:=PObj^.ProcessId;
TempNetRecord.Port:=I;
TempNetRecord.ProcessPath:=GetProcessPathByPID(PObj^.ProcessId);
TempNetRecord.ProcessName:=GetProcessNameByPID(PObj^.ProcessId);
if Length(TempNetRecord.ProcessName)=0 then
begin
TempNetRecord.ProcessName:='System';
PObj^.ProcessId:=Name2PID(TempNetRecord.ProcessName);
end;
List.Add(TempNetRecord);
Inc(PObj);
end;
if Objects<>nil then LocalFree(Cardinal(Objects));
end;
LocalFree(Cardinal(ProcessInfoTable));