Node ffi SHAppBarMessage(ABM_GETTASKBARPOS, data2.ref()) return wrong data

173 views Asked by At
const os = require("os");
const ffi = require("ffi-napi");
const ref = require("ref-napi");
const Struct = require("ref-struct-di")(ref);
const { screen } = require("electron");

const ABM_NEW = 0;
const ABM_REMOVE = 0x1;
const ABM_QUERYPOS = 0x2;
const ABM_SETPOS = 0x3;
const ABM_GETSTATE = 0x4;
const ABM_GETTASKBARPOS = 0x5; // 0x00000005
const ABM_ACTIVATE = 0x6;
const ABM_WINDOWPOSCHANGED = 0x9;

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.types.long,
  uCallbackMessage: ref.types.uint32,
  uEdge: ref.types.uint32,
  rc: RECT_Struct,
  lParam: ref.types.int64,
});

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

const data = new APPBARDATA_Struct();
data.cbSize = APPBARDATA_Struct.size;
const result2 = shell32.SHAppBarMessage(ABM_GETTASKBARPOS, data.ref());
console.log(`ABM_GETTASKBARPOS: ${JSON.stringify(data)}`);

return

ABM_GETTASKBARPOS: {"cbSize":40,"hWnd":0,"uCallbackMessage":0,"uEdge":0,"rc":{"left":0,"top":3,"right":0,"bottom":1380},"lParam":6184752908800}

Why rc value is incorrect? Because another python and ahk sample return correct 0 1380 2560 1440, not 0 3 0 1380? example with reading from address with offset. Why node ffi show incorrect RECT values, starting earlier from 0 3 0 1380, not from 0 1380 2560 1440? I trying to register window as appbar, but nothing happens. So i try to test on this small piece of code, and notice that returned values in rc are incorrect. If they are incorrect, maybe reason that i cant register i somewhere near. But i cant understand further...

1

There are 1 answers

0
w00zla On

I got it to work correctly when defining the APPBARDATA_Struct like this:

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

The culprit was the hWnd type :)