关于ras函数的调用(200分)

  • 主题发起人 主题发起人 tansh
  • 开始时间 开始时间
T

tansh

Unregistered / Unconfirmed
GUEST, unregistred user!
本人在查询如何得知本机的 PPP 的IP中,得知可以使用RAS的函数<br>原文<br>---------------------------------------------------------------------------------------------------------------------------<br>通过SOCK的方法,可以得到本机所有的IP,但是我无法区分出哪个是PPP,哪个是网卡的,<br>用这个方法,我在2000与98在调试,2000中最后一个IP是PPP的,98中第一个IP是PPP的,<br>所以,要取PPP的IP,最好还是用RAS函数<br>&nbsp; &nbsp; RasGetConnectStatus:function(HRASConn: THRASConn;lpRasConnStatus : PRASConnStatus): DWORD; stdcall;<br>&nbsp; &nbsp; RasEnumConnections : function(pRasConn : PRASConn;pCB : PDWord;pcConnections : PDWord) : DWORD; stdcall;<br>&nbsp; &nbsp; RasGetProjectionInfo:function(hrasconn:tHRASCONN ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rasprojection:dword;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lpprojection:PTRASPPPIP;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lpcb:pdword &nbsp; ) : DWORD; stdcall;<br>---------------------------------------------------------------------------------------------------------------------------------------------<br>但是我找了很多书根本没有提到本函数请问有否程序的范例!!!<br>非常感谢<br>
 
