API函數(shù)得到本機的IP

字號:

struct hostent *thisHost;
    struct in_addr in;
    char MyName[80];
    char *IP,*ptr;
    if(gethostname (MyName,80)==SOCKET_ERROR)
    return NULL;
    if(!(thisHost = gethostbyname(MyName)))
    return NULL;
    memset((void *)&in,sizeof(in),0);
    in.s_addr=*((unsigned long *)thisHost->h_addr_list[0]);
    if(!(ptr = inet_ntoa(in)))
    return NULL;
    IP = new char[strlen(ptr)+1],
    strcpy(IP,ptr);
    return IP;考試大(www.Examda。com)
    考試大提示:是在vc下編的,而這兩個函數(shù)是api函數(shù),delphi應該也能用。