[英]electron / node-ffi error running win32 function
I'm trying to get to work calling a Win32 function from electron 4 (node 10.x) and I get an error which seems obscure to me.我正在尝试从电子 4(节点 10.x)调用 Win32 函数开始工作,但我收到一个对我来说似乎很模糊的错误。
I'm using this code:我正在使用此代码:
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);
}
and then within electron context:然后在电子范围内:
registerAppBar(mainWindow.getNativeWindowHandle());
The error I'm getting is "TypeError: error setting argument 1 - writePointer: Buffer instance expected as third argument" and I don't know why it happens.我得到的错误是“TypeError: error setting argument 1 - writePointer: Buffer instance expected as第三个参数”,我不知道为什么会发生。
Any help/ideas are highly appreciated!任何帮助/想法都非常感谢!
What I'm trying to do is register an electron window to become an application toolbar as per https://docs.microsoft.com/en-us/windows/win32/shell/application-desktop-toolbars我想要做的是根据https://docs.microsoft.com/en-us/windows/win32/shell/application-desktop-toolbars注册一个电子窗口以成为应用程序工具栏
The 2nd parameter you need to pass to SHAppBarMessage
is a pointer to the APPBARDATA_Struct
, see the following link: https://github.com/node-ffi/node-ffi/wiki/Node-FFI-Tutorial#structs 你需要传递给第二个参数
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());
And in addition, rc
in APPBARDATA_Struct
is not a structPtr. 另外,
APPBARDATA_Struct
rc
不是structPtr。
Using Node ffi pointer to struct I got that to work using this: 使用Node ffi指针来构造,我可以使用以下代码来工作:
export const shell32 = ffi.Library('shell32.dll', {
SHAppBarMessage: [ 'pointer', [ 'int', APPBARDATA_Struct]]
});
Similar to the suggestion in the comment by Drake Wo, but no '' and not using ref.refType. 与Drake Wo的注释中的建议相似,但不包含'',并且不使用ref.refType。
Now the function returns true :D 现在函数返回true:D
i try make similar taskbar window, but ffi/ref-struct/ref import seems deprecated.我尝试制作类似的任务栏窗口,但 ffi/ref-struct/ref 导入似乎已过时。 I trying use with ffi-napi, but nothing happens (no error, nothing).
我尝试与 ffi-napi 一起使用,但没有任何反应(没有错误,没有)。 How make this work with electron, register electron windos as taskbar, so all other windows would unfold given the size of the taskbar?
如何使用电子使其工作,将电子窗口注册为任务栏,以便所有其他窗口都可以根据任务栏的大小展开?
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.