📄 fgfun.cpp
字号:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//FGFun.cpp-----miscellaneous functions.
#include "StdAfx.h"
#include "ComDef.h"
#include "..\HookShr\KProtectedFileList.h"
#include "..\HookShr\HookError.h"
#include "FileGuard.h"
#include "FGDevice.h"
#include "FileGuardApp.h"
#include "IniFile.h"
#include "MainFrm.h"
#include "ProtFilePropDlg.h"
/*
//FGParsePath------do some formatting(like capitalizing etc.),
//and parse a path to see if it is a drive or a dir or a file.
//return 1 if it is a drive, 2 a dir, 3 a file, and 0 if it cannot recognize it.
//for example, "e: \ A \a1.txt" will be transferred to "E:\A\A1.TXT",
//and "c:\ windows" will be transferred to "C:\WINDOWS\".
int FGParsePath(CString &strPath)
{
int type=0;
if(strPath.IsEmpty())
return 0;
strPath.MakeUpper();
switch((char)(*(LPCSTR)strPath.Right(1))) //get last character.
{
case '\\':
type=2; //It is a folder.
break;
case ':':
//It is a drive.
strPath+="\\";
return 1;
case '*':
//may be formatted.
type=3;
break;
}
WIN32_FIND_DATA findData;
if(FindFirstFile(strPath,&findData)==INVALID_HANDLE_VALUE && GetLastError()==ERROR_FILE_NOT_FOUND && strPath.Right(2)!=":\\")
return 0; //unrecognizable string.
else if(findData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY && type!=3)
{
//It is a floder.
strPath+="\\";
type=2;
}
else if(type==0) type=3; //may be a file.
//remove superfluous white-space.
int i=0;
while((i=strPath.Find('\\', i+1))!=-1)
{
//remove white-space on the left of '\'.
while( i!=0 && strPath[i-1]==' ')
{
strPath.Delete(i-1);
i--;
}
//remove while space on the right of '\'.
while( i<strPath.GetLength() && *((LPCTSTR)strPath+i+1)==' ')
strPath.Delete(i+1);
}
return type;
}
*/
//FGFormatPath------Format a path to see if it is a directory or a file,
//then modify it into a appropriate format.(Adding '*', etc.)
//"c:\ windows" will be transferred to "C:\WINDOWS".
//Path stored in protfilelist is of this kind.
void FGFormatPath(CString &strPath, BOOL bAddWildcard/*=TRUE*/)
{
if(strPath.IsEmpty())
return;
strPath.MakeUpper();
switch((char)(*(LPCSTR)strPath.Right(1))) //get last character.
{
case '\\':
//It is a dir.
strPath+='*';
break;
case ':':
//It is a drive.
strPath+="\\*";
break;
case '*':
break;
default:
if(bAddWildcard)
{
WIN32_FIND_DATA findData;
FindFirstFile(strPath,&findData);
if(findData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)
strPath+="\\*"; //It is a floder.
}
}
//remove superfluous white-space.
int i=0;
while((i=strPath.Find('\\', i+1))!=-1)
{
//remove white-space on the left of '\'.
while( i!=0 && strPath[i-1]==' ')
{
strPath.Delete(i-1);
i--;
}
//remove while space on the right of '\'.
while( i<strPath.GetLength() && *((LPCTSTR)strPath+i+1)==' ')
strPath.Delete(i+1);
}
}
//Gobal error handler for win32 app:
//err: error code. severity: 0 means a fatal error, 1 a minor one.
//return value: 0 ---the moduel where the error occurs must stop processing.
// 1 ---processing can go on.
int ErrorHandler(unsigned int err, int severity/*=1*/)
{
CString strErrInfo;
int retVal=1;
switch(err)
{
case FG_ERR_LIST_INIT_FAIL:
severity=0;
case FG_ERR_NOT_ENOUGH_MEMORY:
strErrInfo=_T("系统资源不足.");
if(severity==0)
strErrInfo+="程序将终止.";
AfxMessageBox(strErrInfo, MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL);
if(severity==0)
{
theApp.PreExitProgram();
::ExitProcess(1);
}
else
{
retVal=0;
goto NO_MORE_MESSAGE;
}
case FG_ERR_INI_FILE_NOT_FOUND:
strErrInfo=_T("未能找到ini文件,被保护文件信息可能丢失.");
break;
case FG_ERR_CANNOT_WRITE_RECORD:
{
static BOOL bWarned=FALSE;
if(bWarned) //Only send warning for one time.
return 1;
bWarned=TRUE;
strErrInfo=_T("无法保存文件信息,请确保ini文件存在。");
}
break;
case FG_ERR_UNINSTALL_HOOK_FAIL:
strErrInfo=_T("无法停止保护.");
retVal=severity;
break;
case FG_ERR_DEVICE_FATAL_ERROR:
case FG_ERR_UNLOAD_DEVICE_FAIL:
strErrInfo=_T("程序发生错误,可能导致系统不稳定。");
retVal=severity;
break;
case FG_ERR_LOAD_DEVICE_FAIL:
case FG_ERR_ACTIVATE_FAIL:
strErrInfo=_T("无法激活保护,请确保FGHook.vxd在安装路径上。");
break;
case FG_ERR_HELP_NOT_FOUND:
strErrInfo=_T("无法显示帮助,请确保FGHelp.chm在安装路径上。");
break;
case FG_ERR_SEND_INFO_FAIL:
case FG_ERR_SET_REG_CLS_KEY_FAIL:
case FG_ERR_SET_REG_SYS_START_FAIL:
strErrInfo=_T("发生错误,某些选项无法实现。");
break;
case FG_ERR_INSTALL_HOOK_FAIL:
return severity;
default:
strErrInfo.Format(_T("发生未知错误,程序可能无法正常工作。"));
}
AfxMessageBox(strErrInfo, MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL);
NO_MORE_MESSAGE:
return retVal;
}
//Get text for protection type from WORD type:
CString GetProtectionTypeText(WORD type)
{
CString strTypeText;
if(type & PT_DELETE)
strTypeText+="D ";
if(type & PT_WRITE)
strTypeText+="W ";
if(type & PT_READ)
strTypeText+="R ";
if(type & PT_HIDE)
strTypeText+="H ";
return strTypeText;
}
//Show property of existing protected file(lpszOldFile),
//or add a new file if lpszOldFile is NULL;
void ShowFileProperty(LPCTSTR lpszOldFile)
{
CString strPath;
CProtFilePropDlg filePropDlg;
PROTECTED_FILE protFile;
if(lpszOldFile)
{
//Get complete information.
strPath=lpszOldFile;
FGFormatPath(strPath, FALSE);
PROTECTED_FILE *pOldFile;
FILE_NODE *pFN=protFileList.IsInList(strPath);
if( !pFN)
{
//not in list. //debug : not so good
protFile.PF_type=defProtectionType;
pOldFile=&protFile;
filePropDlg.m_strPath=strPath;
}
else
{
pOldFile=&pFN->protFile;
filePropDlg.m_strPath=pOldFile->PF_pPath;
}
//todo:
filePropDlg.m_bFileDeleteProt=pOldFile->PF_type & PT_DELETE ? 1: 0;
filePropDlg.m_bFileWriteProt=pOldFile->PF_type & PT_WRITE ? 1: 0;
filePropDlg.m_bFileReadProt=pOldFile->PF_type & PT_READ ? 1: 0;
filePropDlg.m_bFileHide=pOldFile->PF_type & PT_HIDE? 1: 0;
}
else
{
filePropDlg.m_bFileDeleteProt=defProtectionType & PT_DELETE ? 1: 0;
filePropDlg.m_bFileWriteProt=defProtectionType & PT_WRITE ? 1: 0;
filePropDlg.m_bFileReadProt=defProtectionType & PT_READ ? 1: 0;
filePropDlg.m_bFileHide=defProtectionType & PT_HIDE? 1: 0;
}
if(filePropDlg.DoModal()==IDOK)
{
if(lpszOldFile)
protFileList.Remove(strPath);
strPath=filePropDlg.m_strPath;
FGFormatPath(strPath, FALSE);
protFileList.Add(strPath, filePropDlg.m_bFileDeleteProt*PT_DELETE
| filePropDlg.m_bFileWriteProt*PT_WRITE | filePropDlg.m_bFileReadProt*PT_READ
| filePropDlg.m_bFileHide*PT_HIDE);
//Send message to update view.
::SendNotifyMessage(((CMainFrame *)::AfxGetMainWnd())->GetActiveView()->GetSafeHwnd(), WM_SHOW_DATA, 3, NULL);
}
}
BOOL RemoveProtectedFile(LPCTSTR lpszProtectedFilePath, BOOL bIsFromIniFile)
{
if(bIsFromIniFile)
{
CString strPath=lpszProtectedFilePath;
FGFormatPath(strPath);
protFileList.Remove(strPath);
}
else return RemoveProtectedFile(lpszProtectedFilePath);
return TRUE;
}
BOOL AddProtectedFile(LPCTSTR lpszProtectedFilePath, WORD protectionType, BOOL bIsToIniFile)
{
if(bIsToIniFile)
{
CString strPath=lpszProtectedFilePath;
FGFormatPath(strPath);
protFileList.Add(strPath, protectionType);
}
else return AddProtectedFile(lpszProtectedFilePath, protectionType);
return TRUE;
}
/*
//Read protected file infomation from the ini file.
BOOL ReadProtectedFileInfo(KProtectedFileList *pProtFileList)
{
SetDeviceBusy(TRUE); //debug this still didn't work.
CIniFile iniFile;
PROTECTED_FILE protFile;
if(!iniFile.Open(lpszIniFilePath))
{
SetDeviceBusy(FALSE);
return FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -