我想通过VC中MFC封装的函数获取主机信息(主机名,用户名,子网掩码,IP,以及共享的资源),请问都需要哪些函数啊?如果设置这些信息又需要哪些函数呢?希望高手指点一二!先谢谢了!

解决方案 »

  1.   

    //获得本地计算机名称
    int CClientDlg::GetLocalHostName(CString &sHostName)
    {
    char szHostName[256];
    int nRetCode;
    nRetCode=gethostname(szHostName,sizeof(szHostName));
    if(nRetCode!=0)
    {
    //产生错误
    sHostName=_T("没有取得");
    return GetLastError();
    }
    sHostName=szHostName;
    return 0;
    }
    //获得本地IP
    int CClientDlg::GetIpAddress(const CString &sHostName, CString &sIpAddress)
    {
    struct hostent FAR * lpHostEnt=gethostbyname(sHostName);
    if(lpHostEnt==NULL)
    {
    //产生错误
    sIpAddress=_T("");
    return GetLastError();
    }
    //获取IP
    LPSTR lpAddr=lpHostEnt->h_addr_list[0];
    if(lpAddr)
    {
    struct in_addr inAddr;
    memmove(&inAddr,lpAddr,4);
    //转换为标准格式
    sIpAddress=inet_ntoa(inAddr);//把一个inAddr结构转化为点分十进制格式
    if(sIpAddress.IsEmpty())
    sIpAddress=_T("没有取得");
    }
    return 0;
    }
      

  2.   

    同意1樓,用1樓的方法。但是要使用gethostname和gethostbyname,請包含#include <WinSock2.h>,在程序獲取之前加上WSADATA wsd;WSAStartup (MAKEWORD(1, 1), &wsd);之后加上WSACleanup();
      

  3.   

    方法很多呀。试试IP help API。