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

📄 filewather.cpp

📁 一个网络监视的程序
💻 CPP
字号:
// FileWather.cpp: implementation of the CFileWather class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FileWather.h"
#include "string"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//CFileWather::m_hDir = INVALID_HANDLE_VALUE;

CFileWather::CFileWather(): 
m_strWatchDir(""), 
m_hThread(NULL),
m_hDir(INVALID_HANDLE_VALUE)
{
//	m_pDealFun = NULL;
}

CFileWather::~CFileWather()
{
  Close();
}

bool CFileWather::Run( LPCTSTR dir /*, LPDEALFUNCTION pDealFun*/ )
{
	m_strWatchDir = dir;
	// m_pDealFun = pDealFun;
    
	m_hDir = CreateFile(
		m_strWatchDir,
		GENERIC_READ|GENERIC_WRITE,
        FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
        NULL,
        OPEN_EXISTING,
        FILE_FLAG_BACKUP_SEMANTICS,
        NULL
		);

	DWORD ThreadId;
	m_hThread = CreateThread( NULL, 0, Routine, this, 0, &ThreadId );

	return ( NULL != m_hThread && INVALID_HANDLE_VALUE != m_hDir);
}

void CFileWather::Close()
{
	if( NULL != m_hThread )
	{
		TerminateThread( m_hThread, 0 );
		m_hThread = NULL;
	}
	if ( !m_hDir )
	{
		CloseHandle( m_hDir );
		m_hDir = INVALID_HANDLE_VALUE;
	}
}

DWORD WINAPI CFileWather::Routine( LPVOID lParam )
{
	typedef bool(_cdecl *Connect)(HANDLE, LPVOID, DWORD, BOOL, DWORD,
		LPDWORD lpBytesReturned, LPOVERLAPPED,
		LPOVERLAPPED_COMPLETION_ROUTINE );

	HINSTANCE hinstDLL = NULL; 
	hinstDLL = LoadLibrary("FileProbe.dll");
	if ( hinstDLL == 0 )
	{
		AfxMessageBox("加载链接库FileProbe.dll出错!");
		return 0;
	}

	Connect Proc;
	Proc = (Connect)GetProcAddress(hinstDLL, "ReadDirecChange");
	
	if ( Proc == 0 )
	{
		AfxMessageBox("FileProbe.dll取函数地址指针出错!");
		return 0;
	}

	char buf[ 2*(sizeof(FILE_NOTIFY_INFORMATION)+MAX_PATH) ];
	FILE_NOTIFY_INFORMATION *pNotify = (FILE_NOTIFY_INFORMATION *)buf;
	DWORD BytesReturned;

	CFileWather *obj = (CFileWather *)lParam;
    
	while (1)
	{
		if( Proc(
			obj->m_hDir,
			pNotify,
			sizeof(buf),
			true,
			FILE_NOTIFY_CHANGE_FILE_NAME | 
			FILE_NOTIFY_CHANGE_DIR_NAME |
			FILE_NOTIFY_CHANGE_ATTRIBUTES | 
			FILE_NOTIFY_CHANGE_SIZE | 
			FILE_NOTIFY_CHANGE_LAST_WRITE | 
			FILE_NOTIFY_CHANGE_LAST_ACCESS | 
			FILE_NOTIFY_CHANGE_CREATION | 
			FILE_NOTIFY_CHANGE_SECURITY,
			&BytesReturned,
			NULL,
			NULL ) 
			)
		{
			char tmp[MAX_PATH], l_strpath[MAX_PATH], str2[MAX_PATH];
			memset( tmp, 0, sizeof(tmp) );
			memset( l_strpath, 0, sizeof(l_strpath) );
			memset( str2, 0, sizeof(str2) );
			WideCharToMultiByte(
				CP_ACP, 
				0,
				pNotify->FileName,
				pNotify->FileNameLength/2,
				tmp,
				99,
				NULL,
				NULL);

			strcpy( l_strpath, tmp );
/*
			if( 0 != pNotify->NextEntryOffset )
			{
				PFILE_NOTIFY_INFORMATION p;
				p = (PFILE_NOTIFY_INFORMATION)((char*)pNotify + pNotify->NextEntryOffset);
				memset( tmp, 0, sizeof(tmp) );
				WideCharToMultiByte( 
					CP_ACP,
					0,
					p->FileName,
					p->FileNameLength/2,
					tmp,
					99,
					NULL,
					NULL );
				strcpy( str2, tmp );
			}*/
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
// * * * * * * * * * * 填加处理代码* * * * * * * * * * * * * * * * * * * * * * * *
// 
			//obj->m_pDealFun( (ACTION)pNotify->Action, str);

			int type; // 1-create, 2-modify, 3-delete
			std::string l_stract;
			switch ( (ACTION)pNotify->Action )
			{
			case ADDED:
				type = 1;
				l_stract = " CREATE ";
				break;
			case REMOVED:
				type = 2;
				l_stract = " DELETE ";
				break;
			case MODIFIED:
				type = 3;
				l_stract = " MODIFY ";
				break;
			default:
				type = 0;
			}

			if ( type > 0 )
			{
				std::string t_fullPath(obj->m_strWatchDir + l_strpath);
				CTime l_time = CTime::GetCurrentTime();
				std::string  strTime = l_time.Format( "%H:%M:%S" );
				EnterCriticalSection(&gCriticalSectionForPrtBuff);

				
				l_stract= strTime +" FILE "+ l_stract +"\""+ t_fullPath+"\"";
				(PtrBuffWrite->str)+=l_stract;
				(PtrBuffWrite->len)+=l_stract.size();
				
				LeaveCriticalSection(&gCriticalSectionForPrtBuff);
				CString temp;
		//		temp.Format("%s%s%s",strTime ,l_stract , t_fullPath);
	//			AfxMessageBox( temp);
				l_stract.empty();
				
			}
		}
		else
			break;
	}

	FreeLibrary(hinstDLL);
	return 0;
}

⌨️ 快捷键说明

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