⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 onetouchdlg.cpp

📁 可以一键自动检测局域网内的网关和未被占用的IP地址
💻 CPP
📖 第 1 页 / 共 5 页
字号:
		return 0;
	}	
	CloseHandle(hWrite);
	s="";
	while (true) {
		if (ReadFile(hRead,buffer,4095,&bytesRead,NULL) == NULL)
			break;
		buffer[bytesRead] = 0;
		s+=buffer;
		//     Sleep(200); 
	} 
	s.TrimRight();
	if(s!="确定。" && s!="" && s!="Ok.")
		::AfxMessageBox(s);
	else
	{
		p->m_info = "首选DNS服务器配置成功!";
		m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
	}
	/////////////////                   Dns2
		strCommand=sysDir+"/netsh interface ip add dns name = \"本地连接\" addr= ";
		strCommand+="202.119.32.12";
        strChar=strCommand.GetBuffer(strCommand.GetLength());
		strCommand.ReleaseBuffer();
		if (!CreatePipe(&hRead,&hWrite,&sa,0)) {//重新创建管道
		AfxMessageBox("Error On CreatePipe()");
		netshInUse = false;
		ipAutoConfig = false;
		return 0;
		} 
		si.hStdError = hWrite;          
		si.hStdOutput = hWrite;
		p->m_info = "正在配置备选DNS服务器......";
		m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
		if (!CreateProcess(NULL,strChar
			,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi)) {
			AfxMessageBox("Error on CreateProcess()");
			netshInUse = false;
			ipAutoConfig = false;
			return 0;
		}
		CloseHandle(hWrite);
		s="";
		while (true) {
			if (ReadFile(hRead,buffer,4095,&bytesRead,NULL) == NULL)
			break;
			buffer[bytesRead] = 0; 
			s+=buffer;
		 //     Sleep(200); 
		} 
		s.TrimRight();
		if(s!="确定。" && s!="" && s!="Ok.")
			::AfxMessageBox(s);
		else
		{
			p->m_info = "备选DNS服务器配置成功!";
			m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
		}		
		p->m_info = "自动配置完成!";
		m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
		
		netshInUse = false;
		ipAutoConfig = false;
		return 0;		
}


UINT COnetouchDlg::arpListenThread( LPVOID pParam )
{
	//////////////////////获取arp 响应包
	/////////////compile the filter
	
	//pcap_dispatch(adhandle, 200, arpReply_handle, NULL);		
	struct pcap_pkthdr *header;
	const u_char *pkt_data;
	ip_address replyIP;
	mac replyMAC;
	long start = clock();
	int res=0;
	pcap_t * adhandle = (pcap_t*)pParam;
    while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0)
	{ 
		if((double)(clock()-start)/CLK_TCK > 2)//监听两秒钟
			 break;

 		if(res == 0)            // 超时时间到 
            continue;   
		 if (*(unsigned short*)(pkt_data+12) == htons(0x0806) 
			 &&*(unsigned  short*)(pkt_data+20) == htons(0x02) 
			 &&* (unsigned  long*)(pkt_data+38) == inet_addr("127.0.0.1"))
		 {
			 replyIP.byte1 = *(u_char*)(pkt_data+28);
			 replyIP.byte2 = *(u_char*)(pkt_data+29);
			 replyIP.byte3 = *(u_char*)(pkt_data+30);
			 replyIP.byte4 = *(u_char*)(pkt_data+31);
			 repIP_List.push_back(replyIP);
			 replyMAC.b1 = *(u_char*)(pkt_data+22);
			 replyMAC.b2 = *(u_char*)(pkt_data+23);
			 replyMAC.b3 = *(u_char*)(pkt_data+24);
			 replyMAC.b4 = *(u_char*)(pkt_data+25);
			 replyMAC.b5 = *(u_char*)(pkt_data+26);
			 replyMAC.b6 = *(u_char*)(pkt_data+27);
			 repMAC_List.push_back(replyMAC);
		 }		 
    }    
    if(res == -1){
        printf("Error reading the packets: %s\n", pcap_geterr(adhandle));
        return -1;
    }

	return 0;
}


