描述:
我所知道的几个获得IP的方法都需要MFC支持,或需要windows.h的支持,我现在的COM组件中需要获得IP地址,请大家执教
我用过GetComputerName,试图获得计算机名,可是总是报名称过长的错误,我已经把计算机名改成一个字母了,真弄不明白,帮帮忙啊~~
解决方案1:
char hostname[128];
if(::gethostname(hostname,sizeof(hostname))==0)
{
inaddrs=gethostbyname(hostname); ……}这是最简单的获取ip的方法,并不需要mfc的支持。也不需要windows.h的支持。
//C语言下的,不需要MFC
struct hostent *h;
char host[100];
int i;
gethostname(hostent,100);
if ((h=gethostname(host)) == NULL) {
printf("error:%s!\n",hstrerror(h_errno));
return;
};
printf("default IP:%s\n",inet_ntoa(*(struct in_addr *)h->h_addr));
for (i=0;i<h->h_length/sizeof(int);i++) {
printf("IP %d : %s \n",i+1,inet_ntoa(*(struct in_addr *)h->h_addr_list[i]));
};