到MSDN中找<br><br>RasGetConnectStatus<br>The RasGetConnectStatus function retrieves information on the current status of the specified remote access connection. An application can use this call to determine when an asynchronous RasDial call has completed. <br><br>DWORD RasGetConnectStatus(<br>&nbsp; HRASCONN hrasconn, &nbsp;// handle to RAS connection of interest<br>&nbsp; LPRASCONNSTATUS lprasconnstatus<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // buffer to receive status data<br>);<br>&nbsp;<br>Parameters<br>hrasconn <br>Identifies the remote access connection for which to retrieve the status. This handle must have been obtained from RasDial or RasEnumConnections. <br>lprasconnstatus <br>Points to a RASCONNSTATUS structure that the function fills with status information. Before calling the function, an application must set the dwSize member of the structure to sizeof(RASCONNSTATUS) in order to identify the version of the structure being passed. <br>Return Values<br>If the function succeeds, the return value is zero.<br><br>If the function fails, the return value is a nonzero error value listed in the RAS header file or one of ERROR_BUFFER_TOO_SMALL or ERROR_NOT_ENOUGH_MEMORY. <br><br>QuickInfo<br>&nbsp; Windows NT: Requires version 3.1 or later.<br>&nbsp; Windows: Requires Windows 95 or later.<br>&nbsp; Windows CE: Requires version 1.0 or later.<br>&nbsp; Header: Declared in ras.h.<br>&nbsp; Import Library: Use rasapi32.lib.<br>&nbsp; Unicode: Implemented as Unicode and ANSI versions on Windows NT.<br><br>See Also<br>Remote Access Service Overview, RAS Server Administration Functions, RASCONNSTATUS, RasDial, RasEnumConnections <br><br><br>RasEnumConnections<br>The RasEnumConnections function lists all active RAS connections. It returns each connection's handle and phone-book entry name. <br><br>DWORD RasEnumConnections(<br>&nbsp; LPRASCONN lprasconn, &nbsp;// buffer to receive connections data<br>&nbsp; LPDWORD lpcb, &nbsp; &nbsp; &nbsp; &nbsp; // size in bytes of buffer<br>&nbsp; LPDWORD lpcConnections<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // number of connections written to buffer<br>);<br>&nbsp;<br>Parameters<br>lprasconn <br>Points to a buffer that receives an array of RASCONN structures, one for each RAS connection. Before calling the function, an application must set the dwSize member of the first RASCONN structure in the buffer to sizeof(RASCONN) in order to identify the version of the structure being passed. <br>lpcb <br>Points to a variable that contains the size, in bytes, of the buffer specified by lprasconn. On return, the function sets this variable to the number of bytes required to enumerate the RAS connections. <br>lpcConnections <br>Points to a variable that the function sets to the number of RASCONN structures written to the buffer specified by lprasconn. <br>Return Values<br>If the function succeeds, the return value is zero.<br><br>If the function fails, the return value is a nonzero error value listed in the RAS header file or one of ERROR_BUFFER_TOO_SMALL or ERROR_NOT_ENOUGH_MEMORY. <br><br>Remarks<br>If a connection was made without specifying a phone-book entry name, the information returned for that connection will give the connection phone number preceded by ".". <br><br>QuickInfo<br>&nbsp; Windows NT: Requires version 3.1 or later.<br>&nbsp; Windows: Requires Windows 95 or later.<br>&nbsp; Windows CE: Requires version 1.0 or later.<br>&nbsp; Header: Declared in ras.h.<br>&nbsp; Import Library: Use rasapi32.lib.<br>&nbsp; Unicode: Implemented as Unicode and ANSI versions on Windows NT.<br><br>See Also<br>Remote Access Service Overview, RAS Server Administration Functions, RASCONN, RasEnumEntries, RasGetConnectStatus <br><br>
 
用Delphi程序获取拨号连接的动态IP地址<br>王发军<br><br>  在WIN9X下,如果安装了拨号网络,则在WINDOWS系统的系统目录System下将有两个拨号网络管理程序库RasApi32.DLL和RasApi16.DLL,我们可利用其中的RAS系列函数来获取和设置拨号连接网络的信息。当然是可以获取拨号连接的动态IP地址了。在Delphi帮助文件中,有相关RAS函数的详细说明。<br><br>一、先解释一些要用到的数据常量和数据结构。<br><br>const<br>RAS_MaxDeviceType = 16;//设备类型名称长度<br>RAS_MaxEntryName = 256;//连接名称最大长度<br>RAS_MaxDeviceName = 128;//设备名称最大长度<br>RAS_MaxIpAddress = 15;//IP地址的最大长度<br>RASP_PppIp = $8021;//拨号连接的协议类型,该数值表示PPP连接<br><br>type<br>HRASCONN = DWORD;//拨号连接句柄的类型<br>RASCONN = record//活动的拨号连接的句柄和设置信息<br>dwSize : DWORD;//该结构所占内存的大小(Bytes),<br>一般设置为SizeOf(RASCONN)<br>hrasconn : HRASCONN;//活动连接的句柄<br>szEntryName : array[0..RAS_MaxEntryName] of char;<br>//活动连接的名称<br>szDeviceType : array[0..RAS_MaxDeviceType] of char;<br>//活动连接的所用的设备类型<br>szDeviceName : array[0..RAS_MaxDeviceName] of char;<br>//活动连接的所用的设备名称<br>end;<br>TRASPPPIP = record//活动的拨号连接的动态IP地址信息<br>dwSize : DWORD;//该结构所占内存的大小(Bytes),<br>一般设置为SizeOf(TRASPPPIP)<br>dwError : DWORD;//错误类型标识符<br>szIpAddress : array[ 0..RAS_MaxIpAddress ] of char;<br>//活动的拨号连接的IP地址<br>end;<br>二、接着要解释要用到的两个RAS函数。<br><br>//获取所有活动的拨号连接的信息(连接句柄和设置信息)<br>function RasEnumConnections( var lprasconn : RASCONN ;<br>//接收活动连接的缓冲区的指针<br>var lpcb: DWORD;//缓冲区大小<br>var lpcConnections : DWORD//实际的活动连接数<br>) : DWORD; stdcall;<br>function RasEnumConnections;external 'Rasapi32.dll'<br>name 'RasEnumConnectionsA';<br>//获取指定活动的拨号连接的动态IP信息<br>function RasGetProjectionInfo(<br>hrasconn : HRasConn;//指定活动连接的句柄<br>rasprojection : DWORD;//RAS连接类型<br>var lpprojection : TRASPPPIP;//接收动态IP信息的缓冲区<br>var lpcb : DWord//接收缓冲区的大小<br>) : DWORD;stdcall;<br>function RasGetProjectionInfo;external<br>'Rasapi32.dll' name 'RasGetProjectionInfoA';<br>  这两个函数的返回值为0时表示执行成功,非0表示错误代码。<br><br>三、下面要讨论如何用上面的两个RAS函数获取拨号连接的动态IP地址<br><br>  首先,要用函数RasEnumConnections列出已建立的活动拨号连接的信息,其中包括连接名称、连接句柄、连接设备类型和设备名称;然后根据连接句柄用函数RasGetProjectionInfo获取连接对应的一个TRASPPPIP结构,其中包括一个成员属性szIpAddress即为动态IP地址。具体请参见以下程序片段和注释信息。<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>const<br>MaxConnections = 10;//假设最多有10个活动的拨号连接<br>var<br>connections : array[0..MaxConnections-1] of RASCONN;<br>//拨号连接数组<br>longSize : dword;<br>intAvailabelConnections : dword;<br>//活动的拨号连接的实际数目<br>intIndex : integer;<br>strTemp : string;<br>dwResult : DWORD;<br>dwSize : DWORD;<br>RASpppIP : TRASPPPIP;<br>//活动的拨号连接的动态IP地址信息<br>begin<br>connections[ 0 ].dwSize := sizeof(RASCONN);<br>longSize := MaxConnections * connections[ 0 ].dwSize;<br>//接收活动连接的缓冲区大小<br>intAvailabelConnections := 0;<br>//获取所有活动的拨号连接的信息(连接句柄和设置信息)<br>dwResult := RasEnumConnections( connections[ 0 ],<br>longSize,intAvailabelConnections );<br>if 0 &lt; &gt; dwResult then<br>memo1.lines.add( '错误:' + inttostr( dwResult ) )<br>else<br>begin<br>memo1.lines.add( '现有的活动连接有' +<br>IntToStr( intAvailabelConnections ) + '个');<br>//显示所有活动的拨号连接的信息(设置信息和动态IP地址)<br>for intIndex := 0 to intAvailabelConnections - 1 do<br>begin<br>//显示一个活动的拨号连接的设置信息<br>strTemp := '连接名称:'<br>+ StrPAS( connections[ intIndex ].szEntryName )<br>+ ',设备类型:'<br>+ StrPAS( connections[ intIndex ].szDeviceType )<br>+ ',设备名称:'<br>+ StrPAS( connections[ intIndex ].szDeviceName );<br>memo1.lines.add( strTemp );<br>//显示一个活动的拨号连接的动态IP地址<br>dwSize := SizeOf(RASpppIP);<br>RASpppIP.dwSize := dwSize;<br>dwResult := RASGetProjectionInfo<br>( connections[ intIndex ].hRasConn,<br>RASP_PppIp,RasPPPIP,dwSize);//获取动态IP地址<br>if 0 &lt; &gt; dwResult then<br>memo1.lines.add(<br>'错误:' + inttostr( dwResult ))<br>else<br>memo1.lines.add(<br>'动态地址:' + StrPas(RASpppIP.szIPAddress));<br>end;<br>end;<br>end;<br>以上程序在PWIN98+Delphi3.0下调试通过。<br>
 
后退
顶部