📄 tryreaddirectorychangeswview.cpp
字号:
// TryReadDirectoryChangesWView.cpp : implementation of the CTryReadDirectoryChangesWView class
//
#include "stdafx.h"
#include "TryReadDirectoryChangesW.h"
#include "TryReadDirectoryChangesWDoc.h"
#include "TryReadDirectoryChangesWView.h"
#include "XBrowseForFolder.h"
#include <winioctl.h>
#include "IOCTLCMD.H"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
inline void GetSystemErrorString(DWORD dwErrorCode, LPTSTR ptsBuffer, DWORD dwSize)
{
::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwErrorCode, 0, ptsBuffer, dwSize, NULL);
}
/////////////////////////////////////////////////////////////////////////////
// CTryReadDirectoryChangesWView
IMPLEMENT_DYNCREATE(CTryReadDirectoryChangesWView, CEditView)
BEGIN_MESSAGE_MAP(CTryReadDirectoryChangesWView, CEditView)
//{{AFX_MSG_MAP(CTryReadDirectoryChangesWView)
ON_COMMAND(ID_EDIT_SELECTDIRECTORY, OnEditSelectdirectory)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_UPDATE_COMMAND_UI(ID_EDIT_SELECTDIRECTORY, OnUpdateEditSelectdirectory)
#ifdef FORDEMO
ON_WM_TIMER()
#endif
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTryReadDirectoryChangesWView construction/destruction
CTryReadDirectoryChangesWView::CTryReadDirectoryChangesWView() :
m_nLineNo(0)
, m_hDir(INVALID_HANDLE_VALUE)
, m_nReadTimes(0)
, m_hVxD(INVALID_HANDLE_VALUE)
#ifndef FORDEMO
, m_dwDir(0)
#else
, m_fIsMonitoring(FALSE)
, m_nSeqNo(0)
#endif
//, m_csDir(_T("E:\\Working\\Test\\SyncTest"))
{
// TODO: add construction code here
m_fIsNT = GetVersion() < 0x80000000;
}
CTryReadDirectoryChangesWView::~CTryReadDirectoryChangesWView()
{
OutputDebugString(_T("CTryReadDirectoryChangesWView::~CTryReadDirectoryChangesWView()\n"));
}
BOOL CTryReadDirectoryChangesWView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
BOOL bPreCreated = CEditView::PreCreateWindow(cs);
// cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL); // Enable word-wrapping
return bPreCreated;
}
/////////////////////////////////////////////////////////////////////////////
// CTryReadDirectoryChangesWView drawing
void CTryReadDirectoryChangesWView::OnDraw(CDC* pDC)
{
CTryReadDirectoryChangesWDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CTryReadDirectoryChangesWView diagnostics
#ifdef _DEBUG
void CTryReadDirectoryChangesWView::AssertValid() const
{
CEditView::AssertValid();
}
void CTryReadDirectoryChangesWView::Dump(CDumpContext& dc) const
{
CEditView::Dump(dc);
}
CTryReadDirectoryChangesWDoc* CTryReadDirectoryChangesWView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTryReadDirectoryChangesWDoc)));
return (CTryReadDirectoryChangesWDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTryReadDirectoryChangesWView message handlers
/*
VOID CALLBACK FileIOCompletionRoutine(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped)
{
if ( dwErrorCode == 0 )
{
}
return;
}
*/
void CTryReadDirectoryChangesWView::OnUpdateEditSelectdirectory(CCmdUI* pCmdUI)
{
if ( m_fIsNT )
pCmdUI->SetCheck(m_hDir != INVALID_HANDLE_VALUE);
else
{
pCmdUI->Enable(m_hVxD != INVALID_HANDLE_VALUE);
#ifndef FORDEMO
pCmdUI->SetCheck(m_dwDir != 0);
#else
pCmdUI->SetCheck(m_fIsMonitoring);
#endif
}
}
void CTryReadDirectoryChangesWView::OnEditSelectdirectory()
{
if ( m_fIsNT )
{
TCHAR tsNewDir[MAX_PATH + 1];
LPCTSTR msg = NULL;
EnterCriticalSection(&m_objCriticalSection);
if ( m_hDir != INVALID_HANDLE_VALUE )
{
CancelIo(m_hDir);
LeaveCriticalSection(&m_objCriticalSection);
Sleep(0);
EnterCriticalSection(&m_objCriticalSection);
CloseHandle(m_hDir);
m_hDir = INVALID_HANDLE_VALUE;
}
LeaveCriticalSection(&m_objCriticalSection);
if ( XBrowseForFolder(this->m_hWnd, (LPCTSTR)m_csDir, tsNewDir, MAX_PATH + 1) )
{
m_csDir = tsNewDir;
m_hDir = CreateFile(tsNewDir, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL);
// m_hDir = CreateFile(tsNewDir, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if ( m_hDir == INVALID_HANDLE_VALUE )
msg = _T("Failed in CreateFile!");
else
{
// m_nLineNo = 0;
// if ( !ReadDirectoryChangesW(m_hDir, m_cBuf, 65536, 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_CREATION | FILE_NOTIFY_CHANGE_SECURITY, &m_dwBytesReturned, &m_objOverlapped, CTryReadDirectoryChangesWView::FileIOCompletionRoutine) )
// if ( !ReadDirectoryChangesW(m_hDir, m_cBuf, 65536, 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_CREATION | FILE_NOTIFY_CHANGE_SECURITY, &m_dwBytesReturned, &m_objOverlapped, FileIOCompletionRoutine) )
if ( !ReadDirectoryChangesW(m_hDir, m_cBuf, 65536, 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_SECURITY, NULL, &m_objOverlapped, NULL) )
msg = _T("Failed in ReadDirectoryChangesW!");
}
}
if ( msg != NULL )
MessageBox(msg);
}
#ifndef FORDEMO
else
{
TCHAR tsNewDir[MAX_PATH + 1];
LPCTSTR msg = NULL;
EnterCriticalSection(&m_objCriticalSection);
if ( m_dwDir != 0 )
{
DeviceIoControl(m_hVxD, DIRMON_CancelReadChange, NULL, NULL, NULL, NULL, NULL, NULL);
DeviceIoControl(m_hVxD, DIRMON_RemoveMonitorPath, &m_dwDir, sizeof(m_dwDir), NULL, NULL, NULL, NULL);
m_dwDir = 0;
LeaveCriticalSection(&m_objCriticalSection);
Sleep(0);
}
else
LeaveCriticalSection(&m_objCriticalSection);
if ( XBrowseForFolder(this->m_hWnd, (LPCTSTR)m_csDir, tsNewDir, MAX_PATH + 1) )
{
TCHAR tsShortNewDir[MAX_PATH];
CHAR sBuf[MAX_PATH * 2];
int nNewDirLen;
DWORD dwRetBytes = 0;
m_csDir = tsNewDir;
GetShortPathName(tsNewDir, tsShortNewDir, sizeof(tsShortNewDir));
#ifdef UNICODE
WideCharToMultiByte(CP_THREAD_ACP, 0, tsNewDir, _tcslen(tsNewDir) + 1, sBuf, sizeof(sBuf), NULL, NULL);
nNewDirLen = strlen(sBuf) + 1;
WideCharToMultiByte(CP_THREAD_ACP, 0, tsShortNewDir, _tcslen(tsShortNewDir) + 1, sBuf + nNewDirLen, sizeof(sBuf) - nNewDirLen, NULL, NULL);
#else
strcpy(sBuf, tsNewDir);
nNewDirLen = strlen(sBuf) + 1;
strcpy(sBuf + nNewDirLen, tsShortNewDir);
#endif
nNewDirLen += strlen(sBuf + nNewDirLen) + 1;
if ( DeviceIoControl(m_hVxD, DIRMON_AddMonitorPath, sBuf, nNewDirLen, &m_dwDir, sizeof(m_dwDir), &dwRetBytes, NULL) && m_dwDir != 0 )
{
if ( !DeviceIoControl(m_hVxD, DIRMON_ReadChange, NULL, NULL, m_cBuf, 65536, NULL, &m_objOverlapped) && GetLastError() != ERROR_IO_PENDING )
{
msg = _T("Failed in DeviceIoContrl(AddMonitorPath)");
DeviceIoControl(m_hVxD, DIRMON_RemoveMonitorPath, &m_dwDir, sizeof(m_dwDir), NULL, NULL, NULL, NULL);
m_dwDir = 0;
}
}
else
msg = _T("Failed in DeviceIoControl(AddMonitorPath)");
}
if ( msg != NULL )
MessageBox(msg);
}
#else
else
{
DWORD dwReturnBytes;
TCHAR tsNewDir[MAX_PATH + 1];
LPCTSTR msg = NULL;
if ( m_fIsMonitoring )
{
KillTimer(1);
DeviceIoControl(m_hVxD, FILEMON_stopfilter, NULL, 0, NULL, 0, &dwReturnBytes, NULL);
DeviceIoControl(m_hVxD, FILEMON_zerostats, NULL, 0, NULL, 0, &dwReturnBytes, NULL);
m_fIsMonitoring = FALSE;
m_nSeqNo = 0;
}
if ( XBrowseForFolder(this->m_hWnd, (LPCTSTR)m_csDir, tsNewDir, MAX_PATH + 1) )
{
CString csFilter;
FILTER filter = {"*", "", "*", "", TRUE, TRUE};
m_csDir = tsNewDir;
if ( m_csDir[m_csDir.GetLength() - 1] == '\\' )
csFilter = m_csDir + _T("*.*");
else
csFilter = m_csDir + _T("\\*.*");
strcpy(filter.pathfilter, (LPCSTR)csFilter);
if ( !DeviceIoControl(m_hVxD, FILEMON_setfilter, &filter, sizeof(filter), NULL, 0, &dwReturnBytes, NULL) )
msg = _T("Failed in setting path filter...");
if ( !DeviceIoControl(m_hVxD, FILEMON_startfilter, NULL, 0, NULL, 0, &dwReturnBytes, NULL) )
msg = _T("Failed in starting filter...");
else
{
m_fIsMonitoring = TRUE;
SetTimer(1, 500/*ms*/, NULL);
}
}
if ( msg != NULL )
MessageBox(msg);
}
#endif
}
/*
VOID CALLBACK CTryReadDirectoryChangesWView::FileIOCompletionRoutine(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped)
{
if ( dwErrorCode == 0 )
{
CTryReadDirectoryChangesWView* pView = (CTryReadDirectoryChangesWView*)lpOverlapped->hEvent;
CString csText;
PFILE_NOTIFY_INFORMATION p = (PFILE_NOTIFY_INFORMATION)pView->m_cBuf;
LPCTSTR lpctstrAction = NULL;
TCHAR tsFilename[MAX_PATH + 1];
CString cs;
EnterCriticalSection(&pView->m_objCriticalSection);
pView->GetEditCtrl().GetWindowText(csText);
while ( 1 )
{
#ifdef UNICODE
memcpy(tsFilename, p->FileName, p->FileNameLength);
tsFilename[p->FileNameLength] = NULL;
GetLongPathName(tsFilename, tsFilename, MAX_PATH + 1);
#else
DWORD ret = WideCharToMultiByte(CP_THREAD_ACP, 0, p->FileName, (p->FileNameLength >> 1) + 1, tsFilename, MAX_PATH + 1, NULL, NULL);
if ( ret == 0 )
strcpy(tsFilename, "Error to convert the filename!");
else
GetLongPathName(tsFilename, tsFilename, MAX_PATH + 1);
#endif
switch ( p->Action )
{
case FILE_ACTION_ADDED:
lpctstrAction = _T("File Added"); break;
case FILE_ACTION_REMOVED:
lpctstrAction = _T("File Removed"); break;
case FILE_ACTION_MODIFIED:
lpctstrAction = _T("File Modified"); break;
case FILE_ACTION_RENAMED_OLD_NAME:
lpctstrAction = _T("File Renamed, Old Name"); break;
case FILE_ACTION_RENAMED_NEW_NAME:
lpctstrAction = _T("File Renamed, New Name"); break;
}
cs.Format(_T("%s: %s"), lpctstrAction, tsFilename);
csText += cs;
if ( p->NextEntryOffset )
p = (PFILE_NOTIFY_INFORMATION)((char*)p + p->NextEntryOffset);
else
break;
}
pView->SetWindowText(csText);
pView->m_dwBytesReturned = 0;
ReadDirectoryChangesW(pView->m_hDir, pView->m_cBuf, 65536, 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_CREATION | FILE_NOTIFY_CHANGE_SECURITY, &pView->m_dwBytesReturned, &pView->m_objOverlapped, CTryReadDirectoryChangesWView::FileIOCompletionRoutine);
LeaveCriticalSection(&pView->m_objCriticalSection);
}
}
*/
int CTryReadDirectoryChangesWView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CEditView::OnCreate(lpCreateStruct) == -1)
return -1;
if ( m_fIsNT )
{
m_hEventArray[0] = m_objOverlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
m_hEventArray[1] = CreateEvent(NULL, TRUE, FALSE, NULL);
InitializeCriticalSection(&m_objCriticalSection);
AfxBeginThread((AFX_THREADPROC)CTryReadDirectoryChangesWView::ReadDirectoryChangesWCompletionRoutine, this);
}
#ifndef FORDEMO
else
{
if ( m_hVxD == INVALID_HANDLE_VALUE && (m_hVxD = CreateFile(_T("\\\\.\\dirmon.vxd"), 0, 0, NULL, 0, FILE_FLAG_OVERLAPPED | FILE_FLAG_DELETE_ON_CLOSE, NULL)) != INVALID_HANDLE_VALUE )
{
m_hEventArray[0] = m_objOverlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
m_hEventArray[1] = CreateEvent(NULL, TRUE, FALSE, NULL);
InitializeCriticalSection(&m_objCriticalSection);
AfxBeginThread((AFX_THREADPROC)CTryReadDirectoryChangesWView::ReadDirectoryChangesWCompletionRoutine, this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -