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

📄 onetouchdlg.cpp

📁 可以一键自动检测局域网内的网关和未被占用的IP地址
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// onetouchDlg.cpp : implementation file
//
#define _CRTDBG_MAP_ALLOC 
#include<stdlib.h> 
#include<crtdbg.h> 

#include "stdafx.h"
#include "onetouch.h"
#include "onetouchDlg.h"
#include <fstream>
#include <string>
#include <algorithm>
#include <time.h>
#include "ipAddrIp.h"
#include "cmdProcess.h"
//#include   <wininet.h>
#include "Packet32.h"
#include "Ntddndis.h"
#include "Win32-Extensions.h"
//#include <direct.h>
//#include <winhttp.h>


using namespace std;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define   winsock_version   0x0101
#define   conEN   Local Area Connection 
#define   conCH   本地连接
#define   testUrl	"http://www.baidu.com/"//"http://www.cnproxy.com/proxy1.html"//"http://localhost/iishelp/iis/misc/default.asp" //"http://www.cnproxy.com/proxy1.html"
int Base64Encode( char * const aDest, const unsigned char * aSrc, int aLen );
CString tempIp,tempMask,tempGate,tempDns1,tempDns2;
bool httpRetFlag;
CString testProxyServer;
unsigned short testProxyPort;

bool netshInUse = false;
bool testServerInUse = false;
bool ipAutoConfig = false;
CWnd* m_MWnd;
CString sysDir;//系统目录
CString ProgramPath;//当前程序路径	


void EmitErrorMsg (long hr, char* szMsg);
LPBYTE CString_To_LPBYTE(CString str);


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About


class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()





/////////////////////////////////////////////////////////////////////////////
// COnetouchDlg dialog

COnetouchDlg::COnetouchDlg(CWnd* pParent /*=NULL*/)
	: CDialog(COnetouchDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(COnetouchDlg)
	m_port = _T("");
	m_checkPE = FALSE;
	m_addr = _T("");
	m_combo = -1;
	m_userid = _T("");
	m_pwd = _T("");
	m_checkPA = FALSE;
	m_info = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

COnetouchDlg::~COnetouchDlg()
{
//	AfxGetMainWnd()->PostMessage(WM_QUIT); 
//	m_MWnd->SendMessage(WM_CLOSE, 0, 0);
	//m_hWnd->SendMessage(WM_CLOSE,0);
//	CDialog::~CDialog();
}

void COnetouchDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COnetouchDlg)
//	DDX_Control(pDX, IDC_BUTTON2, m_OT);
	DDX_Control(pDX, IDC_EDIT4, m_pwdCtr);
	DDX_Control(pDX, IDC_EDIT3, m_useridCtr);
	DDX_Control(pDX, IDC_GATE, m_gate);
	DDX_Control(pDX, IDC_MASK, m_mask);
	DDX_Control(pDX, IDC_DNS2, m_dns2);
	DDX_Control(pDX, IDC_DNS1, m_dns1);
	DDX_Control(pDX, IDC_IP, m_ip);
	DDX_Control(pDX, IDC_COMBO1, m_comboCtr);
	DDX_Control(pDX, IDC_EDIT2, m_addrCtr);
	DDX_Control(pDX, IDC_EDIT1, m_portCtr);
	DDX_Control(pDX, IDC_CHECK1, m_checkPEC);
	DDX_Text(pDX, IDC_EDIT1, m_port);
	DDX_Check(pDX, IDC_CHECK1, m_checkPE);
	DDX_Text(pDX, IDC_EDIT2, m_addr);
	DDX_CBIndex(pDX, IDC_COMBO1, m_combo);
	DDX_Text(pDX, IDC_EDIT3, m_userid);
	DDX_Text(pDX, IDC_EDIT4, m_pwd);
	DDX_Check(pDX, IDC_CHECK2, m_checkPA);
	DDX_Text(pDX, IDC_EDITinfo, m_info);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(COnetouchDlg, CDialog)
	//{{AFX_MSG_MAP(COnetouchDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1)
	ON_BN_CLICKED(IDC_BUTTONAUTO, OnButtonauto)
	ON_BN_CLICKED(IDC_BUTTONmanual, OnBUTTONmanual)
	ON_BN_CLICKED(IDC_BUTTONinPut, OnBUTTONinPut)
	ON_BN_CLICKED(IDC_BUTTONoutPut, OnBUTTONoutPut)
	ON_MESSAGE(WM_UPDATEDATA, OnUpdateData)
	ON_BN_CLICKED(IDC_CHECK2, OnCheck2)
	ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
	ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
	ON_NOTIFY(IPN_FIELDCHANGED, IDC_IP, OnFieldchangedIp)	
	ON_BN_CLICKED(IDC_BUTTONmanual2, OnBUTTONmanual2)
	ON_WM_DESTROY()
	ON_EN_CHANGE(IDC_EDIT4, OnChangePWD)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COnetouchDlg message handlers

