api:GetHostName,它能获得系统的IP地址。
function LocalIP : string;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;begin
WSAStartup($101, GInitData);
Result := '';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do begin
result:=StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;
function LocalIP : string;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;begin
WSAStartup($101, GInitData);
Result := '';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do begin
result:=StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Menus,winsock;//首先要添加winsock
type
TForm1 = class(TForm)
Timer1: TTimer;
Label1: TLabel;
PopupMenu1: TPopupMenu;
Exit1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Exit1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
oldx,oldy:integer;//添加变量,用做移动窗体
oldIp:string;
implementation{$R *.dfm}
//下面就是关键所在了(自己的ip)
function LIP : string;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;begin
WSAStartup($101, GInitData);
Result := '';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do begin
result:=StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
with lable1 dp
Caption:='';//定义属性
Font.Charset:=ANSI_CHARSET;
Font.Name:='Arial';
Font.Size:=10;
Font.Color:=clRed;
align:=alClient;
PopupMenu:=popupmenu1;timer1.Interval:=1000;
timer1.Enabled:=true;
label1.Caption :='IP:' +LIP; //赋值,把ip赋值给label1
oldIp:=LIP;
alphablend:=true; //呵呵,这个就是让窗口变的透明的办法了
alphablendvalue:=100;
form1.FormStyle :=fsStayOnTop; ///让窗体总在最前面
end;procedure TForm1.Timer1Timer(Sender: TObject);
begin
label1.Caption :='IP:' +LIP; //赋值
if oldip <>LIP then//比较一下
begin
showmessage('IP地址已经改变,请检查!');//提醒用户
end;
end;procedure TForm1.Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ssleft in shift then //移动窗体form1
begin
form1.Left:=form1.Left+x-oldx;
form1.Top:=form1.top+y-oldy;
end;
end;procedure TForm1.Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
oldx:=x;
oldy:=y;
end;procedure TForm1.Exit1Click(Sender: TObject);
begin
form1.Close; ///关闭
end;
end.这是我写的一个小工具,你改一下
昵称: 密码: 我的论坛 我的软件 我的订单 我的文档 我的简历 新闻 软件 商品 文档 论坛 谈用Delphi程序获取拨号连接的动态IP地址 关键字:delphi,ip,拨号 人气值:
911 文档分类:
Delphi 贴文时间
2001-6-5 16:16:55 得票数:
105 给贴子投票
投票 转贴者:gao277
出处: http://delphibig.home.sohu.com/skill/25.htm 在WIN9X下,如果安装了拨号网络,则在WINDOWS系统的系统目录System下将有两个拨号网络管理程序库RasApi32.DLL和RasApi16.DLL,我们可利用其中的RAS系列函数来获取和设置拨号连接网络的信息。当然是可以获取拨号连接的动态IP地址了。在Delphi帮助文件中,有相关RAS函数的详细说明。
---- 一、先解释一些要用到的数据常量和数据结构。 const
RAS_MaxDeviceType = 16;//设备类型名称长度
RAS_MaxEntryName = 256;//连接名称最大长度
RAS_MaxDeviceName = 128;//设备名称最大长度
RAS_MaxIpAddress = 15;//IP地址的最大长度
RASP_PppIp = $8021;//拨号连接的协议类型,该数值表示PPP连接type
HRASCONN = DWORD;//拨号连接句柄的类型
RASCONN = record//活动的拨号连接的句柄和设置信息
dwSize : DWORD;//该结构所占内存的大小(Bytes),
一般设置为SizeOf(RASCONN)
hrasconn : HRASCONN;//活动连接的句柄
szEntryName : array[0..RAS_MaxEntryName] of char;
//活动连接的名称
szDeviceType : array[0..RAS_MaxDeviceType] of char;
//活动连接的所用的设备类型
szDeviceName : array[0..RAS_MaxDeviceName] of char;
//活动连接的所用的设备名称
end;
TRASPPPIP = record//活动的拨号连接的动态IP地址信息
dwSize : DWORD;//该结构所占内存的大小(Bytes),
一般设置为SizeOf(TRASPPPIP)
dwError : DWORD;//错误类型标识符
szIpAddress : array[ 0..RAS_MaxIpAddress ] of char;
//活动的拨号连接的IP地址
end;
---- 二、接着要解释要用到的两个RAS函数。 //获取所有活动的拨号连接的信息(连接句柄和设置信息)
function RasEnumConnections( var lprasconn : RASCONN ;
//接收活动连接的缓冲区的指针
var lpcb: DWORD;//缓冲区大小
var lpcConnections : DWORD//实际的活动连接数
) : DWORD; stdcall;
function RasEnumConnections;external 'Rasapi32.dll'
name 'RasEnumConnectionsA';
//获取指定活动的拨号连接的动态IP信息
function RasGetProjectionInfo(
hrasconn : HRasConn;//指定活动连接的句柄
rasprojection : DWORD;//RAS连接类型
var lpprojection : TRASPPPIP;//接收动态IP信息的缓冲区
var lpcb : DWord//接收缓冲区的大小
) : DWORD;stdcall;
function RasGetProjectionInfo;external
'Rasapi32.dll' name 'RasGetProjectionInfoA';
这两个函数的返回值为0时表示执行成功,非0表示错误代码。 ---- 三、下面要讨论如何用上面的两个RAS函数获取拨号连接的动态IP地址 ---- 首先,要用函数RasEnumConnections列出已建立的活动拨号连接的信息,其中包括连接名称、连接句柄、连接设备类型和设备名称;然后根据连接句柄用函数RasGetProjectionInfo获取连接对应的一个TRASPPPIP结构,其中包括一个成员属性szIpAddress即为动态IP地址。具体请参见以下程序片段和注释信息。 procedure TForm1.Button1Click(Sender: TObject);
const
MaxConnections = 10;//假设最多有10个活动的拨号连接
var
connections : array[0..MaxConnections-1] of RASCONN;
//拨号连接数组
longSize : dword;
intAvailabelConnections : dword;
//活动的拨号连接的实际数目
intIndex : integer;
strTemp : string;
dwResult : DWORD;
dwSize : DWORD;
RASpppIP : TRASPPPIP;
//活动的拨号连接的动态IP地址信息
begin
connections[ 0 ].dwSize := sizeof(RASCONN);
longSize := MaxConnections * connections[ 0 ].dwSize;
//接收活动连接的缓冲区大小
intAvailabelConnections := 0;
//获取所有活动的拨号连接的信息(连接句柄和设置信息)
dwResult := RasEnumConnections( connections[ 0 ],
longSize,intAvailabelConnections );
if 0 < > dwResult then
memo1.lines.add( '错误:' + inttostr( dwResult ) )
else
begin
memo1.lines.add( '现有的活动连接有' +
IntToStr( intAvailabelConnections ) + '个');
//显示所有活动的拨号连接的信息(设置信息和动态IP地址)
for intIndex := 0 to intAvailabelConnections - 1 do
begin
//显示一个活动的拨号连接的设置信息
strTemp := '连接名称:'
+ StrPAS( connections[ intIndex ].szEntryName )
+ ',设备类型:'
+ StrPAS( connections[ intIndex ].szDeviceType )
+ ',设备名称:'
+ StrPAS( connections[ intIndex ].szDeviceName );
memo1.lines.add( strTemp );
//显示一个活动的拨号连接的动态IP地址
dwSize := SizeOf(RASpppIP);
RASpppIP.dwSize := dwSize;
dwResult := RASGetProjectionInfo
( connections[ intIndex ].hRasConn,
RASP_PppIp,RasPPPIP,dwSize);//获取动态IP地址
if 0 < > dwResult then
memo1.lines.add(
'错误:' + inttostr( dwResult ))
else
memo1.lines.add(
'动态地址:' + StrPas(RASpppIP.szIPAddress));
end;
end;
end;对该文的评论 我要评论 你没有登陆,无法发表评论。请先登陆 我要注册
美达美简介 广告服务 英语步步高 程序员大本营 百联美达美科技有限公司 版权所有