因工作需要,根据客户电脑,我要把他的ip 保存下来
我写了个类,做了个封装
//---------------------------------------------------------------------------
#ifndef GetComputeIPH
#define GetComputeIPH
#include <winsock2.h>
#include <Classes.hpp>
//---------------------------------------------------------------------------
class TGetComputeIP
{
private:
int initSocketFlag; //初始化是否成功
public:
__fastcall TGetComputeIP();
__fastcall ~TGetComputeIP();
String __fastcall GetComputeIP(String HostName);
String __fastcall GetComputeName(String IP);
};
#endif
//--------------------------code---------------------------------------------------------
//---------------------------------------------------------------------------
#pragma hdrstop
#include "GetComputeIP.h"
//---------------------------------------------------------------------------
__fastcall TGetComputeIP::TGetComputeIP()
{
initSocketFlag = 0;
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
initSocketFlag = -1;
MessageBox(NULL,"not found winsock2 dll","消息",MB_OK);
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 0 )
{
initSocketFlag = -2;
WSACleanup( );
}
}
__fastcall TGetComputeIP::~TGetComputeIP()
{
if(initSocketFlag == 0)
{
WSACleanup();
}
}
String __fastcall TGetComputeIP::GetComputeIP(String HostName)
{
String IP;
if(initSocketFlag == 0)
{
PHOSTENT hostinfo;
if((hostinfo = gethostbyname(HostName.c_str())) != NULL)
{
IP = inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
}
}
return IP;
}
String __fastcall TGetComputeIP::GetComputeName(String IP)
{
String ComputeName;
if(initSocketFlag == 0)
{
DWORD dwIPAddr ;
dwIPAddr = inet_addr(IP.c_str());
if(dwIPAddr != INADDR_NONE) //无效得地址
{
PHOSTENT hostinfo;
if((hostinfo = gethostbyaddr((LPSTR)&dwIPAddr, 4, AF_INET)) != NULL)
{
ComputeName = hostinfo->h_name;
}
}
}
return ComputeName;
}
#pragma package(smart_init)
我写了个类,做了个封装
//---------------------------------------------------------------------------
#ifndef GetComputeIPH
#define GetComputeIPH
#include <winsock2.h>
#include <Classes.hpp>
//---------------------------------------------------------------------------
class TGetComputeIP
{
private:
int initSocketFlag; //初始化是否成功
public:
__fastcall TGetComputeIP();
__fastcall ~TGetComputeIP();
String __fastcall GetComputeIP(String HostName);
String __fastcall GetComputeName(String IP);
};
#endif
//--------------------------code---------------------------------------------------------
//---------------------------------------------------------------------------
#pragma hdrstop
#include "GetComputeIP.h"
//---------------------------------------------------------------------------
__fastcall TGetComputeIP::TGetComputeIP()
{
initSocketFlag = 0;
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
initSocketFlag = -1;
MessageBox(NULL,"not found winsock2 dll","消息",MB_OK);
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 0 )
{
initSocketFlag = -2;
WSACleanup( );
}
}
__fastcall TGetComputeIP::~TGetComputeIP()
{
if(initSocketFlag == 0)
{
WSACleanup();
}
}
String __fastcall TGetComputeIP::GetComputeIP(String HostName)
{
String IP;
if(initSocketFlag == 0)
{
PHOSTENT hostinfo;
if((hostinfo = gethostbyname(HostName.c_str())) != NULL)
{
IP = inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
}
}
return IP;
}
String __fastcall TGetComputeIP::GetComputeName(String IP)
{
String ComputeName;
if(initSocketFlag == 0)
{
DWORD dwIPAddr ;
dwIPAddr = inet_addr(IP.c_str());
if(dwIPAddr != INADDR_NONE) //无效得地址
{
PHOSTENT hostinfo;
if((hostinfo = gethostbyaddr((LPSTR)&dwIPAddr, 4, AF_INET)) != NULL)
{
ComputeName = hostinfo->h_name;
}
}
}
return ComputeName;
}
#pragma package(smart_init)