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

📄 fgdevice.cpp

📁 文件加密的过滤驱动程序源代码
💻 CPP
字号:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//FGDevice.cpp

#include	"StdAfx.h"
#include	"..\HookShr\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"

HANDLE hDevice=NULL;		//NULL means device is not loaded;
BOOL bIsHookInstalled=FALSE;

//Load driver.
BOOL LoadDevice(LPCTSTR lpszDevicePath)
{
	if(hDevice)		//device has been loaded
		return TRUE;

	hDevice=CreateFile(lpszDevicePath,0,0,0,CREATE_NEW,FILE_FLAG_DELETE_ON_CLOSE,0);

	if(hDevice==INVALID_HANDLE_VALUE)
	{
		hDevice=NULL;
		return FALSE;
	}

	return TRUE;
}

//Unload driver.
BOOL UnloadDevice()
{
	if(!hDevice)	return TRUE;

	if(!CloseHandle(hDevice))	return FALSE;

	return TRUE;
}

//Install file system api hook to start protection.
BOOL InstallHook()
{
	if(!hDevice) return 0;
	if(bIsHookInstalled)  return 1;

	DWORD ret;
	if(!DeviceIoControl(hDevice,FG_DIOC_INSTALL_HOOK,(void *)(protFileList.bDelBak* 0x1 | protFileList.bWriteBak* 0x2),0,0,0,&ret,0))
	{
		UninstallHook();
		bIsHookInstalled=FALSE;
		return 0;
	}
		
	bIsHookInstalled=TRUE;
	return 1;
}
		
//Uninstall file system api hook. Protection will be paused.
BOOL UninstallHook()
{
	if(hDevice==NULL) return 0;

	if(!bIsHookInstalled)  return 1;

	DWORD ret;
	if(!DeviceIoControl(hDevice,FG_DIOC_UNINSTALL_HOOK,NULL,0,NULL,0,&ret,0))  //uninstall hook.
		return 0;

	bIsHookInstalled=FALSE;
	return TRUE;
}

//Tell device to read protection file information from  ini file.
BOOL DeviceReadProtectedFileInfo(LPCTSTR lpszIniPath)
{
	if(hDevice==NULL) return 0;

	DWORD ret;
	if(!DeviceIoControl(hDevice,FG_DIOC_READ_PROTECTED_FILE_INFO,(void *)lpszIniPath,lstrlen(lpszIniPath),NULL,0,&ret,0))  //read  file infomation.
		return 0;

	return TRUE;
}

//Send new protected file information to device.
BOOL AddProtectedFile(LPCTSTR lpszProtectedFilePath, WORD protectionType)
{
	if(hDevice==NULL)
	{
		return FALSE;
	}

	CString strPath=lpszProtectedFilePath;
	FGFormatPath(strPath);

	//if Device is loaded, call the device to add it.
	DWORD ret;
	PROTECTED_FILE protFile(strPath, protectionType);
	if(!DeviceIoControl(hDevice,FG_DIOC_ADD_PROTECTED_FILE,(void *)&protFile,0,NULL,0,&ret,0))  //uninstall hook.
		return 0;

	return TRUE;
}

//Tell device to remove protection of a path.
BOOL RemoveProtectedFile(LPCTSTR lpszProtectedFilePath)
{
	if(hDevice==NULL)	return 0; 

	CString strPath=lpszProtectedFilePath;
	FGFormatPath(strPath);

	DWORD ret;
	if(!DeviceIoControl(hDevice,FG_DIOC_REMOVE_PROTECTED_FILE,(void *)(LPCTSTR)strPath,0,NULL,0,&ret,0))  //uninstall hook.
		return 0;

	return TRUE;
}

//Tell device to reread protected file information from ini file.
BOOL DeviceFlushProtectedFileInfo()
{
	if(hDevice==NULL) return 0;

	DWORD ret;
	if(!DeviceIoControl(hDevice,FG_DIOC_FLUSH_PROTECTED_FILE_INFO,(PVOID)lpszIniFilePath,0,NULL,0,&ret,0))
		return 0;

	return TRUE;
}

//Send extra information.
BOOL SendExtraInfo()
{
	if(hDevice==NULL) return 0;

	DWORD ret;
	if(!DeviceIoControl(hDevice,FG_DIOC_EXTRA_INFO,(PVOID)(protFileList.bDelBak* 0x1 | protFileList.bWriteBak* 0x2),0,(PVOID)maxBakNum,0,&ret,0))
		return 0;

	return 1;
}

//Handling device error:
//err: error code.	severity: 0 means a fatal error, 1 a minor one.
void DeviceErrorHandler(unsigned int err, int severity/*=1*/)
{
	char szErrorInfo[512];
	
	switch(err)
	{
		case FG_ERR_CANNOT_MAKE:
			sprintf(szErrorInfo, "无法打开或创建ini文件, 被保护文件信息不能被读出或保存。");
			break;
		
		case FG_ERR_CANNOT_WRITE_RECORD:
			ErrorHandler(err);
			break;
			
		case FG_ERR_INI_FILE_NOT_FOUND:
			sprintf(szErrorInfo, "未能找到ini文件,被保护文件信息可能丢失。一些文件可能无法保护。");
			//if(::MessageBox(NULL, "未能找到ini文件,被保护文件信息可能丢失。一些文件可能无法保护。\n是否继续文件保护?", "FileGuard message:", MB_YESNO|MB_SYSTEMMODAL)==IDNO)
			//	((CMainFrame *)AfxGetMainWnd())->OnStopProtection();
			goto NO_MORE_MESSAGE;
			
		case FG_ERR_INI_FILE_CORRUPTED:
			sprintf(szErrorInfo, "ini文件被损坏,被保护文件信息可能丢失..");
			break;
		
		case FG_ERR_WARNING:	//Protecting warning:
			if(!bWarning)
				return;
			switch(severity)
			{
				case FG_WARN_PT_DELETE:
					sprintf(szErrorInfo, "此文件正被保护,不能删除。");
					break;
				case FG_WARN_PT_WRITE:
					sprintf(szErrorInfo, "此文件正被保护,不能修改。");
					break;
				case FG_WARN_PT_READ:
					sprintf(szErrorInfo, "此文件正被保护,不能读出。");
					break;
				default:
					sprintf(szErrorInfo, "此文件正被保护。");
			}
			break;
			
		default:
			sprintf(szErrorInfo, "发生未知错误,程序可能无法正常工作。");
	}
	
	MessageBox(AfxGetMainWnd()->GetSafeHwnd(), szErrorInfo, "FileGuard Message", MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL);
	
	NO_MORE_MESSAGE: ;
}

//Tell device stop protection for a while.
//FileGuard app is about to access some protected files.
BOOL SetDeviceBusy(BOOL busy)
{
	if(hDevice==NULL) 
	{
		if(hPrevInstanceWnd && !::SendMessage(hPrevInstanceWnd, WM_SET_DEVICE_BUSY, busy, 0))
			return FALSE;
		else return TRUE;
	}

	DWORD ret;
	if(!DeviceIoControl(hDevice,FG_SET_DEVICE_BUSY,(void *)busy,0,0,0,&ret,0))
		return FALSE;

	if(busy)
		//Set a higher priority for the process in order to make sure of enough time to complete operation.
		::SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
	else
		::SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);	

	return TRUE;
}

⌨️ 快捷键说明

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