📄 fileguardapp.cpp
字号:
// FileGuardApp.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "FileGuardApp.h"
#include "..\HookShr\ComDef.h"
#include "..\HookShr\HookError.h"
#include "FileGuard.h"
#include "FGDevice.h"
#include "MainFrm.h"
#include "FileGuardDoc.h"
#include "FileGuardView.h"
#include "MyPropertySheet.h"
#include "LoginDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFileGuardApp
BEGIN_MESSAGE_MAP(CFileGuardApp, CWinApp)
//{{AFX_MSG_MAP(CFileGuardApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileGuardApp construction
CFileGuardApp::CFileGuardApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
//useless...
//m_nCmdShow==SW_HIDE;
}
/////////////////////////////////////////////////////////////////////////////
// The CFileGuardApp object:
CFileGuardApp theApp;
// gobal variables:
HWND hPrevInstanceWnd=NULL;
KProtectedFileList protFileList;
CString strInstallDir; //Install dir, last char must be '\'
TCHAR lpszIniFilePath[MAX_PATH];
//Gobal handles:
HANDLE hWatcher=NULL;
CWinThread *pThisWatcher=NULL; //Local watcher thread to watch the other watcher process.
//Options:
BOOL bActSysUp; //Activate on system start up.
BOOL bActAppUp;
BOOL bProtectSecretly; //Do protection secretly.
BOOL bRegKey; //Add key to registry, in order to insert an column in right-key menu.
BOOL bWarning; //Give message when protecting a file.
BOOL bLogin=FALSE; //User is login.
BOOL bProtectSelf; //Protect this program.
UINT maxBakNum;
//state
BOOL bSysStartUp=FALSE;
/////////////////////////////////////////////////////////////////////////////
// CFileGuardApp initialization
BOOL CFileGuardApp::InitInstance()
{
AfxEnableControlContainer();
//Get Install dir.
GetModuleFileName(NULL, strInstallDir.GetBuffer(MAX_PATH), MAX_PATH);
strInstallDir.ReleaseBuffer();
GetDir(strInstallDir.GetBuffer(MAX_PATH)); //remove file name.
strInstallDir.ReleaseBuffer();
SetCurrentDirectory(strInstallDir);
strInstallDir.MakeUpper();
SetRegistryKey(_T("FileGuard"));
ModifyRegSysStart();
GetProfile();
ModifyRegClsKey();
ModifyRegSysStart();
// Parse command line for standard shell commands, DDE, file open
CFGCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
//If it is system start up and user doesn't choose to activate protetction on system start up, exit the program.
if(bSysStartUp && !bActSysUp)
::ExitProcess(0);
//Is the user had logged in?
char szUserInfo[9];
if(!GetEnvironmentVariable("USERINFO", szUserInfo, 9) || sscanf(szUserInfo, "%u", &bLogin)==EOF)
bLogin=FALSE;
//Determine whether this process is a watcher.
if(bWatcher)
{
//if it is a watcher process...
HideProcess();
HANDLE hMainProc;
char szHandle[16];
//get main process and watch.
if(GetEnvironmentVariable("HANDLE", szHandle, 15) && sscanf(szHandle, "%u", &hMainProc)!=EOF)
{
::SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS); //give a high priority to watcher.
WatcherProc(hMainProc);
}
else AfxMessageBox("Wrong environment."); //debug
::ExitProcess(0);
}
//Get previous instance.
hPrevInstanceWnd=FindWindow(NULL,"FileGuard");
//Load device:
if(hPrevInstanceWnd)
SetDeviceBusy(TRUE);
if(!LoadDevice(DEVICE_PATH))
ErrorHandler(FG_ERR_LOAD_DEVICE_FAIL);
if(hPrevInstanceWnd)
SetDeviceBusy(FALSE);
//read protected file information from ini file and store in gobal variable protFileList.
if(!ReadProtectedFileInfo(&protFileList))
ErrorHandler(FG_ERR_INI_FILE_NOT_FOUND);
//Open by right key?
if(cmdInfo.m_bIsOpenByRightKey==TRUE)
{
OnProperties(&cmdInfo); //Show a property sheet.
protFileList.Empty();
strInstallDir.Empty();
::ExitProcess(0); //Destructors are not called when using ExitProcess(...) to exit program, WHY???
}
//Normal program:
//Has an instance exists?
if(hPrevInstanceWnd!=NULL)
::ExitProcess(0);
if(bProtectSecretly)
HideProcess();
//if it is the main process, create a new watcher and watch the watcher.
hWatcher=CreateWatcher();
if(!hWatcher)
ErrorHandler(FG_ERR_NOT_ENOUGH_MEMORY);
else pThisWatcher=AfxBeginThread(WatchTheWatcher, NULL);
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CFileGuardDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CFileGuardView));
AddDocTemplate(pDocTemplate);
// Dispatch commands specified on the command line
//Main window will be created by this call.
if (!ProcessShellCommand(cmdInfo))
return FALSE;
//Activate protection.
::PostMessage(m_pMainWnd->GetSafeHwnd(), WM_ACTIVATE_PROTECTION, 0, 0);
//Show window.
((CMainFrame *)m_pMainWnd)->ShowMainWnd(!bProtectSecretly);
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// 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)
// No message handlers
//}}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()
// App command to run the dialog
void CFileGuardApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CFileGuardApp message handlers
void CFileGuardApp::OnProperties(CFGCommandLineInfo *pCmdInfo)
{
//Log in.
if(!hPrevInstanceWnd || !::SendMessage(hPrevInstanceWnd, WM_QUERY_USER_LOGIN, 0, 0))
{
CLoginDlg loginDlg;
loginDlg.DoModal();
if(!bLogin)
::ExitProcess(0);
}
pCmdInfo->ParsePath();
CFileGuardPropertySheet propSheet;
propSheet.SetPath(pCmdInfo->m_strFileName);
propSheet.m_Page1.m_hPrevWnd=hPrevInstanceWnd;
if(propSheet.DoModal()==IDOK)
{
//Get path:
CString strPath=propSheet.GetPath();
FGFormatPath(strPath, FALSE);
//To activate the protection?
if(propSheet.m_Page1.m_bActivate==1)
{
//First make sure the main window of fileguard is exist, then send a message to activate hook.
if(!hPrevInstanceWnd)
{
STARTUPINFO sinfo;
ZeroMemory(&sinfo, sizeof(sinfo));
sinfo.cb=sizeof(sinfo);
PROCESS_INFORMATION pinfo;
char szUserInfo[9];
sprintf(szUserInfo, "%u", bLogin );
SetEnvironmentVariable("USERINFO", (char *)szUserInfo);
if(!CreateProcess("FILEGUARD.EXE", 0, 0, 0, TRUE, 0, 0, 0, &sinfo, &pinfo) || WaitForInputIdle(pinfo.hProcess, 15000))
ErrorHandler(FG_ERR_ACTIVATE_FAIL);
CloseHandle(pinfo.hThread);
CloseHandle(pinfo.hProcess);
}
if(!(hPrevInstanceWnd=FindWindow(NULL,"FileGuard")) || !SendMessage(hPrevInstanceWnd,WM_ACTIVATE_PROTECTION,0,0))
ErrorHandler(FG_ERR_ACTIVATE_FAIL);
} //if(!SendMessage(hPrevInstanceWnd,WM_ACTIVATE_PROTECTION,0,0))
//What action will take, remove or add?
if(propSheet.m_Page1.m_nAction==0) //remove
protFileList.Remove(strPath);
else if(propSheet.m_Page1.m_nAction==1) //add
protFileList.Add(strPath, propSheet.GetProtectionType());
} //if(propSheet.DoModal()==IDOK)
SaveProtectedFileInfo(&protFileList);
if(hPrevInstanceWnd)
if(!::PostMessage(hPrevInstanceWnd, WM_FLUSH_FILE_INFO, 0, 0))
ErrorHandler(FG_ERR_SEND_INFO_FAIL);
}
int CFileGuardApp::ExitInstance()
{
SaveProtectedFileInfo(&protFileList);
WriteProfile();
PreExitProgram();
return CWinApp::ExitInstance();
}
void CFileGuardApp::PreExitProgram()
{
UnloadDevice();
//Stop watcher
if(pThisWatcher)
{
::TerminateThread(pThisWatcher->m_hThread, 0); //pThisWatcher->SuspendThread(); //This may cause memory leak, but it doesn't matter. //debug: can we find a better way?
delete pThisWatcher; //???
}
if(hWatcher)
::TerminateProcess(hWatcher, 0); //Watcher are no longer needed.
}
//Load settings.
void CFileGuardApp::GetProfile()
{
bActSysUp=GetProfileInt("Program", "Sys startup", 0);
bActAppUp=GetProfileInt("Program", "App startup", 1);
bProtectSecretly=GetProfileInt("Program", "Minimized state", 0);
bRegKey=GetProfileInt("Program", "Registry Key", 1);
bWarning=GetProfileInt("Program", "Warning", 1);
lstrcpy(lpszIniFilePath, GetProfileString("Default", "INI file path", strInstallDir+"PROTFILE.INI"));
defProtectionType=GetProfileInt("Default", "Protection type", PT_DELETE | PT_WRITE);
protFileList.bDelBak=GetProfileInt("Protection Options", "Delete", 1);
protFileList.bWriteBak=GetProfileInt("Protection Options", "Write", 1);
bProtectSelf=GetProfileInt("Protection Options", "ProtSelf", 0);
maxBakNum=GetProfileInt("Protection Options", "Max bak file number", DEF_MAX_BAK_NUM);
}
//Save settings:
void CFileGuardApp::WriteProfile()
{
ModifyRegSysStart();
WriteProfileInt("Program", "Sys startup", bActSysUp);
WriteProfileInt("Program", "App startup", bActAppUp);
WriteProfileInt("Program", "Minimized state", bProtectSecretly);
WriteProfileInt("Program", "Registry Key", bRegKey);
WriteProfileInt("Program", "Warning", bWarning);
WriteProfileString("Default", "INI file path", lpszIniFilePath);
WriteProfileInt("Default", "Protection type", defProtectionType);
WriteProfileInt("Protection Options", "Delete", protFileList.bDelBak);
WriteProfileInt("Protection Options", "Write", protFileList.bWriteBak);
WriteProfileInt("Protection Options", "ProtSelf", bProtectSelf);
WriteProfileInt("Protection Options", "Max bak file number", maxBakNum);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -