使用 ReadDirectoryChangesW API 监视目录

发布于 2024-10-17 07:24:21 字数 974 浏览 9 评论 0 原文

我正在尝试使用 e:\test rel="nofollow">ReadDirectoryChangesW API。

我的代码:

#define UNICODE
#define WIN32_WINNT 0x0500
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>


HANDLE hDir;


int _tmain(int argc, _TCHAR* argv[])
{
    FILE_NOTIFY_INFORMATION fniDir;
    DWORD i = 0;

    hDir = CreateFile(_T("e:\\test"), GENERIC_READ , FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);

    ReadDirectoryChangesW(hDir, &fniDir, sizeof(fniDir), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME, &i, NULL, NULL);
    while(TRUE)
    {


    if(i>0)
        wprintf(L"%s", fniDir.FileName);
    }

    CloseHandle(hDir);

    return 0;
}

我不知道我的代码有什么问题,因为我没有完全理解 ReadDirectoryChangesW 文档,特别是 LPOVERLAPPED 参数。

当我运行代码时,除了空白的控制台窗口之外,我没有得到任何输出。有人能指出我正确的方向吗?

谢谢。

I am trying to monitor a directory e:\test using ReadDirectoryChangesW API.

My Code :

#define UNICODE
#define WIN32_WINNT 0x0500
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>


HANDLE hDir;


int _tmain(int argc, _TCHAR* argv[])
{
    FILE_NOTIFY_INFORMATION fniDir;
    DWORD i = 0;

    hDir = CreateFile(_T("e:\\test"), GENERIC_READ , FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);

    ReadDirectoryChangesW(hDir, &fniDir, sizeof(fniDir), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME, &i, NULL, NULL);
    while(TRUE)
    {


    if(i>0)
        wprintf(L"%s", fniDir.FileName);
    }

    CloseHandle(hDir);

    return 0;
}

I don't know what's wrong with my code as I haven't understood ReadDirectoryChangesW documentation completely, specially the LPOVERLAPPED parameters.

When I run the code I don't get any output, except for a blank console window. Can someone point me in a right direction?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事还在继续 2024-10-24 07:24:21

如果您计划异步捕获更改通知,则仅需要重叠结构。在你的代码中你不需要它。

操作方法如下。

HANDLE hDir = CreateFile( 
        p.string().c_str(), /* pointer to the file name */
        FILE_LIST_DIRECTORY,                /* (this is important to be FILE_LIST_DIRECTORY!) access (read-write) mode */
        FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,  /* (file share write is needed, or else user is not able to rename file while you hold it) share mode */
        NULL, /* security descriptor */
        OPEN_EXISTING, /* how to create */
        FILE_FLAG_BACKUP_SEMANTICS, /* file attributes */
        NULL /* file with attributes to copy */
        );

    if(hDir == INVALID_HANDLE_VALUE){
        throw runtime_error(string("Could not open ").append(p.string()).append(" for watching!"));
    }

    FILE_NOTIFY_INFORMATION buffer[1024];
    DWORD BytesReturned;
    while( ReadDirectoryChangesW(
        hDir, /* handle to directory */
        &buffer, /* read results buffer */
        sizeof(buffer), /* length of buffer */
        TRUE, /* monitoring option */           
        FILE_NOTIFY_CHANGE_LAST_WRITE, /* filter conditions */
        &BytesReturned, /* bytes returned */
        NULL, /* overlapped buffer */
        NULL)){
            do{
                            //CANT DO THIS! FileName is NOT \0 terminated
                //wprintf("file: %s\n",buffer.FileName);
                            buffer += buffer.NextEntryOffset;
            }while(buffer.NextEntryOffset);
    }

You only need the overlapped struct if you plan on catching the changes notifications asynchronously. In your code you don't need it.

Here's how you do it.

HANDLE hDir = CreateFile( 
        p.string().c_str(), /* pointer to the file name */
        FILE_LIST_DIRECTORY,                /* (this is important to be FILE_LIST_DIRECTORY!) access (read-write) mode */
        FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,  /* (file share write is needed, or else user is not able to rename file while you hold it) share mode */
        NULL, /* security descriptor */
        OPEN_EXISTING, /* how to create */
        FILE_FLAG_BACKUP_SEMANTICS, /* file attributes */
        NULL /* file with attributes to copy */
        );

    if(hDir == INVALID_HANDLE_VALUE){
        throw runtime_error(string("Could not open ").append(p.string()).append(" for watching!"));
    }

    FILE_NOTIFY_INFORMATION buffer[1024];
    DWORD BytesReturned;
    while( ReadDirectoryChangesW(
        hDir, /* handle to directory */
        &buffer, /* read results buffer */
        sizeof(buffer), /* length of buffer */
        TRUE, /* monitoring option */           
        FILE_NOTIFY_CHANGE_LAST_WRITE, /* filter conditions */
        &BytesReturned, /* bytes returned */
        NULL, /* overlapped buffer */
        NULL)){
            do{
                            //CANT DO THIS! FileName is NOT \0 terminated
                //wprintf("file: %s\n",buffer.FileName);
                            buffer += buffer.NextEntryOffset;
            }while(buffer.NextEntryOffset);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文