谁能帮忙举个例子,用DELPHI写个简单例子,调用PING命令并且取回PING结果显示MSG BOX里,要通过管道技术取

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了谁能帮忙举个例子,用DELPHI写个简单例子,调用PING命令并且取回PING结果显示MSG BOX里,要通过管道技术取相关的知识,希望对你有一定的参考价值。

不要生成文本文件再读取文本文件的。

参考技术A 转自大富翁

函数
procedure pinghost(ip:string;var info:string);
ip:目标IP地址;
info:ping了以后产生的信息(1)或(2);
(1)成功信息
ip 发送测试的字符数 返回时间
(2)出错信息
Can not find host!

使用
uses ping;

procedure TForm1.Button1Click(Sender: TObject);
var
str:string;
ping:Tping;
begin
ping:=Tping.create ;//一定要初试化哦
ping.pinghost('127.0.0.1',str);
memo1.Lines.Add(str);
ping.destroy ;
end;

[ping.pas]

(*作者:e梦缘*)

unit ping;

interface

uses

Windows, SysUtils, Classes, Controls, Winsock,
StdCtrls;

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;
参考技术B RS-232串行通信控制

网络上有电子档

找找看!

我有,可是有26M,超过了 gmail 附件上限!

传不了,如果 要的话,我可以放机器上供你 下载!

祝你顺利

-----
给你 delphi 例程了
Good luck
参考技术C // 自己写的,绝对可用
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TShowMethod = procedure(str: string) of object;
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
Private declarations
public
Public declarations
procedure ShowMethod(str: string);
end;

function RunCommand(const cmd: string; Show: TShowMethod = nil): string;
var
Form1: TForm1;

implementation

$R *.dfm

procedure TForm1.ShowMethod(str: string);
begin
Memo1.Lines.Add(str);
end;

function RunCommand(const cmd: string; Show: TShowMethod = nil): string;
var
hReadPipe,hWritePipe:THandle;
si:STARTUPINFO;
lsa:SECURITY_ATTRIBUTES;
pi:PROCESS_INFORMATION;
cchReadBuffer:DWORD;
pOutStr, pCMD:PChar;
res, strCMD:string;
begin
strcmd := 'cmd.exe /k ' + cmd;
pOutStr := AllocMem(5000);
lsa.nLength := SizeOf(SECURITY_ATTRIBUTES);
lsa.lpSecurityDescriptor := nil;
lsa.bInheritHandle := True;
if not CreatePipe(hReadPipe, hWritePipe, @lsa, 0) then Exit;
FillChar(si, SizeOf(STARTUPINFO), 0);
si.cb:=sizeof(STARTUPINFO);
si.dwFlags:=(STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);
si.wShowWindow:=SW_HIDE;
si.hStdOutput:=hWritePipe;

if not CreateProcess(nil, PChar(strCMD), nil, nil, true, 0, nil, nil, si, pi) then Exit;
while(true) do
begin
if not PeekNamedPipe(hReadPipe, pOutStr, 1, @cchReadBuffer, nil, nil) then break;
if cchReadBuffer <> 0 then
begin
if not ReadFile(hReadPipe, pOutStr^, 4096, cchReadBuffer, nil) then break;
pOutStr[cchReadbuffer]:=chr(0);
if @Show <> nil then Show(pOutStr);
res := res + pOutStr;
end else if(WaitForSingleObject(pi.hProcess ,0) = WAIT_OBJECT_0) then break;
Sleep(10);
Application.ProcessMessages;
end;
pOutStr[cchReadBuffer]:=chr(0);

CloseHandle(hReadPipe);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(hWritePipe);
FreeMem(pOutStr);
Result := res;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
res: string;
begin
res := RunCommand('ping 192.168.0.1', ShowMethod);
ShowMessage(res);
end;

end.追问

RunCommand('ping 192.168.0.1', ShowMethod);
如果这句话换成别的命令,也可以吗

追答

换成什么都行

本回答被提问者采纳

谁能用C语言写个最简单socket通信服务端和客户端示例

参考技术A 网络的Socket数据传输是一种特殊的I/O,Socket也是一种文件描述符。Socket也具有一个类似于打开文件的函数调用Socket(),该函数返回一个整型的Socket描述符,随后的连接建立、数据传输等操作都是通过该Socket实现的。
下面用Socket实现一个windows下的c语言socket通信例子,这里我们客户端传递一个字符串,服务器端进行接收。
【服务器端】
#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
#include <winsock2.h>
#define SERVER_PORT 5208 //侦听端口
void main()本回答被提问者和网友采纳

以上是关于谁能帮忙举个例子,用DELPHI写个简单例子,调用PING命令并且取回PING结果显示MSG BOX里,要通过管道技术取的主要内容,如果未能解决你的问题,请参考以下文章

delphi indy10 无法接收中文

帮忙看下delphi程序 简单按照书上例子写了程序,运行的时候出现错误

Java如何实现动态的组合, Thinking in Java 这本书中 说过 动态的组合就是聚合, 谁能给我举个简单例子?

delphi读取文本问题

delphi中如何模拟最简单的键盘输入?

delphi的与或非