lwahonen / node-ffi-napi

MIT License
13 stars 13 forks source link

TypeError: error setting return value - buf.writeUInt8 is not a function when call EnumWindows #1

Open xmsz opened 1 year ago

xmsz commented 1 year ago

code:

var user32 = new ffi.Library('user32', {
  GetWindowThreadProcessId: ['long', ['long', 'pointer']],
  EnumWindows: ['bool', ['pointer', 'long']],
});
const callback = new ffi.Callback('bool', ['long', 'int32'], (hwnd, lParam) => {
  const lpdwProcessId = ref.alloc('int');
  user32.GetWindowThreadProcessId(hwnd, lpdwProcessId);
  if (lpdwProcessId == lParam) {
    return false;
  }
  return true;
});
user32.EnumWindows(callback, 0);

result

Uncaught Error: TypeError: error setting return value - buf.writeUInt8 is not a function
    at Object.proxy [as EnumWindows] (node_modules\.pnpm\@lwahonen+ffi-napi@4.0.12\node_modules\@lwahonen\ffi-napi\lib\_foreign_function.js:61:14)
    at ./src/renderer/hooks/useRecordController.tsx (useRecordController.tsx:93:8)
    at options.factory (react refresh:6:1)
    at __webpack_require__ (bootstrap:24:1)
    at fn (hot module replacement:62:1)
    at ./src/renderer/components/Aside.tsx (Add.tsx:384:20)
    at options.factory (react refresh:6:1)
    at __webpack_require__ (bootstrap:24:1)
    at fn (hot module replacement:62:1)

I saw this issuse https://github.com/node-ffi-napi/node-ffi-napi/issues/225#issuecomment-1361275385

And i change true to 1, false to 0, it works but no real a good solution

So how can i fix it

lwahonen commented 1 year ago

Either fix the project to support uint8 or use an int instead. I'd just use int instead of bool

xmsz commented 1 year ago

Either fix the project to support uint8 or use an int instead. I'd just use int instead of bool

ok

xieerduos commented 2 weeks ago

Either fix the project to support uint8 or use an int instead. I'd just use int instead of bool Here’s the translation of your request:

@lwahonen @xmsz

I've encountered the error Error: TypeError: error setting return value - buf.writeUInt8 is not a function and am unsure how to resolve it.

Below is the related code:

const ffi = require("@breush/ffi-napi");
const ref = require("@breush/ref-napi");
function getUser32() {
  if (user32) {
    return user32;
  }

  user32 = ffi.Library("user32", {
    EnumWindows: ["bool", ["pointer", "int32"]],
    FindWindowW: ["int32", ["string", "string"]],
    EnumChildWindows: ["bool", ["int32", "pointer", "int32"]],
    GetWindowRect: ["bool", ["int32", "pointer"]],
    GetClassNameW: ["int32", ["int32", "pointer", "int32"]],
    IsWindowVisible: ["bool", ["int32"]],
    EnumDisplayMonitors: ["bool", ["pointer", "pointer", "pointer", "int32"]],
    GetMonitorInfoW: ["bool", ["int32", "pointer"]],
  });
  return user32;
}
// Method to get handles for all displays
function getAllDisplays() {
  const monitors = [];
  // Define MONITORINFO structure
  const MONITORINFO = Buffer.alloc(40);
  MONITORINFO.writeInt32LE(40, 0); // cbSize
  const monitorEnumProc = ffi.Callback(
    "bool",
    ["int32", "pointer", "pointer", "int32"],
    (hMonitor) => {
      if (getUser32().GetMonitorInfoW(hMonitor, MONITORINFO)) {
        const left = MONITORINFO.readInt32LE(4);
        const top = MONITORINFO.readInt32LE(8);
        const right = MONITORINFO.readInt32LE(12);
        const bottom = MONITORINFO.readInt32LE(16);
        monitors.push({
          ...convertToXYWH({ rect: { left, top, right, bottom } }),
          isPrimaryMonitor: isPrimaryMonitor(hMonitor),
          workArea: convertToXYWH({ rect: getMonitorWorkArea(hMonitor) }),
        });
      }

      return true; // Continue enumeration
    }
  );

  getUser32().EnumDisplayMonitors(null, null, monitorEnumProc, 0);
  return monitors;
}

console error

  Error: TypeError: error setting return value - buf.writeUInt8 is not a function
      at Object.proxy [as EnumDisplayMonitors] (D:\project\electron-screenshot\node_modules\@breush\ffi-napi\lib\_foreign_function.js:61:14)
      at Object.getAllDisplays (D:\project\electron-screenshot\main\desktopWindows.js:76:15)
      at MessagePort.<anonymous> (D:\project\electron-screenshot\main\worker-display.js:9:10)
      at [nodejs.internal.kHybridDispatch] (node:internal/event_target:647:20)
      at exports.emitMessage (node:internal/per_context/messageport:23:28)
]

How should I proceed to fix this?