Cactuc
Newbie | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору // --Main Application File pingin.cpp //--------------------------CUT-------------------------------------------- #include <vcl.h> #pragma hdrstop //--------------------------------------------------------------------------- USEFORM("Pinging.cpp", PingingForm); //--------------------------------------------------------------------------- WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { try { Application->Initialize(); Application->CreateForm(__classid(TPingingForm), &PingingForm); Application->Run(); } catch (Exception &exception) { Application->ShowException(&exception); } catch (...) { try { throw Exception(""); } catch (Exception &exception) { Application->ShowException(&exception); } } return 0; } //------------------------END CUT-------------------------------------------- // --Main Form Application File pinging.cpp //-----------------------------CUT------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Pinging.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TPingingForm *PingingForm; //--------------------------------------------------------------------------- __fastcall TPingingForm::TPingingForm(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TPingingForm::OnPingingFormCreate(TObject *Sender) { WSAData wsaData; if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) { ::MessageBox(NULL, "Unable to start networking.", "Error", MB_OK|MB_ICONWARNING); return; } m_hIcmp = LoadLibrary("ICMP.DLL"); if (m_hIcmp == 0) { ::MessageBox(NULL, "Unable to locate ICMP.DLL!", "Error", MB_OK|MB_ICONWARNING); return; } pIcmpCreateFile = (pfnHV)GetProcAddress(m_hIcmp, "IcmpCreateFile"); pIcmpCloseHandle = (pfnBH)GetProcAddress(m_hIcmp, "IcmpCloseHandle"); pIcmpSendEcho = (pfnDHDPWPipPDD)GetProcAddress(m_hIcmp, "IcmpSendEcho"); if ((pIcmpCreateFile == 0) || (pIcmpCloseHandle == 0) || (pIcmpSendEcho == 0)) { ::MessageBox(NULL, "Failed to get proc addr for function.", "Error", MB_OK|MB_ICONWARNING); return; } char ip[16]; m_plExtIps = new TStringList; m_plIntIps = new TStringList; for (int i=0; i<256; i++) { sprintf(ip, "192.168.10.%d", i); m_plIntIps->Add(ip); sprintf(ip, "192.168.11.%d", i); m_plExtIps->Add(ip); } } //--------------------------------------------------------------------------- void __fastcall TPingingForm::OnPingingFormDestroy(TObject *Sender) { m_plExtIps->Clear(); m_plIntIps->Clear(); delete m_plExtIps; delete m_plIntIps; FreeLibrary(m_hIcmp); WSACleanup(); } //--------------------------------------------------------------------------- void __fastcall TPingingForm::OnGetPing(TObject *Sender) { // Open the ping service HANDLE m_hIP = pIcmpCreateFile(); if (m_hIP == INVALID_HANDLE_VALUE) { ::MessageBox(NULL, "Unable to open ping service.", "Error", MB_OK|MB_ICONWARNING); return; } memset(m_acPingBuffer, '\xAA', sizeof(m_acPingBuffer)); m_pIpe = (PIP_ECHO_REPLY)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, sizeof(IP_ECHO_REPLY) + sizeof(m_acPingBuffer)); if (m_pIpe == 0) { ::MessageBox(NULL, "Failed to allocate global ping packet buffer.", "Error", MB_OK|MB_ICONWARNING); return; } m_pIpe->Data = m_acPingBuffer; m_pIpe->DataSize = sizeof(m_acPingBuffer); DWORD dwStatus = 0; m_plWrkIps = new TStringList; for (int i=0; i<256; i++) { dwStatus = pIcmpSendEcho(m_hIP, inet_addr(m_plIntIps->Strings[i].c_str()), m_acPingBuffer, sizeof(m_acPingBuffer), NULL, m_pIpe, sizeof(IP_ECHO_REPLY) + sizeof(m_acPingBuffer), 1000); if (dwStatus != 0) { if (m_pIpe->Status == 0) m_plWrkIps->Add(m_plIntIps->Strings[i].c_str()); // char msgPing[256]; // sprintf(msgPing, "Addr: %s, RTT: %d ms, TTL: %d", // m_plIntIps->Strings[i].c_str(), int(m_pIpe->RoundTripTime), int(m_pIpe->Options.Ttl)); // ::MessageBox(NULL, msgPing, "Ok", MB_OK); } else { // LPVOID lpMsgBuf; // FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, // NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language // (LPTSTR) &lpMsgBuf, 0, NULL); // ::MessageBox(NULL, (const char*)lpMsgBuf, "Error", MB_OK|MB_ICONWARNING); // LocalFree( lpMsgBuf ); } dwStatus = pIcmpSendEcho(m_hIP, inet_addr(m_plExtIps->Strings[i].c_str()), m_acPingBuffer, sizeof(m_acPingBuffer), NULL, m_pIpe, sizeof(IP_ECHO_REPLY) + sizeof(m_acPingBuffer), 1000); if (dwStatus != 0) { if (m_pIpe->Status == 0) m_plWrkIps->Add(m_plExtIps->Strings[i].c_str()); } } ListBox1->Items->AddStrings(m_plWrkIps); GlobalFree(m_pIpe); pIcmpCloseHandle(m_hIP); } //-------------------------END CUT----------------------------------------- // --Main Form Header Application File pinging.h //-----------------------------CUT----------------------------------------- #ifndef PingingH #define PingingH //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include <iostream.h> #include <winsock.h> #include <windowsx.h> //--------------------------------------------------------------------------- typedef struct { unsigned char Ttl; // Time To Live unsigned char Tos; // Type Of Service unsigned char Flags; // IP header flags unsigned char OptionsSize; // Size in bytes of options data unsigned char *OptionsData; // Pointer to options data } IP_OPTION_INFORMATION, * PIP_OPTION_INFORMATION; typedef struct { DWORD Address; // Replying address unsigned long Status; // Reply status unsigned long RoundTripTime; // RTT in milliseconds unsigned short DataSize; // Echo data size unsigned short Reserved; // Reserved for system use void *Data; // Pointer to the echo data IP_OPTION_INFORMATION Options; // Reply options } IP_ECHO_REPLY, * PIP_ECHO_REPLY; //--------------------------------------------------------------------------- class TPingingForm : public TForm { __published: // IDE-managed Components TButton *Button1; TListBox *ListBox1; void __fastcall OnPingingFormCreate(TObject *Sender); void __fastcall OnPingingFormDestroy(TObject *Sender); void __fastcall OnGetPing(TObject *Sender); private: // User declarations HINSTANCE m_hIcmp; HANDLE m_hIP; char m_acPingBuffer[64]; TStringList *m_plExtIps; TStringList *m_plIntIps; TStringList *m_plWrkIps; PIP_ECHO_REPLY m_pIpe; public: // User declarations __fastcall TPingingForm(TComponent* Owner); }; //--------------------------------------------------------------------------- typedef HANDLE (WINAPI* pfnHV)(VOID); typedef BOOL (WINAPI* pfnBH)(HANDLE); typedef DWORD (WINAPI* pfnDHDPWPipPDD)(HANDLE, DWORD, LPVOID, WORD, PIP_OPTION_INFORMATION, LPVOID, DWORD, DWORD); // evil, no? pfnHV pIcmpCreateFile; pfnBH pIcmpCloseHandle; pfnDHDPWPipPDD pIcmpSendEcho; //--------------------------------------------------------------------------- extern PACKAGE TPingingForm *PingingForm; //--------------------------------------------------------------------------- #endif //----------------------END CUT---------------------------------------------- // --Main Form File pinging.dfm //----------------------CUT---------------------------------------------- object PingingForm: TPingingForm Left = 299 Top = 240 BorderStyle = bsDialog BorderWidth = 1 Caption = 'PingingForm' ClientHeight = 284 ClientWidth = 190 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnCreate = OnPingingFormCreate OnDestroy = OnPingingFormDestroy PixelsPerInch = 96 TextHeight = 13 object Button1: TButton Left = 56 Top = 8 Width = 75 Height = 25 Caption = 'GetPings' TabOrder = 0 OnClick = OnGetPing end object ListBox1: TListBox Left = 8 Top = 40 Width = 175 Height = 241 ItemHeight = 13 TabOrder = 1 end end //----------------------END CUT---------------------------------------------- |