Get Local IP Address with Delphi 2009

Hi all

How can I get the local IP Address with Delphi 2009?

All examples i find are only running with Delphi 2007 because they are not using AnsiChar.

Thank you for your help

Herbert
0
Herbert
12/22/2008 12:47:58 PM
embarcadero.delphi.win32 2183 articles. 0 followers. Follow

9 Replies
1658 Views

Similar Articles

[PageSpeed] 36

>
> All examples i find are only running with Delphi 2007 because they are not 
> using AnsiChar.

Replace Char with AnsiChar in the examples ?
0
Uffe
12/22/2008 2:02:29 PM
I got this off the Internet and I have used it in several projects.  I changed char to ansichar for Delphi 2009 and it works.   This function uses the winsock unit.

function TForm1.GETIPAddress(): string;
type pu_long = ^u_long;
var varTWSAData : TWSAData;
    varPHostEnt : PHostEnt;
    varTInAddr : TInAddr;
    namebuf : Array[0..255] of ansichar;
begin
  try
    try
      If WSAStartup($101,varTWSAData) <> 0 Then
        Result := ''
      Else Begin
        gethostname(namebuf,sizeof(namebuf));
        varPHostEnt := gethostbyname(namebuf);
        varTInAddr.S_addr := u_long(pu_long(varPHostEnt^.h_addr_list^)^);
        Result := inet_ntoa(varTInAddr);
      End;
    except
      Result := '';
    end;
  finally
    WSACleanup;
  end;
end;
0
Carl
12/22/2008 2:17:58 PM
> How can I get the local IP Address with Delphi 2009?

function LocalHostIP: string;
begin
  result := '127.0.0.1';
end;

<g>
0
Ed
12/23/2008 3:21:47 AM
> {quote:title=Herbert Schlup wrote:}{quote}
> Hi all
> 
> How can I get the local IP Address with Delphi 2009?
> 
> All examples i find are only running with Delphi 2007 because they are not using AnsiChar.
> 
> Thank you for your help
> 
> Herbert

I know this thread is from 2008, but maybe this will help someone else from having to dig.

Indy's TIdIPWatch works great.  It can even be configured to keep a history of the changes too.  It can be ran with or without assigning event handlers - I call ForceCheck and then call LocalIP, which immediately returns the locally bound IP as type String.
{code}
Flocal  : TidIPWatch;

function GetIP : String;
begin
    Flocal.ForceCheck;
    Result := Flocal.LocalIP;
end;
{code}
0
Josh
10/8/2011 7:35:30 PM
<Josh Young> wrote in message news:409444@forums.embarcadero.com...

> Indy's TIdIPWatch works great.  It can even be configured
> to keep a history of the changes too.  It can be ran with or
> without assigning event handlers - I call ForceCheck and
> then call LocalIP, which immediately returns the locally bound
> IP as type String.

The TIdIPWatch component is meant for monitoring changes to the machine's 
active IP in (near) real-time.  If you just want to know what the current IP 
is, you can use the TIdStack.LocalAddress property directly instead 
(TIdIPWatch uses it internally):

{code:delphi}
uses
  ..., IdStack;

function GetIP : String;
begin
  TIdStack.IncUsage;
  try
    Result := GStack.LocalAddress;
  finally
    TIdStack.DecUsage;
  end;
end;
{code}

If you have at least 1 Indy component instantiated, you can omit the calls 
to Inc/DecUsage(), as TIdComponent calls them internally:

{code:delphi}
uses
  ..., IdStack;

function GetIP : String;
begin
  Result := GStack.LocalAddress;
end;
{code}

With that said, note that a machine may have multiple IPs at a time.  The 
TIdStack.LocalAddress property (and TIdIPWatch) only reports the first 
available IP that is reported by the OS.  If you want to know what all of 
the available IPv4 addresses are (Indy does not report IPv6 addresses yet), 
you can use the TIdStack.LocalAddresses property (not thread safe) or 
TIdStack.AddLocalAddressesToList() method (is thread-safe) instead.

