Windows中通过ipconfig获得网卡信息

我们在网络程序设计中有时可能会用MAC地址,如果我们用VC++的话可以用UCHAR Netbios(
  PNCB
pncb   /* pointer to the network control block*/)获取;但是我在实际的应用过程中发现它并不是每次都管用的.在一个有无线网卡的笔记本上,我一个装有虚拟机的笔记本上都现我了获取不到MAC的情况,也许我对这个函数的用法没有深入了解吧!

 我借用了JAVA程序在通用的一种获取MAC的方法,借用IPCONFIG来获得所有网卡的信息。下面我们就来看一下具体方法。

 首先我们定义一个结构用于保存网卡信息:

typedef struct _tagAdapterInfo{
 CString Description;//网卡描述
 CString PhysicalAddr;//MAC
 CString IpAddr[64];//IP地址最多64个
 CString SubnetMask[64];//每个IP对的掩码最多64个
 CString Gateway;//默认网关
 LONG lIpNum;//网卡对应的IP数
}AdapterInfo,*PAdapterInfo;

  我们定义一个获得本机所有网卡信息的方法:

int GetAllAdapterInfo(CArray<AdapterInfo,AdapterInfo>& AdapterInfoList,

CString& Error);

下面让我们来看一下实现:

int CTestDlg::GetAllAdapterInfo(

CArray<AdapterInfo,AdapterInfo>& AdapterInfoList,CString Error)
{
 HANDLE hRead,hWrite;//定义输入输出句柄
 CString strError;//错误信息
 CString strCon,strBuf;
 BOOL nReturn;
 char buffer[2048]={0};
 LPSTR p="ipconfig /all";
 unsigned long lnReadNum;
 int nIndex=0,from=0,to=0,nLoop=0;
 STARTUPINFO  sa;
 PROCESS_INFORMATION pinfo;
 SECURITY_ATTRIBUTES saPipe;
 saPipe.nLength= sizeof( SECURITY_ATTRIBUTES );
 saPipe.lpSecurityDescriptor = NULL;
 saPipe.bInheritHandle       = TRUE; 
 if (!CreatePipe(&hRead,&hWrite,&saPipe,0))//创建管道
 {
      strError.Format("CREATE PIPE ERRORCODE:%d",GetLastError());
      Error=strError;
      return 1;
 }
 memset( &sa, 0, sizeof(sa));
 sa.cb=sizeof(sa);
 sa.dwFlags=STARTF_USESHOWWINDOW |STARTF_USESTDHANDLES;
 sa.wShowWindow = SW_HIDE;
 sa.hStdOutput=hWrite;
 sa.hStdError=hWrite;

 //建立进程
 nReturn=::CreateProcess(NULL,p,&saPipe,&saPipe,
  TRUE,0,NULL,NULL,&sa,&pinfo);
 if (!nReturn)
 {
  strError.Format("CREATE PROCESS ERRORCODE:%d",GetLastError());
  Error=strError;
  return 2;
 }
 //Sleep(1000);
 CloseHandle( pinfo.hThread );
    CloseHandle( pinfo.hProcess );
 CloseHandle( hWrite);
//读即管道的输出
 do{
  memset(buffer,0,2048);
  nReturn=ReadFile(hRead,buffer,MAX_PATH,&lnReadNum,NULL);
  if (nReturn)
  {
   strBuf.Format("%s",buffer);
   strCon+=strBuf; 
  }
  
 }while(nReturn&&lnReadNum);

  AdapterInfo info;

//输出信息里查找网卡信息

 do
 {
  nIndex=strCon.Find("Description",nIndex);
  if (nIndex<=-1)
  {
   break;
  }
  from=strCon.Find(":",nIndex)+2;
  to=strCon.Find("/n",from);
  info.Description=strCon.Mid(from,to-from-1);
  nIndex=to;
  nIndex=strCon.Find("Physical Address",nIndex);
  from=strCon.Find(":",nIndex)+2;
  to=strCon.Find("/n",from);
  info.PhysicalAddr=strCon.Mid(from,to-from-1);
  nIndex=to;
  nLoop=0;
  while(nLoop<64)
  {
   nIndex=strCon.Find("IP Address",nIndex);
   if (nIndex<=-1)
   {
    break;
   }
   from=strCon.Find(":",nIndex)+2;
   to=strCon.Find("/n",from);
   info.IpAddr[nLoop]=strCon.Mid(from,to-from-1);
   nIndex=to;
   nIndex=strCon.Find("Subnet Mask",nIndex);
   from=strCon.Find(":",nIndex)+2;
   to=strCon.Find("/n",from);
   info.SubnetMask[nLoop]=strCon.Mid(from,to-from-1);
   nLoop++;
  }
  info.lIpNum=nLoop;
  nIndex=to;
  nIndex=strCon.Find("Default Gateway",nIndex);
  from=strCon.Find(":",nIndex)+2;
  to=strCon.Find("/n",from);
  info.Gateway=strCon.Mid(from,to-from-1);
  AdapterInfoList.Add(info);
 }while(TRUE);
 return 0;
}
方法只是管道的一种最基本的应用,拿来与大家分享一下!另外我们还可以用IP帮助函数来获得IP


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值