ReadDirectoryChanges缓冲区对齐问题错误998 Windows

时间:2019-04-17 19:24:39

标签: c++ readdirectorychangesw

我有以下代码来监视系统上的文件更改,但出现错误代码998(无效的内存位置)。我的代码有什么问题?缓冲区对齐应该有问题,但是我找不到我的编码错误?

我用来监视系统更改的代码如下所示:

#if _WIN32
    HANDLE hDir = NULL;
    int TryCount = 0;
    char filename[MAX_PATH];

    HANDLE DirHandle;
    while (hDir == NULL || hDir == INVALID_HANDLE_VALUE) {
        DirHandle = CreateFileA(
            Settings.Directory.c_str(),
            GENERIC_READ | FILE_LIST_DIRECTORY,
            FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
            NULL,
            OPEN_EXISTING,
            FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
            NULL);
        if (DirHandle == INVALID_HANDLE_VALUE) {
            TryCount++;
            if (TryCount == 5) {
                string Message = "Failed to open directory: ";
                Message += Settings.Directory.Real;
                Message += ". Shutting down monitor";
                Log(Message, true, "Warning");
                SleepForSeconds(30);
                }
            }
        else {
            hDir = DirHandle;
            }
        }

    if (TryCount < 5) {
        int offset;
        string RenameOld;
        bool result = true;
        char buffer[65536]; //64kB
        DWORD dwBytesReturned = 0;
        FILE_NOTIFY_INFORMATION* pNotify;
        while (result) {
            result = ReadDirectoryChangesW(DirHandle, &buffer, sizeof(buffer), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE, &dwBytesReturned, NULL, NULL);
            if (result) {
                offset = 0;
                do {
                    pNotify = (FILE_NOTIFY_INFORMATION*)((char*)buffer + offset);
                    strcpy(filename, "");
                    WideCharToMultiByte(CP_ACP, 0, pNotify->FileName, pNotify->FileNameLength/2, filename, sizeof(filename), NULL, NULL);
                    filename[pNotify->FileNameLength/2] = '\0';
                    string FilePath = Settings.Directory + PathSeparator + filename;

                    if (pNotify->Action == FILE_ACTION_ADDED || pNotify->Action == FILE_ACTION_MODIFIED) {
                        HandleMonitorAction("PROCESS", FilePath);
                        }
                    else if (pNotify->Action == FILE_ACTION_REMOVED) {
                        HandleMonitorAction("DELETE", FilePath);
                        }
                    else if (pNotify->Action == FILE_ACTION_RENAMED_OLD_NAME) {
                        RenameOld = FilePath;
                        }
                    else if (pNotify->Action == FILE_ACTION_RENAMED_NEW_NAME) {
                        HandleMonitorAction("RENAME", FilePath, RenameOld);
                        RenameOld = "";
                        }
                    else {
                        Log("Unknown monitor-action received for the file: " + FilePath, false, "Warning");
                        }
                    offset += pNotify->NextEntryOffset;
                    } while(pNotify->NextEntryOffset); //(offset != 0);
                }
            else {

                //THIS GIVES THE 998 INVALID MEMORY ERROR

                string Message = "Stopped monitor. Error: ";
                Message += GetLastErrorAsString();
                Log(Message, true, "Error");
                return;
                }
            }
        }

请告知我的错误

0 个答案:

没有答案