ReadDirectoryChangesW 监控文件夹 (一个简单的监控示例程序)(文件被修改了,也可以探测到)
- // .h文件
- #pragma once
- typedef void (*PFN_NotifyAction)(DWORD dwAction, LPWSTR szFile, DWORD dwLength);
- class CDirectoryWatch
- {
- public:
- CDirectoryWatch(void);
- virtual ~CDirectoryWatch(void);
- public:
- BOOL StartDirectoryWatch(LPCTSTR lpszDirectory, PFN_NotifyAction pFn_NotifyAction);
- BOOL StopDirectoryWatch(void);
- private:
- static UINT __cdecl ThreadProc(LPVOID lParam);
- static UINT __cdecl DirectoryWatch(LPVOID lParam);
- private:
- HANDLE m_hFile;
- CWinThread* m_pThread;
- TCHAR m_szDirectory[MAX_PATH];
- };
- // .cpp文件
- #include "StdAfx.h"
- #include "DirectoryWatch.h"
- #include <strsafe.h>
- typedef enum
- {
- MSG_STARTWATCH = (WM_USER + 0x11),
- MSG_STOPWATCH,
- MSG_EXITTHREAD
- };
- #define MAX_BUFFER_SIZE (1024)
- typedef struct _tagWATCHPARAMETERS
- {
- _tagWATCHPARAMETERS()
- {
- hFile = INVALID_HANDLE_VALUE;
- hEvent = NULL;
- memset(&ol, 0, sizeof(OVERLAPPED));
- pBuffer = NULL;
- dwBufferSize = 0;
- bExit = FALSE;
- pFn_NotifyAction = NULL;
- }
- HANDLE hFile;
- HANDLE hEvent;
- OVERLAPPED ol;
- BYTE* pBuffer;
- DWORD dwBufferSize;
- BOOL bExit;
- PFN_NotifyAction pFn_NotifyAction;
- }WATCH_PARAMETERS, *PWATCH_PARAMETERS;
- CDirectoryWatch::CDirectoryWatch() : m_hFile(INVALID_HANDLE_VALUE), m_pThread(NULL)
- {
- memset(m_szDirectory, 0, sizeof(m_szDirectory));
- m_pThread = AfxBeginThread(ThreadProc, NULL, 0, CREATE_SUSPENDED, 0, NULL);
- if(NULL == m_pThread)
- {
- TRACE("Error Code : %d\n", GetLastError());
- return ;
- }
- m_pThread->m_bAutoDelete = FALSE;
- m_pThread->ResumeThread();
- }
- CDirectoryWatch::~CDirectoryWatch()
- {
- if(INVALID_HANDLE_VALUE != m_hFile)
- {
- CloseHandle(m_hFile);
- m_hFile = INVALID_HANDLE_VALUE;
- }
- if((NULL != m_pThread) && (NULL != m_pThread->m_hThread))
- {
- m_pThread->PostThreadMessage(MSG_EXITTHREAD, 0, 0);
- WaitForSingleObject(m_pThread->m_hThread, INFINITE);
- delete m_pThread;
- m_pThread = NULL;
- }
- }
- BOOL CDirectoryWatch::StartDirectoryWatch(LPCTSTR lpszDirectory, PFN_NotifyAction pFn_NotifyAction)
- {
- if(NULL == m_pThread)
- {
- return FALSE;
- }
- if(NULL == lpszDirectory)
- {
- return FALSE;
- }
- if(NULL == pFn_NotifyAction)
- {
- return FALSE;
- }
- if(!PathFileExists(lpszDirectory))
- {
- TRACE("Error Code : %d\n", GetLastError());
- return FALSE;
- }
- if(!PathIsDirectory(lpszDirectory))
- {
- TRACE("Error Code : %d\n", GetLastError());
- return FALSE;
- }
- if(0 == _tcslen(m_szDirectory))
- {
- StringCchPrintf(m_szDirectory, _countof(m_szDirectory), _T("%s"), lpszDirectory);
- }
- else if(CSTR_EQUAL != CompareStringOrdinal(m_szDirectory, -1, lpszDirectory, -1, TRUE))
- {
- TRACE("Not Change Directory.\n");
- return FALSE;
- }
- if(INVALID_HANDLE_VALUE == m_hFile)
- {
- m_hFile = CreateFile(lpszDirectory, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
- NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL);
- if(INVALID_HANDLE_VALUE == m_hFile)
- {
- TRACE("Error Code : %d\n", GetLastError());
- return FALSE;
- }
- }
- return m_pThread->PostThreadMessage(MSG_STARTWATCH, (WPARAM)m_hFile, (LPARAM)pFn_NotifyAction);
- }
- BOOL CDirectoryWatch::StopDirectoryWatch()
- {
- if(NULL != m_pThread)
- {
- return m_pThread->PostThreadMessage(MSG_STOPWATCH, 0, 0);
- }
- return FALSE;
- }
- UINT __cdecl CDirectoryWatch::DirectoryWatch(LPVOID lParam)
- {
- WATCH_PARAMETERS* pParam = (WATCH_PARAMETERS*)lParam;
- if(NULL == pParam)
- {
- return 0;
- }
- HANDLE& hFile = pParam->hFile;
- BYTE* pBuffer = pParam->pBuffer;
- DWORD dwBufferSize = pParam->dwBufferSize;
- OVERLAPPED& ol = pParam->ol;
- HANDLE& hEvent = pParam->hEvent;
- BOOL& bExit = pParam->bExit;
- PFN_NotifyAction pFn_NotifyAction = pParam->pFn_NotifyAction;
- DWORD dwBytesReturn = 0;
- DWORD dwRet = WAIT_FAILED;
- DWORD dwOffSet = 0;
- TCHAR szFile[MAX_PATH] = {0};
- while(TRUE)
- {
- if(WAIT_OBJECT_0 != WaitForSingleObject(hEvent, INFINITE))
- {
- TRACE("Error Code : %d\n", GetLastError());
- break;
- }
- if(bExit)
- {
- break;
- }
- if(!ReadDirectoryChangesW(hFile, pBuffer, dwBufferSize, 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, &dwBytesReturn, &ol, NULL))
- {
- TRACE("Error Code : %d\n", GetLastError());
- break;
- }
- if(!GetOverlappedResult(hFile, &ol, &dwBytesReturn, TRUE))
- {
- TRACE("Error Code : %d\n", GetLastError());
- break;
- }
- FILE_NOTIFY_INFORMATION* pFileNotify = (FILE_NOTIFY_INFORMATION*)pBuffer;
- do
- {
- if(pFn_NotifyAction && (WAIT_OBJECT_0 == WaitForSingleObject(hEvent, 0)))
- {
- pFn_NotifyAction(pFileNotify->Action, pFileNotify->FileName, (pFileNotify->FileNameLength) / sizeof(WCHAR));
- }
- dwOffSet = pFileNotify->NextEntryOffset;
- pFileNotify = (FILE_NOTIFY_INFORMATION*)((BYTE*)pFileNotify + dwOffSet);
- } while (dwOffSet);
- }
- TRACE0("DirectoryWatch Thread Exit ... \n");
- return 0;
- }
- UINT __cdecl CDirectoryWatch::ThreadProc(LPVOID lParam)
- {
- WATCH_PARAMETERS* pParam = new WATCH_PARAMETERS;
- if(NULL == pParam)
- {
- goto __CLEANUP__;
- }
- BYTE* pBuffer = new BYTE[MAX_BUFFER_SIZE];
- if(NULL == pBuffer)
- {
- goto __CLEANUP__;
- }
- memset(pBuffer, 0, MAX_BUFFER_SIZE);
- pParam->pBuffer = pBuffer;
- pParam->dwBufferSize = MAX_BUFFER_SIZE;
- HANDLE hWatchEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
- if(NULL == hWatchEvent)
- {
- goto __CLEANUP__;
- }
- pParam->ol.hEvent = hWatchEvent;
- CWinThread* pThread = NULL;
- HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
- if(NULL == hEvent)
- {
- goto __CLEANUP__;
- }
- pParam->hEvent = hEvent;
- MSG msg;
- while(GetMessage(&msg, NULL, 0, 0))
- {
- switch(msg.message)
- {
- case MSG_STARTWATCH:
- {
- HANDLE hFile = (HANDLE)(msg.wParam);
- PFN_NotifyAction pFn_NotifyAction = (PFN_NotifyAction)(msg.lParam);
- if((INVALID_HANDLE_VALUE == hFile) && (NULL == pFn_NotifyAction))
- {
- break;
- }
- if(NULL == pThread)
- {
- pParam->hFile = hFile;
- pParam->pFn_NotifyAction = pFn_NotifyAction;
- pThread = AfxBeginThread(DirectoryWatch, (LPVOID)pParam, 0, CREATE_SUSPENDED, NULL);
- if(NULL == pThread)
- {
- goto __CLEANUP__;
- }
- pThread->m_bAutoDelete = FALSE;
- pThread->ResumeThread();
- }
- SetEvent(hEvent);
- }
- break;
- case MSG_STOPWATCH:
- {
- ResetEvent(hEvent);
- }
- break;
- case MSG_EXITTHREAD:
- {
- SetEvent(hEvent);
- pParam->bExit = FALSE;
- if((NULL != pThread) && (NULL != pThread->m_hThread))
- {
- WaitForSingleObject(pThread->m_hThread, INFINITE);
- delete pThread;
- pThread = NULL;
- }
- goto __CLEANUP__;
- }
- default:
- break;
- }
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- __CLEANUP__:
- if(NULL != hWatchEvent)
- {
- CloseHandle(hWatchEvent);
- hWatchEvent = NULL;
- }
- if(NULL != pBuffer)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- if(NULL != pParam)
- {
- delete pParam;
- pParam = NULL;
- }
- TRACE0("ThreadProc Thread Exit ...\n");
- return 0;
- }
- // 测试代码
- #include "stdafx.h"
- #include "DirectoryWatch.h"
- void NotifyAction(DWORD dwAction, LPWSTR szFile, DWORD dwLength)
- {
- switch(dwAction)
- {
- case FILE_ACTION_ADDED:
- wprintf(L"FILE_ACTION_ADDED: \n\t");
- break;
- case FILE_ACTION_REMOVED:
- wprintf(L"FILE_ACTION_REMOVED: \n\t");
- break;
- case FILE_ACTION_MODIFIED:
- wprintf(L"FILE_ACTION_MODIFIED: \n\t");
- break;
- case FILE_ACTION_RENAMED_OLD_NAME:
- wprintf(L"FILE_ACTION_RENAMED_OLD_NAME: \n\t");
- break;
- case FILE_ACTION_RENAMED_NEW_NAME:
- wprintf(L"FILE_ACTION_RENAMED_NEW_NAME: \n\t");
- break;
- default:
- break;
- }
- WCHAR szPath[MAX_PATH] = {0};
- wmemcpy(szPath, szFile, min(dwLength, MAX_PATH));
- wprintf(L"%s\n", szPath);
- }
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- CDirectoryWatch watch;
- wprintf(L"Start Directory Watch ...\n");
- watch.StartDirectoryWatch(_T("F:\\11"), NotifyAction);
- Sleep(30 * 1000);
- watch.StopDirectoryWatch();
- wprintf(L"Stop Directory Watch ...\n");
- Sleep(10 * 1000);
- wprintf(L"Start Directory Watch ...\n");
- watch.StartDirectoryWatch(_T("F:\\11"), NotifyAction);
- Sleep(30 * 1000);
- watch.StopDirectoryWatch();
- wprintf(L"Stop Directory Watch ...\n");
- Sleep(30 * 1000);
- wprintf(L"Process Exit ...\n");
- return 0;
- }
效果如下图所示:
http://blog.csdn.net/visualeleven/article/details/7562014
ReadDirectoryChangesW 监控文件夹 (一个简单的监控示例程序)(文件被修改了,也可以探测到)的更多相关文章
- gulp插件实现压缩一个文件夹下不同目录下的js文件(支持es6)
gulp-uglify:压缩js大小,只支持es5 安装: cnpm: cnpm i gulp-uglify -D yarn: yarn add gulp-uglify -D 使用: 代码实现1:压缩 ...
- META-INF文件夹是干啥的,META-INF文件夹的作用, META-INF文件夹能删吗
今天有人问到 META-INF文件夹是干啥的,META-INF文件夹的作用, META-INF文件夹能删吗,还有项目的META-INF下面一般会有个MANIFEST.MF 文件,都是干啥的. 百度搜了 ...
- selector是在文件夹drawable中进行定义的xml文件。
获取Drawable对象: Resources res = mContext.getResources(); Drawable myImage = res.getDrawable(R.drawable ...
- 导出不带.svn的文件夹或者是不含.class的文件
转载自:http://blog.csdn.net/z278718149/article/details/21537395 如何导出不带.svn的文件夹或者是不含.class的文件 在工作环境中,有的时 ...
- selector是在文件夹drawable中进行定义的xml文件转载 https://www.cnblogs.com/fx2008/p/3157040.html
获取Drawable对象: Resources res = mContext.getResources(); Drawable myImage = res.getDrawable(R.drawable ...
- 一个简单的P2P传输程序
写了一个简单的P2P传输程序,在P2P的圈子中传输文件,不过为了简便,这个程序没有真正的传输文件,只是简单的判断一下文件的位置在哪里.这个程序可以处理当有一个peer闪退的情况,在这种情况下,剩下的p ...
- Android加载SD卡目录,文件夹遍历,图片设置,设置文件对应打开方式等
此案例主要说的是Android使用GridView加载SD卡下所有目录,文件夹多层遍历,文件图标修改,设置文件对应打开方式等功能. 如图: 代码: public class GridViewFile ...
- IOS开发之小实例--使用UIImagePickerController创建一个简单的相机应用程序
前言:本篇博文是本人阅读国外的IOS Programming Tutorial的一篇入门文章的学习过程总结,难度不大,因为是入门.主要是入门UIImagePickerController这个控制器,那 ...
- iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序 一.plist文件和项目结构图 说明:这是一个嵌套模型的示例 二.代码示例: YYcarsgroup.h文件代码: // // YYcar ...
随机推荐
- 用Python作GIS之四:Tkinter基本界面的搭建
Python下的主窗口可以定义如下:def start(self): #self.project = Project("temp") #self.pro ...
- MySQL性能优化的最佳20+套经验
今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我 们程序员需要去关注的事情.当我们去设计数据库表结构,对操 ...
- (转)《深入理解java虚拟机》学习笔记10——并发编程(二)
Java的并发编程是依赖虚拟机内存模型的三个特性实现的: (1).原子性(Atomicity): 原子性是指不可再分的最小操作指令,即单条机器指令,原子性操作任意时刻只能有一个线程,因此是线程安全的. ...
- 【js】随机数
<script> function GetRandomNum(Min,Max){ var Range = Max - Min; var Rand = Math.random() ...
- IOS 基于APNS消息推送原理与实现(JAVA后台)
Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple Pu ...
- C#中DataTable与实体集合通用转换(使用扩展方法)
本案例提供了:把DataRow转换为单个实体.dataTable转换为List泛型支持时间格式转换. 下文的方法都是扩展方法.扩展方法要求写在静态类中,方法也要静态. 它必须在一个非嵌套.非泛型的静态 ...
- Week1 Team Homework #3: 软件工程在北航
在组内成员的共同努力,我们采访了几个学长学姐,顺利完成任务.反馈信息如下: 平均每周花在这门课上的时间 平均写的代码总行数 学到的最有用的部分 最没用的部分 <软件工程>最应该改进的地方 ...
- C#系统缓存全解析
原文:http://blog.csdn.net/wyxhd2008/article/details/8076105 目录(?)[-] 系统缓存的概述 页面输出缓存 页面局部缓存 文件缓存依赖 数据库缓 ...
- 【SharePoint 文档管理解决方案设计系列一】文档使用分析
在我们在 SharePoint 端设计文档管理解决方案之前我们要了解目前客户在他们已有的系统里是怎么对文档进行使用和管理的.只有了解了当前的使用情况才能根据客户的需求量身定做一套适合他们的有效的解决方 ...
- jquery中的事件
一.事件参数 function(event){} 1.停止冒泡事件 event.stopPropagation() <=> return false;2.阻止默认行为 even ...