谈用Delphi程序获取拨号连接的动态IP地址

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 gao277 的 blog

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

本文关键:delphi,ip,拨号
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top