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

📄 dynamicipdlg.cpp

📁 动态IP地址.rar
💻 CPP
字号:
// DynamicIPDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DynamicIP.h"
#include "DynamicIPDlg.h"
#include "ftp.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CDynamicIPDlg dialog

CDynamicIPDlg::CDynamicIPDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDynamicIPDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDynamicIPDlg)
	m_txtServer = _T("");
	m_txtUsername = _T("");
	m_txtPassword = _T("");
	m_txtOnline = _T("");
	m_txtOffline = _T("");
	m_bBinary1 = FALSE;
	m_bBinary2 = FALSE;
	m_txtRemoteDir = _T("");
	m_txtRemoteFileName = _T("");
	m_txtLocalIP = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	//Load in the values from the INI file
	CWinApp* pApp = AfxGetApp();
	CString sSection(_T("General"));
	m_txtServer = pApp->GetProfileString(sSection, _T("FTPServer"));
	m_txtUsername = pApp->GetProfileString(sSection, _T("Username"));
	m_txtPassword = pApp->GetProfileString(sSection, _T("Password"));
	m_txtOnline = pApp->GetProfileString(sSection, _T("OnlineFilenName"));
	m_txtOffline = pApp->GetProfileString(sSection, _T("OfflineFileName"));
	m_txtRemoteDir = pApp->GetProfileString(sSection, _T("RemoteDirName"));
	m_txtRemoteFileName = pApp->GetProfileString(sSection, _T("RemoteFileName"));
	m_bBinary1 = pApp->GetProfileInt(sSection, _T("OnlineBinary"), 1);
	m_bBinary2 = pApp->GetProfileInt(sSection, _T("OfflineBinary"), 1);
}

void CDynamicIPDlg::DoDataExchange(CDataExchange* pDX)
{
	// Put conversion routines from class object to
	// dialog box on this side
	//if(!pDX->m_bSaveAndValidate){
	// .....

	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDynamicIPDlg)
	DDX_Text(pDX, IDC_SERVER, m_txtServer);
	DDX_Text(pDX, IDC_USERNAME, m_txtUsername);
	DDX_Text(pDX, IDC_PASSWORD, m_txtPassword);
	DDX_Text(pDX, IDC_ONLINE, m_txtOnline);
	DDX_Text(pDX, IDC_OFFLINE, m_txtOffline);
	DDX_Check(pDX, IDC_CHECK1, m_bBinary1);
	DDX_Check(pDX, IDC_CHECK2, m_bBinary2);
	DDX_Text(pDX, IDC_REMOTEDIR, m_txtRemoteDir);
	DDX_Text(pDX, IDC_REMOTEFNAME, m_txtRemoteFileName);
	DDX_LBString(pDX, IDC_LOCALIP, m_txtLocalIP);
	//}}AFX_DATA_MAP
	
	// Put conversion routines from dialog to 
	// class object on this side
	//if(pDX->m_bSaveAndValidate){
	// ....
}

BEGIN_MESSAGE_MAP(CDynamicIPDlg, CDialog)
	//{{AFX_MSG_MAP(CDynamicIPDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_RESET, OnReset)
	ON_BN_CLICKED(IDC_SNDOFFLINE, OnSndoffline)
	ON_BN_CLICKED(IDC_SNDONLINE, OnSndonline)
	ON_BN_CLICKED(IDC_GETOFILE1, OnGetofile1)
	ON_BN_CLICKED(IDC_GETOFILE2, OnGetofile2)
	ON_WM_DESTROY()
	ON_LBN_SELCHANGE(IDC_LOCALIP, OnSelchangeLocalip)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDynamicIPDlg message handlers

