读书人

怎样获得本机所有网卡的信息啊有api函

发布时间: 2012-03-13 11:21:12 作者: rapoo

怎样获得本机所有网卡的信息啊?有api函数吗?
怎样获得本机所有网卡的信息啊?有api函数吗?

[解决办法]
IPHELPER GetAdapterInfo()
[解决办法]
struct hostent *hp;

gethostname(LocalName,128);
hp = gethostbyname(LocalName);

for(int i=0; hp!=NULL && hp->h_addr_list[i]!=NULL; i++)
{
.....
//inet_ntoa(*(struct in_addr *)hp->h_addr_list[i])可以得到第i个网卡的IP地址.

}

hp里存着你所有网卡的信息.你挨个处理吧
[解决办法]
我觉得这个问题去下载个cnpacks比较好。里面有详细的吧。
[解决办法]

C/C++ code
CString  GetMacByCmd(){    //command buf size    const long lBufSize = 4096;    //command line string    char szFetCmd[] = "ipconfig /all";    //Search string    string str4Search = "Physical Address. . . . . . . . . : ";    CString strRet = _T("");    BOOL bRet;    SECURITY_ATTRIBUTES sa;     HANDLE hReadPipe,hWritePipe;    //Init        sa.nLength = sizeof(SECURITY_ATTRIBUTES);     sa.lpSecurityDescriptor = NULL;     sa.bInheritHandle = TRUE;      //Create pipe    bRet = CreatePipe(&hReadPipe, &hWritePipe, &sa, 0);    if(FALSE == bRet)    {        return strRet;    }    //Get infomation    STARTUPINFO si;    PROCESS_INFORMATION pi;    si.cb = sizeof(STARTUPINFO);     GetStartupInfo(&si);    si.hStdError = hWritePipe;     si.hStdOutput = hWritePipe;     si.wShowWindow = SW_HIDE; //Hide command windows    si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;    //Create command process    bRet = CreateProcess (NULL, szFetCmd, NULL, NULL, TRUE, 0, NULL,                           NULL, &si, &pi );        char szBuffer[lBufSize + 1];    string strBuffer;    if (TRUE == bRet)     {         WaitForSingleObject (pi.hProcess, INFINITE);                     //Read infomation        unsigned long count;        memset(szBuffer, 0x00, sizeof(szBuffer));        bRet  =  ReadFile(hReadPipe,  szBuffer,  lBufSize,  &count,  0);        if(TRUE == bRet)        {            strBuffer = szBuffer;            long ipos;            ipos = strBuffer.find(str4Search);            //Get adress infomation            strBuffer = strBuffer.substr(ipos+str4Search.length());            ipos = strBuffer.find("\n");            strBuffer = strBuffer.substr(0, ipos);                        strRet.Format("%s", strBuffer.c_str());             //remove“00-50-EB-0F-27-82”'-' get 0050EB0F2782            strRet.Remove('-');            strRet = strRet.Left(strRet.GetLength() - 2 );        }    }        //close handle    CloseHandle(hWritePipe);    CloseHandle(pi.hProcess);     CloseHandle(pi.hThread);     CloseHandle(hReadPipe);    return strRet;} 

读书人网 >VC/MFC

热点推荐