使用那个函数可以获取计算机的IP地址
解决方案 »
- 把注册表启动项内容复制出来咋做?
- 如何在ansi环境下调用unicode的dll (vc6.0)
- 如何实现向一个窗口中的一个地方发送鼠标点击消息?
- 很奇怪的问题,定义一个类的实例(CCurve * cur =new CCurve),然后给这个实例的一些变量付值
- 为什么我的控件注册不了?
- //请问以上sprintf函数在执行时为什么总是说“访问违归”呢?
- 有没有modem的拨号程序原码,用mfc,mscomm控件写的
- 请问大虾:怎么安装visual studio 7.0?
- 《深入浅出MFC》哪个看过?
- 对话框程序,如何判断自身窗口是否处于激活状态?
- 我该怎么做?
- “虚函数”进来拿分!(第一板块)
char FAR *name,
int namelen
);
char FAR *name,
int namelen
);获取本机机器名,然后使用
INT GetAddressByName(
DWORD dwNameSpace, // name space to query for service address
// information
LPGUID lpServiceType, // the type of the service
LPTSTR lpServiceName, // the name of the service
LPINT lpiProtocols, // points to array of protocol identifiers
DWORD dwResolution, // set of bit flags that specify aspects of
// name resolution
LPSERVICE_ASYNC_INFO lpServiceAsyncInfo,
// reserved for future use, must be NULL
LPVOID lpCsaddrBuffer, // points to buffer to receive address
// information
LPDWORD lpdwBufferLength, // points to variable with address
// buffer size information
LPTSTR lpAliasBuffer, // points to buffer to receive alias
// information
LPDWORD lpdwAliasBufferLength
// points to variable with alias buffer
// size information
);可以获取IP地址!
例子:
void CNetBaseDlg::GetHostName()
{
char * name = new char[255];
gethostname(name, 255); hostent * pHostent = gethostbyname(name);
hostent & he = *pHostent;
sockaddr_in sa;
memcpy(&sa.sin_addr.s_addr, he.h_addr_list[0],he.h_length);
m_strIP = CString(inet_ntoa(sa.sin_addr));//ip地址
}
{
char szHostName[128];
WSADATA wsaData;
struct hostent *pHost = NULL; memset(szHostName, 0, sizeof(szHostName)); WSAStartup(0x0101, &wsaData);
gethostname(szHostName, sizeof(szHostName));
pHost = gethostbyname(szHostName);
LPCSTR lpIP = inet_ntoa(*(struct in_addr *)pHost->h_addr_list[0]); WSACleanup();
return lpIP;
}
WORD wVersionRequested;
WSADATA wsaData;
TCHAR name[255],*ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD(1,1);
if(WSAStartup(wVersionRequested,&wsaData)==0)
{
if(gethostname(name,sizeof(name))==0)
{
if((hostinfo=gethostbyname(name))!= NULL)
{
int nCount=0;
while(hostinfo->h_addr_list[nCount])
{
ip=inet_ntoa(*(struct in_addr*)hostinfo->h_addr_list[nCount]);
++nCount;
}
}
}
}
m_csLocalIP.Format("%s",ip);
gethostname(char FAR *name,int namelen);
struct hostent FAR *gethostbyname(const char FAR *name);
void CXXX::GetIP()
{
char szhostname[128];
CString str;//获得主机名
if(gethostname(szhostname,128)==0)
{
struct hostent *phost;// 获得主机ip地址
int occurred;
phost = gethostbyname(szhostname);
m_hostname = szhostname;
occurred = 0;
int j;
int h_length = 4;
for(j=0;j<h_length;j++)
{
CString addr;
if(j>0)
str +=".";
addr.Format("%u",(unsigned int)unsigned char*)phost->h_addr_list[occurred])[j]);
str+=addr;
}
}
m_ipaddress = str;
UpdateData(FALSE);
}