需要在电子中使用一些ffi napi,我试着让它工作,从小的开始。试着去理解并获得成功,但却得不到结果。我知道我将指针传递给struct,并将结果写入到结构中。但是调用返回True,指定指针中没有结果。没有新的数据。请帮个忙。
const ffi = require("ffi-napi");
const ref = require("ref-napi");
const Struct = require("ref-struct-di")(ref);
const ABM_NEW = 0;
const ABM_QUERYPOS = 0x2;
const ABM_GETTASKBARPOS = 5; // 0x00000005
const ABM_GETSTATE = 0x4;
const ABEdgeLeft = 0;
const RECT_Struct = Struct({
left: "long",
top: "long",
right: "long",
bottom: "long",
});
const APPBARDATA_Struct = Struct({
cbSize: "uint32",
hWnd: "int",
uCallbackMessage: "uint32",
uEdge: "uint32",
rc: RECT_Struct,
lParam: "int64",
});
export const shell32 = ffi.Library("shell32.dll", {
SHAppBarMessage: ["long", ["int", APPBARDATA_Struct]],
});
export const user32 = ffi.Library("user32.dll", {
GetWindowRect: ["bool", ["long", RECT_Struct]],
});
const data = new APPBARDATA_Struct();
data.cbSize = APPBARDATA_Struct.size;
const result = shell32.SHAppBarMessage(ABM_GETTASKBARPOS, data);
const rect = new RECT_Struct();
const result2 = user32.GetWindowRect(0x20674, rect);
console.log(`result: ${JSON.stringify(result)}: ${JSON.stringify(data)}`);
console.log(`result2: ${JSON.stringify(result2)}: ${JSON.stringify(rect)}`);
有结果
result: 1: {"cbSize":40,"hWnd":0,"uCallbackMessage":0,"uEdge":0,"rc":{"left":0,"top":0,"right":0,"bottom":0},"lParam":0}
result2: true: {"left":0,"top":0,"right":0,"bottom":0}
当我测试时,函数调用工作-它返回1(真)与hwnd存在的窗口,如果关闭0。但是我无法从缓冲区中获得结果数据,这让我很生气。
发布于 2021-10-23 12:43:25
首先,我对指针不太了解。然而,我对同样的问题感兴趣,所以我试着找到了一个解决方案。当我知道确切的原理时,我会修改它。
您使用的、GetWindowRect、和C++函数从C++接收结构指针类型参数。这些函数具有通过返回值通知成功与否和记录接收到的结构上的数据的功能。因此,在C++中测试函数时,在构建结构类型参数时会出现错误,而不添加Ampsand(例如)。&rect或&data)符号。
参考文献的一个示例显示了使用指向参数的结构指针的示例。我认为这是在传递一个指向函数的结构指针,当代码被修改如下时,它就工作了。必须修改使用struct作为参数的部件和使用ffi定义函数的部分。请参阅代码中的评论!
// GetWindowRect Example
const RECT = Struct({
left: ref.types.long,
top: ref.types.long,
right: ref.types.long,
bottom: ref.types.long,
});
const user32 = ffi.Library('user32.dll', {
GetWindowRect: ['bool', ['long', ref.refType(RECT)]], // RECT_Struct -> ref.refType(RECT_Struct) or 'pointer'
});
const rect = new RECT;
console.log(user32.GetWindowRect(0x007B0F10, rect.ref())); // 0x007B0F10 is one of my windows that I found with Spy++.
console.log('left', rect.left);
console.log('right', rect.right);
console.log('top', rect.top);
console.log('bottom', rect.bottom);
// SHAppBarMessage Example
const ABM_GETTASKBARPOS = 5;
const APPBARDATA = Struct({
cbSize: ref.types.uint32,
hWnd: ref.types.long,
uCallbackMessage: ref.types.uint32,
uEdge: ref.types.uint32,
rc: RECT,
lParam: ref.types.int64,
});
const shell32 = ffi.Library('shell32.dll', {
SHAppBarMessage: ['long', ['int', ref.refType(APPBARDATA)]], // APPBARDATA_Struct -> ref.refType(APPBARDATA_Struct) or 'pointer'
});
const data = new APPBARDATA();
data.cbSize = APPBARDATA.size;
console.log(shell32.SHAppBarMessage(ABM_GETTASKBARPOS, data.ref()));
console.log('cbSize', data.cbSize);
console.log('hWnd', data.hWnd);
console.log('uCallbackMessage', data.uCallbackMessage);
console.log('uEdge', data.uEdge);
console.log('rc.left', data.rc.left);
console.log('rc.right', data.rc.right);
console.log('rc.top', data.rc.top);
console.log('rc.bottom', data.rc.bottom);
console.log('lParam', data.lParam);
https://stackoverflow.com/questions/68928710
复制相似问题