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

📄 dlgencporgress.cpp

📁 COM 组建的开发
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DlgEncPorgress.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "DlgEncPorgress.h"

#include "EncryptSelectFile.h"
#include <io.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgEncPorgress dialog


CDlgEncPorgress::CDlgEncPorgress(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgEncPorgress::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgEncPorgress)
		// NOTE: the ClassWizard will add member initialization here
	m_hExitFlag = NULL;
	m_dwEncryptThreadID = 0;
	//}}AFX_DATA_INIT
}


void CDlgEncPorgress::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgEncPorgress)
	DDX_Control(pDX, IDC_PROGRESS, m_ProgressCtrl);
	DDX_Control(pDX, IDC_LISTBOX, m_listbox);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgEncPorgress, CDialog)
	//{{AFX_MSG_MAP(CDlgEncPorgress)
	ON_MESSAGE(WM_FILELEN, SetProgress)
	ON_MESSAGE(WM_CURPOS, SetCurPos)
	ON_MESSAGE(WM_SENDMSG, ShowEncMsg)
	ON_MESSAGE(WM_AUTOEXIT, AutoExit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgEncPorgress message handlers

DWORD WINAPI CDlgEncPorgress::Encrypt(LPVOID lparam)
{
	CDlgEncPorgress* _this = (CDlgEncPorgress*)lparam;
	if (NULL == _this)
	{
		return -1;
	}
	int nFileCount = g_SelectFileList.GetItemCount();
	if (0 == nFileCount)
	{
		_this->SetMsg(CString("待加密文件列表为空!"),CXListBox::Red);
		return 0;	
	}
	int i = 0;
	CString strMsg = "";
	CSelectFile SelectFile;
	int nFalseCount = 0;
	CToolsKit ToolsKit;
 	for (i = 0;i < nFileCount; i++)
	{
		Sleep(300);// 可能是由于杀毒软件的原因,访问该文件的时候总是提示该文件已占用,暂停一下后,该问题解决 [8/14/2008 By willing]
		SelectFile.SetRunState(encrypt);// 将状态修改为正在加密 [5/8/2008 By willing]
		strMsg.Format("加密文件 [%d/%d]",i+1,nFileCount);
		_this->SetDlgTitle(strMsg);
		if (FALSE == g_SelectFileList.GetHeadItem(SelectFile))
		{
			strMsg.Format("从链表中获取第%d待加密文件信息失败!",i+1);
			_this->SetMsg(strMsg,CXListBox::Red);
			SelectFile.SetRunState(error);// 将状态修改为错误 [5/8/2008 By willing]
			nFalseCount++;
			continue;
		}
		strMsg.Format("开始加密 %s",SelectFile.GetFileName());
		_this->SetMsg(strMsg,CXListBox::Blue);
		if (SelectFile.IsFile() == FALSE)
		{
			strMsg.Format("该对象不是个文件,不加密!");
			_this->SetMsg(strMsg,CXListBox::Red);
			SelectFile.SetRunState(error);// 将状态修改为错误 [5/8/2008 By willing]
			nFalseCount++;
			continue;
		}
		// 判断是否是加密过的文件 [8/15/2008 By willing]
		if (SelectFile.CheckFile() == TRUE)
		{
			_this->SetMsg("该文件已经被加密过!",CXListBox::Red);
			SelectFile.SetRunState(error);// 将状态修改为错误 [5/8/2008 By willing]
			nFalseCount++;
			continue;
		}
		strMsg.Format("正在加密 %s",SelectFile.GetFileName());
		_this->ShowStaticMsg(strMsg);
		
		// 写入加密文件头 [5/14/2008 By willing]
		CString strHeadXml = _this->CreateEncFileHeadXml(SelectFile);
		if ("" == strHeadXml)
		{
			strMsg = "构造输出文件头信息失败";
			OutputDebugString(strMsg);
			_this->SetMsg(strMsg,CXListBox::Red);
			// 创建输出文件失败 [5/14/2008 By willing]
			nFalseCount++;
			SelectFile.SetRunState(error);
			SelectFile.Clear();
			continue;
		}
		// 获取输出的文件名称 [8/14/2008 By willing]
		CString strEncFileName = SelectFile.GetEncFilePatchName();
		if (_access(strEncFileName,0) == 0)
		{
			CFile::Remove(strEncFileName);
		}
		DWORD lFileLen = SelectFile.GetFileLen();// myFile.GetLength();
		CFile encFile;
		if (FALSE == encFile.Open(strEncFileName,CFile::modeCreate|CFile::modeWrite))
		{
			strMsg = "创建输出加密后的文件失败!";
			_this->SetMsg(strMsg,CXListBox::Red);
			// 创建输出文件失败 [5/14/2008 By willing]
			nFalseCount++;
			SelectFile.SetRunState(error);
			SelectFile.Clear();
			continue;
		}
		if (0 != _this->WriteEncFileHead(encFile,strHeadXml))
		{
			// 写入加密文件头信息失败 [5/14/2008 By willing]
			strMsg = "输出加密文件失败!";
			_this->SetMsg(strMsg,CXListBox::Red);
			encFile.Close();
			CFile::Remove(strEncFileName);
			nFalseCount++;
			SelectFile.SetRunState(error);
			SelectFile.Clear();
			// 删除创建的文件 [8/13/2008 By willing]
			CFile::Remove(strEncFileName);
			continue;
		}
		
		_this->SetProgress((WPARAM)lFileLen,NULL);
		if (lFileLen <= 0)
		{
			strMsg = "获取待加密文件大小失败!";
			_this->SetMsg(strMsg,CXListBox::Red);
			nFalseCount++;
			SelectFile.SetRunState(error);
			SelectFile.Clear();
			// 删除创建的文件 [8/13/2008 By willing]
			CFile::Remove(strEncFileName);
			continue;
		}
		// 打开待加密的文件 [5/15/2008 By willing]
		CFile myFile;
		if (FALSE == myFile.Open(SelectFile.GetFilePatchName(),CFile::shareDenyRead))
			if (FALSE == myFile.Open(SelectFile.GetFilePatchName(),CFile::shareDenyRead))
			{

			OutputDebugString(SelectFile.GetFilePatchName());
			strMsg.Format("开始加密时打开待加密文件失败!ErrCode=%d",GetLastError());
			_this->SetMsg(strMsg,CXListBox::Red);
			nFalseCount++;
			SelectFile.SetRunState(error);
			SelectFile.Clear();
			// 删除创建的文件 [8/13/2008 By willing]
			CFile::Remove(strEncFileName);
			continue;
		}
		// 开始加密文件内容 [5/14/2008 By willing]
		char szIn[BUFFER_SIZE]={0};
		UINT unTemp = 0;
		UINT unTotal = 0;
		int nExit = 0;
		DWORD dwRe = 0;
		do 
		{
			// 判断是否需要退出 [5/15/2008 By willing]
			if (::WaitForSingleObject(_this->m_hExitFlag,1) == WAIT_OBJECT_0)
			{
				// 等待到了信号,用户强制要退出了。 [5/15/2008 By willing]
				OutputDebugString("等待到了信号,用户强制要退出了。");
				nExit = 1;
				break;
			}
			memset(szIn,0,BUFFER_SIZE);
			// 读文件内容 [5/15/2008 By willing]
			unTemp = myFile.Read(szIn,BUFFER_SIZE);
			if (unTemp <= 0)
			{
				// 完成 [5/15/2008 By willing]
				break;
			}
			if (_this->WriteBuffer(encFile,szIn,BUFFER_SIZE) < 0)
			{
				strMsg = "密文件失败!";
				_this->SetMsg(strMsg,CXListBox::Red);
				break;
			}
			unTotal += unTemp;
			// 报告进度 [5/15/2008 By willing]
			::PostMessage(_this->m_hWnd,WM_CURPOS,(WPARAM)unTotal,NULL);
		} while(unTotal < lFileLen);
		myFile.Close();
		encFile.Close();
		// 判断加密是否成功完成 [5/15/2008 By willing]
		if (unTotal == lFileLen)
		{
			_this->SetMsg("文件加密成功!",CXListBox::Blue);
			// 文件加密成功,删除源文件 [8/14/2008 By willing] [8/14/2008 By willing]
			CFile::Remove(SelectFile.GetFilePatchName());
		}else{
			if (1 == nExit)// 用户强制退出 [5/15/2008 By willing]
			{
				CFile::Remove(SelectFile.GetEncFilePatchName());
				return 0;
			}
			// 加密失败 [5/15/2008 By willing]
			CFile::Remove(SelectFile.GetEncFilePatchName());
		}
		SelectFile.Clear();// 清空该对象的信息 [8/12/2008 By willing]
	}
	// 统计最终结果 [5/8/2008 By willing]
	if (0 == nFalseCount)
	{
		strMsg = "全部文件加密完毕!";
		//_this->SetMsg(strMsg,CXListBox::Blue);
		ENC_MSG* ptrEncMsg = new ENC_MSG;
		if (NULL != ptrEncMsg)
		{
			sprintf(ptrEncMsg->szMsg,"%s",strMsg);
			ptrEncMsg->nTextColor = CXListBox::Blue;
			::PostMessage(_this->m_hWnd,WM_SENDMSG,(WPARAM)ptrEncMsg,NULL);
		}
	}else{
		strMsg.Format("加密完毕,成功%d个失败%d个。",nFileCount-nFalseCount,nFalseCount);
		ENC_MSG* ptrEncMsg = new ENC_MSG;
		if (NULL != ptrEncMsg)
		{
			sprintf(ptrEncMsg->szMsg,"%s",strMsg);
			ptrEncMsg->nTextColor = CXListBox::Red;
			::PostMessage(_this->m_hWnd,WM_SENDMSG,(WPARAM)ptrEncMsg,NULL);
		}
	}
	// 删除加密成功的文件 [5/8/2008 By willing]
	// 自动关闭窗体 [5/16/2008 By willing]
	::PostMessage(_this->m_hWnd,WM_AUTOEXIT,NULL,NULL);
	return 0;
}

void CDlgEncPorgress::SetDlgTitle(CString str)
{
	this->SetWindowText(str);
}
// 启动工作线程 [5/7/2008 By willing]
DWORD CDlgEncPorgress::Run()
{
	if (g_SelectFileList.IsEmpty())
	{
		MessageBox("请选择您要加密的文件!","提示",MB_ICONINFORMATION|MB_OK);
		return -1;
	}

	m_hWaitFlag = ::CreateEvent(NULL, FALSE, FALSE, NULL);
	::SetEvent(m_hWaitFlag);
	m_hExitFlag = ::CreateEvent(NULL, TRUE, FALSE, NULL);
	if (NULL == m_hExitFlag)
	{
		return -2;
	}
	m_hThreadHandle = CreateThread( NULL, 0, Encrypt,
									   ( LPVOID )this, NULL, &m_dwEncryptThreadID );
	if (NULL == m_hThreadHandle)
	{

⌨️ 快捷键说明

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