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

📄 mainfrm.cpp

📁 文件加密的过滤驱动程序源代码.
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "FileGuardApp.h"
#include "..\HookShr\ComDef.h"
#include "FileGuard.h"
#include "..\HookShr\HookError.h"
#include "FGDevice.h"
#include "MainFrm.h"
#include "FileGuardDoc.h"
#include "FileGuardView.h"
#include "OptPropSheet.h"
#include "LoginDlg.h"
#include "ChgPwdDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_MESSAGE(WM_WINDOWPOSCHANGING,OnWindowPosChanging)
//	ON_MESSAGE(WM_ACTIVATE_PROTECTION,OnActivateProtection)
	ON_WM_CLOSE()
	ON_MESSAGE(SM_DEVICE_FATAL_ERROR, OnDeviceFatalError)
	ON_MESSAGE(SM_DEVICE_ERROR, OnDeviceError)
	ON_COMMAND(ID_DEVICE_DEBUG, OnDeviceDebug)
	ON_WM_ACTIVATE()
	ON_COMMAND(ID_M_ACTIVATE, OnMActivate)
	ON_WM_INITMENUPOPUP()
	ON_COMMAND(ID_OPTION, OnOption)
	ON_MESSAGE(WM_TASKBAR_NOTIFY, OnTaskBarNotify)
	ON_WM_SIZE()
	ON_COMMAND(ID_SHOW_WND, OnShowWnd)
	ON_COMMAND(ID_INI_FILE_SAVE, OnIniFileSave)
	ON_COMMAND(ID_NEW_FILE, OnNewProtFile)
	ON_COMMAND(ID_APP_EXIT, OnAppExit)
	ON_COMMAND(ID_LOGOUT, OnLogout)
	ON_COMMAND(ID_CHG_PWD, OnChangePwd)
//	ON_MESSAGE(WM_QUERY_USER_LOGIN, OnQueryUserLogin)		//debug: maybe incorrect form of  function.
//	ON_MESSAGE(WM_QUERY_PROTECTION, OnQueryProtection)
//	ON_MESSAGE(WM_SET_DEVICE_BUSY, OnSetDeviceBusy)
//	ON_MESSAGE(WM_FLUSH_FILE_INFO, OnFlushProtFileInfo)
	ON_COMMAND(ID_M_PAUSE, OnPauseProtection)
	ON_WM_QUERYENDSESSION()
	//}}AFX_MSG_MAP
	// Global help commands
	ON_COMMAND(ID_HELP_FINDER, CFrameWnd::OnHelpFinder)
	ON_COMMAND(ID_HELP, CFrameWnd::OnHelp)
	ON_COMMAND(ID_CONTEXT_HELP, CFrameWnd::OnContextHelp)
	ON_COMMAND(ID_DEFAULT_HELP, CFrameWnd::OnHelpFinder)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_HOOK_STATUS
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	if (!m_wndStatusBar.CreateEx(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	//Show in task bar?
	if(!bProtectSecretly || !hDevice)
		TaskBarAddIcon();

	//Set ctrl status according to device status.
	ChgCtrlState();

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;

	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs		
	cs.style=cs.style & ~(WS_MAXIMIZEBOX);
	cs.style &= ~FWS_ADDTOTITLE;

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

BOOL CMainFrame::OnLoadDevice()
{
	if(!LoadDevice(DEVICE_PATH))
	{
		hDevice=NULL;
		ErrorHandler(FG_ERR_LOAD_DEVICE_FAIL);		
		return 0;
	}
	
	DWORD ret;
	//Send hwnd to device. If not do so, device will not be able to communicate
	//with app using Shell_postmessage().
	if(!DeviceIoControl(hDevice, FG_DIOC_GET_HWND, &m_hWnd, sizeof(m_hWnd), 0, 0, &ret, 0))
		return 0;

	if(!DeviceReadProtectedFileInfo(lpszIniFilePath))
	{
		ErrorHandler(FG_ERR_INI_FILE_NOT_FOUND);
		return 0;
	}
	
	ChgCtrlState();
	return 1;	//no error
}

LRESULT CMainFrame::OnActivateProtection()
{
	if(!OnLoadDevice() || !InstallHook())
	{
		ErrorHandler(FG_ERR_INSTALL_HOOK_FAIL);		
		return 0;
	}

	ChgCtrlState();
	return 1;	//no error
}

//Stop protection on fatal errors.
BOOL CMainFrame::OnStopProtection()
{
	if(!hDevice)			//device is not loaded.
		return TRUE;

	if(!UninstallHook())
	{
		ErrorHandler(FG_ERR_UNINSTALL_HOOK_FAIL);
		return FALSE;
	}

	if(!UnloadDevice())
	{
		ErrorHandler(FG_ERR_UNLOAD_DEVICE_FAIL);
		return FALSE;
	}

	ChgCtrlState();
	return TRUE;
}

void CMainFrame::OnWindowPosChanging(WPARAM wParam,WINDOWPOS* lpwndpos)
{	
	if(lpwndpos!=NULL)
	{
		lpwndpos->cx=519;//m_clientRect.right*2;
		lpwndpos->cy=489;//m_clientRect.bottom*2;
	}

	//CFrameWnd::OnWindowPosChanging(lpwndpos);
}

void CMainFrame::OnClose() 
{
	if(bLogin)
		OnAppExit();
}

void CMainFrame::OnDeviceFatalError() 
{
	if(ErrorHandler(FG_ERR_DEVICE_FATAL_ERROR, 0)==0)
	{
		AfxMessageBox("程序将终止.", MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL);
		theApp.PreExitProgram();
		::ExitProcess(1);
	}
}

void CMainFrame::OnDeviceError(WORD severity, DWORD err) 
{
	DeviceErrorHandler(err, (int)severity);
}

//debug function.
void CMainFrame::OnDeviceDebug() 
{
	char lpszRecPath[MAX_PATH+1];
	ITEMIDLIST *pIdList;
	if(::SHGetSpecialFolderLocation(m_hWnd, CSIDL_BITBUCKET, &pIdList)!=NOERROR)
		AfxMessageBox("Get location fail.");
	SHGetPathFromIDList(pIdList, lpszRecPath);
	TRACE("Get path error: %u\n", GetLastError());
	AfxMessageBox(lpszRecPath);
/*	if(IsPatternMatchedW(_T("A1.*"), _T("A1.TXT.BAK"), 4, 10))
		AfxMessageBox("Match.");
	else AfxMessageBox("Not match.");*/
	/*
	DWORD ret;
	if(!DeviceIoControl(hDevice, FG_DIOC_DEBUG, 0, 0, 0, 0, &ret, 0))
		AfxMessageBox("Cannot send debug command.");
	*/
}

void CMainFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) 
{
	CFrameWnd::OnActivate(nState, pWndOther, bMinimized);

	static BOOL bOnWarning=FALSE;

	if(nState==WA_INACTIVE && !bOnWarning)
	{
		bOnWarning=TRUE;							//to prevent reentry. (Message box from ErrorHandler will cause reentry.)
		SaveProtectedFileInfo(&protFileList);
		DeviceFlushProtectedFileInfo();
		bOnWarning=FALSE;
	}
}

void CMainFrame::OnPauseProtection()
{
	if(bIsHookInstalled && !UninstallHook())
		ErrorHandler(FG_ERR_UNINSTALL_HOOK_FAIL);

	ChgCtrlState();
}

//Change control &menu &icon state according to device status.
void CMainFrame::ChgCtrlState(CMenu *pMenu/*=NULL*/)
{
	CString indication;
	UINT nMActivateEnable=MF_GRAYED, nMPauseEnable=MF_GRAYED;		//debug: unreferrence variables.

	if(hDevice)		//if device is loaded...
	{
		if(bIsHookInstalled)
		{
			indication.LoadString(IDS_IND_ACTIVATE);
			nMPauseEnable=MF_ENABLED;
		}
		else 
		{
			indication.LoadString(IDS_IND_PAUSE);
			nMActivateEnable=MF_ENABLED;
		}
	}
	else 
	{
		indication.LoadString(IDS_IND_STOP);
		nMActivateEnable=MF_ENABLED;
	}
	
	//Change indicator.
	m_wndStatusBar.SetPaneText(1, indication);

	//Change menu.
	if(pMenu)
	{
		pMenu->EnableMenuItem(ID_M_ACTIVATE, MF_BYCOMMAND | nMActivateEnable);
		pMenu->EnableMenuItem(ID_M_PAUSE, MF_BYCOMMAND | nMPauseEnable);
	}

	//Change icon & tip in task bar.
	if(!bProtectSecretly)
	{
		NOTIFYICONDATA       nid;		
		nid.cbSize=sizeof(NOTIFYICONDATA);
		nid.hWnd=GetSafeHwnd();
		nid.uID=1;
		nid.uFlags=NIF_MESSAGE |NIF_TIP |NIF_ICON;
		nid.uCallbackMessage=WM_TASKBAR_NOTIFY;
		lstrcpy(nid.szTip,indication);
		nid.hIcon=theApp.LoadIcon((int)hDevice*bIsHookInstalled? IDR_MAINFRAME : IDR_INACTIVATE);
		Shell_NotifyIcon(NIM_MODIFY, &nid);
	}

	//Set icon of framewnd
	if(::IsWindow(m_hWnd))
	{
		HICON hIcon=theApp.LoadIcon((int)hDevice*bIsHookInstalled? IDR_MAINFRAME : IDR_INACTIVATE);
		ASSERT(hIcon);
		SendMessage(WM_SETICON,TRUE,(LPARAM)hIcon);
	}
}

void CMainFrame::OnMActivate() 
{
	OnActivateProtection();	
}

void CMainFrame::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) 
{
	CFrameWnd::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
	
	ChgCtrlState(pPopupMenu);	
}

