c++ - How should I read the filename in FILE_NOTIFY_INFORMATION struct -
i'm trying monitor file changes not sure on how read filename in file_notify_information struct:
handle dwchangehandles[2]; dword dwwaitstatus; wchangehandles[0] = findfirstchangenotification(dirname.c_str(), false, file_notify_change_last_write); if (dwchangehandles[0] == invalid_handle_value) printerr(__file__,__line__,"findfirstchangenotification function failed.\n"); ... if ((dwchangehandles[0] == null) || (dwchangehandles[1] == null)) //final validation printerr(__file__,__line__,"unexpected null findfirstchangenotification.\n"); while (true) { std::cout << "waiting notification...\n"; dwwaitstatus = waitformultipleobjects(2, dwchangehandles, false, infinite); if(dwwaitstatus==wait_object_0){ std::cout << "something changed\n"; dword bytesreturned; size_t buflen = 1024; file_notify_information buffer[buflen]; if (readdirectorychangesw(dwchangehandles[0], buffer, buflen, false, file_notify_change_last_write, &bytesreturned, null, null)){ std::wcout << std::wstring(buffer->filename)<< std::endl; //there nothing in expected output here } if (findnextchangenotification(dwchangehandles[0]) == false ) printerr(__file__,__line__,"findnextchangenotification function failed.\n"); } else if(dwwaitstatus==wait_timeout) printerr(__file__,__line__,"no changes in timeout period.\n"); else printerr(__file__,__line__,"unhandled dwwaitstatus.\n"); }
is there doing wrong
you have number of problems can see immediately:
according docs
readdirectorychangesw
function, buffer needsdword
-aligned. using buffer on stack isn't guaranteed - should allocate 1 heap instead.you don't seem using function correctly. call
readdirectorychangesw
first, , then wait on event. not other way around. whenreaddirectorychangesw
returns asynchronous call there no data in buffer @ point. need wait notification request has been completed before using buffer contents.findnextchangenotification
usedfindfirstchangenotification
, wrong. whenreaddirectorychangesw
completes need usenextentryoffset
field infile_notify_information
structure loop through returned events.
edit: since you've added more code question it's obvious mixing 2 apis. findfirstchangenotification
, findnextchangenotification
1 api, , readdirectorychangesw
another. believe you've been confused passage in docs:
this function not indicate change satisfied wait condition. retrieve information specific change part of notification, use readdirectorychangesw function.
i guess confusion understandable, 2 apis can't used together. if you're using findfirstchangenotification
notification something changed, , have re-read directory find out was. if want specific notifications @ file level have use readdirectorychangesw
monitoring.
Comments
Post a Comment