如何使用 Inno Setup 获取本地 IP 地址 [英] How to get the local IP address using Inno Setup

查看:54
本文介绍了如何使用 Inno Setup 获取本地 IP 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Inno Setup 获取用户的本地 IP 地址?

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

我考虑过使用Win32 API GetIpAddrTable,但不清楚如何进行调整.

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?

推荐答案

这取决于你想要 IPv4 地址还是 IPv6 地址.但是既然你提到了 GetIpAddrTable 并且它只返回 IPv4 地址,我怀疑这就是你想要的.

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.

每台机器可以有多个本地 IP 地址.所以我将它们作为 TStringList 返回.
我测试以下的机器有 5 个 IP 地址.

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.

由于 Inno Setup 不支持指针,所以我必须通过缓冲区的字节数组来完成所有操作.

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

下面的代码是一个完整的 Inno Setup 脚本,用于演示如何使用此功能.

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

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

[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;

这篇关于如何使用 Inno Setup 获取本地 IP 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