-- 
Remy Lebeau (TeamB)
0
Remy
10/8/2011 9:44:30 PM
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> <Josh Young> wrote in message news:409444@forums.embarcadero.com...
> 
> > Indy's TIdIPWatch works great.  It can even be configured
> > to keep a history of the changes too.  It can be ran with or
> > without assigning event handlers - I call ForceCheck and
> > then call LocalIP, which immediately returns the locally bound
> > IP as type String.
> 
> The TIdIPWatch component is meant for monitoring changes to the machine's 
> active IP in (near) real-time.  If you just want to know what the current IP 
> is, you can use the TIdStack.LocalAddress property directly instead 
> (TIdIPWatch uses it internally):
> 
> {code:delphi}
> uses
>   ..., IdStack;
> 
> function GetIP : String;
> begin
>   TIdStack.IncUsage;
>   try
>     Result := GStack.LocalAddress;
>   finally
>     TIdStack.DecUsage;
>   end;
> end;
> {code}
> 
> If you have at least 1 Indy component instantiated, you can omit the calls 
> to Inc/DecUsage(), as TIdComponent calls them internally:
> 
> {code:delphi}
> uses
>   ..., IdStack;
> 
> function GetIP : String;
> begin
>   Result := GStack.LocalAddress;
> end;
> {code}
> 
> With that said, note that a machine may have multiple IPs at a time.  The 
> TIdStack.LocalAddress property (and TIdIPWatch) only reports the first 
> available IP that is reported by the OS.  If you want to know what all of 
> the available IPv4 addresses are (Indy does not report IPv6 addresses yet), 
> you can use the TIdStack.LocalAddresses property (not thread safe) or 
> TIdStack.AddLocalAddressesToList() method (is thread-safe) instead.
> 
> -- 
> Remy Lebeau (TeamB)

Even better.  
Thanks Remy, that's good to know.  
And thanks for the samples.
0
Josh
10/9/2011 1:49:51 AM
OR Try

function getIPs: Tstrings;
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 := TstringList.Create;
Result.Clear;
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.Add(inet_ntoa(pptr^[I]^));
  Inc(I);
end;
WSACleanup;
end;
0
Jeffrey
10/9/2011 4:49:08 AM
<Jeffrey Ottery> wrote in message news:409528@forums.embarcadero.com...
> OR Try

That is essentially what Indy does internally (except it uses getaddrinfo() 
instead of gethostbyname() on systems that support IPv6).  However, 
something to be aware of is this approach may report misleading information. 
Technically speaking, a DNS lookup is being performed on the hostname, and 
it is possible for a machine to (a) be configured such that its name does 
not resolve to an IP, or (b) be configured such that its name resolves to 
multiple IPs, only one of which belongs to the local machine. For better 
results, you should use the Win32 API GetAdaptersInfo() and/or 
GetAdaptersAddresses() functions instead.  GetAdaptersInfo() only supports 
IPv4, but GetAdaptersAddresses() supports both IPv4 and IPv6...

-- 
Remy Lebeau (TeamB)
0
Remy
10/9/2011 9:48:23 PM
Thanks for the advice Remy. This was not written by me but I have used it in a non-critical situation.

> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> <Jeffrey Ottery> wrote in message news:409528@forums.embarcadero.com...
> > OR Try
> 
> That is essentially what Indy does internally (except it uses getaddrinfo() 
> instead of gethostbyname() on systems that support IPv6).  However, 
> something to be aware of is this approach may report misleading information. 
> Technically speaking, a DNS lookup is being performed on the hostname, and 
> it is possible for a machine to (a) be configured such that its name does 
> not resolve to an IP, or (b) be configured such that its name resolves to 
> multiple IPs, only one of which belongs to the local machine. For better 
> results, you should use the Win32 API GetAdaptersInfo() and/or 
> GetAdaptersAddresses() functions instead.  GetAdaptersInfo() only supports 
> IPv4, but GetAdaptersAddresses() supports both IPv4 and IPv6...
> 
> -- 
> Remy Lebeau (TeamB)
0
Jeffrey
10/10/2011 1:07:00 AM
Reply:

Similar Artilces:

How to fix get IP-MAC pairs code below from Delphi 7 to Delphi 2009 ?
How to get All IP - MAC address pairs on local PC by Delphi 2009 ? The Code below can get correct answer on Delphi 7, but get error answer on Delphi 2009. How to fix ? or Are there the better method ? // Delphi 7 (Delphi 2009 failed) // // Get Alll IP-MAC address pairs on local PC // unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; function GetIPMAC: String; const ANY_SIZE = 1; MAX_ADAPTER_ADDRESS_LENGTH = 8; // arb. MAX_INTERFACE_NAME_LEN = 256; { mrapi.h } MAXL...

