WORD wVersionRequested;
WSADATA wsaData;
char name[255];
CString ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr
_list);
}
}
WSACleanup( ); }
以上程序在win98、win2k下测试通过
注释:name为本机名称,ip为本机ip地址;
WSADATA wsaData;
char name[255];
CString ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr
_list);
}
}
WSACleanup( ); }
以上程序在win98、win2k下测试通过
注释:name为本机名称,ip为本机ip地址;
解决方案 »
- 请问发送UDP数据报时,怎么做可以不发到外网?
- 怎样绘制3ds文件的阴影(开发环境vc++与opengl)
- 请问属性表(CPropertyPage和CPropertySheet)和Tab Control 使用上有什么区别?
- 如何在基于对话框的MFC,程序中动态建一个窗口?
- 请问.H文件和.LIB文件有何不同。
- 背景问题求教
- CSocket长连接问题
- 我写了一个win32 console Application ,但是总是说“Cannot execute program"
- 请教各位,如何给BCG的菜单上加图标呢?好急!!!
- 有兴趣开发解码器的朋友请进——没有几年做下去这个恒心的人请免看!!!
- ADO的参数问题,再次提问!!!给300分!!!
- 谁可以给我一个tab ctrl的例子阿
const char FAR * name
);
头文件:
#ifndef __IPENUM_H__
#define __IPENUM_H__class CIPEnum
{
public:
//constructors / Destructors
CIPEnum();
~CIPEnum();
//methods
BOOL Enumerate();
protected:
virtual BOOL EnumCallbackFunction(int nAdapter, const in_addr& address) = 0;
BOOL m_bWinsockInitialied;
};
#endif
执行文件:
#include "stdafx.h"
#include "ipenum.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CIPEnum::CIPEnum()
{
m_bWinsockInitialied = FALSE; //Initialise the winsock stack
WORD wVersionRequested = MAKEWORD(1, 1);
WSADATA wsaData;
int err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0)
{
TRACE(_T("Failed in call to WSAStartup, return value was %d\n"), err);
AfxThrowNotSupportedException();
} //remember that we have opened winsock
m_bWinsockInitialied = TRUE; //Code requires at least Winsock 1.1
if ((LOBYTE(wsaData.wVersion) != 1) || (HIBYTE(wsaData.wVersion) != 1))
{
TRACE(_T("Failed to find a usable winsock stack which supports Winsock 1.1\n"));
AfxThrowNotSupportedException();
}
}
CIPEnum::~CIPEnum()
{
//release our use of winsock stack if we successfully initialised it
if (m_bWinsockInitialied)
WSACleanup();
}
BOOL CIPEnum::Enumerate()
{
//get this machines host name
char szHostname[256];
if (gethostname(szHostname, sizeof(szHostname)))
{
TRACE(_T("Failed in call to gethostname, WSAGetLastError returns %d\n"), WSAGetLastError());
return FALSE;
} //get host information from the host name
HOSTENT* pHostEnt = gethostbyname(szHostname);
if (pHostEnt == NULL)
{
TRACE(_T("Failed in call to gethostbyname, WSAGetLastError returns %d\n"), WSAGetLastError());
return FALSE;
} //check the length of the IP adress
if (pHostEnt->h_length != 4)
{
TRACE(_T("IP address returned is not 32 bits !!\n"));
return FALSE;
} //call the virtual callback function in a loop
int nAdapter = 0;
BOOL bContinue = TRUE;
while (pHostEnt->h_addr_list[nAdapter] && bContinue)
{
in_addr address;
CopyMemory(&address.S_un.S_addr, pHostEnt->h_addr_list[nAdapter], pHostEnt->h_length);
bContinue = EnumCallbackFunction(nAdapter, address);
nAdapter++;
} return TRUE;
}
GetIpStatistics() 可以得到当前机器的IP信息,其中包括了IP地址信息。