这是得到动态IP的代码,不是局域网IP 但是运行时出错,说是引用rasgetprojectioninfoa出错,不知道为什么?unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;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;implementation{$R *.dfm}
//获取所有活动的拨号连接的信息(连接句柄和设置信息)
function rasenumconnections( var lprasconn : rasconn ;
//接收活动连接的缓冲区的指针
var lpcb: dword;//缓冲区大小
var lpcconnections : dword//实际的活动连接数
) : dword; stdcall;external 'rasapi32.dll' name 'rasenumconnectionsa';
function rasgetprojectioninfo(
hrasconn : hrasconn;//指定活动连接的句柄
rasprojection : dword;//ras连接类型
var lpprojection : traspppip;//接收动态ip信息的缓冲区
var lpcb : dword//接收缓冲区的大小
) : dword;stdcall;external 'rasapi32.dll' name 'rasgetprojectioninfoa';
// 这两个函数的返回值为0时表示执行成功,非0表示错误代码。
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;
end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;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;implementation{$R *.dfm}
//获取所有活动的拨号连接的信息(连接句柄和设置信息)
function rasenumconnections( var lprasconn : rasconn ;
//接收活动连接的缓冲区的指针
var lpcb: dword;//缓冲区大小
var lpcconnections : dword//实际的活动连接数
) : dword; stdcall;external 'rasapi32.dll' name 'rasenumconnectionsa';
function rasgetprojectioninfo(
hrasconn : hrasconn;//指定活动连接的句柄
rasprojection : dword;//ras连接类型
var lpprojection : traspppip;//接收动态ip信息的缓冲区
var lpcb : dword//接收缓冲区的大小
) : dword;stdcall;external 'rasapi32.dll' name 'rasgetprojectioninfoa';
// 这两个函数的返回值为0时表示执行成功,非0表示错误代码。
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;
end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货