Hi Steve,
A small fix for your module (but necessary!). The "watch_for_file_changes" currently crash when there is several changes at the same time in the directory. It happened to me at the 3rd change, the
if (outchars == 0) {
throw_error(fc->L,"wide char conversion borked");
}
is called in file_change_thread. It appears that to iterate on the PFILE_NOTIFY_INFORMATION structures, it uses directly
next = pni->NextEntryOffset
as an offset. However, for more than 2 changes, it should sum the offset. As the 'next' variable must be kept for the while condition, I have fixed it by adding a variable offset which is the sum of the previous 'next' (or pni->NextEntryOffset):
static void file_change_thread(FileChangeParms *fc) { // background file monitor thread
while (1) {
int next,offset;
DWORD bytes;
// This fills in some gaps:
// http://qualapps.blogspot.com/2010/05/understanding-readdirectorychangesw_19.html
if (! ReadDirectoryChangesW(lcb_handle(fc),lcb_buf(fc),lcb_bufsz(fc),
fc->subdirs, fc->how, &bytes,NULL,NULL)) {
throw_error(fc->L,"read dir changes failed");
}
next = 0;
offset = 0;
do {
int outchars;
char outbuff[MAX_PATH];
PFILE_NOTIFY_INFORMATION pni = (PFILE_NOTIFY_INFORMATION)(lcb_buf(fc)+offset);
outchars = WideCharToMultiByte(
get_encoding(), 0,
pni->FileName,
pni->FileNameLength/2, // it's bytes, not number of characters!
outbuff,sizeof(outbuff),
NULL,NULL);
if (outchars == 0) {
throw_error(fc->L,"wide char conversion borked");
}
outbuff[outchars] = '0円'; // not null-terminated!
lcb_call(fc,pni->Action,outbuff,0);
next = pni->NextEntryOffset;
offset += next;
} while (next != 0);
}
}
I have another problem, that I haven't resolved yet, involving thread and message queue (when using GUI application) . I take a look and described it in a few.
Alexandre
---------------------------------------------------------------------
Alexandre Rion
Fuel and Fire Department (MEYGU)
EADS / Airbus Military
---------------------------------------------------------------------