void CMainFrame::OnFlushProtFileInfo()
{
	ReadProtectedFileInfo(&protFileList);
	::PostMessage(GetActiveView()->m_hWnd, WM_SHOW_DATA, 3, 0);
	DeviceFlushProtectedFileInfo();
}

void CMainFrame::OnOption() 
{
	COptPropSheet propSheet;
	propSheet.DoModal();
}

//Add an icon to task bar when minimized.
void CMainFrame::TaskBarAddIcon()
{
	NOTIFYICONDATA nid;
	nid.cbSize=sizeof(NOTIFYICONDATA);
	nid.hWnd=GetSafeHwnd();
	nid.uID=1;
	nid.uFlags=NIF_MESSAGE |NIF_TIP |NIF_ICON;
	nid.uCallbackMessage=WM_TASKBAR_NOTIFY;
	lstrcpyn(nid.szTip, "FileGuard", sizeof(nid.szTip));
	nid.hIcon=theApp.LoadIcon((int)hDevice*bIsHookInstalled? IDR_MAINFRAME : IDR_INACTIVATE);
	Shell_NotifyIcon(NIM_ADD, &nid);
}

//Call this when maximized.
void CMainFrame::TaskBarDelIcon()
{
	NOTIFYICONDATA nid;
	nid.cbSize=sizeof(NOTIFYICONDATA);
	nid.hWnd=GetSafeHwnd();
	nid.uID=1;
	Shell_NotifyIcon(NIM_DELETE, &nid);
}

//Task bar notify message handler:
void CMainFrame::OnTaskBarNotify(UINT id, UINT msg)
{
	if(msg== WM_LBUTTONDOWN)
		ShowMainWnd(FALSE);

	else if(msg==WM_RBUTTONDOWN && bLogin)
	{
		POINT point;
		if(!GetCursorPos(&point))
			return;
		CMenu menu;
		menu.LoadMenu(IDR_LIST_MENU);
		menu.GetSubMenu(1)->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
	}
}

