VC++修改DNS

         做了一个修改DNS的工具 ,主要是用ShellExecute()调用Dos命令。下面列出主要的部分,我把它放在了一个线程里。

  WORD WINAPI  CDNSDlg::ThreadFun(LPVOID lpParam)
{
  
        CString temp;
char err[1024]={0};
FILE *fp;
///////////////////
fp = fopen("c:\\Myinfo.txt","w");
if(fp == NULL) return FALSE;
fclose(fp);
       ///////////////////////////////////////
CString str = "/c netsh interface ip set dns name =\"本地连接\" source=static addr=8.8.8.8 register=primary >>c:\\Myinfo.txt";
        ShellExecute(NULL, "open", "cmd.exe", str, NULL, SW_HIDE);
ShellExecute(NULL,"open","cmd.exe","/c ipconfig/flushdns > C:\\Myinfo.txt",NULL,SW_HIDE);


Err.Empty();
temp.Empty();
memset(err,0,sizeof(err));
OPEN:
fp = fopen("c:\\Myinfo.txt","r");
if(fp == NULL)
{
       goto OPEN;
return 0;
}
Start:
while(!feof(fp))
{
fgets(err,255,fp);
if(strlen(err)!=1)
{

                        temp = err;
Err = Err+temp;


}
}
fflush(fp);

 if(Err.GetLength()==0)
{

 fseek(fp,0,SEEK_SET);
                 goto Start;
         return 0;
}
 else
 {

         CDNSDlg *dlg;
dlg = (CDNSDlg*)lpParam;
if (Err.Find("因为另一配置")!=-1)
{
dlg->MessageBox("操作失败,"+Err,"提示",MB_OK);
dlg->m_str ="修改失败";
dlg->UpdateData(false);
return 0;
}
dlg->m_str = "修改成功";
dlg->UpdateData(false);
dlg->MessageBox("修改成功","提示",MB_OK);
    
 
 }
  return 0;
}

你可能感兴趣的:(vc++,dns)