繁体   English   中英

运行 win32 函数的电子 / node-ffi 错误

[英]electron / node-ffi error running win32 function

我正在尝试从电子 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: error setting argument 1 - writePointer: Buffer instance expected as第三个参数”,我不知道为什么会发生。

整个错误

任何帮助/想法都非常感谢!


我想要做的是根据https://docs.microsoft.com/en-us/windows/win32/shell/application-desktop-toolbars注册一个电子窗口以成为应用程序工具栏

你需要传递给第二个参数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());

另外, APPBARDATA_Struct rc不是structPtr。

使用Node ffi指针来构造,我可以使用以下代码来工作:

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

与Drake Wo的注释中的建议相似,但不包含'',并且不使用ref.refType。

现在函数返回true:D

我尝试制作类似的任务栏窗口,但 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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM