delphi 获取本机IP 及 主机名

tzc 2012-02-07 04:01:35
网上搜了一些方法 大多不适用于delphi 5
本人delphi 菜鸟 请把步骤尽可能列详细些
...全文
264 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzg827 2012-02-07
  • 打赏
  • 举报
回复
发现7楼的方法更通用
lzg827 2012-02-07
  • 打赏
  • 举报
回复
楼主: 这个应该适合D5,从原来的大富翁论坛找到的,很久很久前的代码。


uses Winsock;
...
function getIP(DNS_hostname: string): string;
var
WSAData: TWSAData;
HostEnt: PHostEnt;
begin
WSAStartup(2, WSAData);
HostEnt := gethostbyname(PChar(DNS_hostname));
with HostEnt^ do
Result := Format('%d.%d.%d.%d', [Byte(h_addr^[0]),
Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]);
WSACleanup;
end;
ADSLAN 2012-02-07
  • 打赏
  • 举报
回复
procedure TForm1.Button3Click(Sender: TObject);
var
namea: array[0..MAX_PATH] of Char;
size: Cardinal;
begin
GetComputerName(namea, size);
ShowMessage(namea);
end;

获取本机IP我没有太好的方法..
tzc 2012-02-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 erhan 的回复:]
Delphi(Pascal) code


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Butt……
[/Quote]
谢谢, 很好的方法 就是要考虑不同的操作系统
erhan 2012-02-07
  • 打赏
  • 举报
回复

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
mstr: string;
ss: TStringList;
i: integer;
begin
WinExec('cmd /c ipconfig/all >d:\abc.txt',SW_HIDE);
sleep(1000);
ss := TStringList.Create;
ss.LoadFromFile('d:\abc.txt');
for i:=0 to ss.Count -1 do
begin
mstr := Trim(ss.Strings[i]);
if Pos('Host Name',mstr)>0 then
begin
Delete(mstr,1,Pos(':',mstr)+1);
showmessage('hostname:' + mstr);
end;

if Pos('IP Address',mstr)>0 then
begin
Delete(mstr,1,Pos(':',mstr)+1);
showmessage('ipaddr:' + mstr);
end;
end;
ss.Destroy;
DeleteFile('d:\abc.txt');
end;

end.
tzc 2012-02-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ljz9425 的回复:]
网上很多,何不自己动手搜一下

引用 3 楼 ljz9425 的回复:

Delphi(Pascal) code
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Sockets, ExtCtrls;
……
[/Quote]
这个方法我见过 什么地方可以找到 UdpSocke 这个控件
tzc 2012-02-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 tianzhichengzhr 的回复:]
引用 1 楼 erhan 的回复:
winexec 执行 ipconfig/all > abc.txt
然后打开abc.txt提取

没有简单一点的办法吗
如果没有能不能提供一下源代码
[/Quote]
这个方法我见过 什么地方可以找到 UdpSocke 这个控件
whisht 2012-02-07
  • 打赏
  • 举报
回复
网上很多,何不自己动手搜一下
[Quote=引用 3 楼 ljz9425 的回复:]

Delphi(Pascal) code
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Sockets, ExtCtrls;

type
TForm1 = class(TForm)
UdpSocke……
[/Quote]
whisht 2012-02-07
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Sockets, ExtCtrls;

type
TForm1 = class(TForm)
UdpSocket1: TUdpSocket;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
self.Caption:=self.UdpSocket1.LocalHostName;
self.Caption:=self.Caption+':'+self.UdpSocket1.LocalHostAddr;
end;

end.
tzc 2012-02-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 erhan 的回复:]
winexec 执行 ipconfig/all > abc.txt
然后打开abc.txt提取
[/Quote]
没有简单一点的办法吗
如果没有能不能提供一下源代码
erhan 2012-02-07
  • 打赏
  • 举报
回复
winexec 执行 ipconfig/all > abc.txt
然后打开abc.txt提取

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