CSDN博客

img zxcdewq

取得拨号上网时动态分配IP的两种方法

发表于2003/12/2 22:00:00  661人阅读

              取得拨号上网时动态分配IP的两种方法

1:使用socket函数。
在拨号前先取得所有本地IP,记录到字符串列表里。
拨号成功后再遍历一次所有本地IP,和已经记录在字符串列表里IP比较。新增加的IP地址就是拨号上网时动态分配IP了。

2:使用和RAS有关API。
在vc里可以方便使用和ras相关的API。但在C++Builder 6里会出现由版本不同引起的错误。
所以在BCB里关键是要自己指定函数的大小。

以下代码在win2000 + C++Builder 6 环境下编译通过。

在Form里添加2个Edit,3个Button;

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
        TEdit *Edit1;
        TButton *Button1;
        TButton *Button2;
    TEdit *Edit2;
    TButton *Button3;
        void __fastcall Button1Click(TObject *Sender);
    void __fastcall Button2Click(TObject *Sender);
    void __fastcall Button3Click(TObject *Sender);
private: // User declarations
        TStringList   *m_slIp;
public:  // User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <winsock2.h>
#include <ras.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    m_slIp = new TStringList();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    WORD        wVersionRequested;
    WSADATA     wsaData;
    char        cHostName[64] = {0};
    HOSTENT     *pHost = NULL;
    AnsiString  psIp;
    int         Index;
    wVersionRequested = MAKEWORD(2, 0);

    if(0 != WSAStartup(wVersionRequested, &wsaData))
    {
        WSACleanup();
        ShowMessage("socket版本号不对");
    }

    if(0 == gethostname(cHostName, 64))
    {
        pHost = gethostbyname(cHostName);
        if(pHost != NULL)
        {
            for(int i=0; pHost->h_addr_list[i]!=NULL; i++)
            {
                IN_ADDR *p = (IN_ADDR *)(pHost->h_addr_list[i]);
                psIp = inet_ntoa(*p);
                m_slIp->Add(psIp);  //IP地址存入字符串列表
            }
        }
        else
        {
            WSACleanup();
            ShowMessage("取IP地址失败");
        }
    }
    else
    {
        ShowMessage("取机器名失败");
    }

    WSACleanup();

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    RASCONN  RASconn;   //活动连接
    DWORD BuffSize ;    //活动连接所占内存大小;
    DWORD ConnNum;      //活动连接数目
    RASconn.dwSize=692; //必须指定一个连接的内存大小;
    BuffSize=692;
   
    RASPPPIP rip;
    AnsiString  ModemIp;
    ZeroMemory(&rip, sizeof(rip));

    DWORD size;
    size = 40;
    rip.dwSize = 40;

    DWORD dwReturn=RasEnumConnections(&RASconn,&BuffSize,&ConnNum);
    if(dwReturn==0)
    {
      if(ConnNum>0)
      {
         //ShowMessage("没有拨号连接!");
          if ( (RasGetProjectionInfo(RASconn.hrasconn,RASP_PppIp,
          &rip, &size ) ) != 0 )
          {
              ShowMessage("取IP地址失败");
          }
          else
          {
              Edit2->Text = rip.szIpAddress ;
          }
      }
      else
      {
         ShowMessage("没有拨号连接!");
      }
        
    }
    else
         ShowMessage("RasEnumConnections函数失败!");

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
    WORD        wVersionRequested;
    WSADATA     wsaData;
    char        cHostName[64] = {0};
    HOSTENT     *pHost = NULL;
    AnsiString  psIp;
    int         Index;
    wVersionRequested = MAKEWORD(2, 0);

    if(0 != WSAStartup(wVersionRequested, &wsaData))
    {
        WSACleanup();
        ShowMessage("socket版本号不对");
    }

    if(0 == gethostname(cHostName, 64))
    {
        pHost = gethostbyname(cHostName);
        if(pHost != NULL)
        {
            for(int i=0; pHost->h_addr_list[i]!=NULL; i++)
            {
                IN_ADDR *p = (IN_ADDR *)(pHost->h_addr_list[i]);
                psIp = inet_ntoa(*p);
                if(m_slIp->IndexOf(psIp) < 0)
                {
                    Edit1->Text = psIp;
                }
            }
        }
        else
        {
            WSACleanup();
            ShowMessage("取IP地址失败");
        }
    }
    else
    {
        ShowMessage("取机器名失败");
    }

    WSACleanup();
    delete m_slIp;  
}
//---------------------------------------------------------------------------

先在上网前单击Button1,
上网后再单击Butto3得到就是拨号IP.

0 0

相关博文

我的热门文章

img
取 消
img