I can get the correct native memory address if I use Node.js
directly such as:
// eg. native dll writen in rust; Of course, this part can be implemented in C or C++ or any others.
#[no_mangle]
pub unsafe extern fn example_concat(
a: *const c_char,
b: *const c_char
) -> *mut c_char
{
let sa = CStr::from_ptr(a).to_str().expect("cannot to_str from a");
let sb = CStr::from_ptr(b).to_str().expect("cannot to_str from b");
let sr = format!("{}{}", sa, sb);
let cr = CString::new(sr).expect("cannot create cr from sr");
println!("[from native lib] cr={} *=0x{:X}", cr.to_str().expect("cannot to_str from cr"), cr.as_ptr() as c_ulonglong);
cr.into_raw()
}
// tester.js
const ffi = require('ffi-napi');
const ref = require('ref-napi');
const str = ref.types.CString;
const lib = ffi.Library( 'lib.dll', { 'example_concat': [ "char *", [ str, str ] ] } );
const buffer = lib.example_concat( "su", "shi" );
console.log( '[from tester.js]', buffer.hexAddress(), buffer.readCString() );
The result of node tester.js
is:
[from native lib] cr=sushi *=0x1E7B3B3E8C0
[from tester.js] 000001E7B3B3E8C0 sushi
It's expected, no problems. But, the problem happen to me if I use the similar FFI method with react-electron
system.
// App.js in react-electron Node.js project
const remote = window.require('electron').remote;
const ffi = remote.require( 'ffi-napi' );
const ref = remote.require( 'ref-napi' );
const str = ref.types.CString;
const lib = ffi.Library( 'lib.dll', { 'example_concat': [ "char *", [ str, str ] ] } );
const buffer = lib.example_concat( "su", "shi" );
console.log( '[from App.js(electron)]', ref.hexAddress( buffer ), ref.readCString( buffer ) ); // <-- It returns wrong memory address!!
The result of this electron version is:
[from native lib] cr=sushi *=0x2BFA2F07A10
[from App.js(electron)] 000002BFA3BA7230 sushi
How can I get the correct native memory address in a project using electron
?
I got the answer for this case. In posted situation, I used nodeIntegration: true
then I implemented the example used const remote = window.require('electron').remote
and remote.require
features. Then, AddressForArgs
(†1) via ref.HexAddress
returns electron client based address maybe ( I don't know more internals of the node-ref-napi, node-ffi-napi and electron just now ).
Therefore, I tried change to nodeIntegration: false
setting with preload
features.
// preload.js
window.ffi = require( 'ffi-napi' );
window.ref = require( 'ref-napi' );
// App.js ( Fixed to the preload feature based )
// const remote = window.require('electron').remote;
const ffi = window.ffi; //remote.require( 'ffi-napi' );
const ref = window.ref; //remote.require( 'ref-napi' );
Then, it works expected! Well done, congraturations to me!!