看拔掉摄像头,获取IP地址如何??

解决方案 »

  1.   

    问题解决了,就是在XP系统上,插上摄像头的时候,获取ip的结果会有错误,过滤掉就行了CString AutoGetIP()
    {
    WORD wVersionRequested;
    WSADATA wsaData;
    char name[255];
    CString ip;
    PHOSTENT hostinfo;
    wVersionRequested = MAKEWORD( 2, 2);
        CString strIP;
    vector<CString> vecStrIP;
    int a=-1,b=-1,c=-1,d=-1;
    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);
    in_addr **ppAddr=(in_addr**)hostinfo->h_addr_list;
    in_addr *pAddr;
    //AllocConsole();
    while (pAddr=*(ppAddr++))
    {
    a=pAddr->S_un.S_un_b.s_b1;
    b=pAddr->S_un.S_un_b.s_b2;
    c=pAddr->S_un.S_un_b.s_b3;
    d=pAddr->S_un.S_un_b.s_b4;
    if (a==0)
    {
    a=-1;b=-1;c=-1;d=-1;
    continue;
    }
    break;
    }
    }
    }
    WSACleanup( );
    }
    ip.Format("%d.%d.%d.%d",a,b,c,d);
    return ip;
    }