BOOL CDynamicIPDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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->DeleteMenu(0, MF_BYPOSITION);	// Delete Restore menu item
			pSysMenu->DeleteMenu(1, MF_BYPOSITION);	// Delete Size menu item
			pSysMenu->DeleteMenu(2, MF_BYPOSITION);	// Delete Maximize menu item
			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

	// Add tooltips to the dialog
	m_wndToolTip.Create(this);
	m_wndToolTip.Activate(TRUE); //c_bShowToolTips);
	CWnd *pWndChild = GetWindow(GW_CHILD);

	CString strToolTip;
	while (pWndChild){
		int nID = pWndChild->GetDlgCtrlID();
		if (strToolTip.LoadString(nID)){
			m_wndToolTip.AddTool(pWndChild, strToolTip);
		}
		pWndChild = pWndChild->GetWindow(GW_HWNDNEXT);
	}

	// Get local host IP address & add to list box
	OnReset();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDynamicIPDlg::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 CDynamicIPDlg::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 CDynamicIPDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL CDynamicIPDlg::PreTranslateMessage(MSG *pMsg)
{
	MSG msg;

	if (//c_bShowToolTips &&
		pMsg->message >= WM_MOUSEFIRST &&
		pMsg->message <= WM_MOUSELAST){

		::CopyMemory(&msg, pMsg, sizeof(MSG));
		HWND hWndParent = ::GetParent(msg.hwnd);
		while (hWndParent && hWndParent != m_hWnd){
			msg.hwnd = hWndParent;
			hWndParent = ::GetParent(hWndParent);
		}
		if (msg.hwnd){
			m_wndToolTip.RelayEvent(&msg);
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CDynamicIPDlg::OnSelchangeLocalip() 
{
	// TODO: Add your control notification handler code here
	char szBuff[30];

	CListBox *plstBox = (CListBox *)GetDlgItem(IDC_LOCALIP);
	plstBox->GetText(plstBox->GetCurSel(),m_txtLocalIP);
	wsprintf(szBuff, "DynamicIP - %s", m_txtLocalIP);
	SetWindowText(szBuff);
}

void CDynamicIPDlg::OnReset() 
{
	// TODO: Add your control notification handler code here
	
	char szHostName[128];
	struct hostent * pHost;	
	CString str, addr;	
	int i, j;  		
	
	CListBox *plstBox = (CListBox *)GetDlgItem(IDC_LOCALIP);
	// Remove all items from the list box
	plstBox->ResetContent();

	// Get local host name
	if( gethostname(szHostName, 128) == 0 ){
		// Get local host IP adresses
		pHost = gethostbyname(szHostName); 
		for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ ){
			for( j = 0; j < pHost->h_length; j++ ){
  				if( j > 0 ) str += "."; 
 				addr.Format("%u", (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]);
				str += addr; 		
			}
  			// str now contains one local IP address 
			// do whatever you want to do with it (probably add it to a list)
			plstBox->AddString( str );
 		}
		// Select the first entry
		plstBox->SetCurSel(0);

		// Get current selected IP address from list box
		plstBox->GetText(plstBox->GetCurSel(),m_txtLocalIP);
	}
}

void CDynamicIPDlg::OnSndonline() 
{
	// TODO: Add your control notification handler code here
	char *szTFN;

	// Get data from the dialog box fields
	UpdateData(TRUE);
	
	// Create a temprary file name
	szTFN = _tempnam( "C:\\Windows\\Temp", "~Dyna");

	if(!ParseFile(m_txtOnline, szTFN))
		return;

	SendFile(szTFN, m_bBinary1);

	remove(szTFN);
//	DeleteFile(szTFN);
	free(szTFN);
}

void CDynamicIPDlg::OnSndoffline() 
{
	// TODO: Add your control notification handler code here
	char *szTFN;
	
	// Get data from the dialog box fields
	UpdateData(TRUE);

	// Create a temprary file name
	szTFN = _tempnam( "C:\\Windows\\Temp", "~Dyna");

	if(!ParseFile(m_txtOffline, szTFN))
		return;

	SendFile(m_txtOffline, m_bBinary2);

	remove(szTFN);
//	DeleteFile(szTFN);
	free(szTFN);
}

void CDynamicIPDlg::OnGetofile1() 
{
	// TODO: Add your control notification handler code here
	
	CFileDialog dlg(TRUE, NULL, m_txtOnline,(OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT),"Web Pages (*.htm;*.hml)|*.htm;*.html|All Files (*.*)|*.*||");
	if (dlg.DoModal() == IDOK){
		m_txtOnline = dlg.GetPathName();
		SetDlgItemText(IDC_ONLINE, m_txtOnline);
	}
}

void CDynamicIPDlg::OnGetofile2() 
{
	// TODO: Add your control notification handler code here

	CFileDialog dlg(TRUE, NULL, m_txtOnline,(OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT),"Web Pages (*.htm;*.hml)|*.htm;*.html|All Files (*.*)|*.*||");
	if (dlg.DoModal() == IDOK){
		m_txtOffline = dlg.GetPathName();
		SetDlgItemText(IDC_OFFLINE, m_txtOffline);
	}
	
}

void CDynamicIPDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here

	m_wndToolTip.DestroyWindow();

	// Get data from dialog box fields
	UpdateData(TRUE);

	// Save the values in the INI file
	CWinApp* pApp = AfxGetApp();
	CString sSection(_T("General"));
	pApp->WriteProfileString(sSection, _T("FTPServer"), m_txtServer);
	pApp->WriteProfileString(sSection, _T("Username"), m_txtUsername);
	pApp->WriteProfileString(sSection, _T("Password"), m_txtPassword);
	pApp->WriteProfileString(sSection, _T("OnlineFilenName"), m_txtOnline);
	pApp->WriteProfileString(sSection, _T("OfflineFileName"), m_txtOffline);
	pApp->WriteProfileString(sSection, _T("RemoteDirName"), m_txtRemoteDir);
	pApp->WriteProfileString(sSection, _T("RemoteFileName"), m_txtRemoteFileName);
	pApp->WriteProfileInt(sSection, _T("OnlineBinary"), m_bBinary1);
	pApp->WriteProfileInt(sSection, _T("OfflineBinary"), m_bBinary2);
}

void CDynamicIPDlg::SendFile(CString strFile, int bType)
{
	char szTemp[80];
	bool connectOK;
	// create CFtp class
	CFtp ftp;

	// Connect to server
	connectOK = ftp.ConnectToServer(m_txtServer, 
									m_txtUsername,
									m_txtPassword);
	if(!connectOK) return;

	if(m_txtRemoteDir.IsEmpty())
		m_txtRemoteDir = '/';
	
	// transfer file
	connectOK = ftp.PutFile(strFile, m_txtRemoteDir, m_txtRemoteFileName, bType);
	
	if(connectOK){
		wsprintf(szTemp, "File %s uploaded to server.", m_txtRemoteFileName);
		AfxMessageBox(szTemp);
	}

	// close connection
	ftp.CloseConnection();
}

void CDynamicIPDlg::Replace(CString &strText, LPCSTR SearchFor, LPCTSTR ReplaceWith)
{
	CString strTemp = strText;
	strText.Empty();

	int len = lstrlen(SearchFor);
	for (int pos = strTemp.Find(SearchFor); pos >= 0; pos = strTemp.Find(SearchFor)){
		strText	+= strTemp.Left(pos) + ReplaceWith;
		strTemp	= strTemp.Mid(pos + len);
	}
	strText += strTemp;
}

int CDynamicIPDlg::ParseFile(LPCSTR FileToParse, LPCSTR NewFileName)
{
	CFile sFile;
	CFileStatus fStatus;
//	CString szTemp;
//	BYTE *szBuffer;

	if( !sFile.Open(FileToParse, CFile::modeRead)){
		AfxMessageBox("Cann't open file.", MB_OK|MB_ICONERROR);
		return 0;
	}
	sFile.GetStatus(fStatus);

	// Get a buffer large enough to hold the file
	BYTE *szBuffer = new BYTE[fStatus.m_size+1];
	if(szBuffer == NULL){
		AfxMessageBox("Cann't Allocate memory.", MB_OK|MB_ICONERROR);
		return 0;
	}
	// Read the file into the buffer
	sFile.Read(szBuffer, fStatus.m_size);
	sFile.Close();

	// Terminate the buffer with a null, forget at first, the result
	// was that a few bogus chars were added to end of buffer.
	szBuffer[fStatus.m_size] = '\0';
	CString szTemp = szBuffer;
	delete [] szBuffer;
/*
	File readed ok now search for <IPADDRESS> when found
	replace it with the Local IP address, similarly search for
	<DATE> & <TIME> and replace them with current date and time ...
*/
	CTime curTime = CTime::GetCurrentTime();

	Replace(szTemp, "<IPADDRESS>", m_txtLocalIP);
	Replace(szTemp, "<TIME>", curTime.Format("%H:%M:%S"));
	Replace(szTemp, "<DATE>", curTime.Format("%A, %B %d, %Y"));

	// Now open the file for writing
	if(!sFile.Open(NewFileName, CFile::modeCreate|CFile::modeWrite)){
		AfxMessageBox("Cann't create Temporary file.", MB_OK|MB_ICONERROR);
		return 0;
	}
	// Write the file
	sFile.Write(szTemp, szTemp.GetLength()); 
	sFile.Close();

	return 1;
}

⌨️ 快捷键说明

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