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

标签 javascript firefox firefox-addon xulrunner jsctypes

我正在尝试实现 answer to this question关于异步监视 Windows 文件系统。我在 ChomeWorker 中使用 js ctypes 作为 XULRunner 应用程序的一部分,但我认为如果我作为 Firefox 附加组件实现,这将是相同的。

作为任务的一部分,我尝试如下声明函数 ReadDirectoryChangesW(基于我对 js ctypes 和 MSDN documentation 的有限了解)。

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()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 .

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);

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

关于javascript - 如何在 XULRunner 中使用 ReadDirectoryChangesW (js-ctypes),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23478502/

相关文章:

javascript - "memset"没有DLL所以如何ctype它

javascript - 应用程序脚本中的谷歌图像搜索

javascript - 在 AngularJs 中动态显示/隐藏元素

javascript - 如何将简单值从 axios.post 发送到 asp.net 核心?

css - Firefox 上背景大小的封面

Firefox 右键菜单太长

ajax - 如何找出发起ajax请求的那段代码

javascript - 递归:如何获取某个节点的所有(子)边?

javascript - Firefox 中 indexedDB.open 出现“UnknownError”

CSS 显示 :table not working in Firefox