如何在 XULRunner (js-ctypes) 中使用 ReadDirectoryChangesW

2024-02-25

我正在尝试实施回答这个问题 https://stackoverflow.com/questions/11495227/how-can-i-monitor-a-file-asynchronously-in-firefox关于异步监视 Windows 文件系统。我在 ChomeWorker 中使用 js ctypes 作为 XULRunner 应用程序的一部分,但我认为如果我作为 Firefox 插件实现,这将是相同的。

作为任务的一部分,我尝试声明函数 ReadDirectoryChangesW 如下(基于我对 js ctypes 和MSDN 文档 http://msdn.microsoft.com/en-us/library/windows/desktop/aa365465%28v=vs.85%29.aspx).

const BOOL = ctypes.bool;
const DWORD = ctypes.uint32_t;
const LPDWORD = ctypes.uint32_t.ptr;
const HANDLE = ctypes.int32_t;
const LPVOID = ctypes.voidptr_t;

var library = self.library = ctypes.open("Kernel32.dll");

ReadDirectoryChangesW = library.declare(
  "ReadDirectoryChangesW"
, ctypes.winapi_abi
, BOOL    // return type
, HANDLE  // hDirectory
, LPVOID  // lpBuffer
, DWORD   // nBufferLength
, BOOL    // bWatchSubtree
, DWORD   // dwNotifyFilter
, LPDWORD // lpBytesReturned
);

另外(这里没有介绍),我已经声明了函数映射FindFirstChangeNotification() and WaitForSingleObject()这似乎工作正常。

我遇到的问题是,当文件系统事件发生时,我不知道应该将什么传递给 lpBuffer 参数,也不知道如何解释结果。

所有 C++ 示例似乎都使用DWORD数组,然后输出结果。我的尝试如下:

const DWORD_ARRAY = new ctypes.ArrayType(DWORD);  
var lBuffer = new DWORD_ARRAY(4000);
var lBufferSize = DWORD.size * 4000;
var lBytesOut = new LPDWORD();

ReadDirectoryChangesW(lHandle, lBuffer.address(), lBufferSize, true, WATCH_ALL, lBytesOut)

这似乎每次都会使 XULRunner 崩溃。

任何人都可以建议我应该为 lpBuffer 参数传递什么和/或如何从ReadDirectoryChangesW()?我在网上能找到的只是 C++ 示例,但它们没有太多帮助。谢谢。


这是一个更干净的解决方案:阅读评论,那里有很多学习内容。对于类型定义,see here https://github.com/Noitidart/jscFileWatcher/blob/36d53bc6b19f36e4612658b90260f4d4cbd2c49d/modules/ostypes_win.jsm.

var path = OS.Constants.Path.desktopDir; // path to monitor

var hDirectory = ostypes.API('CreateFile')(path, ostypes.CONST.FILE_LIST_DIRECTORY | ostypes.CONST.GENERIC_READ, ostypes.CONST.FILE_SHARE_READ | ostypes.CONST.FILE_SHARE_WRITE, null, ostypes.CONST.OPEN_EXISTING, ostypes.CONST.FILE_FLAG_BACKUP_SEMANTICS | ostypes.CONST.FILE_FLAG_OVERLAPPED, null);
console.info('hDirectory:', hDirectory.toString(), uneval(hDirectory));
if (ctypes.winLastError != 0) { //cutils.jscEqual(hDirectory, ostypes.CONST.INVALID_HANDLE_VALUE)) { // commented this out cuz hDirectory is returned as `ctypes.voidptr_t(ctypes.UInt64("0xb18"))` and i dont know what it will be when it returns -1 but the returend when put through jscEqual gives `"breaking as no targetType.size on obj level:" "ctypes.voidptr_t(ctypes.UInt64("0xb18"))"`
    console.error('Failed hDirectory, winLastError:', ctypes.winLastError);
    throw new Error({
        name: 'os-api-error',
        message: 'Failed to CreateFile',
    });
}

var dummyForSize = ostypes.TYPE.FILE_NOTIFY_INFORMATION.array(1)(); // accept max of 1 notifications at once (in application you should set this to like 50 or something higher as its very possible for more then 1 notification to be reported in one read/call to ReadDirectoryChangesW)
console.log('dummyForSize.constructor.size:', dummyForSize.constructor.size);
console.log('ostypes.TYPE.DWORD.size:', ostypes.TYPE.DWORD.size);
var dummyForSize_DIVIDED_BY_DwordSize = dummyForSize.constructor.size / ostypes.TYPE.DWORD.size;

console.log('dummyForSize.constructor.size / ostypes.TYPE.DWORD.size:', dummyForSize_DIVIDED_BY_DwordSize, Math.ceil(dummyForSize_DIVIDED_BY_DwordSize)); // should be whole int but lets round up with Math.ceil just in case

var temp_buffer = ostypes.TYPE.DWORD.array(Math.ceil(dummyForSize_DIVIDED_BY_DwordSize))();
var temp_buffer_size = temp_buffer.constructor.size; // obeys length of .array
console.info('temp_buffer.constructor.size:', temp_buffer.constructor.size); // will be Math.ceil(dummyForSize_DIVIDED_BY_DwordSize)

var bytes_returned = ostypes.TYPE.DWORD();
var changes_to_watch = ostypes.CONST.FILE_NOTIFY_CHANGE_LAST_WRITE | ostypes.CONST.FILE_NOTIFY_CHANGE_FILE_NAME | ostypes.CONST.FILE_NOTIFY_CHANGE_DIR_NAME; //ostypes.TYPE.DWORD(ostypes.CONST.FILE_NOTIFY_CHANGE_LAST_WRITE | ostypes.CONST.FILE_NOTIFY_CHANGE_FILE_NAME | ostypes.CONST.FILE_NOTIFY_CHANGE_DIR_NAME);

console.error('start hang');
var rez_RDC = ostypes.API('ReadDirectoryChanges')(hDirectory, temp_buffer.address(), temp_buffer_size, true, changes_to_watch, bytes_returned.address(), null, null);

var cntNotfications = 0;
var cOffset = 0;
while (cOffset < bytes_returned) {
    cntNotfications++;
    var cNotif = ctypes.cast(temp_buffer.addressOfElement(cOffset), ostypes.TYPE.FILE_NOTIFY_INFORMATION.ptr).contents; // cannot use `temp_buffer[cOffset]` here as this is equivlaent of `temp_buffer.addressOfElement(cOffset).contents` and cast needs a ptr
    console.info('cNotif:', cNotif.toString());
    cOffset += cNotif.NextEntryOffset; // same as doing cNotif.getAddressOfField('NextEntryoffset').contents // also note that .contents getter makes it get a primaive value so DWORD defined as ctypes.unsigned_long will not be returned as expected ctypes.UInt64 it will be primative (due to the .contents getter), so no need to do the typical stuff with a `var blah = ctypes.unsigned_long(10); var number = blah.value.toString();`
}
console.info('total notifications:', cntNotifications);

我正在努力让异步版本正常工作,但遇到了困难。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 XULRunner (js-ctypes) 中使用 ReadDirectoryChangesW 的相关文章

随机推荐