How to copy raw memory to Buffer in nodejs?

蓝咒 提交于 2020-03-14 19:01:12

问题


I use node and node-ffi. I get a callback from native/C that passes a (void *,size_t) to indicate a memory region with interesting data. I'd like to take that and create Buffer with the same contents.

Basically:

function callback_on_write(ptr, size)
{
    var buffer = new Buffer(size);
    buffer.somehow_copy_from_memory(ptr, size);
}

How do I copy raw memory to Buffer?


回答1:


Use ref.reinterpret(buffer, size, offset).

Returns a new Buffer instance with the specified size, with the same memory address as buffer.

var ref = require('ref');

function callback_on_write(ptr, size)
{
    var buffer = ref.reinterpret(ptr, size);
}


来源:https://stackoverflow.com/questions/38476182/how-to-copy-raw-memory-to-buffer-in-nodejs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!