如何通过WIN API函数获得本机IP地址?

ycrao 2000-09-24 11:30:00
如何通过WIN API函数获得本机IP地址?
...全文
838 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
dusj 2000-09-28
  • 打赏
  • 举报
回复
attention
playpcgame 2000-09-28
  • 打赏
  • 举报
回复
void print_all_ip(void)
{
char szHostName[128];
const char* pszAddr;
struct hostent * pHost;
int i,j;
if( gethostname(szHostName, 128) == 0 )
{
pHost = gethostbyname(szHostName);
for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
{/*对每一个IP地址进行处理*/
pszAddr=inet_ntoa (*(struct in_addr *)pHost->h_addr_list[i]);
printf("%s\n",pszAddr);/*打印*/
}
}
}
NiceFeather 2000-09-28
  • 打赏
  • 举报
回复
请看此页下端的“程序员大本营”的“Visual C++”的“VC源码集合”的“对话框处理”的“Parse IP addresses”样例!
beegee 2000-09-28
  • 打赏
  • 举报
回复
对! 有多个网卡怎么办?
halbert 2000-09-28
  • 打赏
  • 举报
回复
dd
halbert 2000-09-28
  • 打赏
  • 举报
回复
dd
wxz 2000-09-27
  • 打赏
  • 举报
回复
为你一段源码:
#include <winsock.h>
CString *GetLocalIP(int &Count)
{
//该函数返回本机的 IP 地址
CString *Result=NULL;
WSADATA wsaData;
char Name[255];
hostent* hostinfo;
WORD wVersionRequested = MAKEWORD( 2, 2 );
if(WSAStartup(wVersionRequested,&wsaData) == 0)
{
if(gethostname(Name,sizeof(Name))==0)
{
hostinfo = gethostbyname(Name);
if(hostinfo != NULL)
{
char FAR * FAR * Ptr=hostinfo->h_addr_list;
for(Count=0;Ptr[Count];Count++);
Result=new CString[Count+1];
Result[Count]=NULL;
Ptr=hostinfo->h_addr_list;
for(int i=0;i<Count;i++)
{
Result=inet_ntoa(*(struct in_addr*)Ptr[i]);
}
}
}
WSACleanup();
}
return Result;
}
// 谢谢xiaoya,利用了你的代码
wxz 2000-09-27
  • 打赏
  • 举报
回复
为你一段源码:
CString *CNetUsers::GetLocalIP(int &Count)
{
//该函数返回本机的 IP 地址
CString *Result=NULL;
WSADATA wsaData;
char Name[255];
hostent* hostinfo;
WORD wVersionRequested = MAKEWORD( 2, 2 );
if(WSAStartup(wVersionRequested,&wsaData) == 0)
{
if(gethostname(Name,sizeof(Name))==0)
{
hostinfo = gethostbyname(Name);
if(hostinfo != NULL)
{
char FAR * FAR * Ptr=hostinfo->h_addr_list;
for(Count=0;Ptr[Count];Count++);
Result=new CString[Count+1];
Result[Count]=NULL;
Ptr=hostinfo->h_addr_list;
for(int i=0;i<Count;i++)
{
Result=inet_ntoa(*(struct in_addr*)Ptr[i]);
}
}
}
WSACleanup();
}
return Result;
}
// 谢谢xiaoya,利用了你的代码
oldworm 2000-09-25
  • 打赏
  • 举报
回复
如果你的应用程序使用MFC的Socket类在在应用程序开始的时候已经通过检测,这样就可以不要检测socket是否安装。
头文件别忘了添加,头文件随是否使用MFC的socket类而不一样,如果你在使用Wizard建立一个应用程序的时候选择了使用socket则在afx.h中已经包含了AfxSocket.h,如此则也不用添加socket支持类,如果你的应用程序只是使用Api方式的socket来则需要添加该头文件。

sxbyl 2000-09-25
  • 打赏
  • 举报
回复
唉!又来晚了。
maptrix 2000-09-25
  • 打赏
  • 举报
回复
gethostname(char *name,int maxname);
CoolHg 2000-09-25
  • 打赏
  • 举报
回复
有多个网卡多个IP时怎么办
xiaoya 2000-09-24
  • 打赏
  • 举报
回复
呵呵,我是在 Win98 上写的,它们都用 2.2 版
1.1 版是足够了
lgs 2000-09-24
  • 打赏
  • 举报
回复
WORD wVersionRequested = MAKEWORD( 1, 1 );
1.1版已经足够了
xiaoya 2000-09-24
  • 打赏
  • 举报
回复
CString CNetUsers::GetLocalIP()
{
//该函数返回本机的 IP 地址
//通过: 2000。7。13

CString Result = "";
WSADATA wsaData;
char Name[255];
hostent* hostinfo;
WORD wVersionRequested = MAKEWORD( 2, 2 );
if(WSAStartup(wVersionRequested,&wsaData) == 0){
if(gethostname(Name,sizeof(Name))==0){
hostinfo = gethostbyname(Name);
if(hostinfo != NULL)
Result = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
}
WSACleanup();
}
return Result;
}
aznarble 2000-09-24
  • 打赏
  • 举报
回复
Attention.

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