gpt4 book ai didi

c++ - CreateFile,ReadDirectoryChanges 问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:03:37 26 4
gpt4 key购买 nike

我正在使用 ReadDirectoryChangesW 来监视我用 CreateFile 打开的文件夹,添加文件后我调用一个函数 (OnFileChanged ) 读取大小并打开它进行阅读,我的应用程序适用于小文件,但当我尝试将大文件复制到我的文件夹 (7,24 M) 时出现问题,并且我得到 Permission denied 调用 fopen 读取它后出错。

watching过程是根据this

    void Init(const QString FullPath)
{ ...
hDir = CreateFile(
dirPath, // pointer to the directory containing the tex files
FILE_LIST_DIRECTORY|GENERIC_READ, // access (read-write) mode
FILE_SHARE_READ|FILE_SHARE_DELETE|FILE_SHARE_WRITE, // share mode
NULL, // security descriptor
OPEN_EXISTING, // how to create
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED , // file attributes
NULL); // file with attributes to copy


SecureZeroMemory (&overl, sizeof(overl));
SecureZeroMemory(buffer, sizeof(buffer));
overl.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

// watch the directory
BOOL res= ReadDirectoryChangesW(
hDir, /* handle to directory */
&buffer[curBuffer], /* read results buffer */
sizeof(buffer[curBuffer]), /* length of buffer */
FALSE, /* monitoring option */
FILE_NOTIFY_CHANGE_FILE_NAME ,
//FILE_NOTIFY_CHANGE_LAST_WRITE, /* filter conditions */
NULL, /* bytes returned */
&overl, /* overlapped buffer */
NULL); /* completion routine */

}

void StartWatchThread()
{
// if the thread already exists then stop it
if (IsThreadRunning())
SynchronousAbort();

//CrashIf(!hDir);
if(!hDir)
{ qDebug()<<" handle "<<hDir<<" last error"<<GetLastError();
exit(-1);}

else
{// reset the hEvtStopWatching event so that it can be set if
// some thread requires the watching thread to stop
ResetEvent(hEvtStopWatching);

DWORD watchingthreadID;
qDebug()<<"befrore creating thread";
hWatchingThread = CreateThread(NULL, 0, WatchingThread, this, 0, &watchingthreadID);
qDebug()<<"watchingthreadID"<<watchingthreadID;
}
}


DWORD WINAPI WatchingThread(void *param)
{
//qDebug()<<"in WatchingThread";
FileWatcher *fw = (FileWatcher *)param;

HANDLE hp[2] = { fw->hEvtStopWatching, fw->overl.hEvent };
for (;;)
{

DWORD dwObj = WaitForMultipleObjects((sizeof(hp)/(sizeof(hp[0])))
, hp, FALSE, INFINITE);
if (dwObj == WAIT_OBJECT_0) // the user asked to quit the program
{
qDebug()<<"in WatchingThread the user asked to quit the program";
//exit(-1);
break;
}
if (dwObj != WAIT_OBJECT_0 + 1)
{
// BUG!
//assert(0);
qDebug()<<"dwObj "<<dwObj<<" last error "<<GetLastError();
break;
}
//qDebug()<<"WatchingThread fw->NotifyChange() ";

//if (fw->wakeup)
fw->NotifyChange();

}
return 0;
}


bool NotifyChange()
{
//qDebug()<<"in NotifyChange";

GetOverlappedResult(hDir, &overl, &dwNumberbytes, FALSE);

FILE_NOTIFY_INFORMATION *pFileNotify = (FILE_NOTIFY_INFORMATION *)buffer[curBuffer];
// Switch the 2 buffers
curBuffer = (curBuffer + 1) % (sizeof(buffer)/(sizeof(buffer[0])));
SecureZeroMemory(buffer[curBuffer], sizeof(buffer[curBuffer]));
// start a new asynchronous call to ReadDirectory in the alternate buffer
ReadDirectoryChangesW(
hDir, /* handle to directory */
&buffer[curBuffer], /* read results buffer */
sizeof(buffer[curBuffer]), /* length of buffer */
FALSE, /* monitoring option */
FILE_NOTIFY_CHANGE_FILE_NAME ,
//FILE_NOTIFY_CHANGE_LAST_WRITE, /* filter conditions */
NULL, /* bytes returned */
&overl, /* overlapped buffer */
NULL); /* completion routine */

// Note: the ReadDirectoryChangesW API fills the buffer with WCHAR strings.
for (;;) {

if (pFileNotify->Action == FILE_ACTION_ADDED)
{
qDebug()<<"in NotifyChange if ";
char szAction[42];
char szFilename[MAX_PATH] ;
memset(szFilename,'\0',sizeof( szFilename));
strcpy(szAction,"added");
wcstombs( szFilename, pFileNotify->FileName, MAX_PATH);

OnFileChanged(szFilename,szAction);
qDebug()<<"in NotifyChange after OnFileChanged ";

}

// step to the next entry if there is one
if (!pFileNotify->NextEntryOffset)
return false;
pFileNotify = (FILE_NOTIFY_INFORMATION *)((PBYTE)pFileNotify + pFileNotify->NextEntryOffset);
}
pFileNotify=NULL;
return true;
}

我无法弄清楚如何处理该文件并且不让我阅读它?我试图在调用 OnFileChanged 后调用 Sleep() 并在它完成时唤醒但徒劳无功。请有任何想法。

最佳答案

I call a function (OnFileChanged) that read the size and open it for reading

这几乎肯定迟早会失败。文件更改通知是在进程写入文件时生成的。如果进程尚未完成写入或以其他方式保持文件打开,这将阻止您打开文件。并且不允许读取共享,这对于写入文件的程序来说很常见。这肯定与文件大小有关,文件越大,写入时间越长,因此您尝试打开它而其他进程尚未关闭它的可能性就越大。

您需要处理这种可能发生的事故。您只能将路径保留在队列中,以便稍后尝试。

关于c++ - CreateFile,ReadDirectoryChanges 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13063710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com