UINT COnetouchDlg::inPutThread( LPVOID pParam )
{
	COnetouchDlg* p = (COnetouchDlg*)pParam;//p访问OnetouchDlg的变量	
	SECURITY_ATTRIBUTES sa;
	HANDLE hRead,hWrite;
	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	sa.nLength = sizeof(SECURITY_ATTRIBUTES);
	sa.lpSecurityDescriptor = NULL;
	sa.bInheritHandle = TRUE;
	GetStartupInfo(&si);
	si.cb = sizeof(STARTUPINFO);
	si.wShowWindow = SW_HIDE;
	si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;	
	CString strPath;
	DWORD bytesRead;
	CString cs;  
	char buffer[4096] = {0}; 

//	AfxMessageBox(ProgramPath);
	CFileDialog fDlg(TRUE,NULL, "ot.ini", 
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"OneTouch Config(*.ini)|*.ini|All files(*.*)|*.*||",NULL);
	fDlg.m_ofn.lpstrInitialDir = ProgramPath;
	fDlg.m_ofn.lpstrTitle = "导入";
	if(fDlg.DoModal()!=IDOK)
	{
		netshInUse = false;
		return 0;
	}
	else 
		strPath=fDlg.GetPathName();
	CFile mFile; 
	///////////////////////////////////////////
	//对文件进行读操作 读取其中的配置消息
    cs ="";
	char text[256] = {0};
	if(mFile.Open(strPath,CFile::modeRead)==0) 
	{
		::AfxMessageBox("打开配置文件失败!");
		netshInUse = false;
		return 0;
	}
	int countOfFile;
	while(true)
	{
		countOfFile=mFile.Read(text,255);  //255
		text[countOfFile] = 0;   //
		cs+=text;
		if(countOfFile<255)
			break;
	}
	mFile.Close(); 
	///得到网络地址
	CString strCommand=sysDir+"/netsh.exe interface ip "; 
    int tempPos=0;
	int position=cs.Find("本地连接");
	int position1=cs.Find("local aera connection");
	if(position<0 && position1<0)
	{
		::AfxMessageBox("没有找到\"本地连接\"(local aera connection),请确认连接名是否合法。");
		netshInUse = false;
		return 0;
	}
	CString outcs;

		if(position == -1)
			position = position1;
		cs.Delete(0,position);
		position=cs.Find("set");		
		if(position<0)
		{
			netshInUse = false;
			return 0;
		}
		cs.Delete(0,position);
		position=cs.Find("mask");	
		if(position == -1)//dhcp自动获得
		{
			tempPos = cs.Find("set dns");
			strCommand+=cs.Left(tempPos-2);
			cs.Delete(0,tempPos);
		}
		else{
		strCommand+=cs.Left(position);
		cs.Delete(0,position);
		position=cs.Find("gateway");
		if(position == -1)
		{//没有网关
			tempPos = cs.Find("set dns");
			strCommand+=cs.Left(tempPos-2);
			cs.Delete(0,tempPos);
		}
		else
		{//包含网关
			tempPos=cs.Find("set address");
			strCommand+=cs.Left(tempPos-2);
			strCommand+=" ";
			cs.Delete(0,position);
			tempPos=cs.Find("set dns");
			strCommand+=cs.Left(tempPos-2);
			cs.Delete(0,tempPos);
		}
		}
		char * charCommand=strCommand.GetBuffer(strCommand.GetLength());
		strCommand.ReleaseBuffer();
		if (!CreatePipe(&hRead,&hWrite,&sa,0)) {//创建管道
			AfxMessageBox("Error On CreatePipe()");
			netshInUse = false;
			return 0;
		} 
		si.hStdError = hWrite; 
		si.hStdOutput = hWrite;	
		p->m_info = "正在配置IP(网关)地址..";
		m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
		if (!CreateProcess(NULL,charCommand
			,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi)) {
			AfxMessageBox("Error on CreateProcess()");
			netshInUse = false;
			return 0;
		}
		CloseHandle(hWrite);
		outcs = "";
		while (true) {//读取管道信息(即netsh返回信息)
			if (ReadFile(hRead,buffer,4095,&bytesRead,NULL) == NULL)
				break;
			buffer[bytesRead] = 0; 
			outcs+=buffer;
			//	Sleep(200); 
		}
		outcs.TrimRight();
		if(outcs!="确定。" && outcs!="" && outcs!="Ok.")
			::AfxMessageBox(outcs);
		else
		{
		//	AfxMessageBox("IP(网关)地址配置完成!");
			p->m_info = "IP(网关)地址配置成功...";
			m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
		}
/////////////////////////////////////////////////////////////////
//////////dns1
	strCommand=sysDir+"/netsh.exe interface ip ";
	position=cs.Find("set");
	tempPos=cs.Find("add dns");
	if(tempPos < 0)
		tempPos=cs.Find("set wins");
	strCommand+=cs.Left(tempPos-2);
	cs.Delete(0,tempPos);
	charCommand=strCommand.GetBuffer(strCommand.GetLength());
	strCommand.ReleaseBuffer();
//	::AfxMessageBox(strCommand);///2
	if (!CreatePipe(&hRead,&hWrite,&sa,0)) {//创建管道
		AfxMessageBox("Error On CreatePipe()");
		netshInUse = false;
		return 0;
		} 
	si.hStdError = hWrite;          
	si.hStdOutput = hWrite;	
	p->m_info = "正在配置首选DNS服务器....";
	m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
	if (!CreateProcess(NULL,charCommand
		,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi)) {
		AfxMessageBox("Error on CreateProcess()");
		netshInUse = false;
		return 0;
	}
	CloseHandle(hWrite);
	outcs = "";
	while (true) {//读取管道信息(即netsh返回信息)
		if (ReadFile(hRead,buffer,4095,&bytesRead,NULL) == NULL)
			break;
		buffer[bytesRead] = 0; 
		outcs+=buffer;
		//	Sleep(200); 
	}
	outcs.TrimRight();
	if(outcs!="确定。" && outcs!="" && outcs!="Ok.")
		::AfxMessageBox(outcs);
	else			
	{
	//	AfxMessageBox("首选DNS地址配置完成!");	
		p->m_info = "首选DNS服务器配置成功!";
		m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
	}
/////////////////////////////////////////////////////////////////
//////////dns2
	tempPos=cs.Find("add dns");
	if(tempPos!=-1)
{
	
	strCommand=sysDir+"/netsh.exe interface ip ";
	position=cs.Find("set wins");		
	strCommand+=cs.Left(position-2);
	cs.Delete(0,position);
	charCommand=strCommand.GetBuffer(strCommand.GetLength());
	strCommand.ReleaseBuffer();
//	::AfxMessageBox(strCommand);///2
	if (!CreatePipe(&hRead,&hWrite,&sa,0)) {//创建管道
		AfxMessageBox("Error On CreatePipe()");
		netshInUse = false;
		return 0;
	} 
	si.hStdError = hWrite;          
	si.hStdOutput = hWrite;	
	p->m_info = "正在配置备选DNS服务器......";
	m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
	if (!CreateProcess(NULL,charCommand
		,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi)) {
		AfxMessageBox("Error on CreateProcess()");
		netshInUse = false;
		return 0;
	}
	CloseHandle(hWrite);
	outcs = "";
	while (true) {//读取管道信息(即netsh返回信息)
		if (ReadFile(hRead,buffer,4095,&bytesRead,NULL) == NULL)
			break;
		buffer[bytesRead] = 0; 
		outcs+=buffer;
		//	Sleep(200); 
	}
	outcs.TrimRight();
	if(outcs!="确定。" && outcs!="" && outcs!="Ok.")
		::AfxMessageBox(outcs);
	else	
	{
		p->m_info = "备选DNS服务器配置成功!";
		m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
	}
}
	//////////////////////////////代理	
	position=cs.Find("ProxyEnable");
	p->m_checkPE = cs[position+12]=='0'?0:1;//
	if(p->m_checkPE)
	{
		p->m_addrCtr.EnableWindow(true);
		p->m_portCtr.EnableWindow(true);
	}
	else
	{
		p->m_addrCtr.EnableWindow(false);
		p->m_portCtr.EnableWindow(false);
	}
	cs.Delete(0,position+26);
//	cs.Delete(0,20);
	position=cs.Find("Port");
	if(position < 2)
		p->m_addr = "";
	else 
		p->m_addr = cs.Left(position-1);
	cs.Delete(0,position+5);
	position=cs.Find("AUEnable");
	if(position < 3)
		p->m_port = "";
	else
		p->m_port = cs.Left(position-2);
	p->m_checkPA = cs[position+9]=='0'?0:1;
	if(p->m_checkPA)
	{
		p->m_useridCtr.EnableWindow(true);
		p->m_pwdCtr.EnableWindow(true);
	}
	else
	{
		p->m_useridCtr.EnableWindow(false);
		p->m_pwdCtr.EnableWindow(false);
	}
	cs.Delete(0,position+14);	
	position=cs.Find("PWD");
	if(position < 2)
		p->m_userid = "";
	else
		p->m_userid = cs.Left(position-1);
	cs.Delete(0,position+4);
	position = cs.Find("END");
	if(position < 2)
		p->m_pwd = "";
	else
		p->m_pwd = cs.Left(position-1);	

	p->m_info = "导入配置完成!";
	m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
	////代理写入注册表
	HKEY Key;
	LPCTSTR data_Set = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";

	long ret0 = ::RegOpenKeyEx(HKEY_CURRENT_USER, data_Set, 0, KEY_WRITE, &Key);
	if(ret0 != ERROR_SUCCESS)
	{
		char msg[1024];
    	EmitErrorMsg (ret0, msg);
		AfxMessageBox(msg);	
		return 0;
	}
	//修改DWORD值(ProxyEnable) !!!
	LPBYTE lpb = new BYTE[4];
	lpb[0] = p->m_checkPE;lpb[1]=0;lpb[2]=0;lpb[3]=0;
	long ret1 = RegSetValueEx(Key, "ProxyEnable", NULL, REG_DWORD, lpb, 4);
	if(ret1 != ERROR_SUCCESS)
	{
		char msg[1024];
    	EmitErrorMsg (ret1, msg);
		AfxMessageBox(msg);
		return 0;
	}
	//修改SZ值(ProxyServer),要先将CString型转换为LPBYTE。
	CString PStemp;
	if(p->m_port == "" && p->m_addr != "")
		PStemp = p->m_addr;
	else if(p->m_port != "" && p->m_addr != "")
		PStemp = p->m_addr+":"+p->m_port;
	else//删除ProxyServer键
	{	
		long ret3 = RegDeleteValue(Key, "ProxyServer");
	//	char msg[1024];
    //	EmitErrorMsg (ret3, msg);
	//	AfxMessageBox(msg);
		return 0;
	}		
	LPBYTE ProxyServer = CString_To_LPBYTE(PStemp);
	long ret2 = RegSetValueEx(Key, "ProxyServer", NULL, REG_SZ, ProxyServer, PStemp.GetLength()+1);//不存在该键会自动添加
	if(ret2 != ERROR_SUCCESS)
	{
		char msg[1024];
    	EmitErrorMsg (ret2, msg);
		AfxMessageBox(msg);
		return 0;
	}
/////
	netshInUse = false;
	return 0;
}

UINT COnetouchDlg::manualThread( LPVOID pParam )
{
	COnetouchDlg* p = (COnetouchDlg*)pParam;//p访问OnetouchDlg的变量	
	SECURITY_ATTRIBUTES sa;
	HANDLE hRead,hWrite;
	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	sa.nLength = sizeof(SECURITY_ATTRIBUTES);
	sa.lpSecurityDescriptor = NULL;
	sa.bInheritHandle = TRUE;
	GetStartupInfo(&si);
	si.cb = sizeof(STARTUPINFO);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -