更改Mac,禁用启用网卡,查询网卡信息

主要的代码

PIP_ADAPTER_INFO  pAdapterInfo,pAdapter;
 ULONG ulOutBufLen;
 CString strName[10];
 CString strMac[10];
 CString strIP[10];
 CString strGate[10];

void CAdapterDlg::GetInfo()    //获取网卡信息
{
 pAdapterInfo=(PIP_ADAPTER_INFO)malloc(sizeof(IP_ADAPTER_INFO));
 ulOutBufLen = sizeof(IP_ADAPTER_INFO);
 ncount=0;
    DWORD dwRetVal;
    if(GetAdaptersInfo(pAdapterInfo,&ulOutBufLen)==ERROR_BUFFER_OVERFLOW)
 {
 free(pAdapterInfo);
 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
 }
 if((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
 {
 pAdapter = pAdapterInfo;
 while (pAdapter)
 {
 strName[ncount].Format("%s", pAdapter->Description);
 strMac[ncount].Format("%02x%02x%02x%02x%02x%02x",
 pAdapter->Address[0],
 pAdapter->Address[1],
 pAdapter->Address[2],
 pAdapter->Address[3],
 pAdapter->Address[4],
 pAdapter->Address[5]);
 strIP[ncount].Format("%s", pAdapter->IpAddressList.IpAddress.String);
 strGate[ncount].Format("%s", pAdapter->GatewayList.IpAddress.String);
 pAdapter = pAdapter->Next;
 ncount++;
 }// end while   
 }
   else
 {
     AfxMessageBox("获取网卡信息失败!");
 }
}

 

void CAdapterDlg::OnButton2()                                  //通过改写注册表改写Mac
{
 // TODO: Add your control notification handler code here
 HKEY  hKeyRoot = HKEY_LOCAL_MACHINE, hKEY;
 LPCTSTR lpSubKey=_T("NetworkAddress"),strVersionValue,lpSubKey1;
 CString strtemp,strtemp1,getValue,strtemp3;
 CString strUser =_T("DriverDesc");//要查询的键名称
 DWORD keyType = REG_DWORD,DataLen;
 CString strVersion = _T("NetworkAddress");//要写入的键名称
 GetDlgItemText(IDC_EDIT5,strtemp);
    GetDlgItemText(IDC_EDIT1,strtemp3);
 if(strtemp.IsEmpty())
 {
  AfxMessageBox("不能为空值!");
  return;
 }
 strVersionValue=strtemp;
 
 for(int i=1;i<10;i++)
 {
  DataLen = 128;
  strtemp1.Format("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\000%d",i);
  lpSubKey1=strtemp1;
  long ret=::RegOpenKeyEx(hKeyRoot,lpSubKey1,0,KEY_ALL_ACCESS,&hKEY);
  ret=RegQueryValueEx(hKEY,strUser,NULL,NULL,(unsigned char *)getValue.GetBuffer(DataLen),&DataLen);
  getValue.ReleaseBuffer();
  if(0==getValue.Find(strtemp3))
  {
   ret = ::RegSetValueEx(hKEY, strVersion, 0, REG_SZ, (const BYTE *) strVersionValue, strlen(strVersionValue)+1);
   if(ret!=ERROR_SUCCESS)
   {
    AfxMessageBox("错误:无法查询有关的注册表信息");
    RegCloseKey(hKEY);
    return;
   }
   
   break;
  }
  getValue.Empty();
  
 }  
 RegCloseKey(hKEY);
 AfxMessageBox("改写成功!");
}

 

void CAdapterDlg::OnButton3()    //禁用启用网卡,使Mac生效
{
 // TODO: Add your control notification handler code here
 bok=!bok;
    if(!bok)
 {
  SetDlgItemText(IDC_BUTTON3,"启用网卡");
 }
 else
 {
  SetDlgItemText(IDC_BUTTON3,"禁用网卡");
 }
 HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
 hDevInfo = SetupDiGetClassDevs(NULL,NULL,NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
 SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
 LPOLESTR guid;
 char devName[128];
 char instanceId[128];
 for (int i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData);i++)
 {
  StringFromCLSID(DeviceInfoData.ClassGuid,&guid);
  SetupDiClassNameFromGuid(&DeviceInfoData.ClassGuid,devName,128,NULL);
  if(!strcmp(devName,"Net"))
  {
   
   SetupDiGetDeviceInstanceId(hDevInfo,&DeviceInfoData,instanceId,128,NULL);
   if(!strncmp(instanceId,"PCI",3))
   {
    
    SP_PROPCHANGE_PARAMS params = {sizeof(SP_CLASSINSTALL_HEADER )};
    params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
    params.Scope = DICS_FLAG_CONFIGSPECIFIC;
    if(bok)
    {
     params.StateChange = DICS_ENABLE; //禁用:DICS_DISABLE,DICS_ENABLE启用
    }
    else params.StateChange = DICS_DISABLE;
    params.HwProfile = 0;
    SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER*)&params, sizeof(SP_PROPCHANGE_PARAMS));
    SetupDiChangeState(hDevInfo, &DeviceInfoData);
   }
  }
  CoTaskMemFree(guid);
 }
 SetupDiDestroyDeviceInfoList(hDevInfo);
 
}

posted on 2012-02-24 21:45  fanhongyue  阅读(1841)  评论(0编辑  收藏  举报

导航