如何在 electron + node-ffi-napi 中获取正确的本机内存地址
How to get the correct native memory address in electron + node-ffi-napi
如果我直接使用Node.js
,我可以获得正确的本机内存地址,例如:
// 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() );
node tester.js
的结果是:
[from native lib] cr=sushi *=0x1E7B3B3E8C0
[from tester.js] 000001E7B3B3E8C0 sushi
符合预期,没问题。但是,如果我对 react-electron
系统使用类似的 FFI 方法,问题就会发生在我身上。
// 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!!
本次电子版结果为:
[from native lib] cr=sushi *=0x2BFA2F07A10
[from App.js(electron)] 000002BFA3BA7230 sushi
如何使用 electron
在项目中获取正确的本机内存地址?
- os = Microsoft Windows 10(64 位)10.0.19041.153
- 节点 = 13.5.0
- node-ffi-napi = 2.4.7
- node-ffi-ref = 1.4.3
- 反应 = 16.13.0
- 电子=8.1.1
我得到了这个案例的答案。在发布的情况下,我使用 nodeIntegration: true
然后我实现了使用 const remote = window.require('electron').remote
和 remote.require
功能的示例。然后,AddressForArgs
(†1) 通过 ref.HexAddress
returns 基于电子客户端的地址可能(我不知道 node-ref-napi、node-ffi-napi 和 electron 的更多内部结构刚刚 ).
因此,我尝试更改为具有 preload
功能的 nodeIntegration: false
设置。
// 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' );
然后,它按预期工作!干得好,恭喜我!!
如果我直接使用Node.js
,我可以获得正确的本机内存地址,例如:
// 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() );
node tester.js
的结果是:
[from native lib] cr=sushi *=0x1E7B3B3E8C0
[from tester.js] 000001E7B3B3E8C0 sushi
符合预期,没问题。但是,如果我对 react-electron
系统使用类似的 FFI 方法,问题就会发生在我身上。
// 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!!
本次电子版结果为:
[from native lib] cr=sushi *=0x2BFA2F07A10
[from App.js(electron)] 000002BFA3BA7230 sushi
如何使用 electron
在项目中获取正确的本机内存地址?
- os = Microsoft Windows 10(64 位)10.0.19041.153
- 节点 = 13.5.0
- node-ffi-napi = 2.4.7
- node-ffi-ref = 1.4.3
- 反应 = 16.13.0
- 电子=8.1.1
我得到了这个案例的答案。在发布的情况下,我使用 nodeIntegration: true
然后我实现了使用 const remote = window.require('electron').remote
和 remote.require
功能的示例。然后,AddressForArgs
(†1) 通过 ref.HexAddress
returns 基于电子客户端的地址可能(我不知道 node-ref-napi、node-ffi-napi 和 electron 的更多内部结构刚刚 ).
因此,我尝试更改为具有 preload
功能的 nodeIntegration: false
设置。
// 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' );
然后,它按预期工作!干得好,恭喜我!!