Could I use IdIPWatch to get "All" IP address by Delphi 2009 ?
Could I use IdIPWatch to get "All" IP address by Delphi 2009 ? ( IdIPWatch in Indy Misc palette) The code below only get one IP. Thank you very much . uses IdIPWatch; procedure TForm1.FormCreate(Sender: TObject); begin Memo1.Clear; Memo1.Lines.Add(IdIPWatch1.LocalIP); end; <ICST ICST> wrote in message news:113236@forums.codegear.com... > Could I use IdIPWatch to get "All" IP address by Delphi 2009 ? No. That is not what it is meant for. > The code below only get one IP. As it should be, because that is what TIdIPWatch is desi...

How to use TIdStack to Get IP by Delphi 2009 ?
uses IdStack; function GetAllIP:String; var Ids: TIdStack; begin Ids := TIdStack.Create // Failed in Delphi 2009, How to fix ? try Result := Ids.LocalAddresses.Text; finally Ids.Free; end; end; procedure TForm1.FormCreate(Sender: TObject); begin Memo1.Lines.Add(GetAllIP); end; <ICST ICST> wrote in message news:112914@forums.codegear.com... > Ids := TIdStack.Create // Failed in Delphi 2009, How to fix ? TIdStack is an abstract base class. Do not instantiate it directly. Use the global GStack variable instead. Call the TIdStack.I...

Best Place to get help on converting Delphi 2007 string to Delphi 2009
Where is the best place to get help on converting Delphi 2007 strings to Delphi 2009? The most common problem getting Delphi 7-Delphi 2007 components to compile seems to be caused by the new Unicode strings in Delphi 2009. It would be great if there was some place to go to get help with this. Maybe there should be a new group for String conversion where developers could ask short questions about conversion. For example: Delphi 7 - Delphi 2007 CharArrayPtr = ^CharArray; CharArray = array[0..MaxInt-1] of Char; Compiles Delphi 2009 CharArrayPtr = ^CharArray; CharArray ...

Delphi 4 to Delphi 2009
Hello, Thanks to all who answered my previous question. That was a great help. And atlast our client agreed to upgrade our delphi version from 4 to Delphi 2009. *Sigh*. But before that, I need to give the estimation and cost regarding the migration to delphi 2009. Can anyone tell me is there any tool to migrate from delphi 4 to delphi 2009 or just I need to compile our Delphi 4 application in Delphi 2009. I have read from the delphi 2009 feature matrix that Delphi 1 through Delphi 2007 import is possible in delphi 2009. But i am not that sure considering the size of our application. ...

Delphi 5 To Delphi 2009
I upgraded to Delphi 2009 from D5. The install says I can install Delphi and/or C++. Delphi installed OK but I see nothing of C++. What am I missing or does my upgrade not include C++? Thanks It depends on what you bought. If you bought Delphi 2009 only, that's what you get. If you bought Delphi 2009 and C++ Builder 2009 you get both. My guess is you got Delphi 2009 only. The simplest way to verify is look your invoice - it should say I would think. You could also go to members.embarcadero.com, login, then click on my registered products. There will be a textual description of...

Get trial license Delphi and C++Builder 2009 on e-mail address
Hi! When I click link (button) Send Activation on this link - http://cc.codegear.com/Free.aspx?id=26043 (Activate Delphi and C++Builder 2009 Trial with ISO download ) I see this: *No connection could be made because the target machine actively refused it 12.233.153.45:8080* An error has occured while processing the page. Please try to refresh the page, or return to the home page. : CDC1 Help me please! ...

Delphi 2007 to Delphi 2009 Conversion
CharArrayPtr = ^CharArray; CharArray = array[0..MaxInt-1] of Char; How to convert to Delphi 2009? Bill Bill Miller wrote: > CharArrayPtr = ^CharArray; > CharArray = array[0..MaxInt-1] of Char; > > How to convert to Delphi 2009? > > Bill it depends heavelly on how it is used fearther down in your project and what you want to convert it to. I you want to simple convert the declarations ignoring UNicode altogether then the only think you have to change is the CharArray declaratin from a Char to an AnsiChar eg CharArrayPtr = ^CharArray; CharArray =...

Upgrading to Delphi 2009 from Delphi 5
I've decided it's time to upgrade to Delphi 2009 Professional from my current Delphi 5 Professional which (according to the FAQ) makes me eligible for the upgrade pricing. However, I'd just like to clarify how this process works. My concern is that my Delphi 5 is not registered on the CodeGear site and this doesn't seem to be possible as far as I can tell. When installing the trial I noticed that Delphi 2009 registers itself with your CodeGear account so does this mean that the previous version must be registered there to verify you own a Professional edition? I plan to o...

