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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#include <vcl.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <winsock.h>
#include <iphlpapi.h>
#include <icmpapi.h>
#include <vector>
#include <WinBase.h>
#include <Windows.h>
using namespace std;
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma hdrstop
#include "U1_Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
char *test;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
unsigned long WINAPI MyThread(LPVOID ip)
{
string* ip_ptr = static_cast<string*>(ip);
string bis = *ip_ptr;
unsigned int taille = bis.length();
test = new char(taille);
strcpy(test, bis.c_str());
PosteDeTravail Poste(test);
ExitThread(0);
return 0;
}
int TForm1::Ping(AnsiString ansAdressIP)
{
HANDLE hIcmpFile;
char SendData[32] = "Data Buffer";
DWORD dwRetVal = 0 ;
LPVOID ReplyBuffer = NULL;
DWORD ReplySize = 0;
unsigned long ipadresse;
ipadresse = inet_addr(ansAdressIP.c_str());
hIcmpFile = IcmpCreateFile();
if (hIcmpFile == INVALID_HANDLE_VALUE)
{
//Form1->Memo1->Lines->Add("Impossible d'ouvrir le HANDLE");
}
ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData);
ReplyBuffer = (VOID*) malloc(ReplySize);
if (ReplyBuffer == NULL)
{
//Form1->Memo1->Lines->Add("Impossible d'allouer la mémoire");
}
dwRetVal = IcmpSendEcho(hIcmpFile,ipadresse,SendData,sizeof(SendData),NULL,ReplyBuffer,ReplySize,1);
if (dwRetVal != 0)
{
PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
struct in_addr ReplyAddr;
ReplyAddr.S_un.S_addr = pEchoReply->Address;
return 1;
}
else return 0;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString ansIp11,ansIp12,ansIp13,ansIp14,ansIp21,ansIp22,ansIp23,ansIp24;
int IpDeb,IpFin;
std::vector<string> strIp;
AnsiString ansAddIp;
HANDLE th = 0;
bool bflag = true;
SECURITY_ATTRIBUTES attr;
int j=0,i =0;
ansIp11 = Edit1->Text;
ansIp12 = Edit2->Text;
ansIp13 = Edit3->Text;
ansIp14 = Edit4->Text;
ansIp21 = Edit5->Text;
ansIp22 = Edit6->Text;
ansIp23 = Edit7->Text;
ansIp24 = Edit8->Text;
if(bflag==true)
{
IpDeb = StrToInt(ansIp14.c_str());
IpFin = StrToInt(ansIp24.c_str());
attr.nLength = sizeof(SECURITY_ATTRIBUTES);
attr.lpSecurityDescriptor = NULL;
attr.bInheritHandle = 0;
for(i=IpDeb;i<=IpFin;i++)
{
ansIp14 =i;
ansAddIp=("");
ansAddIp = ansIp11+"."+ansIp12+"."+ansIp13+"."+ansIp14;
if (Ping(ansAddIp)==1)
{
strIp.push_back(ansAddIp.c_str());
}
}
int const taille(strIp.size());
for(i=0;i<=taille;i++)
{
th = CreateThread(NULL,0,&MyThread,&strIp[i], 0,NULL);
if(th ==NULL){Form1->Memo1->Lines->Add("Impossible de creer un thread");}
}
WaitForSingleObject(th,5);
Memo1->Lines->Add("Fin de scrutation, nombre de poste trouvé : ");
Memo1->Lines->Add(taille);
}
}
//--------------------------------------------------------------------------- |