zoukankan
html css js c++ java
用Delphi实现Ping类
网络上的一个实现PING功能的类,修改了部分代码。
=======================================
unit ping; interface uses Windows, SysUtils, Classes, Controls, Winsock, StdCtrls; function pingip(ip:string):string; type PIPOptionInformation = ^TIPOptionInformation; TIPOptionInformation = packed record TTL: Byte; TOS: Byte; Flags: Byte; OptionsSize: Byte; OptionsData: PChar; end; PIcmpEchoReply = ^TIcmpEchoReply; TIcmpEchoReply = packed record Address: DWORD; Status: DWORD; RTT: DWORD; DataSize: Word; Reserved: Word; Data: Pointer; Options: TIPOptionInformation; end; TIcmpCreateFile = function: THandle; stdcall; TIcmpCloseHandle = function(IcmpHandle: THandle): Boolean; stdcall; TIcmpSendEcho = function( IcmpHandle:THandle; DestinationAddress: DWORD; RequestData: Pointer; RequestSize: Word; RequestOptions: PIPOptionInformation; ReplyBuffer: Pointer; ReplySize: DWord; Timeout: DWord ): DWord; stdcall; Tping =class(Tobject) private { Private declarations } hICMP: THANDLE; IcmpCreateFile : TIcmpCreateFile; IcmpCloseHandle: TIcmpCloseHandle; IcmpSendEcho: TIcmpSendEcho; public procedure pinghost(ip:string;var info:string); constructor create; destructor destroy;override; { Public declarations } end; var hICMPdll: HMODULE; implementation constructor Tping.create; begin inherited create; hICMPdll := LoadLibrary('icmp.dll'); @ICMPCreateFile := GetProcAddress(hICMPdll, 'IcmpCreateFile'); @IcmpCloseHandle := GetProcAddress(hICMPdll,'IcmpCloseHandle'); @IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho'); hICMP := IcmpCreateFile; end; destructor Tping.destroy; begin FreeLibrary(hIcmpDll); inherited destroy; end; procedure Tping.pinghost(ip:string;var info:string); var // IP Options for packet to send IPOpt:TIPOptionInformation; FIPAddress:DWORD; pReqData,pRevData:PChar; // ICMP Echo reply buffer pIPE:PIcmpEchoReply; FSize: DWORD; MyString:string; FTimeOut:DWORD; BufferSize:DWORD; begin if ip<>'' then begin FIPAddress := inet_addr(PChar(ip)); FSize := 40; BufferSize := SizeOf(TICMPEchoReply) + FSize; GetMem(pRevData,FSize); GetMem(pIPE,BufferSize); FillChar(pIPE^, SizeOf(pIPE^), 0); pIPE^.Data := pRevData; MyString := 'Test Net - Sos Admin'; pReqData := PChar(MyString); FillChar(IPOpt, Sizeof(IPOpt), 0); IPOpt.TTL := 64; FTimeOut := 4000; try IcmpSendEcho(hICMP, FIPAddress, pReqData, Length(MyString),@IPOpt, pIPE, BufferSize, FTimeOut); if pReqData^ = pIPE^.Options.OptionsData^ then info:=ip+ ' ' + IntToStr(pIPE^.DataSize) + ' ' +IntToStr(pIPE^.RTT); except info:='Can not find host!'; FreeMem(pRevData); FreeMem(pIPE); Exit; end; FreeMem(pRevData); FreeMem(pIPE); end; end; function pingip(ip:string):string; var str:string; ping:Tping; begin ping:=Tping.create ;//一定要初试化哦 ping.pinghost('127.0.0.1',str); result:=str; ping.destroy ; end; end.
谢祥选【小宇飞刀(xieyunc)】
查看全文
相关阅读:
使用 Markdown Flow 画流程图
两串锂电池的电池匹配
笔记: CC2540 和 CC2541 的区别
Elasticsearch 5.x 关于term query和match query的认识
es 批量导入文件
mac 下搭建Elasticsearch 5.4.3分布式集群
Elastic Search 5.4.3 java api 入门
solr java demo 基础入门
创建索引并进行查询
RabbitMq 之简单队列
原文地址:https://www.cnblogs.com/xieyunc/p/2793657.html
最新文章
ABAP DMEO 通过工单号读取内部对象号
ABAP DEMO ALV-监听数据修改
ABAP DEMO ALVtree显示BOM层级
ABAP DEMO 下拉框
增强篇4 CO01生产订单屏幕增强
增强篇3 SAP表字段增强
致菜鸟、新手、初学者以及我
Win10手记-为应用集成日志工具Logger
.NET手记-Autofac进阶(属性和方法注入 Property and Method Injection)
.NET手记-Autofac进阶(传递注册参数 Passing Parameters to Register)
热门文章
.NET手记-Autofac进阶(注册的概念 Registering Concepts)
Using SQLite database in your Windows 10 apps
Win10手记-为应用集成SQLite(二)
Win10手记-IIS部署网站问题解决
Win10手记-为应用集成SQLite(一)
笔记:电阻的选择之耐压
可控硅的控制
从 FastAdmin 看国内开源现状
最近收集的几个元件信息、磁隔离、电源隔离模块
KiCad EDA 画圆弧
Copyright © 2011-2022 走看看