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

发布于 2024-11-10 07:46:03 字数 128 浏览 0 评论 0原文

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

我想过使用Win32 API GetIpAddrTable,但不清楚如何进行调整。

有人还有其他办法吗?或者知道怎么做吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傾城如夢未必闌珊 2024-11-17 07:46:03

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

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

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

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

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

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;
西瓜 2024-11-17 07:46:03

构建一个返回 IP 地址列表的外部 DLL,并在 Inno Setup 脚本中读取该列表。

本文中,您将找到如何构建 DLL 以及如何调用它的代码示例在 InnoSetup 脚本中。

在这篇文章中,您将了解如何使用 Indy 获取 IP 地址库或普通 WinApi。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文