如何取得本机的ip地址,在线等待。 谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 进入dos命令方式,输入命令:ipconfig /all回车,即可获得本机的ip地址,并能知道本机网卡的物理地址。 Platform SDK: Internet Protocol HelperGetAdaptersInfo()typedef struct _IP_ADAPTER_INFO { struct _IP_ADAPTER_INFO* Next; DWORD ComboIndex; char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4]; char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4]; UINT AddressLength; BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH]; DWORD Index; UINT Type; UINT DhcpEnabled; PIP_ADDR_STRING CurrentIpAddress;//** IP_ADDR_STRING IpAddressList; IP_ADDR_STRING GatewayList; IP_ADDR_STRING DhcpServer; BOOL HaveWins; IP_ADDR_STRING PrimaryWinsServer; IP_ADDR_STRING SecondaryWinsServer; time_t LeaseObtained; time_t LeaseExpires; } IP_ADAPTER_INFO, *PIP_ADAPTER_INFO; 先 gethostname 获得本机机器名,再 gethostbyname 获得IP等信息。 抢分!! 需要ws2_32.lib ,和#include <winsock2.h>CString ip;WORD wVersionRequested; WSADATA wsaData; char name[255]; 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( ); } if (ip.IsEmpty()){ ip = "127.0.0.0";} char csPcName[MAX_PCNAME_LEN]; if (SOCKET_ERROR == gethostname (csPcName, MAX_PCNAME_LEN)) { TRACE0("GetComputerNameError!"); return 0; } hostent* pHst = gethostbyname(csPcName); if ( NULL == pHst ) { TRACE0("GetHostIPAddressByName Error!"); return 0; } DWORD dwIpAddress = *(DWORD*)pHst->h_addr_list[0]; 菜鸟求着急求助 !!!!!!矩形问题 音乐播放不完的问题 DEBUG时出现:there is no source code available for the current location 对scrollView抓图问题 各位大哥进来看看怎么练不上数据库啊!我都试了一天了!! 急!有关CListCtrl的滚动条拖动事件!(在线等待) 如何将CString类型转换为int型? 请问怎么使应用程序全屏显示? 单目运算符“::function()"的用法的迷雾 《深入浅出MFC》Vs《VC++技术内幕》 如何設定DIALOG窗體的彈出位置,請詳細説明,謝謝! MS Word与 IPersist
ipconfig /all
回车,即可获得本机的ip地址,并能知道本机网卡的物理地址。
GetAdaptersInfo()typedef struct _IP_ADAPTER_INFO {
struct _IP_ADAPTER_INFO* Next;
DWORD ComboIndex;
char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
UINT AddressLength;
BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
DWORD Index;
UINT Type;
UINT DhcpEnabled;
PIP_ADDR_STRING CurrentIpAddress;//**
IP_ADDR_STRING IpAddressList;
IP_ADDR_STRING GatewayList;
IP_ADDR_STRING DhcpServer;
BOOL HaveWins;
IP_ADDR_STRING PrimaryWinsServer;
IP_ADDR_STRING SecondaryWinsServer;
time_t LeaseObtained;
time_t LeaseExpires;
} IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;
再 gethostbyname 获得IP等信息。
WORD wVersionRequested;
WSADATA wsaData;
char name[255];
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( );
}
if (ip.IsEmpty())
{
ip = "127.0.0.0";
}
if (SOCKET_ERROR == gethostname (csPcName, MAX_PCNAME_LEN))
{
TRACE0("GetComputerNameError!");
return 0;
}
hostent* pHst = gethostbyname(csPcName);
if ( NULL == pHst )
{
TRACE0("GetHostIPAddressByName Error!");
return 0;
} DWORD dwIpAddress = *(DWORD*)pHst->h_addr_list[0];