Search code examples
cnode.jselectronffiref

How to save pointer in JavaScript and retrieve further


I am using a dll at the server side for some computation . I am calling the dll from nodeJS using ffi module , so somewhere in the middle i need to save the address of the C pointer and used it further but I don't know how to save it so that i can pass it after some time to dll for executing operation on the same address.

does anyone have idea how to do this.


Solution

  • After searching a lot and trying different-2 scenarios finally i found a solution that how to store the address and retrieve in future for further operations :-

    (Note:- here i am using ref, ffi modules) so for storing pointer , create a buffer that can hold pointer type like below

         const ref = require('ref');        
         const  ffi = require('ffi');
    
         var buf = ref.alloc('pointer');
         ref.writePointer(buf, 0, pointer);  // pointer or memory address
    

    so here you store the memory address in buffer and where you want to use you can retrieve it from buffer like below:-

         memory_pointer = ref.readPointer(buf,0);   // buf is buffer which we created above, 0 for offset 
    

    so now you get the memory_pointer so you can pass it to your function call using ffi module.