WCHAR_T的地址传递给ReadProcessMemory

时间:2014-06-22 10:35:28

标签: javascript firefox-addon jsctypes

我无法将WCHAR_T传递给ReadProcessMemory

这是如何成功地将指针地址传递给ReadProcessMemory,我可以用结构来做:

    remote_tbb = ralloc_alloc(struct_TBButton.size);
    var rez = SendMessage(hToolbar, TB_GETBUTTON, i, ctypes.voidptr_t(remote_tbb));
    if (!rez) { throw new Error('Failed on SendMessage of TB_GETBUTTON') }

    var local_tbb = new struct_TBButton();
    var retRead = ralloc_read(remote_tbb, local_tbb.address());
    var freed = ralloc_free(remote_tbb);

但现在我需要处理WCHAR_T,所以这就是我所拥有的:

    var chars = SendMessage(hToolbar, TB_GETBUTTONTEXTW, local_tbb.idCommand, ctypes.voidptr_t(0));
    console.log('chars=', chars, chars.toString(), uneval(chars));

    if (chars && parseInt(chars.toString()) > 0) {
        var remote_buf = ralloc_alloc(parseInt(chars.toString()));
        var charsRe = SendMessage(hToolbar, TB_GETBUTTONTEXTW, local_tbb.idCommand, ctypes.voidptr_t(remote_buf));
        console.log('charsRe=', charsRe);

        var local_buf = ctypes.jschar; //WCHAR_T
        var retRead = ralloc_read(remote_buf, local_buf.address()); ///PROBLEM LINE
        console.log('retRead=', retRead);

        var freed = ralloc_free(remote_buf);
        console.log('freed=', freed);

        console.log('Button Text = ', local_buf, local_buf.toString());
    } else {
        console.log('Button Text = NONE');
    }

所以我的问题在线:

var retRead = ralloc_read(remote_buf, local_buf.address());`

,具体在local_buf.address()

我的实验中出现的错误是:

  • expected type pointer, got ctypes.jschar
  • local_buf.address is not a function

那么如何传递WCHAR_T作为参考?

编辑: 这是我的ralloc_read实现:

function ralloc_read(remote_address, local_buffer) {
    var found_addr;
    for (var i = 0; i < buffers.length; i++) {
        if (buffers[i][0] == remote_address) {
            found_addr = buffers[i]
            break;
        }
    }
    if (!found_addr) {
        return null;
    }
    /*using the found remote address(found_addr[0]), 
     *i read size bytes (found_addr[1]) into my local_buffer*/
    //console.info('found_addr[0]', found_addr[0].toString());
    var rez = ReadProcessMemory(proc, found_addr[0], local_buffer, found_addr[1], 0);
    return rez;
}

1 个答案:

答案 0 :(得分:1)

如果ralloc_read调用ReadProcessMemory,那么您需要分配一个将收到结果的jschar数组。

var local_buf = ctypes.jschar.array()(chars);
ralloc_read(remote_buf, local_buf.address());
var str = local_buf.readString();

编辑但是,分配调用错误:

ralloc_alloc(parseInt(chars.toString()));

这将分配chars个字节,例如chars = 11,11个字节。 然而,wchar_t / jschar不是1个字节,而是2个字节。

ctypes.jschar.size
// 2

因此,您实际上需要分配更大的远程内存缓冲区:

ralloc_alloc(parseInt(chars.toString()) * ctypes.jschar.size);
// That would be ralloc_alloc(count * sizeof(wchar_t*)) in C/C++

local_buf的东西是正确的,但是如果js-ctypes数组知道数组元素类型的大小,它将自动计算所需的存储,因此ctypes.jschar.array()(11)缓冲区实际上有11个元素大小为2个字节,即11 items * 2 bytes/item == 22 bytes