12

How can I get user's local IP address using Inno Setup?

I thought about using Win32 API GetIpAddrTable, but it is unclear how to make the adjustment.

Dos someone have any other way? Or know how to do it?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Toda Raba
  • 614
  • 1
  • 10
  • 25
  • What do you mean by using inno setup ? – opc0de May 29 '11 at 09:33
  • 1
    @opc0de: He probably is writing an Inno Setup script. Inno Setup supports Pascal scripting. – Andreas Rejbrand May 29 '11 at 09:38
  • In the installation process I need to show the user a list of the **local** IP address (in a case that he have more than one network card physical or logic) and let the user select from a list of is IP address. – Toda Raba May 29 '11 at 10:09
  • 1
    May I suggest that putting such functionality in the installer is a waste of time. Instead, launch a UI afterwards that lets the user configure the utility. Later, the user will want to to run the same options again. Do you want them to reinstall to change this option? What about when the user's IP changes after installation? – Warren P May 29 '11 at 19:39
  • 6
    It depends on if you want IPv4 address or the IPv6 address. Also a machine can have more than one local IP address. – Robert Love May 29 '11 at 21:51
  • 4
    @Toda, what happens if I install your software while connected to the WiFi network at the hotel I'm staying in, then go home and try using the software on the wired network I've got there? Hint: IP's are bound to change, don't ask about IP addresses at install time. Adapter information might also not be enough (what if I configure a VPN or plug a USB WiFi dongle, or connect using my phone's BT). If your application depends so heavily on network equipment and addresses, then it's designed for "Power Users" and they'll surely demand the ability to select IP or Adapter at run time. – Cosmin Prund May 30 '11 at 06:07

2 Answers2

22

It depends on if you want IPv4 address or the IPv6 address. But since you mentioned GetIpAddrTable and it only returns IPv4 addresses, I suspect that is what you wanted.

Each machine can have more than one local IP address. So I return them as a TStringList.
The machine I tested the following on had 5 IP addresses.

Since Inno Setup does not support pointers, I had to do everything through an Array of Byte for the buffer.

The code below is a complete Inno Setup script that demonstrates, how to use this function.

[Setup]
AppName=Test
AppVersion=1.5
DefaultDirName={pf}\test

[Code]

const
 ERROR_INSUFFICIENT_BUFFER = 122;


function GetIpAddrTable( pIpAddrTable: Array of Byte;
  var pdwSize: Cardinal; bOrder: WordBool ): DWORD;
external 'GetIpAddrTable@IpHlpApi.dll stdcall';


procedure GetIpAddresses(Addresses : TStringList);
var 
 Size : Cardinal;
 Buffer : Array of Byte;
 IpAddr : String;
 AddrCount : Integer;
 I, J : Integer;
begin
  { Find Size }
  if GetIpAddrTable(Buffer,Size,False) = ERROR_INSUFFICIENT_BUFFER then
  begin
     { Allocate Buffer with large enough size }
     SetLength(Buffer,Size);
     { Get List of IP Addresses into Buffer }
     if GetIpAddrTable(Buffer,Size,True) = 0 then
     begin
       { Find out how many addresses will be returned. }
       AddrCount := (Buffer[1] * 256) + Buffer[0];
       { Loop through addresses. }
       For I := 0 to AddrCount - 1 do
       begin
         IpAddr := '';
         { Loop through each byte of the address }
         For J := 0 to 3 do
         begin
           if J > 0 then
             IpAddr := IpAddr + '.';
           { Navigate through record structure to find correct byte of Addr }
           IpAddr := IpAddr + IntToStr(Buffer[I*24+J+4]);
         end;
         Addresses.Add(IpAddr);
       end;
     end;
  end;
end;

function InitializeSetup(): Boolean;
var
 SL : TStringList;
begin
  SL := TStringList.Create;
  GetIpAddresses(SL);
  MsgBox(SL.Text, mbInformation, MB_OK);
  SL.Free;
end;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Robert Love
  • 12,447
  • 2
  • 48
  • 80
8

Build an external DLL that returns a list of IP addresses and read that list in Inno Setup script.

In this article you will find code example how to build a DLL and how to call it in the InnoSetup script.

In this SO post you will find how to get IP addresses using Indy library or plain WinApi.

Community
  • 1
  • 1
andrius
  • 693
  • 4
  • 8