BOOL COnetouchDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	Refresh();
	m_checkPA = false;
	m_useridCtr.EnableWindow(false);
	m_pwdCtr.EnableWindow(false);
   /* m_ip.SetAddress(192,168,1,87);
	m_mask.SetAddress(255,255,255,0);
	m_gate.SetAddress(192,168,1,1);
	m_dns1.SetAddress(192,168,1,1);
	m_dns2.SetAddress(192,168,1,2);*/
	char buffer[128];
	GetSystemDirectory(buffer,128);
	sysDir = buffer;//系统路径	
	/////////////////////////得到当前工作路径 
//	getcwd(ProgramPath.GetBuffer(MAX_PATH),   MAX_PATH); 
	char t[MAX_PATH];
	GetModuleFileName(NULL,   t,   MAX_PATH);//获取可执行程序路径
//	GetModuleFileName(NULL,   ProgramPath.GetBuffer(MAX_PATH),   MAX_PATH);//获取可执行程序路径
//    GetCurrentDirectory(MAX_PATH,ProgramPath.GetBuffer(MAX_PATH));
//    ProgramPath.ReleaseBuffer();//必须,否则后面ProgramPath+"df"出错
	int i=0;
	while(t[i]!=0)
		i++;
	while(i>=0 && t[i]!=92)//'\\'
	{		
		t[i] = 0;
		i--;
	}
	ProgramPath = t;
    m_MWnd=AfxGetMainWnd();
	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void COnetouchDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void COnetouchDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR COnetouchDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void COnetouchDlg::OnOK() 
{//将控件中值写入注册表
	UpdateData(true); //将控件值传至控件变量中
	LPCTSTR data_Set = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
	long ret0 = ::RegOpenKeyEx(HKEY_CURRENT_USER, data_Set, 0, KEY_WRITE, &hKEY);
	if(ret0 != ERROR_SUCCESS)
	{
		char msg[1024];
    	EmitErrorMsg (ret0, msg);
		AfxMessageBox(msg);	
		return;
	}
	//修改DWORD值(ProxyEnable) !!!
	LPBYTE lpb = new BYTE[4];
	lpb[0] = m_checkPE;lpb[1]=0;lpb[2]=0;lpb[3]=0;
	long ret1 = RegSetValueEx(hKEY, "ProxyEnable", NULL, REG_DWORD, lpb, 4);
	if(ret1 != ERROR_SUCCESS)
	{
		char msg[1024];
    	EmitErrorMsg (ret1, msg);
		AfxMessageBox(msg);
		return;
	}
	//修改SZ值(ProxyServer),要先将CString型转换为LPBYTE。
	CString PStemp;
	if(m_port == "" && m_addr != "")
		PStemp = m_addr;
	else if(m_port != "" && m_addr != "")
		PStemp = m_addr+":"+m_port;
	else//删除ProxyServer键
	{	
		long ret3 = ::RegDeleteValue(hKEY, "ProxyServer");
	//	char msg[1024];
    //	EmitErrorMsg (ret3, msg);
	//	AfxMessageBox(msg);
		return;
	}		
	LPBYTE ProxyServer = CString_To_LPBYTE(PStemp);
// DWORD type_1=REG_SZ;
//  DWORD cbData_1=str_owner.GetLength()+1; 
	long ret2 = RegSetValueEx(hKEY, "ProxyServer", NULL, REG_SZ, ProxyServer, PStemp.GetLength()+1);//不存在该键会自动添加
	if(ret2 != ERROR_SUCCESS)
	{
		char msg[1024];
    	EmitErrorMsg (ret2, msg);
		AfxMessageBox(msg);
		return;
	}
	m_info = "代理配置成功,重新启动IE后方可使用.(如代理需验证请在IE弹出的验证框中手动输入)";
	UpdateData(false);
	//	CDialog::OnOK();	
}

