简体   繁体   中英

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.

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.

整个错误

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

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

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.

Using Node ffi pointer to struct I got that to work using this:

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.

Now the function returns true :D

i try make similar taskbar window, but ffi/ref-struct/ref import seems deprecated. I trying use with ffi-napi, but nothing happens (no error, nothing). 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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