本帖最后由 VisualEleven 于 2012-05-16 23:06:39 编辑

解决方案 »

  1.   

    一个函数,sHostName=主机名,sIPAddress=本机IP
    UnicodToAnsi()将Unicode字串转换为ANSI字串。
    调用例子:GetIPAddress(psend->hostName,psend->hostIP); // 本机IP
    // 取本机IP
    int CUDPcommThread::GetIPAddress(const CString& sHostName, CString& sIPAddress)
    {
    char buf[64];
    memset(buf,0x00,sizeof(buf));
    UnicodToAnsi(sHostName,buf);
    struct hostent FAR *lpHostEnt = gethostbyname (buf); if (lpHostEnt == NULL) {
    // An error occurred. 
    sIPAddress = _T("");
    return WSAGetLastError();
    } LPSTR lpAddr = lpHostEnt->h_addr_list[0];
    if (lpAddr) {
    struct in_addr  inAddr;
    memmove (&inAddr, lpAddr, 4);
    sIPAddress = inet_ntoa (inAddr);
    if (sIPAddress.IsEmpty())
    sIPAddress = _T("Not available");
    }

    return 0;
    }