void COnetouchDlg::readReg(HKEY Root,LPCTSTR data_Set)
{
//	LPCTSTR data_Set = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
	long ret0 = ::RegOpenKeyEx(Root, data_Set, 0, KEY_READ, &hKEY);
	if(ret0 != ERROR_SUCCESS)
	{
		char msg[1024];
    	EmitErrorMsg (ret0, msg);
		AfxMessageBox(msg);	
		return;
	}
}

void COnetouchDlg::readRegValue2Box()
{	
	//查询ProxyEnable
	DWORD ProxyEnable_Get[1];   //此处必须表示成DWORD数组,why?
	DWORD type_ProxyEnable = REG_DWORD;
	DWORD cbData_1=4; 
	//hKEY为刚才RegOpenKeyEx()函数所打开的键的句柄
	//cbData_1表示预设置的数据长度。
	long ret1 = RegQueryValueEx(hKEY, "ProxyEnable", NULL,&type_ProxyEnable, (LPBYTE)ProxyEnable_Get, &cbData_1);
	if(ret1 != ERROR_SUCCESS)
	{
		char msg[1024];
    	EmitErrorMsg (ret1, msg);
		AfxMessageBox(msg);
		return;
	}
	ProxyEnableFlag = (int)ProxyEnable_Get[0]==1?true:false;
	// 查询ProxyServer
	LPBYTE ProxyServer_Get = new BYTE [80];
	DWORD type_ProxyServer = REG_SZ;
	DWORD cbData_2 = 80;
	long ret2 = ::RegQueryValueEx(hKEY, "ProxyServer", NULL, &type_ProxyServer, ProxyServer_Get, &cbData_2);
	bool proxyFlag = 1;
	if(ret2 != ERROR_SUCCESS)
	{//注册表中没有ProxyServer键
		proxyFlag = 0;
	//	char msg[1024];
    //	EmitErrorMsg (ret2, msg);
	//	AfxMessageBox(msg);	
	//	return;
	}
	if (proxyFlag == 1)
	{
		CString ProxyStr = CString(ProxyServer_Get);
		int len = ProxyStr.GetLength();
		int pos = -1,i,j;
		for(i=len-1;i>=0;i--)
		{
			if(ProxyStr[i] == ':')
			{
				pos = i;
				break;
			}
		}
		if(pos == -1)
			pos = len;
		CString ProxyServer;
		for(i=0;i<pos;i++)
		{
			ProxyServer += ProxyStr[i];
		}
		CString Port;
		for(j=0,i=pos+1;i<len;i++,j++)
		{
			Port += ProxyStr[i];
		}
		m_addr = ProxyServer;
		/*
		//将CString类型转化成IPAddress类型 !!!
		DWORD  dwIP;  
		dwIP  =  inet_addr(ProxyServer);
		unsigned  char  *pIP  =  (unsigned  char*)&dwIP;  
		m_address.SetAddress(*pIP,  *(pIP+1),  *(pIP+2),  *(pIP+3));*/
		m_port = Port;	
	}
	else
	{
	//	m_address.SetAddress(NULL);
		m_addr = "";
		m_port = "";		
	}
	if (m_checkPE = ProxyEnableFlag)
		m_checkPEC.SetCheck(true);
	else
	{
		m_checkPEC.SetCheck(false);
		//m_checkPEC.EnableWindow(0);
		m_addrCtr.EnableWindow(0);
		m_portCtr.EnableWindow(0);
	}
	UpdateData(false);
	delete[] ProxyServer_Get;
	// 程序结束前要关闭已经打开的 hKEY。
	RegCloseKey(hKEY);
}

void COnetouchDlg::OnCheck1() 
{
	if(m_checkPE == false)
	{	
		//checkBox2.Enabled = true;
		m_checkPE = true;
		m_addrCtr.EnableWindow(true);
		m_portCtr.EnableWindow(true);
	}
	else
	{	
		//	checkBox2.Enabled = false;
		m_checkPE = false;
		m_addrCtr.EnableWindow(false);
		m_portCtr.EnableWindow(false);

⌨️ 快捷键说明

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