IP info

ip

//---------------------------------------------------------------------------

#include <vcl.h>
#include <windows.h>
#include <stdio.h>
#include <winsock.h>
#pragma comment (lib, "ws2_32.lib")
#pragma hdrstop

#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
	: TForm(Owner)
{
}
AnsiString getinfo(void){
WSADATA wsaData;
AnsiString Return = "";

  if (WSAStartup(MAKEWORD(2, 2), &wsaData)) return "Chyba";

  char HostName[256];
  ZeroMemory(HostName,sizeof(HostName));
  if (!gethostname(HostName,sizeof(HostName))) {
	struct hostent *host = gethostbyname(HostName);
	if (host) {
	  Return = Return.Insert("Name: "+AnsiString(host->h_name) + "\r\n", Return.Length());
	  for (int i=0; host->h_addr_list[i]; i++) {       
		Return = Return.Insert(AnsiString(IntToStr(i))+":\r\n", Return.Length());
		Return = Return.Insert("IP: "+AnsiString(inet_ntoa(*(in_addr *)host->h_addr_list[i])) + "\r\n", Return.Length());
	  };
	};
  };

  WSACleanup();

  return Return;
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
Memo1->Lines->Text = getinfo();
}
//---------------------------------------------------------------------------

Leave a Reply