这个程序那里为什么不出结果?调通立即结贴,请帮我!谢谢
dszch 2002-11-20 04:14:20 //这个程序结果输出本机的IP地址,可是为什么不显示结果呢?
//请指教,调通立即结贴
//IPenum.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include <winsock.h>
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void print_ip(void)
//只能得到一个IP地址
{
WORD wVersionRequested;
WSADATA wsadata;
char name[255];
CString ip;
PHOSTENT hpstinfo;
wVersionRequested=MAKEWORD(2,0);
if (WSAStartup (wVersionRequested,&wsaData) ==0 )
{
if(GetHostName ( name, sieof(name)) == 0)
{
//如果能够获取计算机主机信息的话,则获取本机IP地址
if ((hostinfo = GetHostByName(name)) != NULL)
{
//获取本机IP地址
LPCSTR ip=inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);
//输出IP地址
printf("%s\n",ip);
}
}
WSACleanup();
}
}
//主函数,调用print_ip()函数,输出本机IP地址
int main(int argc, char* argv[])
{
print_ip();
return 0;
}