Link to home
Start Free TrialLog in
Avatar of LeTay
LeTay

asked on

Get PC IP address with Delphi XE2

Is there a simple way for a Delphi XE2 application to get the IP address of the PC where the application is running ?
Thanks
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

uses winsock;
{$R *.dfm}

function LocalIP: string;
type
  TaPInAddr = array [0..10] of PInAddr;
  PaPInAddr = ^TaPInAddr;
var
  phe: PHostEnt;
  pptr: PaPInAddr;
  Buffer: array [0..63] of Ansichar;
  i: Integer;
  GInitData: TWSADATA;
begin
  WSAStartup($101, GInitData);
  Result := '';
  GetHostName(Buffer, SizeOf(Buffer));
  phe := GetHostByName(Buffer);
  if phe = nil then
    Exit;
  pptr := PaPInAddr(phe^.h_addr_list);
  i := 0;
  while pptr^[i] <> nil do
  begin
    Result := StrPas(inet_ntoa(pptr^[i]^));
    Inc(i);
  end;
  WSACleanup;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(LocalIP);
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LeTay
LeTay

ASKER

Hello Geert, I wonder how you can find to for "productive" (making money) work seeing all the good answers/solutions you provide to the community !
Many thanks
Delphi programming is a niche market.  Jobs are not always as readily available as they are with other languages, but they can pay very well.  Just in the last 3 months I have seen Delphi contracting jobs in the $65.00 - $80.00 per hour range ($130,000.00 - $160,000.00) per year.

This is yet another reason why I like Delphi.  I can take a 12 or 18 month contract and afford to have some time off!
Avatar of LeTay

ASKER

You mean time off to help EE ?
LeTay,
i admit it is difficult to cram everything in a 24 hour day.
it helps if you only need 5 to 7 hours of sleep

while oracle things are installing i try and solve delphi questions on EE
currently i'm installing a lot :)

current job: oracle dba + sap basis 3 years
previous jobs: delphi developer 10 years,
  technician (plumbing, electricity, hvac, car mechanics in the weekend) 4 years
next job: don't know yet

old hobbies: martial arts (black belt), golf (handicap 9)
current hobby: house makeover, kids

you won't see me much in the weekends and evenings
i'm doing a makeover of our house
> currently the bathroom is 90% finished.
i didn't know a bathroom was so much work, been busy for 6 weeks now
the walk-in rain shower I installed actually works , no leaks !
my old job as a plumber is finally paying off :)

you can do anything you want, you just have to set your mind to it
Avatar of LeTay

ASKER

Nice to read activities of Experts that often are in the shadow ...
For me Delphi is not the major task but the one I prefer
I live in Brussels, Belgium and I will retire in July but ... will continue working as consultant ...
nice to see a fellow country member
retirement ... nice,
by the time i get there, there will be no money left the way di rupo and the rest are going on
Mmm, I think that is simplest to use an embedded unit instead of a component coming from a huge component package.
Anyway, having already it installed, I agree that it's easy to use for this need
Avatar of LeTay

ASKER

I think the opposite, on a code review point of view.