簡體   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