问题
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 asbuffer
.
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