电子/节点ffi错误,运行win32函数

时间:2019-07-15 14:07:33

标签: node.js typescript winapi ffi node-ffi

我试图从电子4(节点10.x)调用Win32函数,但出现一个错误,这对我来说似乎很隐蔽。

我正在使用以下代码:

import * as ffi from 'ffi';
import * as Struct from 'ref-struct';
import * as ref from "ref";

const
    ABM_NEW = 0x0,
    ABM_REMOVE = 0x1,
    ABM_QUERYPOS = 0x2,
    ABM_SETPOS = 0x3;

const RECT_Struct = new Struct({
   left: ref.types.long,
   top: ref.types.long,
   right: ref.types.long,
   bottom: ref.types.long
});

const APPBARDATA_Struct = new Struct({
    cbSize: ref.types.uint32,
    hWnd: ref.refType(ref.types.void),
    uCallbackMessage: ref.types.uint32,
    uEdge: ref.types.uint32,
    rc: ref.refType(RECT_Struct),
    lParam: ref.types.int64
});

export const shell32 = ffi.Library('shell32.dll', {
    SHAppBarMessage: [ 'pointer', [ 'int', 'pointer']]
});

export function registerAppBar(windowHandle: any) {
    let data = new APPBARDATA_Struct();
    data.cbSize = APPBARDATA_Struct.size;
    data.hWnd = windowHandle;
    data.uCallbackMessage = 1234;
    let res = shell32.SHAppBarMessage(ABM_NEW, data);
}

,然后在电子上下文中:

registerAppBar(mainWindow.getNativeWindowHandle());

我得到的错误是“ TypeError:错误设置参数1-writePointer:缓冲区实例应作为第三个参数”,我不知道为什么会发生。

Whole error

我们非常感谢您的帮助/想法!


我要做的是按照https://docs.microsoft.com/en-us/windows/win32/shell/application-desktop-toolbars

注册一个电子窗口成为应用程序工具栏。

3 个答案:

答案 0 :(得分:0)

您需要传递给SHAppBarMessage的第二个参数是指向APPBARDATA_Struct的指针,请参见以下链接: https://github.com/node-ffi/node-ffi/wiki/Node-FFI-Tutorial#structs

var APPBARDATA_StructPtr = ref.refType('APPBARDATA_Struct');
export const shell32 = ffi.Library('shell32.dll', {
    SHAppBarMessage: [ 'pointer', [ 'int', 'APPBARDATA_StructPtr']]
});
...
let res = shell32.SHAppBarMessage(ABM_NEW, data.ref());

此外,rc中的APPBARDATA_Struct不是structPtr。

答案 1 :(得分:0)

使用Node ffi pointer to struct,我可以使用它来工作:

export const shell32 = ffi.Library('shell32.dll', {
    SHAppBarMessage: [ 'pointer', [ 'int', APPBARDATA_Struct]]
});

与Drake Wo的评论中的建议类似,但没有”,并且未使用ref.refType。

现在函数返回true:D

答案 2 :(得分:0)

我尝试制作类似的任务栏窗口,但 ffi/ref-struct/ref 导入似乎已过时。 我尝试与 ffi-napi 一起使用,但没有任何反应(没有错误,没有)。如何使这个与电子一起工作,将电子窗口注册为任务栏,以便根据任务栏的大小展开所有其他窗口?

import * as ffi from "ffi-napi";

const ref = require("ref-napi");
const Struct = require("ref-struct-di")(ref);

const ABM_NEW = 0;
const ABM_REMOVE = 0x1;
const ABM_QUERYPOS = 0x2;
const ABM_SETPOS = 0x3;

const ABEdgeLeft = 0;
const ABEdgeTop = 1;
const ABEdgeRight = 2;
const ABEdgeBottom = 3;
const ABEdgeFloat = 4;

const RECT_Struct = Struct({
  left: ref.types.long,
  top: ref.types.long,
  right: ref.types.long,
  bottom: ref.types.long,
});

const APPBARDATA_Struct = Struct({
  cbSize: ref.types.uint32,
  hWnd: ref.refType(ref.types.void),
  uCallbackMessage: ref.types.uint32,
  uEdge: ref.types.uint32,
  rc: ref.refType(RECT_Struct),
  lParam: ref.types.int64,
});

export const shell32 = ffi.Library("shell32.dll", {
  SHAppBarMessage: ["pointer", ["int", APPBARDATA_Struct]],
});

export function registerAppBar(windowHandle: Buffer): void {
  const data = new APPBARDATA_Struct();
  data.cbSize = APPBARDATA_Struct.size;
  data.edge = ABEdgeLeft;
  data.hWnd = windowHandle;
  data.uCallbackMessage = 1234;
  shell32.SHAppBarMessage(ABM_NEW, data);
}