1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| #include <vcl.h>
#pragma hdrstop
#include "PingTest.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
// les entetes ICMP.DLL
extern "C"
{
#include "ipexport.h""
#include "icmpapi.h"
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
// An array of error message strings.
String ErrorStrings[] = {
"Error Base",
"Buffer too small.",
"Destination net unreachable.",
"Destination host unreachable.",
"Destination protocol unreachable.",
"Destination port unreachable.",
"Out of resources.",
"Bad option.",
"Hardware error.",
"Packet too large.",
"Request timed out.",
"Bad request.",
"Bad route.",
"TTL expired in transit.",
"TTL expired REASSEM.",
"Param problem.",
"Source quench.",
"Option too large.",
"Bad destination.",
"Address deleted.",
"Spec MNU change.",
"MTU change.",
"Unload"
};
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// Obtain an ICMP handle.
HANDLE hIcmp = IcmpCreateFile();
if (!hIcmp) {
ShowMessage("Error getting ICMP handle");
return;
}
// Clear the Memo of any previous text.
Memo1->Lines->Clear();
Memo1->Lines->Add("Pinging " + Edit1->Text);
// Parse the IP address in the Edit.
String S = Edit1->Text;
int addr1 = Trim(S.SubString(1, 3)).ToInt();
int addr2 = Trim(S.SubString(5, 3)).ToInt();
int addr3 = Trim(S.SubString(9, 3)).ToInt();
int addr4 = Trim(S.SubString(13, 3)).ToInt();
// Make an int out of the IP address.
int addr = MAKELONG(
MAKEWORD(addr1, addr2),
MAKEWORD(addr3, addr4));
// Allocate a buffer for the reply info.
int size = sizeof(icmp_echo_reply) + 8;
char* buff = new char[size];
// Show the user we'll be busy for a while.
Screen->Cursor = crHourGlass;
// Send the echo request three times to
// emulate what the PING program does.
for (int i=0;i<3;i++) {
Application->ProcessMessages();
// Call IcmpSendEcho().
DWORD res = IcmpSendEcho(hIcmp,
addr, 0, 0, 0, buff, size, 1500);
if (!res) {
Memo1->Lines->Add("Request timed out.");
continue;
}
// Prepare to report the status.
icmp_echo_reply reply;
memcpy(&reply, buff, sizeof(reply));
// If the status is non-zero then show the
// corresponding error message from the
// ErrorStrings array to the user.
if (reply.Status > 0)
Memo1->Lines->Add(
ErrorStrings[reply.Status - 11000]);
else {
// Build a string to report the results.
String rtt = reply.RoundTripTime;
String ttl = reply.Options.Ttl;
String S = "Reply from " + Edit1->Text +
" time=" + rtt +"ms TTL=" + ttl + "ms";
// Add it to the memo.
Memo1->Lines->Add(S);
}
// Pause a second and then loop.
Sleep(1000);
}
// Close the ICMP handle.
IcmpCloseHandle(hIcmp);
// Restore the cursor.
Screen->Cursor = crArrow;
} |
Partager