节点 ffi napi,调用 return true 但指针没有结果

Node ffi napi, call return true but no result in pointer

需要在电子中使用一些 ffi napi,我试图让它工作,从小做起。试图理解并获得 ffi 的作品,但无法获得结果。我知道我将指针传递给结构,结果将写入其中。但是在指定的指针中调用 return 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}

根据我的测试,函数调用有效 - 它 return 1(真)hwnd 存在 window,如果关闭则为 0。但是我无法从缓冲区获取结果数据,这让我很生气。

首先,我对指针不是很了解。但是,我对同样的问题很感兴趣,所以我尝试并找到了解决方案。等我知道具体原理再修改。

GetWindowRectSHAppBarMessage函数您使用了从 C++ 接收结构指针类型的参数。这些函数具有通过 return 值通知成功与否并将数据记录在接收到的结构上的功能。因此,在C++中测试该函数时,在构建结构类型参数时未添加Ampsand(ex. &rect or &data) 符号时会出错。

ref-struct-di 的示例显示了使用指向参数的结构指针的示例。我认为这是传递一个指向函数的结构指针,当代码被修改如下时它起作用了。必须修改使用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);