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?
To obtain the computer's IP address, we must use GetHostByName in conjunction with GetHostName. Each computer is called a host and we can get the hostname with a special function call: GetHostName. We then use GetHostByName to get the IP-address, related to this hostname.
A Local IP address is the identifier (IPv4 or IPv6) that is assigned to your laptop, PC, mobile and any other device within your local network. It is not visible to the outer world, but it is required in order to use the internet.
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 '[email protected] 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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With