Delphi 2009 alongside Delphi 2007
Hi all, I am about to install Delphi 2009 on the same development system as Delphi 2007, before I embark is there anything I should be aware of so as to not break my Delphi 2007 installation and projects ? Thanks JT John Taylor wrote: > Hi all, > > I am about to install Delphi 2009 on the same development system > as Delphi 2007, before I embark is there anything I should be aware > of so as to not break my Delphi 2007 installation and projects ? As long as you install into a separate folder there is no problem with the two versions getting into each other...

is there a Delphi 2007
Would be nice to have a bundle of the latest Delphi packages. Gilbert Padilla wrote: > Would be nice to have a bundle of the latest Delphi packages. > I expect Delphi 2007 will only be on sale until the full RAD Studio 2009 edition is published, including Delphi 2009.net. W ...

Delphi 7 to Delphi 2009 conversion
Hello group, What do I need to do to comile the following code in D2009. Compiler error after the Else E2010: Incompatible Types 'AnsiChar' and 'Char' if isUnicode then SearchHandle := FindFirstFilew( PWideChar( fn ), FindDataW ) else SearchHandle := FindFirstFile( PAnsiChar( Ansistring( fn ) ), FindDataA ); <<<<<compiler error in the above line >>>>>>>>>>> Regards, Bryan > What do I need to do to comile the following code in D2009. > Compiler error after the Else > E2010: Incompati...

Delphi 2009 and Localization?
A while back there was talk about D2009 having improved Localization tools (aside from Unicode)? Any info on this? Or would it be better to use something like this: http://dybdahl.dk/dxgettext/ -- Tony Caduto AM Software Design http://www.amsoftwaredesign.com Tony Caduto wrote: > A while back there was talk about D2009 having improved Localization > tools (aside from Unicode)? > > Any info on this? Or would it be better to use something like this: > http://dybdahl.dk/dxgettext/ Definetly lacking in at least one way from the included localization tools and th...

delphi Win32 using delphi .NET dll
Hi, I'm trying to use a delphi.NET dll in delphi.WIN32. I am currently using CodeGear Delphi 2007 with version2(base version) of .NET I can get the dll to import into the WIN32 application the only problem is when i include things such as: "using Classes,DateUtils, SysUtils" in the .NET dll the win32 application will instantly hang when any of the dll functions are called. Any help would be great thanks. Also I have tried this example and it also crashes for me? http://cc.embarcadero.com/Item/22688 -Braden I also found this.. "The problem is that, wehn you instal...

Web resources about - Get Local IP Address with Delphi 2009 - embarcadero.delphi.win32

File:Ipv6 address.svg - Wikipedia, the free encyclopedia
I, the copyright holder of this work, release this work into the public domain . This applies worldwide. This file contains additional information, ...

Ronald Reagan Challenger address
Former US President Ronald Reagan postponed the State of the Union speech for one week and instead addressed the nation from the Oval Office ...

Ronald Reagan Challenger address
Former US President Ronald Reagan postponed the State of the Union speech for one week and instead addressed the nation from the Oval Office ...

Kevin Andrews skips Parliament to address conservative lobby group in Washington on defence
Kevin Andrews will become the second Liberal backbencher in a week to address a Right wing US lobby group when he speaks at the largest conservative ...

Ronald Reagan Challenger address
Former US President Ronald Reagan postponed the State of the Union speech for one week and instead addressed the nation from the Oval Office ...

Ronald Reagan Challenger address
Former US President Ronald Reagan postponed the State of the Union speech for one week and instead addressed the nation from the Oval Office ...

Clinton and Sanders Address Voter Skepticism at Iowa Democratic Forum
The audience at CNN's event gave voice to the issues that have dogged the leading candidates during the 2016 presidential campaign.

Watch Megyn Kelly address Donald Trump's debate boycott during a remarkable segment of her show
Fox News host Megyn Kelly covered Donald Trump's sudden announcement that he won't attend the Fox-hosted presidential debate during an unusual ...

What Can Be Done To Address Harassment In Science?
It's good that federal agencies have made strong statements against harassment and discrimination in science. It would be even better if they ...

Steven Avery's Defense Attorney and Prosecutor Address Important Information Making A Murderer Left Out ...
Since Making a Murderer debuted late last year on Netflix, the internet has been overrun with amateur sleuths looking for evidence presented ...

Resources last updated: 2/3/2016 7:09:58 PM