void CMainFrame::OnSize(UINT nType, int cx, int cy) 
{
	CFrameWnd::OnSize(nType, cx, cy);
	
	if(nType==SIZE_MINIMIZED)
		ShowMainWnd(FALSE);
}

void CMainFrame::OnShowWnd() 
{	
	ShowMainWnd(FALSE);
}

//Show window or prompt user to enter password.
//bShow: force the window to show.
void CMainFrame::ShowMainWnd(BOOL bShow)
{
	static BOOL bHided=0;

	if(!bShow && !bHided)
	{
		ShowWindow(SW_HIDE);
		bHided=1;
		return;
	}
	
	if(!bLogin)
	{
		ShowWindow(SW_HIDE);
		bHided=1;
		CLoginDlg loginDlg;
		loginDlg.DoModal();
	}
	if(!bLogin)
		return;

	this->ShowWindow(SW_RESTORE);
	bHided=0;
}


void CMainFrame::OnIniFileSave() 
{
	if(!SaveProtectedFileInfo(&protFileList))
		ErrorHandler(FG_ERR_CANNOT_WRITE_RECORD);
}


void CMainFrame::OnNewProtFile() 
{
	ShowFileProperty(NULL);
}

void CMainFrame::OnAppExit() 
{
	if(hDevice && !OnStopProtection())
	{
		//debug
		MessageBox("Unable to stop protection, the application will not close.", "Error message", MB_OK|MB_ICONSTOP);
		return;
	}

	TaskBarDelIcon();

	CFrameWnd::OnClose();
}

void CMainFrame::OnLogout() 
{
	bLogin=FALSE;
	ShowMainWnd(FALSE);
}

void CMainFrame::OnChangePwd() 
{
	CChgPwdDlg pwdDlg;
	
	pwdDlg.DoModal();
}

LRESULT  CMainFrame::OnQueryUserLogin()
{
	return bLogin;
}

LRESULT  CMainFrame::OnQueryProtection()
{
	return (LRESULT)hDevice&&bIsHookInstalled;
}

LRESULT CMainFrame::OnSetDeviceBusy(BOOL bBusy)
{
	return (LRESULT)SetDeviceBusy(bBusy);
}

BOOL CMainFrame::OnQueryEndSession() 
{
	if (!CFrameWnd::OnQueryEndSession())
		return FALSE;
	
	ModifyRegSysStart();

	return FALSE;
}

void CMainFrame::WinHelp(DWORD dwData, UINT nCmd) 
{
	if(!HtmlHelp(NULL, HELP_PATH, HH_DISPLAY_TOPIC, 0))
		ErrorHandler(FG_ERR_HELP_NOT_FOUND);
}

LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	TRACE("message flush.");
	switch(message)
	{
		case SM_SHOW_WINDOW:
		{
			ShowMainWnd(FALSE);
			return 1;
		}

		case WM_FLUSH_FILE_INFO:
		{
			ReadProtectedFileInfo(&protFileList);
			//::PostMessage(GetActiveView()->m_hWnd, WM_SHOW_DATA, 3, 0);
			((CFileGuardView *)GetActiveView())->OnShowData(3);
			DeviceFlushProtectedFileInfo();
			return 1;
		}

		case WM_QUERY_USER_LOGIN:
			return bLogin;

		case WM_QUERY_PROTECTION:
			return (LRESULT)(hDevice&&bIsHookInstalled);

		case WM_SET_DEVICE_BUSY:
			return (LRESULT)SetDeviceBusy((BOOL)wParam);

		case WM_ACTIVATE_PROTECTION:
			{
				if(!OnLoadDevice() || !InstallHook())
				{
					ErrorHandler(FG_ERR_INSTALL_HOOK_FAIL);		
					return 0;
				}

			ChgCtrlState();
			return 1;	//no error
			}
	}


	return CFrameWnd::WindowProc(message, wParam, lParam);
}

⌨️ 快捷键说明

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